2016-11-08 77 views
1

以下是綴實施與postfix轉換,它工作正常在我的電腦上,但我對SPOJ提交在它給我運行時錯誤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); 
    } 
} 
+0

如果用戶輸入大於100,會發生什麼情況?此外,更喜歡std :: string而不是char數組 – Garf365

回答

0

你只需要增加輸入數組的長度, 1000的大小獲得AC。

SIGSEGV信號意味着發生了分段錯誤,這基本上意味着您訪問了不屬於您的內存。