版权声明:本文由 高小调 创作,转载请带链接,侵权必究! https://blog.csdn.net/gaoben668/article/details/51578858
本代码有严重BUG!!!
本代码有严重BUG!!!
本代码有严重BUG!!!
重要的事情说三遍!!!!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#define ElemType int
using namespace std;
typedef struct Stack{
ElemType * base;
ElemType * top;
int stacksize;
}SqStack;
int In_Op(char a); //判断是否为运算符
int Pop(SqStack * S,ElemType e); //出栈
int GetTop(SqStack * S,ElemType e); //获得栈顶元素
int CreateStack(SqStack * S); //初始化栈
int Push(SqStack * S,ElemType e); //压栈
void CreateExpression(char * str); //创建中缀表达式
char * TransmitExpression(char * a);//将中缀表达式转换为后缀表达式
int Is_Pop(char x,char y); //根据运算符优先级判断是否出栈
ElemType EvaluateExpression(char *a);//根据后缀表达式计算结果
ElemType Theta(int l,char m,int r); //计算的元操作
int main(){
char str[30];
char s_str[30];
CreateExpression(str);
strcpy(s_str,TransmitExpression(str));
cout<<"后缀表达式为:"<<s_str<<endl;
cout<<"最终结果为:"<<EvaluateExpression(s_str)<<endl;
}
//初始化一个栈
int CreateStack(SqStack * S){
S->base = (ElemType *)malloc(sizeof(SqStack)*10);
if(!S->base) exit(-1);
S->top = S->base;
S->stacksize = 10;
return 1;
}
//获得栈顶元素
ElemType GetTop(SqStack * S){
if(S->top == S->base)
return 0;
return *(S->top-1);
}
//压栈
int Push(SqStack * S,ElemType e){
if((S->top-S->base)>=S->stacksize){
S->base = (ElemType *)realloc(S->base,(S->stacksize+5)*sizeof(SqStack));
if(!S->base)
exit(-1);
S->top = S->base + S->stacksize;
S->stacksize +=5;
}
*S->top=e;
S->top++;
return 1;
}
//弹出
int Pop(SqStack * S,ElemType * e){
if(S->top==S->base)
return 0;
*e = *(--S->top);
return 1;
}
//创建中缀表达式
void CreateExpression(char * str){
gets(str);
}
//判断是否为运算符
int In_Op(char a){
switch(a){
case '+' : return 1;
case '-' : return 1;
case '*' : return 1;
case '/' : return 1;
case '(' : return 1;
case ')' : return 1;
case '#' : return 1;
default:return 0;
}
}
/*
函数功能:将中缀表达式转换为后缀表达式
参数:中缀表达式
返 回 值:后缀表达式
*/
char * TransmitExpression(char * a){
int i,j=0;
char b[20]={0};
SqStack OPTR;
ElemType e;
CreateStack(&OPTR);
Push(&OPTR,'#');
for(i=0;a[i];i++){
if(!In_Op(a[i])){
b[j++]=a[i];//如果不是运算符则直接入b
}else{
if(GetTop(&OPTR)=='#'){
Push(&OPTR,a[i]); //如果栈顶元素为空,直接入栈。
continue;
}
if(Is_Pop(GetTop(&OPTR),a[i])){ //如果栈内运算符优先级高于表达式运算符优先级
if(a[i]==')'){ //如果遇到右括号
while(GetTop(&OPTR)!='('){
Pop(&OPTR,&e); //将左括号以上的运算符弹出并存入b
b[j++]=(char)e;
}
Pop(&OPTR,&e); //将左括号弹出
}else{ //如果不是括号情形
while(GetTop(&OPTR)!='#'){//将栈内所有运算符弹出
Pop(&OPTR,&e);
b[j++]=(char)e;
}
Push(&OPTR,a[i]);//将新运算符入栈
}
}else{
Push(&OPTR,a[i]); //如果栈内优先级低于新运算符,
//则新运算符直接入栈
}
}
}
//中缀表达式扫描完成后,弹出所有剩下的运算符
while(GetTop(&OPTR)!='#'){
Pop(&OPTR,&e);
b[j++]=(char)e;
}
//新分配一个内存,用于返回
char * result = (char *)malloc(sizeof(char)*20);
strcpy(result,b);//将b中的元素结果复制到新分配内存中
return result;
}
//判断栈内与栈外运算符的优先级,然后再次判断是否将栈内元素弹出
/*
栈外
栈 + - * / ( )
内 + 1 1 0 0 0 1
- 1 1 0 0 0 1
* 1 1 0 0 0 1
/ 1 1 1 1 0 1
( 0 0 0 0 0 0
) 1 1 1 1 1 1
*/
int Is_Pop(char x,char y){
if(x=='(')
return 0;
if(y=='('&&x!=')')
return 0;
if(y=='*'||y=='/'){
if(x=='+'||x=='-')
return 0;
}
return 1;
}
//根据后缀表达式计算结果
ElemType EvaluateExpression(char *a){
int i;
ElemType l,r,e;
SqStack OPND;
CreateStack(&OPND);
for(i=0;a[i];i++){
if(!In_Op(a[i])){
Push(&OPND,int(a[i]-48));
}else{
Pop(&OPND,&r);
Pop(&OPND,&l);
Push(&OPND,Theta(l,a[i],r));
}
}
return GetTop(&OPND);
}
//基本的元运算
ElemType Theta(int l,char m,int r){
switch(m){
case'+': return l+r;
case'-': return 1-r;
case'*': return l*r;
case'/': return l-r;
}
}