使用开关的简单计算器if if in C

时间:2016-02-16 03:36:23

标签: c if-statement switch-statement case

到目前为止,这是我的简单计算器代码。我正在研究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;
}   

2 个答案:

答案 0 :(得分:1)

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

目前您的printf格式也存在问题,因为您传递了double,但告诉printf期待int。您需要使用%f代替%d

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

答案 1 :(得分:1)

此代码中存在以下几个问题:

  1. scanf("%d", Deg1);更改为scanf("%d", &Deg1);,因为scanf()要求地址。另外,我认为将Deg1声明为double
  2. 可能更好 C.中的
  3. 0 > Deg1 > 360错了。你必须写Deg1 < 0 || Deg1 > 360。运营商||代表“逻辑或”。
  4. math.h中,sin()以弧度为单位。所以使用sin(Deg1 * 3.14159265 / 180)。或者,为了提高可读性和维护性,#define PI 3.14159265sin(Deg1 * PI / 180)。请注意,您不能编写Deg1 / 180 * 3.14159265,因为C中的整数文字是int s,而int / int = int。例如,3/2 == 1,而不是1.5。要获得准确的值,请写入3.0 / 2.0。
  5. math.hsin()返回double,所以请写printf("sin(%d) = %g", Deg1, sin(...));
  6. 此处修正了代码:

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