在C语言中如何用switch语句进行简单的四则运算,除法要能处理零的情况
- 提问者网友:谁的错
- 2021-04-30 09:12
- 五星知识达人网友:琴狂剑也妄
- 2021-04-30 10:35
先声明,以前帮人改的。
#include<stdio.h>
int main()
{
double number1 = 0.0;
double number2 = 0.0;
char operation = 0;
char answer[10] = "";
start:
printf("\n计算公式:");
scanf("%lf %c %lf",&number1,&operation,&number2);
switch(operation)
{
case '+':
printf("=%.2lf",number1 + number2);
break;
case '-':
printf("=%.2lf",number1 - number2);
break;
case '*':
printf("=%.2lf",number1*number2);
break;
case '/':
if(number2==0)
printf("除数不能为0");
else
printf("=%.2lf",number1/number2);
break;
case '%':
if(number2==0)
printf("除数不能为0");
else
printf("=%ld",(long)number1%(long)number2);
break;
default:
printf("\n\n\a 不合法的运算!\n");
break;
}
printf("\n继续计算请选择Y或y,否则请选择N或n:");
scanf("%s",answer);
if(answer[0]=='Y'||answer[0]=='y')
goto start;
return 0;
}