【问题标题】:Copying a Stack to a stack without Segmentation Fault在没有分段错误的情况下将堆栈复制到堆栈
【发布时间】:2016-08-30 18:03:09
【问题描述】:

我正在开发的程序是一个下推自动机,它只接受 a^nb^n 的输入:$ab、$aabb、$aaabbb 等。除了 4主while循环的迭代它得到一个分段错误。我认为这与我在每个循环中都将堆栈复制到另一个堆栈中的事实有关,但我不明白为什么它不只是每次迭代都出错。非常感谢任何帮助!

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
{
    cout << "Enter an expression: ";
    string expression;
    getline(cin, expression);

    stack<char> expressionStack;
    stack<char> unreadStack;
    stack<char> expressionStackPOP;
    stack<char> unreadStackPOP;
    char expressionOutput;
    char readInput;
    char transitionState = 'p';
    int step=0;
    int counter;
    int trule = 0;
    int rrule = 0;
    char expressionO;
    char unreadO;

    //Enter expression into Stack<char> unreadInput
    for(unsigned int i = 0; i<expression.length(); i++)
    {
        unreadStack.push(expression[i]
            );
    }
    counter = expression.length();
    cout << "Got passed putting the expression in unreadStack" << endl;
    cout <<"Step"<< "\tState" << "\tUnread Input" << "\t\tStack" <<"\t\tΔ Rule used " << "\tR rule used "<< endl;
    while(!unreadStack.empty())
    {
        switch(transitionState)
        {
        case 'p':
            expressionStack.push('S');
            transitionState = 'q';
            trule = 1;
            //cout << "Changed state to P and pushed S into stack" << endl;
            break;
        case 'q':
            if(unreadStack.top() == 'a' && transitionState=='q')
            {
                readInput = unreadStack.top();
                unreadStack.pop();
                transitionState = 'a';
                trule = 2;
                //cout << "Changed state to qa and popped a from unread stack" << endl;
            }
            if(unreadStack.top() == 'b' && transitionState=='q')
            {
                readInput = unreadStack.top();
                unreadStack.pop();
                transitionState = 'b';
                trule = 3;
                //cout << "Changed state to qb and popped b from unread stack"<<endl;
            }
            if(unreadStack.top() == '$' && transitionState=='q')
            {
                readInput = unreadStack.top();
                unreadStack.pop();
                transitionState = '$';
                trule = 4;
                //cout << "This is the end" << endl;
            }
            break;
        case 'a':
            //cout << "in case a" << endl;
            if(readInput=='a' && transitionState=='a')
            {
                expressionStack.pop();
                transitionState='q';
                trule = 5;
                //cout << "Changed state to q and popped a from stack" << endl;
            }
            if(readInput=='S' && transitionState=='a')
            {
                expressionStack.pop();
                expressionStack.push('b');
                expressionStack.push('S');
                expressionStack.push('a');
                transitionState='q';
                trule = 6;
                //cout << "Changed state to q and popped S from stack and pushed aSb into stack" << endl;
            }
            break;
        case 'b':
            if(readInput=='b' && transitionState=='b')
            {
                expressionStack.pop();
                transitionState='q';
                trule = 7;
                //cout << "Changed state to q and popped b from stack" << endl;
            }
            if(readInput=='S' && transitionState=='b')
            {
                expressionStack.pop();
                transitionState='b';
                trule = 8;
                //cout << "Changed state to qb and popped S from stack" << endl;
            }
            break;
        case '$':
            cout<<"test"<<endl;
            break;
            trule = 9;
        }

        //Output RIGHT HERE THIS IS THE SPOT-----------------------------------------------------
        expressionStackPOP = expressionStack;
        unreadStackPOP = unreadStack;
        //--------------------------------------------------------------

        if (step<=9)
            cout<<" ";
        cout <<step<<"\t"<<transitionState<<"\t";
        while(!unreadStackPOP.empty())//copy unreadStackPOP stack to string
        {
            cout<<unreadO;
            unreadO = unreadStackPOP.top();
            unreadStackPOP.pop();
        }
        cout<<"\t\t\t";
        while(!expressionStackPOP.empty())//copy expressionStackPOP stack to string
        {
            cout<<expressionO;
            expressionO = expressionStackPOP.top();
            expressionStackPOP.pop();
        }
        cout<<"\t\t"<<trule<<"\t\t"<<rrule<<endl;
        step++;
    }
}

【问题讨论】:

  • 看来至少偶尔检查一下expressionStack.empty() 会在这段代码的很大一部分中证明是有用的。

标签: c++ segmentation-fault stack


【解决方案1】:

这段代码:

        if(unreadStack.top() == 'a' && transitionState=='q')
        {
            readInput = unreadStack.top();
            unreadStack.pop();
            transitionState = 'a';
            trule = 2;
            //cout << "Changed state to qa and popped a from unread stack" << endl;
        }
        if(unreadStack.top() == 'b' && transitionState=='q')
        {

考虑一下,如果在这段代码的开头,unreadStack 只包含一个'a',会发生什么。

第二个if 语句会尝试做什么?

【讨论】:

  • 对不起,我忘了说,自动机只接受 a^nb^n 的输入:$ab、$aabb、$aaabbb 等。
猜你喜欢
  • 2011-12-30
  • 1970-01-01
  • 2013-12-31
  • 2012-05-21
  • 2018-03-29
  • 2012-04-23
  • 2018-09-28
  • 2012-05-01
  • 1970-01-01
相关资源
最近更新 更多