• 全部
...

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

mko_y 2012-02-28 09:53:52
/*
计算器,让用户选择输入y或者Y,以执行另一个计算,输入N或n就结束程序
*/


  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main(void)
  4. {
  5. double number2,number1;
  6. char operation;
  7. char x;

  8. select1 :
  9. {printf("Input number1 operation number2!");
  10. scanf("%lf %c %lf",&number1,&operation,&number2);

  11. switch (operation)
  12. {
  13. case '+':
  14. printf("%.2lf %c %.2lf = %.2lf",number1,operation,number2,number2+number1);
  15. printf("Would u need another calculation?y for yes, and N for NO!\n");
  16. scanf("%c",&x);
  17. if (toupper(x)=='Y')
  18. {
  19. goto select1;

  20. }
  21. else break;
  22. case '-':
  23. printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number2+number1);
  24. printf("Would u need another calculation?y for yes, and N for NO!\n");
  25. scanf("%c",&x);
  26. if (toupper(x)=='Y')
  27. {
  28. goto select1;

  29. }
  30. else break;
  31. case '/':
  32. if (number2==0)
  33. {
  34. printf("Sorry!iput the right number!");
  35. break;
  36. }
  37. else
  38. printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1/number2);
  39. printf("Would u need another calculation?y for yes, and N for NO!\n");
  40. scanf("%c",&x);
  41. if (toupper(x)=='Y')
  42. {
  43. goto select1;

  44. }
  45. else break;
  46. case '*':
  47. printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1*number2);
  48. printf("Would u need another calculation?y for yes, and N for NO!\n");
  49. scanf("%c",&x);
  50. if (toupper(x)=='Y')
  51. {
  52. goto select1;

  53. }
  54. else break;

  55. }
  56. }


  57. return 0;
  58. }



运行结果:
-----------------------------
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
这个是怎么回事出现的?
...全文
给本帖投票
285 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
mko_y 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 czh3642210 的回复:]

只要在你上次输入和这次输入之间getchar()一下就好
[/Quote]

谢谢了!
十分感谢!
cougar0709 2012-02-28
  • 打赏
  • 举报
回复
getchar,神奇。。。
面包大师 2012-02-28
  • 打赏
  • 举报
回复
只要在你上次输入和这次输入之间getchar()一下就好
mko_y 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 czh3642210 的回复:]

getchar();//是用来清空输入缓冲区的,因为你上次输入的时候,有回车等字符,所以,你getchar一下就清空了,否则你直接scanf的话,就会获取到以前残留的
[/Quote]

那流程应该是怎么样的啊,我想是:
比如 1+2
应该执行case ‘+’:

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;

输出输出 然后再输入, 那和getchar(),那怎么会回到getchar()啊
面包大师 2012-02-28
  • 打赏
  • 举报
回复
getchar();//是用来清空输入缓冲区的,因为你上次输入的时候,有回车等字符,所以,你getchar一下就清空了,否则你直接scanf的话,就会获取到以前残留的
luciferisnotsatan 2012-02-28
  • 打赏
  • 举报
回复
stdin 是标准输入流
mko_y 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luciferisnotsatan 的回复:]

scanf("%c",&x);前清下缓冲看看fflush( stdin );
[/Quote]

您好fflush(stdin),fflush 是刷新的意思, 那stdin 是什么意思啊
mko_y 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 czh3642210 的回复:]

C/C++ code
select1 :
{printf("Input number1 operation number2!");
scanf("%lf %c %lf",&number1,&operation,&number2);
getchar();//在这儿加个这个就好了
[/Quote]

是诶,加上ok了,请问是什么原理啊, 我看不懂啊
flowingfirefly 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 czh3642210 的回复:]

C/C++ code
select1 :
{printf("Input number1 operation number2!");
scanf("%lf %c %lf",&number1,&operation,&number2);
getchar();//在这儿加个这个就好了
[/Quote]
这个方法行,简单而且行之有效。
xuxinmmy 2012-02-28
  • 打赏
  • 举报
回复
#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);

switch (operation)
{
case '+':
printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1+number2);
break;
case '-':
printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1-number2);
break;
case '/':
if (number2==0)
{
printf("Sorry!iput the right number!\n");
}
else
{
printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1/number2);
}
break;
case '*':
printf("%.2lf %c %.2lf = %.2lf\n",number1,operation,number2,number1*number2);
break;
default:
break;

}
printf("Would u need another calculation?\ny for yes, and N for NO!\n");

while(true)
{
x = getchar();
if('y' == x || 'Y' == x)
{
goto select1;
}
else if('n' == x || 'N' == x)
{
break;
}
}


return 0;
}

面包大师 2012-02-28
  • 打赏
  • 举报
回复
由以上可见fflush(stdin)在VC上还是有效地!鉴于各编译器对fflush的未定义行为实现不一样,不推荐使用fflush(stdin)刷新输入缓冲区。
面包大师 2012-02-28
  • 打赏
  • 举报
回复
但由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.
面包大师 2012-02-28
  • 打赏
  • 举报
回复
要清除输入缓冲区,不然会自动运行的。。。
c_losed 2012-02-28
  • 打赏
  • 举报
回复
#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;
}
luciferisnotsatan 2012-02-28
  • 打赏
  • 举报
回复
scanf("%c",&x);前清下缓冲看看fflush( stdin );
面包大师 2012-02-28
  • 打赏
  • 举报
回复
select1 :
{printf("Input number1 operation number2!");
scanf("%lf %c %lf",&number1,&operation,&number2);
getchar();//在这儿加个这个就好了

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部