用C语言在数组中存储算术运算符?

用C语言在数组中存储算术运算符?,c,C,我只是想知道是否有可能将基本算术运算符(+、-、*、/)存储在C中的变量中。我之所以需要这样做,是因为我正在尝试构建一个基本的计算器程序,它最多可以接受5个数字,并且可以对它们执行任何这些操作。现在我能想到的唯一方法是将操作符存储在一个char数组中,然后使用for循环和switch语句对其进行迭代,以决定每个步骤使用哪个操作符。它相当笨重,现在实际上不起作用,我不知道为什么。而且,即使它真的工作了,它也不会遵循我需要它也要执行的操作顺序 这是我的程序输出的一个示例: Enter a numbe

我只是想知道是否有可能将基本算术运算符(+、-、*、/)存储在C中的变量中。我之所以需要这样做,是因为我正在尝试构建一个基本的计算器程序,它最多可以接受5个数字,并且可以对它们执行任何这些操作。现在我能想到的唯一方法是将操作符存储在一个char数组中,然后使用for循环和switch语句对其进行迭代,以决定每个步骤使用哪个操作符。它相当笨重,现在实际上不起作用,我不知道为什么。而且,即使它真的工作了,它也不会遵循我需要它也要执行的操作顺序

这是我的程序输出的一个示例:

Enter a number: 4
Enter an operator (+, -, *, /, or =): +
Enter a number: 8
Enter an operator (+, -, *, /, or =): -
Enter a number: 7
Enter an operator (+, -, *, /, or =): *
Enter a number: 9
Enter an operator (+, -, *, /, or =): /
Enter a number: 2
4.00 + 8.00 - 7.00 * 9.00 / 2.00
0.500000
-6.500000
-6.500000
-3.250000
-3.250000
Result: -3.25
[Finished in 24.13s]
前9行只是输入数字和运算符,第10行按顺序打印输入的数字和运算符,第11-15行应该在每次操作后打印结果,您可以看到这些操作都不正确,最后一行打印结果。你认为是什么导致了数学错误

这是我目前的代码:

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

int main() {
    double nums[5];
    char operators[5];
    double result;
    int i = 0;

    while ((i <= 4) && (operators[i - 1] != '=')) {
        /* Getting the user's input */
        printf("Enter a number: ");
        scanf("%lf", &nums[i]);
        if (i == 4) {
            operators[4] = '=';
        } else {
            printf("Enter an operator (+, -, *, /, or =): ");
            scanf(" %c", &operators[i]);
        }

        i++;
    }
    printf("%.2f %c %.2f %c %.2f %c %.2f %c %.2f\n", nums[0], operators[0], nums[1], operators[1], nums[2], operators[2], nums[3], operators[3], nums[4]);

    for (i = 0; i <= 4; i++) {
        /* Doing the math */
        if (i == 0) {
            /* Getting the value of the first two numbers */
            switch(operators[i]) {
                case '+' :
                    result = nums[0] + nums[1];
                case '-' :
                    result = nums[0] - nums[1];
                case '*' :
                    result = nums[0] * nums[1];
                case '/' :
                    result = nums[0] / nums[1];
            }
            printf("%f\n", result);

        } else {
            /* Iterating through the rest of both arrays and
            continuing to perform operations on the result */
            switch(operators[i]) {
                case '+' :
                    result = result + nums[i + 1];
                case '-' :
                    result = result - nums[i + 1];
                case '*' :
                    result = result * nums[i + 1];
                case '/' :
                    result = result / nums[i + 1];
            }
            printf("%f\n", result);
        }
    }
    // Printing out the answer rounded to 2 decimal points
    printf("Result: %.2f", result);

    return 0;
}
#包括
#包括
int main(){
双nums[5];
字符运算符[5];
双重结果;
int i=0;
而((i首先是错误:

while ((i <= 4) && (operators[i - 1] != '=')) {

如果您创建执行这些操作的函数,那么您可以将指向这些函数的指针存储在解析树中。您所要做的就是规范化函数参数以处理二元和一元操作。您能举一个例子说明我可能如何做到这一点吗?正如我所说,我对C还是很陌生,不知道如何做这些事情t、 这个经典问题的复杂解决方案是使用。好的,这正是我所需要的,只是它太复杂了,我真的不明白里面发生了什么……你能解释一下发生了什么吗?
#include <stdio.h>
#include <stdlib.h>

double op_add(double x, double y)
{
    return x + y;
}

double op_sub(double x, double y)
{
    return x - y;
}

double op_mul(double x, double y)
{
    return x * y;
}

double op_div(double x, double y)
{
    return x / y;
}

typedef double (*op_type)(double, double);

int main() {
    double nums[5];
    char operator;
    op_type operators[5];
    double result;
    int i = 0;

    while (i < 5) {
        /* Getting the user's input */
        printf("Enter a number: ");
        scanf("%lf", &nums[i]);
        if (i == 4) {
            operators[i] = NULL;
        } else {
            printf("Enter an operator (+, -, *, /, or =): ");
            scanf(" %c", &operator);
            switch(operator) {
                case '+' :
                    operators[i] = op_add;
                    break;
                case '-' :
                    operators[i] = op_sub;
                    break;
                case '*' :
                    operators[i] = op_mul;
                    break;
                case '/' :
                    operators[i] = op_div;
                    break;
                default :
                    operators[i] = NULL;
                    break;
            }
        }
        if (!operators[i]) break;
        i++;
    }

    result = nums[0];
    for (i = 1; i < 5; i++) {
        if (operators[i-1]) {
            result = operators[i-1](result, nums[i]);
            printf("%f\n", result);
        } else {
            break;
        }
    }
    // Printing out the answer rounded to 2 decimal points
    printf("Result: %.2f", result);

    return 0;
}