在 C 中使用带有 if else 的 switch 的简单计算器

Simple calculator using switch with if else in C

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

$ ./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 中的 sine 函数(以及其余三角函数)以弧度而不是度为单位工作。在将值传递给 sine.

之前,您必须将度数从弧度转换为弧度

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

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

这段代码有几个问题:

  1. scanf("%d", Deg1);改为scanf("%d", &Deg1);,因为scanf()需要地址。另外,我认为将 Deg1 声明为 double.
  2. 可能会更好
  3. 0 > Deg1 > 360在C中是错误的,必须写成Deg1 < 0 || Deg1 > 360。运算符 || 代表 "logical Or".
  4. math.h 中,sin() 以弧度表示。所以使用sin(Deg1 * 3.14159265 / 180)。或者,为了提高可读性和维护性,#define PI 3.14159265sin(Deg1 * PI / 180)。注意不能写Deg1 / 180 * 3.14159265,因为C中的整型字面量是int,而int/int = int。例如,3 / 2 == 1,而不是 1.5。要得到准确的值,写 3.0 / 2.0.
  5. math.h,sin()returnsdouble,所以写成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;