大佬帮忙看看,下面这段代码为啥老是段错误呢?本地IDE可以通过示例。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
    ///a/./b/../../c/
    string s;
    while (cin >> s)
    {
        string res;
        int n = s.size();
        stack<string> st;
        stack<string> st2;
        int start = 0;
        int end = 0;
        int flag = 0;
        while (1)
        {
            start = s.find_first_of('/', start);
            if (start == -1)
            {
                st.push(s);
                break;
            }
            string str;
            end = s.find_first_of('/', start + 1);
            str = s.substr(start + 1, end - start - 1);
            start = end;
            if (str == "")
            {
                break;
            }
            if (str == "..")
            {
                st.pop();
            }
            else if (str == ".")
            {
                continue;
            }
            else
            {
                st.push(str);
            }
        }
        for (int i = 0; i < st.size();)
        {
            string top = st.top();
            st.pop();
            st2.push(top);
        }
        for (int i = 0; i < st2.size();)
        {
            string top = st2.top();
            cout << "/" << top;
            st2.pop();
        }
        cout << endl;
    }

    return 0;
}