C语言程序从键盘输入两个整数及一个运算符(加、减、乘、除)。 在线等

键盘输入两个整数及一个运算符(加、减、乘、除), 如果运算符为“+、-、*、/”中的一个,则求其结果并输出,然后换行再重新输入;否则退出程序。分别用Do while 和While语句实现。

输入 : 12 + 34
输出: 46

输入 quit 程序退出

#include<string.h>
#include<stdio.h>
void main()
{
char s[100], flag;
double a, b;
while(1)
{
printf(">>");
gets(s);
if( strcmp(s, "quit")==0 )
break;
sscanf(s, "%lf %c %lf", &a, &flag, &b);
switch(flag)
{
case '+':a+= b;break;
case '-':a-= b;break;
case '*':a+= b;break;
case '/':a/= b;break;
default: puts("输入格式错误!");
continue;
}
printf("%lf\n\n", a);
}
}追问

分别用Do while 和While语句实现

追答

#include
#include
void main()
{
char s[100], flag;
double a, b;
do
{
printf(">>");
gets(s);
if( strcmp(s, "quit")==0 )
break;
sscanf(s, "%lf %c %lf", &a, &flag, &b);
switch(flag)
{
case '+':a+= b;break;
case '-':a-= b;break;
case '*':a+= b;break;
case '/':a/= b;break;
default: puts("输入格式错误!");
continue;
}while(1);
printf("%lf\n\n", a);
}
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答