如何让我的计算器应用程序接受 C 语言中的浮点值?

How to make my calculator app accept float values in C?

提问人:Ali Eren Sirkeci 提问时间:11/16/2023 更新时间:11/16/2023 访问量:58

问:

我用 C 制作了一个基本的计算器,但它只接受整数值,我想编程以接受浮点值。法典:

#include <stdio.h>
#include <math.h>

int main() {
        int num1;
        int num2;
        int mod;
        printf("Enter function 1:Add 2:Subtract 3:Multiply 4:Divide : ");
        scanf("%d",&mod);
        printf("Enter first number: ");
        scanf("%d",&num1);
        printf("Enter second number: ");
        scanf("%d",&num2);
        if(mod == 1) {
                printf("Answer is %d\n",num1+num2);
        }
        if(mod == 2) {
                printf("Answer is %d\n",num1-num2);
        }
        if(mod == 3) {
                printf("Answer is %d\n",num1*num2);
        }
        if(mod == 4) {
                printf("Answer is %d\n",num1/num2);
        }

        return 0;
}

我希望我的程序接受浮点输入。

c

评论

2赞 DevSolar 11/16/2023
scanf如果不检查返回值 - >坏事即将发生,你不知道你是否真的匹配了任何输入,你的变量可能是未初始化的。除此之外,请查看文档
0赞 Jabberwocky 11/16/2023
如果您使用浮点 () 变量而不是变量,它将接受浮点值。doubleint
2赞 Marco Bonelli 11/16/2023
我会先改成或改成......intfloatdouble%d%f

答:

1赞 Ali Eren Sirkeci 11/16/2023 #1

我替换了 to 和 to,它起作用了。新代码:int num1; int num2;float num1; float num2;%d%f

#include <stdio.h>
#include <math.h>

int main() {
        float num1;
        float num2;
        int mod;
        printf("Enter function 1:Add 2:Subtract 3:Multiply 4:Divide : ");
        scanf("%d",&mod);
        printf("Enter first number: ");
        scanf("%f",&num1);
        printf("Enter second number: ");
        scanf("%f",&num2);
        if(mod == 1) {
                printf("Answer is %f\n",num1+num2);
        }
        if(mod == 2) {
                printf("Answer is %f\n",num1-num2);
        }
        if(mod == 3) {
                printf("Answer is %f\n",num1*num2);
        }
        if(mod == 4) {
                printf("Answer is %f\n",num1/num2);
        }

        return 0;
}
2赞 Gino V 11/16/2023 #2

因此,为了简要回答您的问题,您使用“int”而不是“float”作为操作数。您需要将它们声明为“float”而不是“int”,实际上考虑到现代处理器的速度、它们的 64 位架构以及当今内存的廉价性,您最好为每个处理器使用“double”而不是“float”。

但这实际上仍然行不通。为什么不呢?因为你的格式字符串是 .这意味着它会将用户输入字符串解析为整数,然后将该整数(无十进制和后面的所有内容)存储在您传递给的整数、浮点数或双精度型中。为了存储单个精度浮点值,变量必须为类型,并且 scanf 必须使用格式字符串。对于双精度浮点,您需要将它们声明为 并使用 format string 。“LF”代表“长浮点”。scanf"%d"scanffloat"%f"double"lf"

使用代码扫描成双精度的示例

#include <stdio.h>
#include <math.h> /* I don't see you using any math functions though */
#include <stdlib.h> /* needed for exit() */

int main()
{
    double num1;
    double num2;

    int mod; /* this can remain an int because it is a menu choice */

    /* You can see below that I put something around your scanf calls.
     * These are error checks that the calls do not fail to produce a
     * value to use inside the pointer. */
    
    printf("Enter function 1:Add 2:Subtract 3:Multiply 4:Divide : ");
    if (scanf("%d",&mod) != 1) { 
        fprintf(stderr, "Error, invalid input"); 
        exit(1); 
    }

    printf("Enter first number: ");
    if (scanf("%lf",&num1) != 1) { 
        fprintf(stderr, "Error, invalid input"); 
        exit(1); 
    }

    printf("Enter second number: ");
    if (scanf("%lf",&num2) != 1) { 
        fprintf(stderr, "Error, invalid input"); 
        exit(1); 
    }

    /* The rest of the code is correct except that if you use 
     * this code above, you'll want to use `"%lf"` in your
     * printf statements for outputting the result. */

}

评论

1赞 Ali Eren Sirkeci 11/16/2023
我知道我应该检查变量。