c语言怎么用switch语句编写四则运算? 20
1个回答
展开全部
switch 是一个开关语句,和case配套使用, 和if else 判断语句差不多, switch 语句是用于多分支语句进行条件判断。下例为用switch语句编写的四则运算:
#include <stdio.h>
void main()
{
double N1,N2;
char Operation;
printf("输入运算的两个数");
scanf("%lf%lf",&N1,N2);
printf("请输入运算符(+,-,*,/):");
switch(Operation)
{
case '+':printf("%lf%c%lf=%lf",N1,N2,Operation,N1+N2);
break;
case '-':printf("%lf%c%lf=%lf",N1,N2,Operation,N1-N2);
break;
case '*':printf("%lf%c%lf=%lf",N1,N2,Operation,N1*N2);
break;
case '/':printf("%lf%c%lf=%lf",N1,N2,Operation,N1/N2);
break;
}
}
#include <stdio.h>
void main()
{
double N1,N2;
char Operation;
printf("输入运算的两个数");
scanf("%lf%lf",&N1,N2);
printf("请输入运算符(+,-,*,/):");
switch(Operation)
{
case '+':printf("%lf%c%lf=%lf",N1,N2,Operation,N1+N2);
break;
case '-':printf("%lf%c%lf=%lf",N1,N2,Operation,N1-N2);
break;
case '*':printf("%lf%c%lf=%lf",N1,N2,Operation,N1*N2);
break;
case '/':printf("%lf%c%lf=%lf",N1,N2,Operation,N1/N2);
break;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询