数据结构表达式求值.docx
《数据结构表达式求值.docx》由会员分享,可在线阅读,更多相关《数据结构表达式求值.docx(17页珍藏版)》请在冰豆网上搜索。
![数据结构表达式求值.docx](https://file1.bdocx.com/fileroot1/2022-11/16/89604a63-14a9-4f2b-bf5c-7bd69d3229bf/89604a63-14a9-4f2b-bf5c-7bd69d3229bf1.gif)
数据结构表达式求值
数据结构表达式求值
最佳答案
简易版
#include
usingnamespacestd;
#defineTRUE1
#defineFALSE0
#defineStack_Size20
#defineStack_Float30
/*建立字符栈*/
typedefstruct
{
charelem[Stack_Size];//存储定义
inttop;
}Stack_char;
voidInitStack(Stack_char*S)//初始化顺序栈
{
S->top=-1;
}
intPush(Stack_char*S,charx)//进栈
{
if(S->top==Stack_Size-1)return(FALSE);
S->top++;
S->elem[S->top]=x;
return(TRUE);
}
intPop(Stack_char*S,char*x)//出栈
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
S->top--;
return(TRUE);
}
}
intGetTop(Stack_char*S,char*x)//取栈顶
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
return(TRUE);
}
}
charGetTop(Stack_charS)
{
charx;
GetTop(&S,&x);
returnx;
}
//建立数字栈
typedefstruct//建立
{
floatelem[Stack_Float];
inttop;
}Stack_float;
voidInitStack(Stack_float*S)//初始化
{
S->top=-1;
}
intPush(Stack_float*S,floate)//进栈
{
if(S->top==Stack_Float-1)return(FALSE);
else
{
S->top++;
S->elem[S->top]=e;
return(TRUE);
}
}
intPop(Stack_float*S,float*x)//出栈
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
S->top--;
return(TRUE);
}
}
intGetTop(Stack_float*S,float*x)//取栈顶
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
return(TRUE);
}
}
floatGetTop(Stack_floatS)
{
floatx;
GetTop(&S,&x);
returnx;
}
boolIn(charch)//判断字符
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#')
return(TRUE);
elsereturn(FALSE);
}
floatGetNumber(char*ch)//转化数码
{
return(*ch-48);
}
floatExecute(floata,charop,floatb)
{
switch(op)
{
case'+':
return(a+b);break;
case'-':
return(a-b);break;
case'*':
return(a*b);break;
case'/':
return(a/b);break;
default:
cout<<"不能运算";break;
}
}
charCompare(charx,charch)
{
if(x=='+'||x=='-')
if(ch=='+'||ch=='-'||ch==')'||ch=='#')return('>');
elsereturn('<');
if(x=='*'||x=='/')
if(ch=='(')return('<');
elsereturn('>');
if(x=='(')
if(ch==')')return('=');
elsereturn('<');
if(x==')')
if(ch!
='(')return('>');
if(x=='#')
if(ch=='#')return('=');
elsereturn('<');
}
floatExpEvaluation()
{
floatn,v,a,b;charop;
Stack_charOPTR;
Stack_floatOVS;
InitStack(&OPTR);
InitStack(&OVS);
Push(&OPTR,'#');
cout<<"请输入一个表达式串(以#结束)"<charch;
ch=getchar();
while(ch!
='#'||GetTop(OPTR)!
='#')
{
if(!
In(ch))
{
n=GetNumber(&ch);
Push(&OVS,n);
ch=getchar();
}
else
switch(Compare(GetTop(OPTR),ch))
{
case'<':
Push(&OPTR,ch);
ch=getchar();
break;
case'>':
Pop(&OPTR,&op);
Pop(&OVS,&b);
Pop(&OVS,&a);
v=Execute(a,op,b);
Push(&OVS,v);
break;
case'=':
Pop(&OPTR,&op);
ch=getchar();
break;
}
}
v=GetTop(OVS);
return(v);
}
intmain()
{
cout<system("pause");
return0;
}
完善版
#include
usingnamespacestd;
#defineTRUE1
#defineFALSE0
#defineStack_Size20
#defineStack_Float30
/*建立字符栈*/
typedefstruct
{
charelem[Stack_Size];//存储定义
inttop;
}Stack_char;
voidInitStack(Stack_char*S)//初始化顺序栈
{
S->top=-1;
}
intPush(Stack_char*S,charx)//进栈
{
if(S->top==Stack_Size-1)return(FALSE);
S->top++;
S->elem[S->top]=x;
return(TRUE);
}
intPop(Stack_char*S,char*x)//出栈
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
S->top--;
return(TRUE);
}
}
intGetTop(Stack_char*S,char*x)//取栈顶
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
return(TRUE);
}
}
charGetTop(Stack_charS)
{
charx;
GetTop(&S,&x);
returnx;
}
voidClearStack(Stack_char*S)//清空栈
{
if(S->top!
=-1)S->top=-1;
}
/*建立数字栈*/
typedefstruct//建立
{
floatelem[Stack_Float];
inttop;
}Stack_float;
voidInitStack(Stack_float*S)//初始化
{
S->top=-1;
}
intPush(Stack_float*S,floate)//进栈
{
if(S->top==Stack_Float-1)return(FALSE);
else
{
S->top++;
S->elem[S->top]=e;
return(TRUE);
}
}
intPop(Stack_float*S,float*x)//出栈
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
S->top--;
return(TRUE);
}
}
intGetTop(Stack_float*S,float*x)//取栈顶
{
if(S->top==-1)return(FALSE);
else
{
*x=S->elem[S->top];
return(TRUE);
}
}
floatGetTop(Stack_floatS)
{
floatx;
GetTop(&S,&x);
returnx;
}
voidClearStack(Stack_float*S)//清空栈
{
if(S->top!
=-1)S->top=-1;
}
/*一些函数*/
chara[7]={'+','-','*','/','(',')','#'};
charp[7][7]=//优先权集合
{{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','@'},
{'>','>','>','>','@','>','>'},
{'<','<','<','<','<','@','='}};
boolIns(charch)//判断数字
{
if(ch>=48&&ch<=57)return(TRUE);
elsereturn(FALSE);
}
boolInc(charch)//判断字符
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#')
return