写了一个中缀转后缀的程序,怎么出错?自己看不明白,求助。
#include<iostream.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
const int MaxStacksize=50;
typedef char DataType
class Stack
{
public:
Stack(void);
void Push( DataType & item);
DataType Pop(void);
DataType Peek(void)const;
int StackEmpty(void)const;
int StackFull(void)const;
private:
DataType stacklist[MaxStacksize];
int top;
};
Stack::Stack(void):top(-1)
{}
void Stack::Push( DataType &item)
{
if(top==MaxStacksize-1)
{
cout<<"The stack is full!"<<endl;
exit(1);
}
top++;
stacklist[top]=item;
}
DataType Stack::Pop(void)
{
DataType temp;
if(top==-1)
{
cout<<"Attempt to pop an empty
stack!"<<endl;
exit(1);
}
temp=stacklist[top];
top--;
return temp;
}
DataType Stack::Peek(void)const
{
if(top==-1)
{
cout<<"Attempt to peek an empty
stack!"<<endl;
exit(1);
}
return stacklist[top];
}
int Stack::StackEmpty(void)const
{
return top==-1;
}
int Stack::StackFull(void)const
{
return top==MaxStacksize-1;
}
void infixToPostfix(string &infixStr);
void main()
{
char *infixs;
char *postfixs;
cout<<"Please input the infix expression:"<<endl;
cin>>infixs;
cout<<"The postfix expression is:"<<endl;
cout<<infixToPostfix(infixs)<<endl;
}
void infixToPostfix(string &infixStr)
{
Stack <char> tStack;
string result("");
char op;
int i=0;
while(infixStr[i]!='\0')
{
if(isdigit(infixStr[i]))
{
while(isdigit(infixStr[i]))
result+=infixStr[i++];
result+=' ';
}
else
switch(op=infixStr[i++])
{
case '(':
tStack.Push('(');
break;
case ')':
while(tStack.Peek()!='(')
result+=string(tStack.Pop());
tStack.Pop();
break;
case '+':case '-':
while((!tStack.StackEmpty())&&(tStack.Peek()!='('))
result+=string(tStack.Pop());
tStack.Push(op);
break;
case '*':case '/':
while((!tStack.StackEmpty())&&((tStack.Peek()=='*')||(tStack.Peek()=='/')))
result+=string(tStack.Pop());
tStack.Push(op);
break;
}
}
while(!tStack.StackEmpty())
result+=string(tStack.Pop());
infixStr=result;
}
这是源程序,编译结果如下:
:\english\convert.cpp(8) : error C2143: syntax error : missing ';' before '<class-head>'
D:\english\convert.cpp(8) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
convert.exe - 2 error(s), 0 warning(s)
请各位指教指教,谢谢啊!