在 SPOJ 上获取运行时错误(SIGSEGV),无法找出我的代码中的错误

Getting Runtime Error(SIGSEGV) on SPOJ , can't find out what's wrong in my code

我知道以前有人问过这种类型的问题,但我找不到解决方法,我知道它是无效内存引用错误或数组越界,但我似乎找不到原因我的代码中的错误。 我刚刚在 SPOJ 上试过这个问题,是 'Transform the Expression' https://www.spoj.com/problems/ONP/ 我所有的测试用例都是正确的!

这是我的代码:

#include <bits/stdc++.h>
#include<string>
using namespace std;


int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
string str;
std::stack<char> f ;
cin>>t;
while(t--){
    cin>>str;
    string ans="";
    for(int i=0;i<str.size();i++){
        switch(str[i]){
            case '(':break;
            case '+':
            case '*':
            case '-':
            case '^':
               f.push(str[i]);
               break;
            case ')':
               ans+=f.top();
               f.pop();
               break;
            default:
               ans+=str[i];
               break;
        }

        }
        cout<<ans<<endl;
        
    }

return 0;
}
#include <bits/stdc++.h>
using namespace std;


int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin>>t;
while(t--){
    string str;
    std::stack<char> f ;
    cin>>str;
    string ans="";
    for(int i=0;i<str.size();i++){
        switch(str[i]){
            case '(':
                break;
            case '+':
            case '*':
            case '/': // Change 1
            case '-':
            case '^':
               f.push(str[i]);
               break;
            case ')':
            if(f.size()>0){  // Change 2
               ans+=f.top();
               f.pop();
            }
               break;
            default:
               ans+=str[i];
               break;
        }
        }
        if(f.size()>0){
            while(f.size()>0){
                ans+=f.top();
                f.pop();
            }
        }
        cout<<ans<<endl;
        
    }

return 0;
}

您的代码中有两个错误

  1. 您忘记添加“/”(这会给 WA)
  2. 如果堆栈仍为空,您的代码仍在将其添加到字符串并弹出空堆栈

(我对你的代码做了一些修改)