SPOJ - 运行时错误SIGSEGV

时间:2016-11-08 13:28:21

标签: c++11 stack runtime-error sigsegv

以下是中缀到后缀转换的实现,它在我的计算机上工作正常,但是当我在SPOJ上提交它时,它给了我运行时错误SIGSEGV ,我是竞争性编程的新手,我无法处理这类错误。

#include <iostream>
#include <stack>
#include<string.h>
#include<ctype.h>
using namespace std;
int prec(char ch){
    switch(ch){
        case '^' : return 3;
                    break;
        case '*':
        case '/': return 2;
                break;
        case '+':
        case '-': return 1;
                break;
        default: return -1;
    }
}
void pti(char a[]){
    stack<int> post;
    int k = -1;
    for(int i = 0;i<strlen(a);i++){
        if(isalnum(a[i]))
            a[++k] = a[i];
        else if(a[i] == '(')
            post.push(a[i]);
        else if(a[i] == ')'){
            while(!post.empty() && post.top()!= '('){
                a[++k] = post.top();
                post.pop();
            }
            post.pop();
        }
        else {
            while(!post.empty() && prec(a[i]) <= prec(post.top())){
                a[++k] = post.top();
                post.pop();
            }
            post.push(a[i]);
        }
    }
    while(!post.empty()){
        a[++k] = post.top();
        post.pop();
    }
    a[++k] = '\0';
    cout<<a<<endl;
}
int main()
{
   int t;
   cin>>t;
   for(int i = 0;i<t;i++){
        char a[100];
        cin>>a;
        pti(a);
   }
}

1 个答案:

答案 0 :(得分:0)

您只需要使输入数组更长,例如大小为1000得到AC。

SIGSEGV信号表示发生了分段故障,这基本上意味着您访问了不属于您的内存。

相关问题