鱼C论坛

 找回密码
 立即注册
查看: 829|回复: 2

[已解决]加减乘除

[复制链接]
发表于 2020-4-25 22:56:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 沐丶心 于 2020-4-25 23:46 编辑

写了半天
结果一输入字母(a,s,m,d,)就选择default的选项
有没有大佬帮忙找找错
  1. #include <stdio.h>
  2. void pr();
  3. float add();//加
  4. float subtract();//减
  5. float multiply();//乘法
  6. float divide();//除
  7. char wdnmd();
  8. float first();
  9. float last();
  10. int main()
  11. {
  12.     pr();
  13.     char ch;
  14.     while((ch = wdnmd()) != 'q')
  15.     {
  16.         switch (ch)
  17.         {
  18.         case 'a':add();break;
  19.         case 's':subtract();break;
  20.         case 'm':multiply();break;
  21.         case 'd':divide();break;
  22.         default:printf("非法输入,请重新输入:");continue;
  23.         }

  24.     }
  25.     printf("game over");
  26.     return 0;
  27. }
  28. void pr()
  29. {
  30.     printf("Enter the operation of your choice:\n");
  31.     printf("a. add(+)         s. subtract(-)\n");
  32.     printf("m. mulit(*)       d. divide(/)\n");
  33.     printf("q. quit\n");
  34. }
  35. char wdnmd()
  36. {
  37.         char ch;
  38.         ch = getchar();
  39.         while(getchar() != '\n')
  40.         continue;
  41.         return ch;
  42. }
  43. float first()
  44. {
  45.     float lbw;
  46.     char ch;
  47.     printf("Enter first number:");
  48.     while(scanf("%f", &lbw) == 0)
  49.     {
  50.         while((ch=getchar()) != '\n')
  51.         putchar(ch);
  52.         printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
  53.     }
  54.     while(getchar()!= '\n')
  55.         continue;

  56.     return lbw;
  57. }
  58. float last()
  59. {
  60.     float lbw;
  61.     char ch;
  62.     printf("Enter first number:");
  63.     while(scanf("%f", &lbw) == 0)
  64.     {
  65.         while((ch=getchar()) != '\n')
  66.         putchar(ch);
  67.         printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
  68.     }
  69.     while(getchar()!= '\n')
  70.         continue;

  71.     return lbw;
  72. }
  73. float add()
  74. {
  75.     float a = first();
  76.     float b = last();
  77.     printf("%.2f + %.2f = %.2f\n", a, b, a+b);
  78. }
  79. float subtract()
  80. {
  81.     float a = first();
  82.     float b = last();
  83.     printf("%.2f - %.2f = %.2f\n", a, b, a-b);
  84. }
  85. float multiply()
  86. {
  87.     float a = first();
  88.     float b = last();
  89.     printf("%.2f * %.2f = %.2f\n", a, b, a*b);
  90. }
  91. float divide()
  92. {
  93.    
  94.     float a = first();
  95.     float b = last();
  96.     if(b == 0)
  97.     {
  98.         printf("除数不能为0,请重新输入:");
  99.         scanf("%f", &b);
  100.     }
  101.     printf("%.2f / %.2f = %.2f\n", a, b, a/b);
  102. }

  103.    

复制代码
最佳答案
2020-4-25 23:06:13
  1. #include <stdio.h>
  2. void pr();
  3. float add();//加
  4. float subtract();//减
  5. float multiply();//乘法
  6. float divide();//除
  7. char wdnmd();
  8. float first();
  9. float last();
  10. int main()
  11. {
  12.     pr();
  13.     char ch;
  14.     //while(ch = wdnmd() != 'q')
  15.     while((ch = wdnmd()) != 'q')
  16.     {
  17.         switch (ch)
  18.         {
  19.         case 'a':add();break;
  20.         case 's':subtract();break;
  21.         case 'm':multiply();break;
  22.         case 'd':divide();break;
  23.         default:printf("非法输入,请重新输入:");continue;
  24.         }

  25.     }
  26.     printf("game over");
  27.     return 0;
  28. }
  29. void pr()
  30. {
  31.     printf("Enter the operation of your choice:\n");
  32.     printf("a. add(+)         s. subtract(-)\n");
  33.     printf("m. mulit(*)       d. divide(/)\n");
  34.     printf("q. quit\n");
  35. }
  36. char wdnmd()
  37. {
  38.         char ch;
  39.         ch = getchar();
  40.         while(getchar() != '\n')
  41.         continue;
  42.         return ch;
  43. }
  44. float first()
  45. {
  46.     float ch;
  47.     printf("Enter first number:");
  48.     while((ch = getchar()) == 0)
  49.     {
  50.         while(getchar() != '\n')
  51.         putchar(ch);
  52.         printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
  53.     }
  54.     while(getchar()!= '\n')
  55.         continue;

  56.     return ch;
  57. }
  58. float last()
  59. {
  60.     float ch;
  61.     printf("Enter first number:");
  62.     while((ch = getchar()) == 0)
  63.     {
  64.         while(getchar() != '\n')
  65.         putchar(ch);
  66.         printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
  67.     }
  68.     while(getchar()!= '\n')
  69.         continue;

  70.     return ch;
  71. }
  72. float add()
  73. {
  74.     float a = first();
  75.     float b = last();
  76.     printf("%.2f + %.2f = %.2f\n", a, b, a+b);
  77. }
  78. float subtract()
  79. {
  80.     float a = first();
  81.     float b = last();
  82.     printf("%.2f + %.2f = %.2f\n", a, b, a-b);
  83. }
  84. float multiply()
  85. {
  86.     float a = first();
  87.     float b = last();
  88.     printf("%.2f + %.2f = %.2f\n", a, b, a*b);
  89. }
  90. float divide()
  91. {
  92.    
  93.     float a = first();
  94.     float b = last();
  95.     if(b == 0)
  96.     {
  97.         printf("除数不能为0,请重新输入:");
  98.         scanf("%f", &b);
  99.     }
  100.     printf("%.2f + %.2f = %.2f\n", a, b, a/b);
  101. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-25 23:06:13 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. void pr();
  3. float add();//加
  4. float subtract();//减
  5. float multiply();//乘法
  6. float divide();//除
  7. char wdnmd();
  8. float first();
  9. float last();
  10. int main()
  11. {
  12.     pr();
  13.     char ch;
  14.     //while(ch = wdnmd() != 'q')
  15.     while((ch = wdnmd()) != 'q')
  16.     {
  17.         switch (ch)
  18.         {
  19.         case 'a':add();break;
  20.         case 's':subtract();break;
  21.         case 'm':multiply();break;
  22.         case 'd':divide();break;
  23.         default:printf("非法输入,请重新输入:");continue;
  24.         }

  25.     }
  26.     printf("game over");
  27.     return 0;
  28. }
  29. void pr()
  30. {
  31.     printf("Enter the operation of your choice:\n");
  32.     printf("a. add(+)         s. subtract(-)\n");
  33.     printf("m. mulit(*)       d. divide(/)\n");
  34.     printf("q. quit\n");
  35. }
  36. char wdnmd()
  37. {
  38.         char ch;
  39.         ch = getchar();
  40.         while(getchar() != '\n')
  41.         continue;
  42.         return ch;
  43. }
  44. float first()
  45. {
  46.     float ch;
  47.     printf("Enter first number:");
  48.     while((ch = getchar()) == 0)
  49.     {
  50.         while(getchar() != '\n')
  51.         putchar(ch);
  52.         printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
  53.     }
  54.     while(getchar()!= '\n')
  55.         continue;

  56.     return ch;
  57. }
  58. float last()
  59. {
  60.     float ch;
  61.     printf("Enter first number:");
  62.     while((ch = getchar()) == 0)
  63.     {
  64.         while(getchar() != '\n')
  65.         putchar(ch);
  66.         printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
  67.     }
  68.     while(getchar()!= '\n')
  69.         continue;

  70.     return ch;
  71. }
  72. float add()
  73. {
  74.     float a = first();
  75.     float b = last();
  76.     printf("%.2f + %.2f = %.2f\n", a, b, a+b);
  77. }
  78. float subtract()
  79. {
  80.     float a = first();
  81.     float b = last();
  82.     printf("%.2f + %.2f = %.2f\n", a, b, a-b);
  83. }
  84. float multiply()
  85. {
  86.     float a = first();
  87.     float b = last();
  88.     printf("%.2f + %.2f = %.2f\n", a, b, a*b);
  89. }
  90. float divide()
  91. {
  92.    
  93.     float a = first();
  94.     float b = last();
  95.     if(b == 0)
  96.     {
  97.         printf("除数不能为0,请重新输入:");
  98.         scanf("%f", &b);
  99.     }
  100.     printf("%.2f + %.2f = %.2f\n", a, b, a/b);
  101. }
复制代码

评分

参与人数 1鱼币 +5 贡献 +3 收起 理由
沐丶心 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-4-25 23:10:53 | 显示全部楼层

谢谢人造人老师
小细节太重要了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-5 14:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表