首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

计算器,结果输出后,输入Y或N,决定进入下一次计算还是退出程序解决方案

2012-03-14 
计算器,结果输出后,输入Y或N,决定进入下一次计算还是退出程序/*计算器,让用户选择输入y或者Y,以执行另一个

计算器,结果输出后,输入Y或N,决定进入下一次计算还是退出程序
/*
计算器,让用户选择输入y或者Y,以执行另一个计算,输入N或n就结束程序
*/

C/C++ code
#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;}



运行结果:
-----------------------------
Input number1 operation number2!
1+2
1.00 + 2.00 = 3.00
Would u need another calculation?y for yes, and N for NO!
Press any key to continue
-------------------------------------
此时键入任何信息,都退出窗体,
请高人赐教,如何调整?
为什么会直接出现Press any key to continue
这个是怎么回事出现的?

[解决办法]
scanf("%c",&x);前清下缓冲看看fflush( stdin );
[解决办法]
C/C++ code
#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.

热点排行