存储Char并在if语句中使用它

时间:2015-09-25 10:57:49

标签: c

我只想知道如何保存或存储符号,如+, - ,/和*,以便我可以在if语句中使用它来执行用户输入的任何符号。我知道我的代码是错的,所以任何形式的帮助都将受到赞赏。

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int int1, int2, sum;
    char oper;
    printf("Enter Value Here: ");
    scanf("%d", &int1);
    printf("Enter Operation Here: ");
    scanf("%s", &oper);
    printf("Enter Value Here: ");
    scanf("%d", &int2);
    if (oper == "+")
        sum = int1 + int2;
    printf("The sum is %d", sum);

    return 0;
}

3 个答案:

答案 0 :(得分:1)

你可以这样做:

#include<stdio.h>

int main(void){
    int int1, int2, sum=0;
    char op;


    printf("Please enter one of the following Operators [*] [/] [+] [-]   ");
    if((scanf("%c",&op)) != 1){
        printf("Error\n");
    }

    printf("Enter Value Here: ");
    if((scanf("%d", &int1)) != 1){
        printf("Error\n");
    }

    printf("Enter Value Here: ");
    if((scanf("%d", &int2)) != 1){
        printf("Error\n");
    }

    if (op == '/'){
        sum = int1 / int2;
    }else if(op == '*'){
        sum = int1 * int2;
    }else if(op == '+'){
        sum = int1 + int2;
    }else if(op == '*'){
        sum = int1 - int2;
    }

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

    return 0;
}

编辑: 为了获得更好的精确度,您可以使用 float double ,如果您不确定,请使用 double

#include<stdio.h>

int main(void){
    float int1, int2, sum=0;
    char op;


    printf("Please enter one of the following Operators [*] [/] [+] [-]   ");
    if((scanf("%c",&op)) != 1){
        printf("Error\n");
    }

    printf("Enter Value Here: ");
    if((scanf("%f", &int1)) != 1){
        printf("Error\n");
    }

    printf("Enter Value Here: ");
    if((scanf("%f", &int2)) != 1){
        printf("Error\n");
    }

    if (op == '/'){
        sum = int1 / int2;
    }else if(op == '*'){
        sum = int1 * int2;
    }else if(op == '+'){
        sum = int1 + int2;
    }else if(op == '*'){
        sum = int1 - int2;
    }

    printf("The sum is %.1f\n", sum);

    return 0;
}

答案 1 :(得分:0)

为什么不在switch中使用C案例

    char oper;
    float num1,num2;
    printf("Enter operator either + or - or * or / : ");
    scanf("%c",&oper);
    printf("Enter two operands: ");
    scanf("%f%f",&num1,&num2);
    switch(oper) {
        case '+':
            printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
            break;
        case '-':
            printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
            break;
        case '*':
            printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
            break;
        case '/':
            printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            printf("Error! operator is not correct");
            break;
    }

现在使用if-else

 //same code
 scanf("%f%f",&num1,&num2);
            if(oper=='+')
                printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
            else if(oper=='-')
                printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
            else if(oper=='*')
                printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
            else if(oper=='/')
                printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
            else
                /* If operator is other than +, -, * or /, error message is shown */
                printf("Error! operator is not correct");

答案 2 :(得分:0)

很可能你正在寻找一个能让你做一些面向对象编程的函数指针:

(mailto:|)[a-z0-9_\.\-\+]+@[a-z0-9\-\.]+\.[a-z]{2,}+)

这样您就可以将功能存储在容器中,也可以将它们传递给其他功能等等。