能帮我修改下程序吗?
问题补充:求大神帮我修改下程序
#include
float f(float a,float b, int c)
{
float s;
if(c==1) s=a+b;
if(c==2) s=a-b;
if(c==3) s=a*b;
if(c==4) s=a/b;
return s;
}
int main()
{
float x,y;
int m;
printf("请输入两个数:n");
scanf("%f%f",&x,&y);
printf("请输入操作选项!n");
printf(" 1:做加法 2:做减法 3:做乘法 4:做除法n");
scanf("%d",&m);
while(m<1||m>4)
{
printf("请输入1,2,3,4这4个数!n");
scanf("%d",&m);
}
while(m==4&&y==0.0)
{
printf("0不能做除数n");
scanf("%f%f",&x,&y);
printf("请输入操作选项n");
printf(" 1:做加法 2:做减法 3:做乘法 4:做除法n");
scanf("%d",&m);
while(m<1||m>4)
{
printf("请输入1,2,3,4这4个数n");
scanf("%d",&m);
}
while()
{
printf("你是否继续(Y/N o r y/n)?n");
scanf("%c", &quit);
if(quit == 'N' || quit == 'n') break;
}
}
printf("计算结果为:%fn",f(x,y,m));
system("pause");
return 0;
}
要求加入printf("你是否继续(Y/N o r y/n)?n");
scanf("%c", &quit);
if(quit == 'N' || quit == 'n') break;
实现
程序要求可以连续做多次算术运算,每次运算结束后,程序都给出提示: Do you want to continue(Y/N o r y/n)? 用户输入Y或y时,程序继续进行其他算术运算;否则程序退出运行状态。
速求各位大神帮助呀
提问者:工业新星 3回答 浏览次数:2959 提问时间:
2021/12/01 11:48
提问者采纳
这么多人答了,我都不想答了,但是已经写了,还是贴上吧
按照你的思路来
#include
#include
float f(float a,float b, int c)
{
float s;
if(c==1) s=a+b;
if(c==2) s=a-b;
if(c==3) s=a*b;
if(c==4) s=a/b;
return s;
}
int main()
{
float x,y;
int m;
char quit;
q:printf("请输入两个数:n");
p:scanf("%f%f",&x,&y);
printf("请输入操作选项!n");
printf(" 1:做加法 2:做减法 3:做乘法 4:做除法n");
s:scanf("%d",&m);
if(m<1||m>4)
{
printf("请输入1,2,3,4这4个数!n");
goto s;
}
else if (m==4&&y==0.0)
{
printf("0不能做除数n");
goto p;
}
printf("计算结果为:%fn",f(x,y,m));
system("pause");
printf("你是否继续(y/n)?n");
scanf("%c", &quit);
if(quit=='y')
goto q;
else
goto g;
g:return 0;
}
我比较喜欢goto,没那么多的循环,再给你个建议,你没必要使用那个f 函数,可以直接用switch语句代替,我也是刚学