如何用switch语句编写四则运算?要求输入运算符跟两个数字,直接输出结
2个回答
展开全部
#include <stdio.h>
int main(int argc, char **argv)
{
double operand1;
double operand2;
char operator[1];
while (1)
{
printf("输入数值和运算符(数值->运算符->数值):");
scanf("%lf%c%lf", &operand1,&operator[0], & operand2);
switch (operator[0])
{
case '+': printf("%.2lf\n", operand1 + operand1);
break;
case '-': printf("%.2lf\n", operand1 - operand2);
break;
case '*': printf("%.2f\n", operand1 * operand2);
break;
case '/': if (operand2 == 0)
{
printf("invalid operand!\n");
break;
}
printf("%.2f\n", operand1 / operand2);
break;
default: printf("unknown operator\n");
break;
}
}
}
int main(int argc, char **argv)
{
double operand1;
double operand2;
char operator[1];
while (1)
{
printf("输入数值和运算符(数值->运算符->数值):");
scanf("%lf%c%lf", &operand1,&operator[0], & operand2);
switch (operator[0])
{
case '+': printf("%.2lf\n", operand1 + operand1);
break;
case '-': printf("%.2lf\n", operand1 - operand2);
break;
case '*': printf("%.2f\n", operand1 * operand2);
break;
case '/': if (operand2 == 0)
{
printf("invalid operand!\n");
break;
}
printf("%.2f\n", operand1 / operand2);
break;
default: printf("unknown operator\n");
break;
}
}
}
展开全部
#include#includeintmain(){intnumOne=0;intnumTwo=0;intresult=0;charoperator;cin>>a;cin>>b;cin>>operator;switch(operator){case"+":sum=numOne+numTwo;break;case"-":sum=numOne-numTwo;break;case"*":sum=numOne*numTwo;break;case"/":sum=numOne/numTwo;break;default:break;}cout<<"Theresultis:"<
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |