求大神解答C语言问题,要原创的,万分感谢!!

2024-11-07 12:33:49
推荐回答(2个)
回答(1):

#include
#include
int main()
{
float a,b;
int yunsuanfu;
printf("请输入要计算的2个操作数:\n");
scanf("%f",&a);
scanf("%f",&b);
printf("运算的类型有:\n");
printf("\t\t1+\n");
printf("\t\t2-\n");
printf("\t\t3*\n");
printf("\t\t4/\n");
printf("请输出你要的运算类型:\n");
scanf("%d",&yunsuanfu);
float c1=a+b;
float c2=a-b;
float c3=a*b;
float c4=a/b;
for(int i=0;i<40;i++)
printf("-");
printf("/n");
switch(yunsuanfu)
{
case 1: printf("%f\n",c1); break;
case 2: printf("%f\n",c2); break;
case 3: printf("%f\n",c3); break;
case 4:
if (b==0)
printf("除数不能为0\n");
else printf("%f\n",c4); break;
}
return 0;

回答(2):

#include
#include

int main()
{
double operand1,operand2;
char op,ch='n';

scanf("%lf",&operand1);

do{
for(op=getchar();isspace(op);op=getchar())
;
scanf("%lf",&operand2);
switch(op){
case '+':operand1+=operand2;break;
case '-':operand1-=operand2;break;
case '*':operand1*=operand2;break;
case '/':while(!operand2){
printf("error:attemped to devide by zero,please input again");
scanf("%lf",&operand2);
}
operand1/=operand2;break;
}
printf("%lf\n",operand1);
printf("Do you want to continue(Y/N or y/n)?\n");
for(ch=getchar();isspace(ch);ch=getchar())
;
}while(!(ch-'Y')&&!(ch-'y'));
return 0;
}