数据结构表达式求值.docx
- 文档编号:2999751
- 上传时间:2022-11-17
- 格式:DOCX
- 页数:17
- 大小:22.38KB
数据结构表达式求值.docx
《数据结构表达式求值.docx》由会员分享,可在线阅读,更多相关《数据结构表达式求值.docx(17页珍藏版)》请在冰豆网上搜索。
数据结构表达式求值
数据结构表达式求值
最佳答案
简易版
#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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 表达式 求值