【问题标题】:Simple calculator using switch with if else in C在 C 中使用带有 if else 的 switch 的简单计算器
【发布时间】:2016-02-16 03:36:23
【问题描述】:

到目前为止,这是我的简单计算器的代码。我现在正在处理sine(案例6),度数范围为0-360。这是输出。

$ ./a.exe ProblemSolving.c
Arithmetic              : Add(0) Sub(1) Mult(2) Div(4) Mod(5)
Trigonometry    : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)
Exponent                : x^y(11) 2^x(12) 10^x(13)
Enter the choice of operation:6
The choice of operation is:6
Enter degree range from 0 to 360
Enter degrees:400

在我输入所需的学位后,没有其他任何反应,程序结束。我认为我的 if 语句或 sine 函数有问题。

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

int main() {        
    double Add1, Add2, Sub1, Sub2, Mult1, Mult2;
    int Choice, Div1, Div2, Mod1, Mod2, Base1, Power1, Deg1; 

    printf("Arithmetic      : Add(0) Sub(1) Mult(2) Div(4) Mod(5)\n");
    printf("Trigonometry    : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)\n");
    printf("Exponent        : x^y(11) 2^x(12) 10^x(13)\n");
    printf("Enter the choice of operation:");
    scanf("%d", &Choice);
    printf("The choice of operation is:%d\n", Choice);

    switch(Choice) {
            case 0:
                printf("Enter number one:");
                scanf("%lf", &Add1);
                printf("Enter number two:");
                scanf("%lf", &Add2);
                printf("%2.2lf + %2.2lf = %2.2lf", Add1, Add2, Add1+Add2);
                break;
            case 1:
                printf("Enter number one:");
                scanf("%lf", &Sub1);
                printf("Enter number two:");
                scanf("%lf", &Sub2);
                printf("%2.2lf - %2.2lf = %2.2lf", Sub1, Sub2, Sub1-Sub2);
                break;
            case 2:
                printf("Enter number one:");
                scanf("%lf", &Mult1);
                printf("Enter number two:");
                scanf("%lf", &Mult2);
                printf("%2.2lf * %2.2lf = %2.2lf", Mult1, Mult2, Mult1*Mult2);
                break;
            case 4:
                printf("Enter number one:");
                scanf("%d", &Div1);
                printf("Enter number two:");
                scanf("%d", &Div2);
                if (Div2 == 0)
                    printf("Error! Denominator cannot equal 0");
                else 
                    printf("%d / %d = %d", Div1, Div2, Div1/Div2);
                break;
            case 5:
                printf("Enter number one:");
                scanf("%d", Mod1);
                printf("Enter number two:");
                scanf("%d", Mod2);
                if (Mod2 == 0)
                    printf("Error! Denominator cannot equal 0");
                else
                    printf("%d % %d = %d", Mod1, Mod2, Mod1%Mod2);
                break;
            case 6:
                printf("Enter degree range from 0 to 360\n");
                printf("Enter degrees:");
                scanf("%d", Deg1);
                if (0 > Deg1 > 360) 
                    printf("Error! Value Entered is not within valid range");
                else 
                    printf("sin(%d) = %d", Deg1, sin(Deg1));
                break;
            default:
                printf("Error! operator is not correct");
                break;  
    }
    return 0;
}   

【问题讨论】:

    标签: c if-statement switch-statement case


    【解决方案1】:

    这段代码有几个问题:

    1. scanf("%d", Deg1); 更改为scanf("%d", &amp;Deg1);,因为scanf() 需要地址。另外,我认为将Deg1 声明为double 可能会更好。
    2. 0 &gt; Deg1 &gt; 360 在 C 中是错误的。你必须写 Deg1 &lt; 0 || Deg1 &gt; 360。运算符|| 代表“逻辑或”。
    3. math.hsin() 中以弧度表示。所以使用sin(Deg1 * 3.14159265 / 180)。或者,为了提高可读性和维护性,#define PI 3.14159265sin(Deg1 * PI / 180)。请注意,您不能写 Deg1 / 180 * 3.14159265,因为 C 中的整数文字是 ints,而 int/int = int。例如,3 / 2 == 1,而不是 1.5。要获得准确的值,请写入 3.0 / 2.0。
    4. math.hsin() 返回double,所以写printf("sin(%d) = %g", Deg1, sin(...));

    这里的固定代码:

    #include <stdio.h>
    #include <math.h>
    
    #define PI 3.14159265
    
    // ...many lines of code...
    
        case 6:
            printf("Enter degree range from 0 to 360\n");
            printf("Enter degrees:");
            scanf("%d", &Deg1);
            if (0 > Deg1 || Deg1 > 360) 
                printf("Error! Value Entered is not within valid range");
            else 
                printf("sin(%d) = %g", Deg1, sin(Deg1 * PI / 180));
            break;
    

    【讨论】:

    • 即使使用新的 if 语句,程序仍会以超出允许范围的值执行算术:Add(0) Sub(1) Mult(2) Div(4) Mod(5) Trigonometry : sine (6) cosine(7) tan(8) arc_sin(9) arc_cos(10) 指数:x^y(11) 2^x(12) 10^x(13) 输入运算的选择:6运算的选择is:6 输入度数范围从 0 到 360 输入度数:-50 sin(-50) = 3.70358e-316
    • @JoeRiefski 使用 clang 在我的计算机上运行良好。我去看看。
    • @JoeRiefski 该程序是否如您所愿? #include #include #define PI 3.14159265 int main() { int Deg1; printf("输入度数范围 0 到 360\n"); printf("请输入度数:"); scanf("%d", &Deg1); if (0 > Deg1 || Deg1 > 360) printf("错误!输入的值不在有效范围内");否则 printf("sin(%d) = %g", Deg1, sin(Deg1 * PI / 180));返回0; }
    【解决方案2】:

    C 中的sine 函数(以及其他三角函数)以弧度而非度数工作。在将值传递给 sine 之前,您必须先从弧度转换度数。

    现在您的printf 的格式也有问题,因为您传递的是double,但告诉printf 期待int。您需要使用%f 而不是%d

    此外,您的if 声明目前没有多大意义,而且几乎可以肯定并不代表您的想法。你显然想要的是if (Deg1 &lt; 0.0 || Deg1 &gt; 360.0)

    【讨论】:

    • 如果用户输入弧度与度数,它也会起作用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多