#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;?? ?
}