#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MaxSize 1000
#define MaxOp 1000
using namespace std;
struct charac //设定运算符优先级
{
char ch; //运算符
int pri; //优先级
};
struct charac lpri[]=//左运算符及优先级
{
{'=',0},
{'(',1},
{'*',5},
{'/',5},
{'+',3},
{'-',3},
{')',8},
{'a',7},//a代表abs
{'q',7},//q代表sqrt
{'c',7},//c代表cos
{'%',5},//模运算
{'s',7},//s代表sin
{'t',7},//t代表tan
{'^',7},
{'!',7}//阶乘
},
rpri[]=//右运算符及优先级
{
{'=',0},
{'(',8},
{'*',4},
{'/',4},
{'+',2},
{'-',2},
{')',1},
{'a',6},
{'q',6},
{'c',6},
{'%',4},
{'s',6},
{'t',6},
{'^',6},
{'!',6}
};
int leftpri(char op) //求左运算符op的优先级
{
int i;
for (i=0; i<MaxOp; i++)
if (lpri[i].ch==op) return lpri[i].pri;
}
int rightpri(char op) //求右运算符op的优先级
{
int i;
for (i=0; i<MaxOp; i++)
if (rpri[i].ch==op) return rpri[i].pri;
}
bool InOp(char ch) //判断ch是否为运算符
{
if (ch=='(' || ch==')' || ch=='+' || ch=='-'|| ch=='*' || ch=='/'||ch=='a'
||ch=='q'||ch=='c'||ch=='%'||ch=='s'||ch=='t'||ch=='^'||ch=='!')
return true;
else
return false;
}
int Precede(char op1,char op2) //op1和op2运算符优先级的比较结果
{
if (leftpri(op1)==rightpri(op2))
return 0;
else if (leftpri(op1)<rightpri(op2))
return -1;
else
return 1;
}
void trans(char *exp,char postexp[])//将算术表达式exp转换成后缀表达式postexp
{
struct
{
char data[MaxSize]; //存放运算符
int top; //栈指针
} op; //定义运算符栈
int i=0; //i作为postexp的下标
op.top=-1;
op.top++; //将'='进栈
op.data[op.top]='=';
while (*exp!='\0') //exp表达式未扫描完时循环
{
if (!InOp(*exp)) //为数字字符的情况或小数
{
while (