长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx

上传人:b****5 文档编号:19910949 上传时间:2023-01-12 格式:DOCX 页数:10 大小:16.79KB
下载 相关 举报
长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx_第1页
第1页 / 共10页
长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx_第2页
第2页 / 共10页
长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx_第3页
第3页 / 共10页
长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx_第4页
第4页 / 共10页
长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx

《长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx(10页珍藏版)》请在冰豆网上搜索。

长沙理工大学数据结构栈的实现及应用算术表达式求值实验报告Word文档格式.docx

四、数据结构与算法思想描述

顺序读取中缀表达式:

1、当遇到数字时,将数字入数字栈

2、当遇到操作符时,与操作符栈栈顶比较:

If(当前操作符优先级大于操作符栈栈顶的优先级)

If(非”)”操作符)

将当前操作符进操作符栈;

Else

While(操作符栈栈顶不等于”(“)

取操作符栈栈顶及数字栈的两个数进行运算,并将结果压入数字栈;

If(非(“操作符)

取操作符栈栈顶及数字栈的两个数进行运算,并将结果压入数字栈;

Continue;

(直到当前操作符比栈顶操作符优先级大)

Else

将当前操作符进操作符栈;

3、While(操作符栈非空)

操作符栈栈顶及数字栈的两个数进行运算,并将结果压入数字栈;

4、在数字栈取最后结果并输出。

五、程序清单

//10*8^2++5**5+/4-(-10)++=

//100+(-100)-(-10^2)=100

//(((2016-2017+(((2015-2014))))))=0

//-1+(((((((((1^0))))))))+100%10^2=0

#include<

iostream>

>

iomanip>

map>

usingnamespacestd;

constintMAX=105;

typedefdoubleType;

typedefstruct

{

TypeTypeStack[MAX];

charcharStack[MAX];

intTypeTop,charTop;

}Stack;

//初始化栈

voidInitStack(Stack*S)

S->

charTop=S->

TypeTop=0;

}

//判断charStack是否为空

boolIsEmpty_Char(StackS)

return==0;

//判断TypeStack是否为空

boolIsEmpty_Type(StackS)

//判断charStack是否为满

boolIsFull_Char(StackS)

return==MAX;

//判断TypeStack是否为满

boolIsFull_Type(StackS)

voidPush_Char(Stack*S,charch)

//charStack不为满则入栈,否则输出提示

if(!

IsFull_Char(*S))

S->

charStack[S->

charTop++]=ch;

else

cout<

<

"

TheCharStackIsFull!

<

endl;

voidPush_Type(Stack*S,Typea)

//TypeStack不为满则入栈,否则输出提示

IsFull_Type(*S))

TypeStack[S->

TypeTop++]=a;

TheTypeStackIsFull!

charPop_Char(Stack*S)

IsEmpty_Char(*S))

{

charTop--;

returnS->

charTop];

}

TheCharStackIsEmpty!

return-1;

TypePop_Type(Stack*S)

IsEmpty_Type(*S))

TypeTop--;

TypeTop];

TheTypeStackIsEmpty!

charTop_Char(StackS)

IsEmpty_Char(S))

return[];

TypeTop_Type(StackS)

IsEmpty_Type(S))

TypeCalculate(Typeleft,Typeright,charop)

Typevalue=0;

switch(op)

case'

+'

:

value=left+right;

break;

-'

value=left-right;

*'

value=left*right;

/'

if(right!

=0)

value=left/right;

else

cout<

被除数不能为零!

"

break;

%'

value=(int)left%(int)right;

被余数不能为零!

^'

value=pow(left,right);

/*value=1;

if(right>

while(right--)

value*=left;

{

right=-right;

value/=left;

}*/

returnvalue;

voidComputer(char*mid_equotion,Typelen)

Typeright,left,result;

char*p_mid_equotion=mid_equotion;

charafter_equotion='

'

;

map<

char,Type>

Oper;

Oper['

#'

]=1;

('

]=2;

]=3;

]=4;

]=5;

)'

]=6;

StackMyStack;

InitStack(&

MyStack);

Push_Char(&

MyStack,'

);

chartop_oper,current_oper;

for(;

*p_mid_equotion!

='

\0'

top_oper=Top_Char(MyStack);

current_oper=*p_mid_equotion;

if(!

Oper[current_oper])

{

Push_Type(&

MyStack,strtod(p_mid_equotion,&

p_mid_equotion));

continue;

}//endif

else//为操作符

if(Oper[current_oper]>

Oper[top_oper])

{

if(current_oper!

Push_Char(&

MyStack,current_oper);

while(top_oper!

{

right=Pop_Type(&

if(!

IsEmpty_Type(MyStack))

left=Pop_Type(&

else

left=0;

Push_Type(&

MyStack,Calculate(left,right,Top_Char(MyStack)));

Pop_Char(&

top_oper=Top_Char(MyStack);

}

Pop_Char(&

}//endelse

}//endif

else

if(current_oper=='

if(*(p_mid_equotion+1)=='

MyStack,0);

}

right=Pop_Type(&

if(!

left=Pop_Type(&

else

left=0;

Push_Type(&

MyStack,Calculate(left,right,top_oper));

continue;

}//endelse

}//endelse

p_mid_equotion++;

}//endfor

top_oper=Pop_Char(&

while(top_oper!

right=Pop_Type(&

left=Pop_Type(&

else

left=0;

Push_Type(&

top_oper=Pop_Char(&

//cout<

setprecision(6)<

\nTheResult="

(result=Pop_Type(&

MyStack))<

printf("

TheResult=%lf\n\n"

(result=Pop_Type(&

MyStack)));

intmain()

chars[MAX]="

Typei=0;

cout<

请输入你要求值的表达式!

(以-1结束)\n"

while(cin>

s&

&

strcmp(s,"

-1"

)!

Computer(s,strlen(s));

return0;

六、程序执行结果及其分析

对“+”,“-”,“*”,“/”,“%”,“^”运算的实现

可运算多位数和小数,求余,求平方,括号里包含负数如(-1),及首个数字为负数如-1+1

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

当前位置:首页 > 法律文书 > 调解书

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

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