将中缀转换为前缀

时间:2013-10-14 14:48:27

标签: c++

我已经制作了以下程序来将中缀转换为profix,但是我遇到了运行时错误。

  

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

main.cpp:

#include "stack.h"

int main( )
{
    stack<char> s;
    char infix_exp[100]={NULL};
    ofstream file;

    file.open("Infix.txt",ios::binary);
    cout<<"Enter the Infix Expression:";
    cin.getline(infix_exp,80);
    file<<infix_exp;
    file.close();
    s.coversion(infix_exp);
    return 0;
}

stack.h:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <conio.h>

using namespace std;

template <class T>
class stack
{
    int top;
    char Stack[100];

  public:

    stack();
    void push(const char);
    const char pop( );
    void coversion(const char*);    
};


template <class T> 
stack<T>::stack()
{
    top=-1;
    Stack[100]=NULL;
}

template <class T>
void stack<T>::push(const char Symbol)
{
    if(top==99)
        cout<<"Error : Stack is full."<<endl;
else
    {
        top++;
        Stack[top]=Symbol;
    }
}

template <class T>
const char stack<T>::pop()
{
    char Symbol=NULL;
    if(top==-1)
        cout<<"Error : Stack is empty."<<endl;
    else
    {
        Symbol=Stack[top];
        Stack[top]=NULL;
        top--;
    }
    return Symbol;
}

template <class T>
void stack<T>::coversion(const char *infix)
{
    char infix_exp[100]={NULL};
    char postfix_exp[100]={NULL};
    strcpy(infix_exp,"(");
    strcat(infix_exp,infix);
    strcat(infix_exp,")");

    char Symbol[5]={NULL};
    char Temp[5]={NULL};

    for(int count=0; count<strlen(infix_exp); count++)
    {
        Symbol[0]=infix_exp[count];
        if(Symbol[0]=='(')
            push(Symbol[0]);
        else
            if(Symbol[0]==')')
            {
                Symbol[0]=pop( );
                while(Symbol[0]!='(')
                {
                    strcat(postfix_exp,Symbol);
                    Symbol[0]=pop( );
                }
            }
            else
                if (Symbol[0]=='^' || Symbol[0]=='*' ||
                    Symbol[0]=='/' || Symbol[0]=='+' || Symbol[0]=='-')
                {
                    if (Symbol[0]=='*' || Symbol[0]=='/')
                    {
                        Temp[0]=pop( );
                        while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
                        {
                            strcat(postfix_exp,Temp);
                            Temp[0]=pop( );
                        }
                        push(Temp[0]);
                    }
                    else 
                        if(Symbol[0]=='+' || Symbol[0]=='-')
                        {
                            Temp[0]=pop( );
                            while(Temp[0]!='(')
                            {
                                strcat(postfix_exp,Temp);
                                Temp[0]=pop( );
                            }
                            push(Temp[0]);
                        }
                    push(Symbol[0]);
                }
                else
                    strcat(postfix_exp,Symbol);

    } //for
    cout<<"Postfix Expression : "<<postfix_exp<<endl;
    ofstream pfile;
    pfile.open("PostFix.txt",ios::binary);
    pfile<<postfix_exp;
    pfile.close();
}

1 个答案:

答案 0 :(得分:4)

stack构造函数中,您正在访问Stack成员的界限:Stack[100]=NULL;

它被声明为char Stack[100];,因此您只能访问Stack[0] .. Stack[99]