c - C中程序的错误答案(中缀符号->后缀符号)

标签 c stack

我制作了一个程序,通过在 C 中使用堆栈将中缀表示法更改为后缀表示法。

我做了一个函数来打印后缀符号(来自中缀符号)的结果。

但是,符号的结果是错误的。这应该是“195”,但它的结果是“-61”。

其他notation的结果都是正确的,只有notation(op2)有这个问题。

我应该怎么做才能解决这个问题?

这是我的代码:

typedef char element;
 
typedef struct {
    element stack[MAX_STACK_SIZE];
    int top;
} StackType;
 

void init(StackType *s) {
    s->top = -1;
}

int is_empty(StackType *s) {
    return (s->top == -1);
}

int is_full(StackType *s) {
    return (s->top == (MAX_STACK_SIZE - 1));
}

 
void push(StackType *s, element item) {
    if (is_full(s)) {
        fprintf(stderr, "FULL STACK ERROR\n");
        return;
    }
    else s->stack[++(s->top)] = item;
}
 
element pop(StackType *s) {
    if (is_empty(s)) {
        fprintf(stderr, "EMPTY STACK ERROR\n");
        exit(1);
    }
    else return s->stack[(s->top)--];
}
 
 
int eval(char exp[]) {
    int op1, op2, value, i = 0;
    int len = strlen(exp);
    char ch;
    StackType s;
    init(&s);
    for (i = 0; i < len; i++) {
        ch = exp[i];
        if (ch != '+' && ch != '-' && ch != '*' && ch != '/') {
            value = ch - '0';        
            push(&s, value);
        }
        else {                          
            op2 = pop(&s);
            op1 = pop(&s);
            switch (ch) {            
            case '+': push(&s, op1 + op2); break;
            case '-': push(&s, op1 - op2); break;
            case '*': push(&s, op1 * op2); break;
            case '/': push(&s, op1 / op2); break;
            }
        }
    }
    return pop(&s);
}
 
char* infix_to_postfix(char exp[]) {
    int i = 0, j = 0;
    char ch, top_op;
    int len = strlen(exp);
    char *ex = (char *)malloc(sizeof(char)*(len + 1));
    StackType s;

    init(&s);
    for (i = 0; i < len; i++) {
        ch = exp[i];
        switch (ch) {
        case '+': case '-': case '*': case '/': 
            while (!is_empty(&s) && (prec(ch) <= prec(peek(&s)))) {
                ex[j++] = pop(&s);
            }
            push(&s, ch);
            break;
        case '(':       
            push(&s, ch);
            break;
        case ')':       
            top_op = pop(&s);
            while (top_op != '(') {
                ex[j++] = top_op;
                top_op = pop(&s);
            }
            break;
        default:
            ex[j++] = ch;
            break;
        }
    }
    while (!is_empty(&s)) {
        ex[j++] = pop(&s);
    }
    ex[j] = NULL;           
    return ex;
}

void main() {
    char *op1 = "(9-(3+2))*3+4*((4+2)/3)-1";
    char *op2 = "(4*5-3)/3+((2+5*7)-8+9)*5";          
    char *op3 = "7*3-7-4+1/3+6-8*2";
    char *pos1, *pos2, *pos3;
 
    pos1 = infix_to_postfix(op1);
    pos2 = infix_to_postfix(op2);
    pos3 = infix_to_postfix(op3);
 
    printf(" Result : %d\n", eval(pos1));
    printf(" Result : %d\n", eval(pos2));
    printf(" Result : %d\n", eval(pos3));
}

[结果]

Result : 19

Result : -61 // This should be '195'.

Result : 0

最佳答案

线索是61+195 = 256。这意味着在某个地方,您的计算被保存为字符。看起来是 element

typedef element int;

关于c - C中程序的错误答案(中缀符号->后缀符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44157718/

相关文章:

c++ - 如果我在同一语句中使用预增量和后增量会怎样?

C 目录和子目录递归

c - 为什么 for 循环中的 Push 函数在每次迭代中被多次调用,而不是每次迭代只调用一次?

java - 我应该使用什么样的堆栈数据结构?

c - c 中函数不能返回 null

c - C 中函数参数的自省(introspection)是否可能?

c - 哪种查找方法对于简单的整数到字符串查找最有效

c - strtol 未按预期运行,c

c - 程序集如何访问/存储堆栈上的变量

gcc - 为什么 GCC 在分配一个没有后续函数调用的大数组时将错误的值减去堆栈指针?