写了一个中缀转后缀的程序,怎么出错?自己看不明白,求助。

sakaer 2003-09-21 11:20:04
#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)
请各位指教指教,谢谢啊!
...全文
31 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sakaer 2003-09-21
  • 打赏
  • 举报
回复
谢谢,结帖。
lionhu2003 2003-09-21
  • 打赏
  • 举报
回复
错误还不少,不止两个
改正后如下:

#include<iostream>
#include<string>
#include<ctype.h>
#include<stdlib.h>

using namespace std;

const int MaxStacksize=50;
typedef char DataType;

class Stack
{
public:
Stack(void);
void Push( const 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( const 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)
{
Stack 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+=tStack.Pop();
tStack.Pop();
break;
case '+':case '-':
while((!tStack.StackEmpty())&&(tStack.Peek()!='('))
result+=tStack.Pop();
tStack.Push(op);
break;
case '*':case '/':
while((!tStack.StackEmpty())&&((tStack.Peek()=='*')||(tStack.Peek()=='/')))
result+=tStack.Pop();
tStack.Push(op);
break;
}
}
while(!tStack.StackEmpty())
result+=tStack.Pop();
infixStr=result;
}

void main()
{
string infixs;

cout<<"Please input the infix expression:"<<endl;
cin>>infixs;
cout<<"The postfix expression is:"<<endl;
infixToPostfix(infixs);
cout<<infixs<<endl;
}



sakaer 2003-09-21
  • 打赏
  • 举报
回复
除此之外呢?好像关于string的使用以及void infixToPostfix(string &infixStr)的使用也存在这很大的错误,能不能帮我看一下?一下是加上‘;’后的结果。
D:\english\convert.cpp(62) : error C2065: 'string' : undeclared identifier
D:\english\convert.cpp(62) : error C2065: 'infixStr' : undeclared identifier
D:\english\convert.cpp(62) : error C2182: 'infixToPostfix' : illegal use of type 'void'
D:\english\convert.cpp(71) : error C2064: term does not evaluate to a function
D:\english\convert.cpp(74) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
Error executing cl.exe.
tonybaobao 2003-09-21
  • 打赏
  • 举报
回复
typedef char DataType;//缺少分号

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧