【问题标题】:Simple calculator program in CC语言的简单计算器程序
【发布时间】:2014-06-08 09:57:03
【问题描述】:

我需要用 C 编写一个简单的程序来进行简单的计算:+,-,*,/

现在,我使用的是 Visual Studio Express 2013,代码如下:

#include <stdio.h>
#include <stdlib.h>

int main(){

    double a, b;
    double sum = 0;
    char o; //operator

    printf("Enter operator\n");
    scanf_s("%c", &o);
    printf("Enter first operand\n");
    scanf_s("%f", &a);
    printf("Enter second operand\n");
    scanf_s("%f", &b);


    if (o == '+'){
        sum = a + b;
        printf("The result is", &sum);
    }

    if (o == '-'){

        sum = a - b;
        printf("The result is", sum);

    }

    if (o == '*'){

        sum = a*b;
        printf("The result is", sum);

    }

    if (o == '/'){

        if (b == !0){

            sum = a / b;
            printf("The result is", sum);
        }
        else printf("Error");
    }
getchar();

    }

我的输出:输入运算符 + 输入第一个操作数 3.5 输入第二个操作数 5.4

在我输入第二个数字后——程序退出,什么也没有! 没有编译错误,我不知道该怎么做。有人可以帮忙吗?

【问题讨论】:

  • 我假设你的意思不是 printf("The result is", &sum); ;-)(&sum 将是答案的地址,而不是答案本身)
  • 另外,if (b == !0) 应该是 if (b != 0)
  • 正确,我的意思是 sum 是结果的地址,但它仍然不起作用

标签: c calculator


【解决方案1】:

您没有正确使用printf。这就是你正在使用的。

printf("The result is", &sum);

您没有在格式字符串中指定输出类型,而是传递了要打印的变量的地址,而不是值。

你应该使用:

printf("The result is %lf\n", sum);

%lf 指定要打印double\n 添加换行符,并且您传递变量sum 的值,而不是地址。

另外,您应该将if (b == !0){ 更改为if (b != 0){。如果你留下你放的东西,它就相当于if (b == 1){,这可能不是你想要的。

编辑这是代码,经过我的修改,它给出了正确的结果。我会指出我更改了​​哪些行

#include <stdio.h>
#include <stdlib.h>

int main(){

    double a, b;
    double sum = 0;
    char o; //operator

    /* I had to use scanf, since I'm not using MS/Visual Studio, but GCC */
    printf("Enter operator\n");
    scanf("%c", &o);
    printf("Enter first operand\n");
    scanf("%lf", &a); /* changed %f to %lf */
    printf("Enter second operand\n");
    scanf("%lf", &b); /* changed %f to %lf */

    /* I prefer to use if ... else if ..., this is personal preference */
    if (o == '+'){
        sum = a + b;
        printf("The result is %lf\n", sum); /* Changed, see original post */
    } else if (o == '-'){
        sum = a - b;
        printf("The result is %lf\n", sum); /* Changed, see original post */
    } else if (o == '*'){
        sum = a*b;
        printf("The result is %lf\n", sum); /* Changed, see original post */
    } else if (o == '/'){
        if (b != 0){
        sum = a / b;
            printf("The result is %lf\n", sum); /* Changed, see original post */
        }
        else printf("Error");
    }

    getchar();

    return 0;

}

【讨论】:

  • @user2309261 我已编辑以添加更多信息。检查它是否有帮助。
  • 我尝试添加 while (getchar != '\n') ;它所做的是在输入两个数字后卡住了..它不会继续
  • @user2309261 我已在我的代码中添加,它运行并提供正确的值。看看我的代码中是否有与你的不同的地方。
【解决方案2】:
  1. 您正在使用%f 格式读取双打。你应该使用%lfscanf_s("%lf", &amp;a);
  2. 打印方法实际上不打印结果(格式不完整)。而且,您传递的不是值,而是变量的地址。它应该是: printf("The result is %e", sum);
  3. 您还应该将if (b == !0) 更改为if (b != 0)

【讨论】:

  • 执行此操作后,我得到的错误是:运行时检查失败 #2 - 变量“a”周围的堆栈已损坏。
【解决方案3】:

现在它工作正常,我对其进行了一些更改

问题出在你的“scanf_s”和“%f”上

#include<stdio.h>
#include<stdlib.h>

int main(){

    double a, b;
    double sum = 0;
    char o; //operator

    printf("Enter operator\n");
    scanf("%c", &o);
    printf("Enter first operand\n");
    scanf("%d", &a);
    printf("Enter second operand\n");
    scanf("%d", &b);


    if (o == '+'){
        sum = a + b;
        printf(" The result is %d", sum);
    }

    if (o == '-'){

        sum = a - b;
        printf("The result is %d", sum);

    }

    if (o == '*'){

        sum = a*b;
        printf("The result is %d", sum);
 }

    if (o == '/'){

        if (b == !0){

            sum = a / b;
            printf("The result is %d", sum);
        }
        else printf("Error");
    }
getchar();

    }

【讨论】:

  • 您提供的代码可能看似可以工作,但并不好!对 double 参数使用 %d 格式说明符是完全错误的!
猜你喜欢
  • 2013-12-03
  • 1970-01-01
  • 2012-05-14
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多