急求!!C++四则运算问题(附源代码)

mk476734929 2011-04-13 08:55:42
#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 个 ==========
求各位指教啊!!!
...全文
173 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
TandyT 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mk476734929 的回复:]
具体咋改啊。。。求教了

引用 3 楼 visualeleven 的回复:

你的类的构造函数或者是析构函数没有定义~
[/Quote]

我看了你的工程,你把 stack.h 里的

		
Stack();
virtual ~Stack();


改为

Stack(){};
virtual ~Stack(){};


再重新编译就可以了。

我这里是VC2010 编译的,发给你你也编译不了,你的VC版本比我的低,你先这样改后试试编译是否通过。
mk476734929 2011-04-13
  • 打赏
  • 举报
回复
嗯。。。
改成
char Precede(char e,char c){//比较运算任符的优先权
if(e=='+')
{ if(c=='+'||c=='-'||c==')'||c=='#')
return '>';
return '<';
}
else if(e=='-')
{
if(c=='+'||c=='-'||c==')'||c=='#')
return '>';
return '<';
}
else if(e=='*')
{
if(c=='(')
return '<';
return '>';
}
else if(e=='/')
{
if(c=='(')
return '<';
return '>';
}
else if(e=='(')
{
if(c==')')
return '=';
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
}
else if(e==')')
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='#')
return '>';
}
else if(e=='#')
{ if(c=='#')
return '=';
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
}
return 0;
}
貌似好点[Quote=引用 5 楼 superhanxin5 的回复:]

这么多if 。。。 return,显得好复杂啊。。。网上有这方面现成的啊,你可以再找找
是实现四则运算用的吧?我以前弄过
[/Quote]
superhanxin5 2011-04-13
  • 打赏
  • 举报
回复
这么多if 。。。 return,显得好复杂啊。。。网上有这方面现成的啊,你可以再找找
是实现四则运算用的吧?我以前弄过
mk476734929 2011-04-13
  • 打赏
  • 举报
回复
具体咋改啊。。。求教了[Quote=引用 3 楼 visualeleven 的回复:]

你的类的构造函数或者是析构函数没有定义~
[/Quote]
Eleven 2011-04-13
  • 打赏
  • 举报
回复
你的类的构造函数或者是析构函数没有定义~
TandyT 2011-04-13
  • 打赏
  • 举报
回复
你是用到了第三方的 LIB 或者 DLL 库文件么?如果是的话,要把这些库文件链接到你的程序里面,或者看看是不是还有其它文件未包含的啊?

最好是把工程传上来
TandyT 2011-04-13
  • 打赏
  • 举报
回复
又发一遍?

我没有 "stack.h" 这个头文件,调试不了,要么你也把这个头文件的代码贴上来,或者把你整个工程传上来吧

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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