#include <stdio.h>
#include <dos.h>
main()
{
float num1,num2,result;
char operator;
char answer=’Y';
int right;
printf(“**********************欢迎进入四则运算系统************************\n”);
do
{
printf(“请输入num1,operator,num2\n”);
scanf(“%f%c%f”,&num1,&operator,&num2);
printf(“请输入你的答案\n”);
scanf(“%f”,&result);
switch(operator)
{
case ‘+’:if(result==(num1+num2)) right=1;
else right=0;
break;
case ‘-’:if(result==(num1-num2)) right=1;
else right=0;
break;
case ‘*’:if(result==(num1*num2)) right=1;
else right=0;
break;
case ‘/’:if(num2==0) {printf(“输入有误”);right=0;break;}
else
{
if(result==(num1/num2)) right=1;
else right=0;
break;
}
default:printf(“输入有误\n”);
}
if(right)
printf(“答案正确\n”);
else
printf(“答案错误\n”);
printf(“是否退出系统?(Y or N)\n”); /*提示用户是否继续运算*/
scanf(“%c”,&answer);
}
while((answer==’Y')||(answer==’y'));
system(“pause”);
}
这个程序在最后的是不会执行do-while的的循环,而是直接结束程序
>> 本文固定链接: http://www.vcgood.com/archives/2845
在scanf(“%c”,&answer);前加上一句getchar()
为什么要加上
getchar() 呢????
用来接收“回车”字符
例如
你输入 1+2之后肯定要敲“回车”才能运行
getchar()就是用来接收这个回车
#include <stdio.h>
#include <dos.h>
main()
{
float num1,num2,result;
char operator;
char answer;
int right;
printf(“**********************welcome************************\n”);
do
{
printf(“please input num1,operator,num2\n”);
scanf(“%f%c%f”,&num1,&operator,&num2);
printf(“your answer is \n”);
scanf(“%f”,&result);
switch(operator)
{
case ‘+’:if(result==(num1+num2)) right=1;
else right=0;
break;
case ‘-’:if(result==(num1-num2)) right=1;
else right=0;
break;
case ‘*’:if(result==(num1*num2)) right=1;
else right=0;
break;
case ‘/’:if(num2==0) {printf(“scanf error”);right=0;break;}
else
{
if(result==(num1/num2)) right=1;
else right=0;
break;
}
default:printf(“scanf error\n”);
}
if(right)
printf(“right ans\n”);
else
printf(“wrong ans\n”);
printf(“dou you want to exit ?(Y or N)\n”);
getchar();
scanf(“%c”,&answer);
}
while((answer==’N')||(answer==’n'));
}