C编程计算器使用switch语句和调用函数

时间:2017-02-08 04:12:48

标签: c

一切都顺利但我只是想知道为什么它没有读取switch语句并输入运算符。

EX:输入第一个数字5然后自动读取默认语句输入第二个数字2,结果为0。

float addNum(float, float);
float subtractNum(float, float);
float multiplyNum(float, float);
float divideNum(float, float);

#include <stdio.h>
#include <ctype.h>

int main()
{
    char opNum;
    float result;
    float firstNum;
    float secNum;

    printf("Enter first number: ");
    scanf("%f", &firstNum);
    printf("Enter the operator:\n");
    scanf("%c", &opNum);
    switch (opNum)
    {
        case '+':
            result = addNum(firstNum, secNum);
            break;

        case '-':
            result = subtractNum(firstNum, secNum);
            break;

        case '*':
            result = multiplyNum(firstNum, secNum);
            break;

        case '/':
            result = divideNum(firstNum, secNum);
            break;

        default:
            printf("The operator you entered is invalid.");
    }
    printf("Enter second number: ");
    scanf("%f", &secNum);
    printf("Your total number is: %f", result);
    return 0;
}

float addNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum + secNum;
    return result;
}

float subtractNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum - secNum;
    return result;
}

float multiplyNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum * secNum;
    return result;
}

float divideNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum / secNum;
    return result;
}

0 个答案:

没有答案