c语言中栈的四则运算

这是2010版能帮忙看一下错误吗,这个转换和运算应该都有问题,谢谢各位大佬了 #define _CRT_SECURE_NO_WARNINGS #include #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<string.h> #define MaxSize 30 struct //设定运算优先级 { char ch; //运算符 int pri; //优先级 }leftpri[] = { { '=', 0 }, { '(', 1 }, { '', 5 }, { '/', 5 }, { '+', 3 }, { '-', 3 }, { ')', 6 } }, rightpri[] = { { '=', 0 }, { '(', 6 }, { '', 4 }, { '/', 4 }, { '+', 2 }, { '-', 2 }, { ')', 1 } };

inline int LeftPri(char op) { for (int i = 0; i<7; i++) //在leftpri[]数组中找到与之对应的优先级并返回 { if (leftpri[i].ch == op) return leftpri[i].pri; } return (op); } int RightPri(char op) { int i; for (i = 0; i<7; i++) { if (rightpri[i].ch == op) return rightpri[i].pri; } return (op); } bool InOP(char ch) { if (ch == '(' || ch == ')' || ch == '+' || ch == '-' || ch == '*' || ch == '/') return true; else return false; } int ConpareCode(char op1, char op2) { if (LeftPri(op1) == RightPri(op2)) return 0; else if (LeftPri(op1)<RightPri(op2)) return -1; else return 1; } void Transsform(char *exp, char poseExp[]) { char postExp[30]; struct { char data[MaxSize]; int top; }op; int i = 0; int flag = 0; op.top = -1; //令其指向空 op.top++; op.data[op.top] = '='; //top=0,将栈顶存入“=”作为结束的标语 while (*exp != '\0') { if (!InOP(*exp)) { while (*exp >= '0' && *exp <= '9') { postExp[i++] = *exp; exp++; } postExp[i++] = '#'; } else { switch (ConpareCode(op.data[op.top], *exp)) //top = 0即op.data[op.top] = '=' { case -1: op.top++; //top = 1 op.data[op.top] = *exp; exp++; break; case 0: op.top--; //top = 0 exp++; break; case 1: postExp[i++] = op.data[op.top]; op.top--; break; } }

}
while (op.data[op.top] != '=') //减到0之后就退出,
{

    postExp[i++] = op.data[op.top];
    op.top--;  //top的值越大,优先级就越高!
}
postExp[i] = '\0';

}

float Compute(char *postExp) { struct { float data[MaxSize] int top; } ser;

float a, b, c, d;
ser.top = -1;
while (*postExp != '\0')
{
    switch (*postExp)
    {
    case '+':
        a = ser.data[ser.top];
        ser.top--;
        b = ser.data[ser.top];
        ser.top--;
        c = a + b;
        ser.top++;
        ser.data[ser.top] = c;
        break;
    case '-':
        a = ser.data[ser.top];
        ser.top--;
        b = ser.data[ser.top];
        ser.top--;
        c = b - a;
        ser.top++;
        ser.data[ser.top] = c;
        break;
    case '*':
        a = ser.data[ser.top];
        ser.top--;
        b = ser.data[ser.top];
        ser.top--;
        c = b * a;
        ser.top++;
        ser.data[ser.top] = c;
        break;
    case '/':
        a = ser.data[ser.top];
        ser.top--;
        b = ser.data[ser.top];
        ser.top--;
        if (a != 0)
        {
            c = b / a;
            ser.top++;
            ser.data[ser.top] = c;
        }
        else
        {
            printf("\n\t格式错误");
            exit(0);
        }

        break;
    default:
        d = 0;
        while (*postExp >= '0' && *postExp <= '9')
        {
            d = 10 * d + *postExp - '0';
            postExp++;               //两个postExp++恰好跳过了‘#’符号
        }
        ser.top++;
        ser.data[ser.top] = d;

    }
    postExp++;
}
return(ser.data[ser.top]);

} int main() { char exp[30]; printf("计算器: \n\n"); printf("请输入数学运算公式: "); scanf("%s", exp); char postExp[MaxSize]; //postExp是储存exp转换的后缀表达式数组,MaxSize是栈储存最大容量 Transsform(exp, postExp); printf("答案: %g\n\n", Compute(postExp)); getchar(); getchar(); }

参考GPT和自己的思路:

根据代码的分析和实现,该程序存在以下问题:

  1. typedef struct 应该在结构体前面定义,否则会出现编译错误;

  2. 在结构体中添加了'='运算符,但实际上并没有使用,可以删除;

  3. 在比较运算符优先级时,乘号和除号的转义符使用错误,应该使用'*'和'/';

  4. 在InOP函数中,乘号和除号的判断使用了转义符,应该改为'*'和'/';

  5. 在while循环中,缺少以'.'结尾的处理,可以在while循环后加入postExp[i] = '.';

  6. 在Compute函数中,当运算符无法识别时,应该输出错误提示并退出程序;

  7. 在Compute函数中,除数为0时,应该输出错误提示并退出程序;

  8. 在Compute函数中,数字使用float型而非double型,可能会导致精度问题。

以上是该程序可能存在的问题和需要修改的地方,建议在修改后进行测试和验证。