我正在使用C语言的GMP库创建一个具有无限精度的二进制计算器.在计算后缀表达式的过程中,当我试图将结果赋给一个变量时,我遇到了错误"Assignment to Expression with ARRAY TYPE".

我搞不懂为什么结果(ans)被视为数组,以及如何正确地将后缀表达式的值赋给ans.evaluatePostfix函数应该返回单个值,而不是array.

如能深入了解此错误发生的原因,并指导如何将结果正确分配给变量,我们将不胜感激.

下面是我的代码:

mpf_t evaluatePostfix(char* postfix) { //postfix func
    Stack operandStack;
    initializeStack(&operandStack);

    int i;
    mpf_t operand, op1, op2, result;
    mpf_init(operand);
    mpf_init(op1);
    mpf_init(op2);
    mpf_init(result);

    for (i = 0; postfix[i] != '\0'; i++) {
        char token = postfix[i];

        if (isOperand(token)) {
            mpf_set_str(operand, &postfix[i], 10);
            push(&operandStack, operand);
            while (postfix[i] != ' ') {
                i++;
            }
        } else if (token != ' ') {
            mpf_set_ui(op2, pop(&operandStack));
            mpf_set_ui(op1, pop(&operandStack));

            switch (token) {
                case '+':
                    mpf_add(result, op1, op2);
                    break;
                case '-':
                    mpf_sub(result, op1, op2);
                    break;
                case '*':
                    mpf_mul(result, op1, op2);
                    break;
                case '/':
                    if (mpf_cmp_d(op2, 0.0) != 0) {
                        mpf_div(result, op1, op2);
                    } else {
                        fprintf(stderr, "Error: Division by zero\n");
                        exit(EXIT_FAILURE);
                    }
                    break;
                case '%':
                    fprintf(stderr, "Error: Modulo operation not supported for floating-point numbers\n");
                    exit(EXIT_FAILURE);
                case '^':
                    fprintf(stderr, "Error: Exponentiation operation not supported for floating-point numbers\n");
                    exit(EXIT_FAILURE);
                default:
                    fprintf(stderr, "Invalid operator\n");
                    exit(EXIT_FAILURE);
            }

            push(&operandStack, result);
        }
    }

    mpf_clear(operand);
    mpf_clear(op1);
    mpf_clear(op2);

    // Create a new variable to store the result and copy the value
    mpf_t finalResult;
    mpf_init(finalResult);
    mpf_set(finalResult, result);

    // Clear the original result
    mpf_clear(result);

    return finalResult;
}

int main() {//driver
    while (1) {
        struct Num* linked_list = NULL;
        char temp;

        printf("\nEnter infix expression: ");
        while ((temp = getchar()) != '\n') {
            append(&linked_list, temp);
        }

        char* infix = linkedListToString(linked_list);

        char* postfix = (char*)malloc(strlen(infix) + 1);

        infixToPostfix(infix, postfix);
        printf("Postfix expression: %s\n", postfix);

        mpf_t ans;
        mpf_init(ans);
        ans = evaluatePostfix(postfix);
        gmp_printf("\nResult: %.2Ff\n", ans);

        free(postfix);
        free(infix);
        freeLinkedList(linked_list);
        printf("\nCtrl + c to exit\n");
    }

    return 0;
}

推荐答案

GMP documentation种类型开始:

在内部,诸如MPZ_t之类的GMP数据类型被定义为单元素数组,其元素类型是GMP内部的一部分(参见Internals).

所以你不能把它们像普通变量一样赋值.函数应该接收ans作为参数,并在适当的位置更新.

使用mpf_set()将值指定为mpf_t.

void evaluatePostfix(char* postfix, mpf_t finalResult) { //postfix func
    Stack operandStack;
    initializeStack(&operandStack);

    int i;
    mpf_t operand, op1, op2, result;
    mpf_init(operand);
    mpf_init(op1);
    mpf_init(op2);
    mpf_init(result);

    for (i = 0; postfix[i] != '\0'; i++) {
        char token = postfix[i];

        if (isOperand(token)) {
            mpf_set_str(operand, &postfix[i], 10);
            push(&operandStack, operand);
            while (postfix[i] != ' ') {
                i++;
            }
        } else if (token != ' ') {
            mpf_set_ui(op2, pop(&operandStack));
            mpf_set_ui(op1, pop(&operandStack));

            switch (token) {
                case '+':
                    mpf_add(result, op1, op2);
                    break;
                case '-':
                    mpf_sub(result, op1, op2);
                    break;
                case '*':
                    mpf_mul(result, op1, op2);
                    break;
                case '/':
                    if (mpf_cmp_d(op2, 0.0) != 0) {
                        mpf_div(result, op1, op2);
                    } else {
                        fprintf(stderr, "Error: Division by zero\n");
                        exit(EXIT_FAILURE);
                    }
                    break;
                case '%':
                    fprintf(stderr, "Error: Modulo operation not supported for floating-point numbers\n");
                    exit(EXIT_FAILURE);
                case '^':
                    fprintf(stderr, "Error: Exponentiation operation not supported for floating-point numbers\n");
                    exit(EXIT_FAILURE);
                default:
                    fprintf(stderr, "Invalid operator\n");
                    exit(EXIT_FAILURE);
            }

            push(&operandStack, result);
        }
    }

    mpf_clear(operand);
    mpf_clear(op1);
    mpf_clear(op2);

    mpf_set(finalResult, result);

    // Clear the original result
    mpf_clear(result);

    return finalResult;
}

int main() {//driver
    while (1) {
        struct Num* linked_list = NULL;
        char temp;

        printf("\nEnter infix expression: ");
        while ((temp = getchar()) != '\n') {
            append(&linked_list, temp);
        }

        char* infix = linkedListToString(linked_list);

        char* postfix = (char*)malloc(strlen(infix) + 1);

        infixToPostfix(infix, postfix);
        printf("Postfix expression: %s\n", postfix);

        mpf_t ans;
        mpf_init(ans);
        evaluatePostfix(postfix, ans);
        gmp_printf("\nResult: %.2Ff\n", ans);

        free(postfix);
        free(infix);
        freeLinkedList(linked_list);
        printf("\nCtrl + c to exit\n");
    }

    return 0;
}

C++相关问答推荐

为什么PLT表中没有push指令?

如何创建由符号组成的垂直结果图形?

不会停在空格或换行符上的错误

在传统操作系统上可以在虚拟0x0写入吗?

struct 上的OpenMP缩减

为静态库做准备中的奇怪行为

在CLANG中调试预处理器宏

如何使用指向 struct 数组的指针并访问数组中特定索引处的 struct

限制不同类型的限定符

如何在CANbus RX/TX FIFO起始地址寄存器(ATSAME 51)的特定地址初始化数组?

S将C语言宏定义为自身的目的是什么?(在glibc标题中看到)

#定义SSL_CONNECTION_NO_CONST

如何确保我将使用C标准库函数的函数版本,如&getc";,而不是类似函数的宏版本?

C:如何将此代码转换为与数组一起使用?

可以对两种 struct 类型中的任何一种进行操作的C函数

将size_t分配给off_t会产生符号转换错误

GDB 跳过动态加载器代码

malloc:损坏的顶部大小无法找出问题

比 * 更快的乘法

当 a 是代码块时使用逗号运算符 (a, b)