计算器,结果输出后,输入Y或N,决定进入下一次计算还是退出程序
/*
计算器,让用户选择输入y或者Y,以执行另一个计算,输入N或n就结束程序
*/
#include <stdio.h>#include <ctype.h>int main(void){ double number2,number1; char operation; char x; select1 : {printf("Input number1 operation number2!"); scanf("%lf %c %lf",&number1,&operation,&number2); switch (operation) { case '+': printf("%.2lf %c %.2lf = %.2lf",number1,operation,number2,number2+number1); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; case '-': printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number2+number1); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; case '/': if (number2==0) { printf("Sorry!iput the right number!"); break; } else printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1/number2); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; case '*': printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1*number2); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; } } return 0;}#include <stdio.h>#include <ctype.h>int main(void){ double number2,number1; char operation; char x; select1 : {printf("Input number1 operation number2!\n"); scanf("%lf %c %lf",&number1,&operation,&number2); fflush(stdin);//刷新下 switch (operation) { case '+': printf("%.2lf %c %.2lf = %.2lf",number1,operation,number2,number2+number1); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; case '-': printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number2+number1); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; case '/': if (number2==0) { printf("Sorry!iput the right number!"); break; } else printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1/number2); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; case '*': printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1*number2); printf("Would u need another calculation?y for yes, and N for NO!\n"); scanf("%c",&x); if (toupper(x)=='Y') { goto select1; } else break; } } return 0;}
[解决办法]
但由MSDN上的fflush定义:
If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer.