运行时检查失败#2:变量'expression'周围的堆栈已损坏

时间:2014-04-03 19:42:34

标签: c++ c memory data-structures

以下是我将infix转换为postfix符号的代码,之后postfix也正在评估中。此代码工作正常并给出正确的答案,但最终我在运行时收到此错误。

为什么会出现此错误? 我正在使用Viusal C++ 2010.

enter image description here

代码:

#include<iostream>
#include<stack>
#include<string.h>

using namespace std;


int getPrecedence( char tmp )
{
    if(tmp=='+')
    {
        return 1;
    }
    else if(tmp == '-')
    {
        return 1;
    }
    else if(tmp == '*')
    {
        return 2;
    }
    else if(tmp == '/')
    {
        return 2;
    }
    else if(tmp == '=')
    {
        return 0;
    }
    else if(tmp == '(')
    {
        return -1;
    }
    else if(tmp == ')')
    {
        return -1;
    }
}

int main()
{

    stack<char> st;

    char expression[10];

    cout<<"Enter expression : ";
    //cin>>expression;
    strcpy(expression,"x=(0+1)+(y=2+3)");
    char postfix[100];  // its postfix string
    int counter=0;

    int i=0;

    int bracketCheck = 0;

    //(a+b)
    while( expression[i] != '\0' )  // iterate till '/0' does not come.
    {
        if(expression[i]== '+' || expression[i]== '-' || expression[i]== '*' || expression[i]== '/' || expression[i]== '='  )
        {
            if( st.empty() )
            {
                st.push(expression[i]);
            }
            else // when stack not empty
            {
                int topPrecedence = getPrecedence( st.top() );
                int expressionPrecedence = getPrecedence( expression[i] );


                while( !(topPrecedence < expressionPrecedence) )
                {
                    postfix[counter++] = st.top();
                    st.pop() ;

                    if(! st.empty() )
                    {
                        topPrecedence = getPrecedence( st.top() );
                    }
                    else
                    {
                        break;
                    }


                }

                //if( st.empty() )
                //{
                //  st.push( expression[i] );
                //}

                if( topPrecedence < expressionPrecedence )
                {
                    st.push( expression[i] );
                }


            }
        }
        else if( expression[i]=='(' )
        {
            st.push( expression[i] );
            bracketCheck++;
        }
        else if( expression[i]==')' )
        {
            int topPrecedence = getPrecedence( st.top() );
            int expressionPrecedence = getPrecedence( expression[i] );

            while( topPrecedence >= expressionPrecedence)   // +>=) --- 1 >= -1 ------- +>=) --- -1 >= -1
            {

                if( getPrecedence( st.top() ) != -1 )
                {
                    postfix[counter++] = st.top();
                }
                char BracketFound = st.top();
                st.pop();
                if( !st.empty() )
                    topPrecedence = getPrecedence( st.top() );
                if( st.empty() )   // break out of loop when stack is empty
                    break;
                if( BracketFound == '(' )
                    break;

            }

        }
        else // when its an alphabet 
        {
            postfix[counter++] = expression[i];
        }


        i++;
    } // outer while ends 

    while( ! st.empty() )
    {
        postfix[counter++] = st.top();
        st.pop();
    }


    postfix[counter] = '\0';
    i=0;

    while( postfix[i] != '\0' )
    {
        cout<<postfix[i]<<" ";
        i++;
    }

    stack<char> e; // eval stands for evaluation
    i=0;

    while( postfix[i] != '\0' )
    {
        if( postfix[i] == '+' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left + right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );

        }
        else if( postfix[i] == '-' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left - right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );
        }
        else if( postfix[i] == '*' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left * right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );
        }
        else if( postfix[i] == '/' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left / right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );
        }
        else if( postfix[i] == '=' )
        {
            int right = e.top();
            //left = left - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            //right = right - 48;
            e.pop();


            //int result = right + left;
            //result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( right );

        }
        else
            e.push( postfix[i] );
        i++;
    }

    // while ends

    cout<<endl;
    cout<<"result= "<<e.top()<<endl;





    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:5)

嗯,我看到的第一个问题是你将15个字符+空终止符复制到表达式[10]中。基本上,你将16磅重装入10磅重的袋中。

答案 1 :(得分:0)

char expression[10];

//<snip>

strcpy(expression,"x=(0+1)+(y=2+3)");

查看您要复制到expression的内容的长度(不要忘记尾随\0)。您已在阵列中分配了10个空格,但是您尝试在其中存储16个空格,并且您的缓冲区已经溢出。