#pragma once
#include<iostream>
using namespace std;
const char oper[7] = {'+', '-', '*', '/', '(', ')', '#' };
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef char SElemType;
typedef int Status;
typedef struct SNode {
int data;
struct SNode *next;
} SNode, *LinkStack;
Status InitStack(LinkStack &S) {
S = NULL;
return OK;
}
bool StackEmpty(LinkStack S) {
if (!S)
return true;
return false;
}
Status Push(LinkStack &S, SElemType e) {
LinkStack p = new SNode;
if (!p) {
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack &S, SElemType &e) {
SNode *p;
if (!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
Status GetTop(LinkStack S) {
if (S) {
return S->data;
}
return ERROR;
}
bool In(char c) {
for (int i = 0; i < 7; i++) {
if (c == 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 '>'; // 退出2个数和一个优先级数进行计算,并重新回到OPND中
}
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 EvaluateExpression() {
LinkStack OPTR, OPND;
char ch, a, b, theta, x;
InitStack(OPND);
InitStack(OPTR);
cin >> ch;
Push(OPTR, '#');
while (ch != '#' || (GetTop(OPTR) != '#'))
{
if (!In(ch)) {
Push(OPND, ch);
cin >> ch;
}
else {
switch (Precede(GetTop(OPTR), ch))
{
//比较OPTR的栈顶元素和ch的优先级
case '<':
Push(OPTR, ch); cin >> ch;
break;
case '>':
Pop(OPTR, theta); // 弹出栈顶运算符
Pop(OPND, b); Pop(OPND, a);//一并弹出OPND中的两元素进行运算
Push(OPND, Operate(a, theta, b)); //新的运算结果重新弹入栈中
break;
case '=':
Pop(OPTR, x); cin >> ch;
break;
default:
break;
}
}
}
return GetTop(OPND);
}
#include<iostream>
using namespace std;
const char oper[7] = {'+', '-', '*', '/', '(', ')', '#' };
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef char SElemType;
typedef int Status;
typedef struct SNode {
int data;
struct SNode *next;
} SNode, *LinkStack;
Status InitStack(LinkStack &S) {
S = NULL;
return OK;
}
bool StackEmpty(LinkStack S) {
if (!S)
return true;
return false;
}
Status Push(LinkStack &S, SElemType e) {
LinkStack p = new SNode;
if (!p) {
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack &S, SElemType &e) {
SNode *p;
if (!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
Status GetTop(LinkStack S) {
if (S) {
return S->data;
}
return ERROR;
}
bool In(char c) {
for (int i = 0; i < 7; i++) {
if (c == 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 '>'; // 退出2个数和一个优先级数进行计算,并重新回到OPND中
}
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 EvaluateExpression() {
LinkStack OPTR, OPND;
char ch, a, b, theta, x;
InitStack(OPND);
InitStack(OPTR);
cin >> ch;
Push(OPTR, '#');
while (ch != '#' || (GetTop(OPTR) != '#'))
{
if (!In(ch)) {
Push(OPND, ch);
cin >> ch;
}
else {
switch (Precede(GetTop(OPTR), ch))
{
//比较OPTR的栈顶元素和ch的优先级
case '<':
Push(OPTR, ch); cin >> ch;
break;
case '>':
Pop(OPTR, theta); // 弹出栈顶运算符
Pop(OPND, b); Pop(OPND, a);//一并弹出OPND中的两元素进行运算
Push(OPND, Operate(a, theta, b)); //新的运算结果重新弹入栈中
break;
case '=':
Pop(OPTR, x); cin >> ch;
break;
default:
break;
}
}
}
return GetTop(OPND);
}