spoj:ONP-转换表达式

spoj : ONP - Transform the Expression

本文关键字:表达式 转换 ONP- spoj      更新时间:2023-10-16

我正在尝试解决spoj问题:链接它显示了运行时错误,测试用例在我的机器上运行良好。无法找出我程序中的错误。我正在使用队列和堆栈来实现它。

编辑:我已经编辑了源代码,并更正了pop函数中的int temp-->char temp。。但我仍然得到了一个运行时错误;

 #include <iostream>
using namespace std;
int top = -1;
int endQueue = -1;
char stack[505];
char queue[505];

int pref(char a) {
    switch (a) {
        case '+':
        case '-' :return 1;
        break;
        case '*' :
        case '/' :return 2;
        break;
        case '^' :return 3;
        break;
        default: return 0;
    }
}
void push(char a) {
    if (top < 0) 
        top =0;
    stack[++top] = a;
}
char pop() {
    char temp = stack[top];
    top--;
    return temp;
}
void enque(char a) {
    if (endQueue < 0) 
        endQueue = 0;
    queue[++endQueue] = a;
}
char topElement() {
    if (top > -1)
    return stack[top];
else
    return '0';
}


int main() {
    // your code here
    int t;
    char temp;
    char exp[500];
    cin>>t;
    while(t--) {
        cin>>exp;
        //cout<<exp;
        for (int i = 0; exp[i]!= '';i++) {
            if (exp[i]>= 'a' && exp[i] <='z') {
                // variables
                enque(exp[i]);
            } else {
                //operator
                if (exp[i] == ')') {
                    // pop till ( 
                    // and append
                    while (top > -1 ) {
                        temp = pop();
                        if (temp == '(') 
                            break;
                            else
                        enque(temp);
                    }
                } else {
                    while (exp[i] != '(' && top > -1 && pref(topElement()) > pref(exp[i]) ) {
                        char popped = pop();
                        enque(popped);

                    }
                    push(exp[i]);
                }
            }
        }
        while(top > -1) {
            enque(pop());
        }
        for (int j = 0;j<=endQueue;j++) {
            cout<<queue[j];
        }
        cout<<"n";
    }
    return 0;
}

你不认为main中表达式的大小与队列和堆栈的大小不匹配吗

在pop函数中,您已将temp声明为int,而返回类型为char