数据结构表达式求值.docx

上传人:b****4 文档编号:2999751 上传时间:2022-11-17 格式:DOCX 页数:17 大小:22.38KB
下载 相关 举报
数据结构表达式求值.docx_第1页
第1页 / 共17页
数据结构表达式求值.docx_第2页
第2页 / 共17页
数据结构表达式求值.docx_第3页
第3页 / 共17页
数据结构表达式求值.docx_第4页
第4页 / 共17页
数据结构表达式求值.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

数据结构表达式求值.docx

《数据结构表达式求值.docx》由会员分享,可在线阅读,更多相关《数据结构表达式求值.docx(17页珍藏版)》请在冰豆网上搜索。

数据结构表达式求值.docx

数据结构表达式求值

数据结构表达式求值

最佳答案

简易版

#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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1