简体   繁体   English

C语言的简单计算器程序

[英]Simple calculator program in C

I need to write a simple program in C that makes simple calculations of: +,-,*,/我需要用 C 编写一个简单的程序来进行简单的计算:+,-,*,/

Now, I am using Visual Studio Express 2013, and here is the code:现在,我使用的是 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();

    }

My output: Enter operator + Enter first operand 3.5 Enter second operand 5.4我的输出:输入运算符 + 输入第一个操作数 3.5 输入第二个操作数 5.4

And after I type the second number- the program exits, and nothing!在我输入第二个数字后 - 程序退出,什么也没有! There are no compilation errors, and I have no idea what to do.没有编译错误,我不知道该怎么做。 Can someone help, please?有人可以帮忙吗?

You're not using printf correctly.您没有正确使用printf This is what you're using.这就是你正在使用的。

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

You're not specifying the type of output in the format string, and you're passing the address of the variable you want to print, not the value.您没有在格式字符串中指定输出类型,而是传递要打印的变量的地址,而不是值。

You should use:你应该使用:

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

%lf is specifying that you want to print a double , \\n adds a newline, and you pass the value of the variable sum , not it's address. %lf指定要打印double\\n添加换行符,然后传递变量sum的值,而不是它的地址。

Also, you should change if (b == !0){ to if (b != 0){ .此外,您应该将if (b == !0){更改为if (b != 0){ If you leave what you put, it's the equivalent to if (b == 1){ , which probably isn't what you want.如果你留下你所放的东西,它相当于if (b == 1){ ,这可能不是你想要的。

EDIT Here is the code, with my modifications, which gives correct results.编辑这是经过我修改的代码,它给出了正确的结果。 I'll indicate which lines I changed我会指出我更改了​​哪些行

#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;

}
  1. You are using %f format to read doubles.您正在使用%f格式读取双打。 You should use %lf : scanf_s("%lf", &a);你应该使用%lfscanf_s("%lf", &a);
  2. The print methods actually don't print the result (format is incomplete).打印方法实际上不打印结果(格式不完整)。 And, instead of the value, you are passing variable's address.而且,您传递的是变量的地址,而不是值。 It should be: printf("The result is %e", sum);应该是: printf("The result is %e", sum);
  3. You should also change if (b == !0) to if (b != 0)您还应该将if (b == !0)更改为if (b != 0)

Now it's working fine I made some changes to it现在它工作正常我对它做了一些改变

the problem was with ur "scanf_s" and "%f" go throw with it问题出在你的“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();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM