C Primer Plus(第六版编程练习)8.11 编程练习 第8题
#include <stdio.h>
float get_number(void);
float charge_number(float t1);
int main(void)
{?? ?
??? ?float t=0.0,t1=0.0,anwser=0.0,tax=0.0;
??? ?char ch='0';
?? ?while(ch!='q')
?? ?{
?? ?printf("Enter the operation of your choice:\n");
?? ?printf("a.add\ts.subtract\n");
?? ?printf("m.multiply\td.divide\n");
?? ?printf("q.quit\n");
?? ?scanf("%c",&ch);
?? ?if(ch=='a'){?? ?
?? ??? ?printf("Enter your first number: ");
?? ??? ?t=get_number();
?? ??? ?printf("Enter your second number: ");
?? ??? ?t1=get_number();
?? ??? ?t1=charge_number(t1);
?? ??? ?anwser= t+t1;
?? ??? ?printf("%f=%f+%f",anwser,t,t1);
?? ??? ?break;}
?? ?else if(ch=='s'){
?? ??? ?printf("Enter your first number: ");
?? ??? ?t=get_number();
?? ??? ?printf("Enter your second number: ");
?? ??? ?t1=get_number();
?? ??? ?t1=charge_number(t1);
?? ??? ?anwser= t-t1;
?? ??? ?printf("%f=%f-%f",anwser,t,t1);
?? ??? ?break;}
?? ?else if(ch=='m'){
?? ??? ?printf("Enter your first number: ");
?? ??? ?t=get_number();
?? ??? ?printf("Enter your second number: ");
?? ??? ?t1=get_number();
?? ??? ?t1=charge_number(t1);
?? ??? ?anwser= t*t1;
?? ??? ?printf("%f=%f*%f",anwser,t,t1);
?? ??? ?break;}
?? ?else if(ch=='d'){
?? ??? ?printf("Enter your first number: ");
?? ??? ?t=get_number();
?? ??? ?printf("Enter your second number: ");
?? ??? ?t1=get_number();
?? ??? ?t1=charge_number(t1);
?? ??? ?anwser= t/t1;
?? ??? ?printf("%f=%f/%f",anwser,t,t1);
?? ??? ?break;}
?? ?else if(ch=='q')printf("bye!\n ");
?? ?else {getchar();
?? ??? ??? ?continue;}
?? ??? ??? ?}
?? ?return 0;
}?? ?
float get_number(void)
{?? ?
?? ?float t;
?? ?char erro;
?? ?while((scanf("%f",&t)!=1))
?? ?{
?? ??? ?while((t=getchar())!='\n')//这里是关键 在循环中读取错误中的单个字符存到t中
?? ??? ?{
?? ??? ??? ?putchar(t); //打印刚才存到的单个字符
?? ??? ?}
?? ??? ?printf(" is not a number.\nPlease enter an number,such as 2.5,-1.78E8,or 3:");
?? ?}?? ?
?? ?return t;
}
float charge_number(float t1)
{
?? ?while(t1==0.0)
?? ?{?? ?
?? ??? ?printf("Enter a number other than 0: ");
?? ??? ?t1=get_number();
?? ?}
?? ?return t1;?? ?
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!