该问题的求解可以分为两部分:
1、中缀表达式到后缀表达式的转换;
2、后缀表达式的计算;
具体代码如下:
#include
#define StackSize 100
#define QueueSize 100
/*队列的相关操作*/
typedef char DataType;
typedef struct{
char data[100];
int front, rear;
}SeqQueue; // 定义队列类型
void InitQueue(SeqQueue *Q) // 初始化队列
{
Q->front = 0;
Q->rear = 0;
}
int QueueEmpty(SeqQueue *Q) // 判空队列
{
return Q->rear == Q->front;
}
void EnQueue(SeqQueue *Q, DataType x) // 入队列
{
if ((Q->rear + 1) % QueueSize == Q->front)
{
cout << "Queue overflow" << endl;
}
else
{
Q->data[Q->rear] = x;
Q->rear = (Q->rear + 1) % QueueSize;
}
}
DataType DeQueue(SeqQueue *Q) // 出队列
{
if (Q->rear == Q->front)
{
cout << "Queue empty" << endl;
}
else
{
DataType temp = Q->data[Q->front];
Q->front = (Q->front + 1) % QueueSize;
return temp;
}
}
/*栈的相关操作*/
typedef struct{
DataType data[100];
int top;
}SeqStack; //栈类型的定义
void InitStack(SeqStack *S) //初始化栈
{
S->top = 1;
}
void Push(SeqStack * S, DataType d) // 入栈
{
if(S->top == StackSize - 1)
{
cout << "stack overflow" << endl;
}
else
{
S->top = S->top + 1;
S->data[S->top] = d;
}
}
DataType Pop(SeqStack * S) // 出栈
{
if (S->top == -1)
{
cout << "stack underflow" << endl;
}
else
return S->data[S->top--];
}
DataType GetTop(SeqStack *S) // 取栈顶元素
{
if (S->top == -1)
{
cout << "stack empty" << endl;
}
else
{
return S->data[S->top];
}
}
//求运算符优先级
int Priority(DataType op)
{
switch (op)
{
case '(':
case '#': return (0);
case '-':
case '+': return (1);
case '*':
case '/': return (2);
}
}
void CTPostExp(SeqQueue *Q)
{
SeqStack OS; // 运算符栈
char c,t;
SeqStack *S;
S = &OS;
InitStack(S);
Push(S,'#'); // 压入栈底元素'#'
do // 扫描表达式
{
c = getchar();
switch (c)
{
case ' ':
break; // 去除空格
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
EnQueue(Q, c); break;
case '(': Push(S, c); break;
case ')':
case '#':
do {
t = Pop(S);
if(t != '(' && t != '#')
EnQueue(Q, t);
}while (t != '(' && S->top != 1);
break;
case '+':
case '-':
case '*':
case '/':
while(Priority(c) <= Priority(GetTop(S)))
{
t = Pop(S);
EnQueue(Q, t);
}
Push(S, c);
break;
}
}while(c != '#'); // 以'#'号结束表达式扫描
}
// 计算后缀表达式
int CPostExp(SeqQueue *Q)
{
SeqStack VS, *S;
char ch;
int x,y;
S = &VS;
InitStack(S);
while(!QueueEmpty(Q))
{
ch = DeQueue(Q);
if(ch >= '0' && ch <= '9')
{
Push(S, ch - '0');
}
else
{
y = Pop(S);
x = Pop(S);
switch (ch)
{
case '+' : Push(S, x + y); break;
case '-' : Push(S, x - y); break;
case '*' : Push(S, x * y); break;
case '/' : Push(S, x / y); break;
}
}
}
return GetTop(S);
}
int main()
{
SeqQueue *Q;
SeqQueue PostQ; //定义队列,存放后缀表达式
Q = &PostQ;
InitQueue(Q); //初始化队列
cout << "请输入数学表达式(以'#'结束):\n";
CTPostExp(Q);
// 计算后缀表达式
cout << "该表达式的值为: " << CPostExp(Q) << endl;
return 0;
}
这是我写的,能看懂吗,都是基本的#include
#include
using namespace std;
class actor;
class weapon
{
private:
string _name;
int _power;
public:
weapon();
weapon(string name,int power);
friend class actor;
};
weapon::weapon()
:_name("没有佩戴兵器"),_power(0)
{
}
weapon::weapon(string name,int power)
{
_name=name;
_power=power;
}
class actor
{
protected:
string _name;
bool _gender;
weapon _weapon;
public:
actor();
actor(string name,string gender);
void say();
void getweapon(weapon & weapon);
void showbingqi(weapon & weapon);
void operator+(weapon & weapon);
~actor();
};
actor::~actor()
{
cout<<_name<<"至此退隐江湖(基类析构)"<
actor::actor()
:_name("无名"),_gender(true)
{}
actor::actor(string name,string gender)
{
_name=name;
if(gender=="男")
{
_gender=true;
}
else
{
_gender=false;
}
cout<<"\n"<<_name<<"出现(基类构造)"<
void actor::say()
{
cout<<"我乃"<<_name<<"是也(基类说)"<
void actor::getweapon(weapon & weapon)
{
_weapon=weapon;
}
void actor::showbingqi(weapon & weapon)
{
cout<<"佩戴兵器:";
_weapon=weapon;
cout<<_weapon._name<<",威力:"<<_weapon._power<
void actor:perator+(weapon & weapon)
{
_weapon=weapon;
}
class monk:public actor
{
public:
monk(string name,string gender)
:actor(name,gender)
{
}
void patter();//念经
void say();
void getmonkname(string monkname);
~monk();
private:
string _monkname;
};
monk::~monk()
{
cout<<"\n"<<_name<<"退仙了(派生类析构)"<
void monk::patter()
{
cout<<"南无阿弥陀佛"<
void monk::getmonkname(string monkname)
{
_monkname=monkname;
}
void monk::say()
{
cout<<"我乃西天"<<_name<<"也(派生类说)"<
cout<<"贫僧法名:"<<_monkname<
int main()
{
actor tang("唐三藏","男");
tang.say();
weapon w("奔雷杖",80);
tang+w;
tang.showbingqi(w);
monk t("如来佛主","男");
t.patter();
t.getmonkname("玄奘");
t.actor::say();
t.say();
return 0;
}
声明析构函数中声明释放对象