急求!!C++四则运算问题(附源代码)
#include<iostream>
using namespace std;
template<class T>
class Stack
{
public:
Stack();
virtual ~Stack();
virtual bool IsEmpty()const=0;
virtual bool IsFull()const=0;
virtual T&GetTop(void) const=0;
virtual bool Push(const T&elem)=0;
virtual bool Pop(T&x)=0;
virtual bool Pop()=0;
virtual void ClearStack()=0;
virtual void Output(ostream&out)const=0;
};
#include"stack.h"
template<class T>
class SeqStack:public Stack<T>
{
public:
SeqStack(int MaxStackSize=10);
~SeqStack(){if(stack)delete[]stack;}
virtual bool IsEmpty()const{return top==-1;}
virtual bool IsFull()const{return top==MaxSize-1;}
T&GetTop()const;
virtual bool Push(const T&x);
virtual bool Pop(T&x);
virtual bool Pop();
virtual void ClearStack(){top=-1;};
virtual void Output(ostream&out)const;
private:
int top;
int MaxSize;
T*stack;
};
template<class T>
SeqStack<T>::SeqStack(int MaxStackSize)
{MaxSize=MaxStackSize;stack=new T[MaxStackSize];
if(stack==NULL) throw NoMem();top=-1;
}
template<class T>
T&SeqStack<T>::GetTop() const
{ if(IsEmpty()) throw OutOfBounds();else return stack[top];}
template<class T>
bool SeqStack<T>::Push(const T&x)
{
if(IsFull()) throw OutOfBounds();stack[++top]=x;
return true;
}
template<class T>
bool SeqStack<T>::Pop(T&x)
{if (IsEmpty()) throw OutOfBounds();x=stack[top--];
return true;
}
template<class T>
bool SeqStack<T>::Pop()
{if(IsEmpty()) throw OutOfBounds(); top--;return true;}
template<class T>
void SeqStack<T>::Output(ostream& out) const
{for (int i=0;i<=top;i++)out<<stack[i]<<" ";}
template<class T>
ostream& operator<<(ostream& out,const SeqStack<T>&x)
{x.Output(out);
return out;
}
class NoMem{
public:
NoMem(){cout<<"memory is out enough";};
};
class OutOfBounds{
public:
OutOfBounds(){cout<<"out of bounds"<<endl;};
};
#include"SeqStack.h"
const int Max=50;
bool In(char c,char *OPx)
{char Inx;
char* OP;
Inx=c;OP=OPx;
for(int i=0;i<4;i++){if (OP[i]==Inx) return true;}
return false;}
char Precede(char x,char c)
{char a,b;
a=x,b=c;
if (a=='+'&&b=='+')return '>';
if (a=='+'&&b=='-')return '>';
if (a=='+'&&b=='*')return '>';
if (a=='+'&&b=='/')return '>';
if (a=='+'&&b=='(')return '<';
if (a=='+'&&b==')')return '>';
if (a=='+'&&b=='#')return '<';
if (a=='-'&&b=='+')return '>';
if (a=='-'&&b=='-')return '>';
if (a=='-'&&b=='*')return '>';
if (a=='-'&&b=='/')return '>';
if (a=='-'&&b=='(')return '<';
if (a=='-'&&b==')')return '>';
if (a=='-'&&b=='#')return '<';
if (a=='*'&&b=='+')return '<';
if (a=='*'&&b=='-')return '<';
if (a=='*'&&b=='*')return '>';
if (a=='*'&&b=='/')return '>';
if (a=='*'&&b=='(')return '<';
if (a=='*'&&b==')')return '>';
if (a=='*'&&b=='#')return '<';
if (a=='/'&&b=='+')return '<';
if (a=='/'&&b=='-')return '<';
if (a=='/'&&b=='*')return '>';
if (a=='/'&&b=='/')return '>';
if (a=='/'&&b=='(')return '<';
if (a=='/'&&b==')')return '>';
if (a=='/'&&b=='#')return '<';
if (a=='('&&b=='+')return '<';
if (a=='('&&b=='-')return '<';
if (a=='('&&b=='*')return '<';
if (a=='('&&b=='/')return '<';
if (a=='('&&b==')')return '<';
if (a=='('&&b=='#')return '<';
if (a==')'&&b=='+')return '>';
if (a==')'&&b=='-')return '>';
if (a==')'&&b=='*')return '>';
if (a==')'&&b=='/')return '>';
if (a==')'&&b=='(')return '=';
if (a==')'&&b==')')return '>';
if (a=='#'&&b=='+')return '>';
if (a=='#'&&b=='-')return '>';
if (a=='#'&&b=='*')return '>';
if (a=='#'&&b=='/')return '>';
if (a=='#'&&b==')')return '>';
if (a=='#'&&b=='#')return '=';
}
int Operate(int a,char theta,int b)
{int ao=a,bo=b;
char thetao=theta;
switch(thetao){
case'+':return ao+bo;break;
case'-':return ao-bo;break;
case'*':return ao*bo;break;
case'/':return ao/bo;break;}
}
int EvaluateExpression()
{
char c,theta,x;
char OP[]={'+','-','*','/'};
int a,b;
SeqStack<char>OPTR(Max);
OPTR.Push('#');
SeqStack<int>OPND(Max);
cin>>c;
while(c!='#'||OPTR.GetTop()!='#')
{if(!In(c,OP))
{OPND.Push(c);cin>>c;}
else switch(Precede(OPTR.GetTop(),c)){
case'<':
OPTR.Push(c);cin>>c;break;
case'=':
OPTR.Pop(x);cin>>c;break;
case'>':
OPTR.Pop(theta);OPND.Pop(b);
OPND.Pop(a);OPND.Push(Operate(a,theta,b));
break;
}
}
return OPND.GetTop();
}
void main(){
cout<<"input the expression"<<endl;
cout<<"="<<EvaluateExpression();
}
包含两个头文件"stack.h" "SeqStack.h"
出现了这个错误:
1>c:\users\gilbert young\documents\visual studio 2008\projects\data structure1\data structure1\1.cpp(61) : warning C4715: “Precede”: 不是所有的控件路径都返回值
1>c:\users\gilbert young\documents\visual studio 2008\projects\data structure1\data structure1\1.cpp(71) : warning C4715: “Operate”: 不是所有的控件路径都返回值
1>正在链接...
1>1.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Stack<char>::~Stack<char>(void)" (??1?$Stack@D@@UAE@XZ),该符号在函数 "public: virtual __thiscall SeqStack<char>::~SeqStack<char>(void)" (??1?$SeqStack@D@@UAE@XZ) 中被引用
1>1.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@UAE@XZ),该符号在函数 "public: virtual __thiscall SeqStack<int>::~SeqStack<int>(void)" (??1?$SeqStack@H@@UAE@XZ) 中被引用
1>1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Stack<char>::Stack<char>(void)" (??0?$Stack@D@@QAE@XZ),该符号在函数 "public: __thiscall SeqStack<char>::SeqStack<char>(int)" (??0?$SeqStack@D@@QAE@H@Z) 中被引用
1>1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Stack<int>::Stack<int>(void)" (??0?$Stack@H@@QAE@XZ),该符号在函数 "public: __thiscall SeqStack<int>::SeqStack<int>(int)" (??0?$SeqStack@H@@QAE@H@Z) 中被引用
1>C:\Users\Gilbert Young\Documents\Visual Studio 2008\Projects\data structure1\Debug\data structure1.exe : fatal error LNK1120: 4 个无法解析的外部命令
1>生成日志保存在“file://c:\Users\Gilbert Young\Documents\Visual Studio 2008\Projects\data structure1\data structure1\Debug\BuildLog.htm”
1>data structure1 - 5 个错误,2 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
求各位指教啊!!!