鱼C论坛

 找回密码
 立即注册
查看: 1420|回复: 12

[已解决]帮我看看错哪啦

[复制链接]
发表于 2018-7-26 16:02:04 | 显示全部楼层 |阅读模式
15鱼币
本帖最后由 皇天在上 于 2018-7-26 16:03 编辑

写一个最简单的计算器,支持四则运算。
#include <stdio.h>
#include <math.h>

int main()
{
        double a, b , answer;
        char c;
       
       
        printf("请输入式子:");
        scanf("%d%c%d", &a, &c, &b);
       
        if  ('c' == '+')
        {
                answer = a +b;
                printf("%.2f + %.2f = %.2f", a, b, answer);
        }
        else if (c == '-')
        {
                answer = a- b;
                printf("%.2f - %.2f = %.2f", a, b, answer);
        }
        else if ('c' == '*')
        {
                answer = a * b;
                printf("%.2f * %.2f = %.2f", a, b, answer);
        }
        else if ('c' == '/')
        {
                if (b = 0)
                {
                        printf("很遗憾,除数不能为零!");
                }
                else
                {
                answer = a / b;
                printf("%.2f / %.2f = %.2f", a, b, answer);
        }
        }
        else
        {
                printf("请输入正确的式子");
        }
   return 0;       
}
最佳答案
2018-7-26 16:02:05
同学,
if条件判断不对,字符比较应该用c=='+'; c=='-'; c=='*'; c=='/'
还有输入的格式不对,应该用scanf("%lf %c %lf", &num1, &c, &num2);中间加个空格比较好点

改进下你的代码,可以参考下:
  1. #include<stdio.h>
  2. #include<math.h>

  3. int main(int argc, char **argv)
  4. {
  5.         double num1, num2, result;
  6.         char c;

  7.         printf("input (eg:10-3):");
  8.         scanf("%lf %c %lf", &num1, &c, &num2);
  9.         if (c=='+')
  10.         {
  11.                 result = num1+num2;
  12.                 printf("%.2f + %.2f = %.2f\n", num1, num2, result);
  13.         }
  14.         else if (c=='-')
  15.         {
  16.                 result = num1-num2;
  17.                 printf("%.2f - %.2f = %.2f\n", num1, num2, result);
  18.         }
  19.         else if (c=='*')
  20.         {
  21.                 result = num1*num2;
  22.                 printf("%.2f * %.2f = %.2f\n", num1, num2, result);
  23.         }
  24.         else if (c=='/')
  25.         {
  26.                 result = num1/num2;
  27.                 printf("%.2f / %.2f = %.2f\n", num1, num2, result);
  28.         }
  29.         else
  30.         {
  31.                 printf("input format is worng!\n");
  32.         }
  33.         return 0;
  34. }
复制代码

测试如:

baiyang@UbYang:~/ubshare/testcode$ gcc calu.c -o exe
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 - 3
10.00 - 3.00 = 7.00
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 + 3
10.00 + 3.00 = 13.00
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 * 3
10.00 * 3.00 = 30.00
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 / 3
10.00 / 3.00 = 3.33

最佳答案

查看完整内容

同学, if条件判断不对,字符比较应该用c=='+'; c=='-'; c=='*'; c=='/' 还有输入的格式不对,应该用scanf("%lf %c %lf", &num1, &c, &num2);中间加个空格比较好点 改进下你的代码,可以参考下: 测试如: baiyang@UbYang:~/ubshare/testcode$ gcc calu.c -o exe baiyang@UbYang:~/ubshare/testcode$ ./exe input (eg:10-3):10 - 3 10.00 - 3.00 = 7.00 baiyang@UbYang:~/ubshare/testcode$ ./exe input (eg:10-3) ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 16:02:05 | 显示全部楼层    本楼为最佳答案   
同学,
if条件判断不对,字符比较应该用c=='+'; c=='-'; c=='*'; c=='/'
还有输入的格式不对,应该用scanf("%lf %c %lf", &num1, &c, &num2);中间加个空格比较好点

改进下你的代码,可以参考下:
  1. #include<stdio.h>
  2. #include<math.h>

  3. int main(int argc, char **argv)
  4. {
  5.         double num1, num2, result;
  6.         char c;

  7.         printf("input (eg:10-3):");
  8.         scanf("%lf %c %lf", &num1, &c, &num2);
  9.         if (c=='+')
  10.         {
  11.                 result = num1+num2;
  12.                 printf("%.2f + %.2f = %.2f\n", num1, num2, result);
  13.         }
  14.         else if (c=='-')
  15.         {
  16.                 result = num1-num2;
  17.                 printf("%.2f - %.2f = %.2f\n", num1, num2, result);
  18.         }
  19.         else if (c=='*')
  20.         {
  21.                 result = num1*num2;
  22.                 printf("%.2f * %.2f = %.2f\n", num1, num2, result);
  23.         }
  24.         else if (c=='/')
  25.         {
  26.                 result = num1/num2;
  27.                 printf("%.2f / %.2f = %.2f\n", num1, num2, result);
  28.         }
  29.         else
  30.         {
  31.                 printf("input format is worng!\n");
  32.         }
  33.         return 0;
  34. }
复制代码

测试如:

baiyang@UbYang:~/ubshare/testcode$ gcc calu.c -o exe
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 - 3
10.00 - 3.00 = 7.00
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 + 3
10.00 + 3.00 = 13.00
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 * 3
10.00 * 3.00 = 30.00
baiyang@UbYang:~/ubshare/testcode$ ./exe
input (eg:10-3):10 / 3
10.00 / 3.00 = 3.33
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 16:22:03 | 显示全部楼层
ab都是double,在scnaf中用%lf接收输入
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 16:33:15 | 显示全部楼层
BngThea 发表于 2018-7-26 16:22
ab都是double,在scnaf中用%lf接收输入

不管怎样,都是打印出:不是正确的式子。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 16:41:34 | 显示全部楼层
皇天在上 发表于 2018-7-26 16:33
不管怎样,都是打印出:不是正确的式子。

你可以测试一下,scanf以后abc的值是什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 16:52:50 | 显示全部楼层
看看看看下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 16:53:26 | 显示全部楼层
皇天在上 发表于 2018-7-26 16:33
不管怎样,都是打印出:不是正确的式子。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 17:07:01 | 显示全部楼层
double,所以全部改用 %lf
scanf(%lf %c %lf, &a, &c, &b); 之间最好有空格,因为有些人会输入数字之后空格。

if (c == '+') 不是 if ('c' == '+')
* 与 / 的情形一样
/ 内部, if (b == 0)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-26 17:32:07 | 显示全部楼层
貌似scanf无法智能识别啊,建议用%s输入然后再分析(遍历找运算符然后strtok,再atof即可)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 19:03:55 | 显示全部楼层
...........结果我发现,scanf中的应该是lf不是if。最大的问题在这啊= =
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 19:05:37 | 显示全部楼层
还有下面的除法中,应该是b == 0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 19:06:27 | 显示全部楼层
这打赏能不能喂给自己呀= =
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 19:07:46 | 显示全部楼层
weizhongyang 发表于 2018-7-26 16:02
同学,
if条件判断不对,字符比较应该用c=='+'; c=='-'; c=='*'; c=='/'
还有输入的格式不对,应该用scan ...

为什么if判断里不用空格隔开呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 03:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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