loading请求处理中...
10大优享服务
62项会员特权

c语言程序怎么修改?

问题补充:求大神帮我修改下程序
#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时,程序继续进行其他算术运算;否则程序退出运行状态。
速求各位大神帮主呀
提问者:诡魅/玫瑰 2回答 浏览次数:3821 提问时间: 2021/12/01 11:49
提问者采纳
代码可以作适当的简化。依程序原风格修改如下:


#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=0;
    char quit;


    do{
        do{
            printf("n请输入操作选项!n 1:做加法  2:做减法  3:做乘法  4:做除法n");
            scanf("%d",&m);
        }while(m<1||m>4);
        do{
            if(m==4&&y==0.0)
                printf("n除数不能为零!n");
            printf("n请输入两个数:n");
            scanf("%f%f",&x,&y);
        }while(m==4&&y==0.0);
        printf("n计算结果为%f",f(x,y,m));
        printf("n你是否继续(Y/N o r  y/n)?n");
        quit=getch();
    }while(quit=='y'||quit=='Y');
    return 0;
}

全部回答

  • 94800 2021/12/01 11:54
    #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()
    {
    char quit;
    float x,y;
    int m;
    while(1)
    {
    printf("请输入两数:n");
    scanf("%f%f",&x,&y);
    printf("请输入操作选项!n");
    printf(" 1:做加 2:做减 3:做乘 4:做除n");
    scanf("%d",&m);
    while(m<1||m>4)
    {
    printf("请输入12344数!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("请输入12344数n");
    scanf("%d",&m);
    }
    }
    printf("计算结:%fn",f(x,y,m));
    system("pause");
    printf("否继续(Y/N o r  y/n)?n");
    quit=getch();
    if(quit == 'N' || quit == 'n') break;
    }

问题分类

我 要 回 答 我 要 提 问
问题分类   验证码 换一组

向专家快速提问

人才 更多>