Common.h
cpp
复制代码
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULL 0
typedef int Status;
typedef int SElemType;
typedef int ElemType;
typedef int QElemType;
Evaluate.h
cpp
复制代码
#include "common.h"
#include "SqStack.h"
bool In(char ch);
char Precede(char theta1, char theta2);
char Operate(char first, char theta, char second);
char Evaluate(char* expression);
LinkQueue.h
cpp
复制代码
#include "Common.h"
typedef struct QNode
{
QElemType data;
struct QNode* next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr front; //队头指针
QueuePtr rear; //队尾指针
}LinkQueue;
Status InitQueue(LinkQueue& Q);
Status EnQueue(LinkQueue& Q, QElemType e);
Status DeQueue(LinkQueue& Q, QElemType& e);
SElemType GetHead(LinkQueue Q);
LinkStack.h
cpp
复制代码
#include "Common.h"
//
#define MAXSIZE 100
typedef struct StackNode
{
ElemType data;
struct StackNode* next;
}StackNode, * LinkStack;
Status InitStack(LinkStack& S);
Status Push(LinkStack& S, SElemType e);
Status Pop(LinkStack& S, SElemType& e);
SElemType GetTop(LinkStack S);
SqQueue.h
cpp
复制代码
#include "Common.h"
#define MAXQSIZE 100 //队列可能达到的最大长度
typedef struct
{
QElemType* base; //存储空间的基地址
int front; //头指针
int rear; //尾指针
}SqQueue;
Status InitQueue(SqQueue& Q);
int QueueLength(SqQueue Q);
Status EnQueue(SqQueue& Q, QElemType e);
Status DeQueue(SqQueue& Q, QElemType& e);
SElemType GetHead(SqQueue Q);
SqStack.h
cpp
复制代码
#include "Common.h"
//
#define MAXSIZE 100
typedef struct
{
SElemType* base;//栈底指针
SElemType* top; //栈顶指针
int stacksize;
}SqStack;
Status InitStack(SqStack& S);
Status Push(SqStack& S, SElemType e);
Status Pop(SqStack& S, SElemType& e);
SElemType GetTop(SqStack S);
Evaluate.cpp
cpp
复制代码
#include "Evaluate.h"
#include <iostream>
using namespace std;
const char oper[7] = { '+','-','*','/','(',')','#' };
bool In(char ch)
{
for (int i = 0;i < 7;i++)
{
if (ch == oper[i])
{
return true;
}
}
return false;
}
char Precede(char theta1, char theta2)
{
if ((theta1 == '(' && theta2 == ')') || (theta1 == '#' && theta2 == '#'))
{
return '=';
}
else if (theta1 == '(' || theta1 == '#' || theta2 == '('
|| (theta1 == '+' || theta1 == '-') && (theta2 == '*' || theta2 == '/'))
{
return '<';
}
else
return'>';
}
char Operate(char first, char theta, char second)
{
switch (theta)
{
case '+':
return(first - '0') + (second - '0' )+48;
case '-':
return(first - '0') - (second - '0' )+48;
case '*':
return(first - '0') * (second - '0' )+48;
case '/':
return(first - '0') / (second - '0' )+48;
}
return 0;
}
char Evaluate(char* expression)
{
int length = strlen(expression);
if (length < 2)return ERROR;
SqStack OPTR, OPND;
int ch, theta, a, b, x;
int i = 0;
InitStack(OPND); InitStack(OPTR); Push(OPTR, '#');
while (expression[i] != '#' || GetTop(OPTR) != '#')
{
ch = expression[i];
if(!In(ch)){Push(OPND,ch);i++; }
else
{
switch (Precede(GetTop(OPTR), ch))
{
case '<':
Push(OPTR, ch);i++;
break;
case '>':
Pop(OPTR, theta);Pop(OPND, b);Pop(OPND, a);
Push(OPND, Operate(a, theta, b));
break;
case '=':
Pop(OPTR, x);i++;
break;
}
}
}
return GetTop(OPND);
}
LinkQueue.cpp
cpp
复制代码
#include "LinkQueue.h"
Status InitQueue(LinkQueue& Q)
{//构造一个空队列Q
Q.front = Q.rear = new QNode; //生成新结点作为头结点,队头和队尾指针指向此结点
Q.front->next = NULL; //头结点的指针域置空
return OK;
};
Status EnQueue(LinkQueue& Q, QElemType e)
{//插入元素为Q的新的队尾元素
QueuePtr p = new QNode; //为入队元素分配节点空间,用指针p指向
p->data = e; //将新结点数据域置为e
p->next = NULL;Q.rear->next = p;//将新结点插入到队尾
Q.rear = p; //修改队尾指针
return OK;
};
Status DeQueue(LinkQueue& Q, QElemType& e)
{//删除Q的队头元素,用e返回其值
if (Q.front == Q.rear)return ERROR; //若队列空,则返回ERROR
QueuePtr p = Q.front->next; //p指向队头元素
e = p->data; //e保存队头元素的值
Q.front->next = p->next; //修改头结点的指针域
if (Q.rear == p)Q.rear = Q.front; //最后一个元素被删,队尾指针指向头结点
delete p; //释放原队头元素的空间
return OK;
};
SElemType GetHead(LinkQueue Q)
{//返回Q的队头元素,不修改队头指针
if (Q.front != Q.rear) //队列非空
return Q.front->next->data; //返回队头元素的值,队头指针不变
};
LinkStack.cpp
cpp
复制代码
#include "LinkStack.h"
Status InitStack(LinkStack& S)
{//构造一个空栈S,栈顶指针置空
S = NULL;
return OK;
};
Status Push(LinkStack& S, SElemType e)
{//在栈顶插入元素e
LinkStack p = new StackNode; //生成新的结点
p->data = e; //将新结点数据域置为e
p->next = S; //将新结点插入栈顶
S = p; //修改栈顶指针为p
return OK;
};
Status Pop(LinkStack& S, SElemType& e)
{//删除S的栈顶元素,用e返回其值
if (S == NULL) return ERROR;//栈空
e = S->data; //将栈顶元素赋给e
LinkStack p = S; //用p临时保存栈顶元素空间,以备释放
S = S->next; //修改栈顶指针
delete p; //释放原栈顶元素的空间
return OK;
};
SElemType GetTop(LinkStack S)
{//返回S的栈顶元素,不修改栈顶指针
if (S != NULL) //栈非空
return S->data; //返回栈顶元素的值,栈顶指针不变
};
main.cpp
cpp
复制代码
#include <iostream>
using namespace std;
#include "Evaluate.h"
int main()
{
while (1)
{
cout << "请输入要计算的表达式,以#结束:" << endl;
char expression[1024];
cin.getline(expression, 1024);
if (expression[strlen(expression) - 1] != '#')
{
cout << "错误:表达式必须以#结束..." << endl;
}
else
{
char e = Evaluate(expression);
cout << e - '0' << endl;
}
}
}
SqQueue.cpp
cpp
复制代码
#include "SqQueue.h"
#include <stdlib.h>
Status InitQueue(SqQueue& Q)
{//构造一个空队列Q
Q.base = new QElemType[MAXQSIZE]; //为队列分配一个最大容量为MAXQSIZE的数组空间
if (!Q.base) exit(OVERFLOW); //存储分配失败
Q.front = Q.rear = 0; //头指针和尾指针置为零,队列为空
return OK;
};
int QueueLength(SqQueue Q)
{//返回Q的元素个数,即队列的长度
return(Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
};
Status EnQueue(SqQueue& Q, QElemType e)
{//插入元素e为Q的新的队尾元素
if ((Q.rear + 1) % MAXQSIZE == Q.front) //尾指针在循环意义上加1后等于头指针,表明队满
return ERROR;
Q.base[Q.rear] = e; //新元素插入队尾
Q.rear = (Q.rear + 1) % MAXQSIZE; //队尾指针加1
return OK;
};
Status DeQueue(SqQueue& Q, QElemType& e)
{//删除Q的队头元素,用e返回其值
if (Q.front == Q.rear) return ERROR; //队空
e = Q.base[Q.front]; //保存队头元素
Q.front = (Q.front + 1) % MAXQSIZE; //队头指针加一
return OK;
};
SElemType GetHead(SqQueue Q)
{//返回Q的队头元素,不修改队头指针
if (Q.front != Q.rear) //队列非空
return Q.base[Q.front]; //返回队头元素的值,队头指针不变
};
SqStack.cpp
cpp
复制代码
#include "SqStack.h"
#include <stdlib.h>
Status InitStack(SqStack& S)
{//构造一个空栈S
S.base = new SElemType[MAXSIZE];//为顺序栈动态分配一个最大容量为MAXSIZE的数组空间
if (!S.base) exit(OVERFLOW); //存储分配失败
S.top = S.base; //top初始为base,空栈
S.stacksize = MAXSIZE; //stacksize置为栈的最大容量MAXSIZE
return OK;
};
Status Push(SqStack& S, SElemType e)
{//插入元素为e为新的栈顶元素
if (S.top - S.base == S.stacksize) return ERROR;//栈满
*S.top++ = e; //元素e压入栈顶,栈顶指针加1
return OK;
}
Status Pop(SqStack& S, SElemType& e)
{//删除S的栈顶元素,用e返回其值
if (S.top == S.base) return ERROR;//栈空
e = *--S.top; //栈顶指针减一,将栈顶元素赋给e
return OK;
};
SElemType GetTop(SqStack S)
{//返回S的栈顶元素,不修改栈顶元素
if (S.top != S.base) //栈非空
return *(S.top - 1);//返回栈顶元素的值,栈顶指针不变
};