数据结构中缀表达式转后缀表达式.docx

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

数据结构中缀表达式转后缀表达式.docx

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

数据结构中缀表达式转后缀表达式.docx

数据结构中缀表达式转后缀表达式

 

中缀转后缀,后缀求值

 

13070319

张乐

2015.4.21

 

1、需求分析

明确规定:

需要运用栈来实现对中缀表达式转换为后缀表达式,并且再次输入后缀表达式,得出结果。

输入形式、输入值的范围;中缀表达式的输入,操作数必须不为负数,并且表达式以=结束输入

输出形式;第一次输出后缀表达式,接着输出后缀表达式求得的值

程序功能;中缀表达式转换为后缀表达式,后缀表达式求值

测试数据:

10(20-10)+10=

2、概要设计

ADT定义:

classarrStack

{

private:

intmSize;//栈最多存放元素个数

inttop;//栈顶指针

T*st;//存栈元素的数组

public:

arrStack(intsizee){//创建定长顺序栈的实例

mSize=sizee;

top=-1;

st=newT[mSize];}

arrStack(){}

~arrStack(){}

voidclear(){}

boolisEmpty(){}

boolpush(constTitem){}

boolpop(T&item){}

T&gettop(){}

boolinput(){}

inttrans(){}

boolCaculator(){}

}

主程序流程:

各程序模块间的调用关系;

3、详细设计

实现ADT定义的数据类型:

arrStack(intsizee){//创建定长顺序栈的实例

mSize=sizee;

top=-1;

st=newT[mSize];

}

arrStack(){//清空

top=-1;

}

~arrStack(){//销毁

delete[]st;

}

voidclear(){//清空

top=-1;

}

boolisEmpty(){//若栈已空返回true

if(top==-1)

returntrue;

returnfalse;

}

boolpush(constTitem)//入栈O

(1)

{

/*if(top==(mSize-1)){//若上溢

T*newst=newT[mSize*2];//扩容到2倍

for(inti=0;i<=top;i++)//复制

newst[i]=st[i];

delete[]st;//释放旧空间

st=newst;//恢复st

mSize*=2;//改写mSize

}*/

st[++top]=item;//插入item

returntrue;

}

boolpop(T&item)//出栈O

(1)

{

if(top==-1){

cout<<"空栈不能删"<

returnfalse;

}

else{

item=st[top--];

returntrue;

}

}

T&gettop()//取栈顶O

(1)

{

if(top==-1){

cout<<"空栈不能读取"<

}

else

returnst[top];

}

boolinput(){

charin;

inti=0;

cout<<"请您输入您要转换的前缀表达式"<

cin>>in;

while(in!

='='){

a[i]=in;

cin>>in;

i++;

}

a[i]='=';

i=0;

while(a[i]!

='='){

cout<

i++;

}

}

inttrans(){

inti=0,j=0;

charch,e;

ch=a[i];

while(ch!

='='){

switch(ch){

case'(':

push(ch);

break;

case'+':

case'-':

case'/':

case'*':

{

while(!

isEmpty()&&gettop()!

='('&&gettop()<=ch){

e=gettop();

b[j]=e;

pop(e);

j++;

}

push(ch);

}

break;

case')':

if(!

isEmpty()){

while(gettop()!

='('){

e=gettop();

b[j]=e;

pop(e);

j++;

}

}

pop(e);

break;

default:

if(ch>='0'<='9')

b[j++]=ch;

}

ch=a[++i];

}

while(!

isEmpty()){

e=gettop();

b[j++]=e;

pop(e);

}

intk=0;

cout<

"<

for(;k

cout<

returnj;

}

};

boolCaculator(){//全局函数来计算后缀表达式

arrStacks(100);

intnewope,ope1,ope2,e;

charc;

cout<

"<

while(cin>>c,c!

='='){

switch(c){

case'+':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1+ope2);break;

case'-':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1-ope2);break;

case'*':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1*ope2);break;

case'/':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1/ope2);break;

default:

cin.putback(c);

cin>>newope;

s.push(newope);

break;

}

}

cout<

"<

e=s.gettop();

cout<

s.pop(e);

}

4、调试分析

调试中遇到的问题、如何解决、对设计与实现的分析讨论;

调试过程中出现了中缀表达式转换为后缀表达式转换错误的问题,原因是操作符号优先级判断判断错。

算法时空分析;O(n)

5、用户使用说明

A.按照提示输入中缀表达式

B.看到屏幕输出的后缀表达式

C.将屏幕输出的后缀表达式再次按照屏幕提示输入

D.看到屏幕输出的后缀表达式的值

6、测试结果

列出测试用输入、输出;

7、源程序

列出源程序清单:

#include

#include

usingnamespacestd;

template

classarrStack

{

private:

intmSize;//栈最多存放元素个数

inttop;//栈顶指针

T*st;//存栈元素的数组

public:

chara[100];

charb[100];

arrStack(intsizee){//创建定长顺序栈的实例

mSize=sizee;

top=-1;

st=newT[mSize];

}

arrStack(){//清空

top=-1;

}

~arrStack(){//销毁

delete[]st;

}

voidclear(){//清空

top=-1;

}

boolisEmpty(){//若栈已空返回true

if(top==-1)

returntrue;

returnfalse;

}

boolpush(constTitem)//入栈O

(1)

{

/*if(top==(mSize-1)){//若上溢

T*newst=newT[mSize*2];//扩容到2倍

for(inti=0;i<=top;i++)//复制

newst[i]=st[i];

delete[]st;//释放旧空间

st=newst;//恢复st

mSize*=2;//改写mSize

}*/

st[++top]=item;//插入item

returntrue;

}

boolpop(T&item)//出栈O

(1)

{

if(top==-1){

cout<<"空栈不能删"<

returnfalse;

}

else{

item=st[top--];

returntrue;

}

}

T&gettop()//取栈顶O

(1)

{

if(top==-1){

cout<<"空栈不能读取"<

}

else

returnst[top];

}

boolinput(){

charin;

inti=0;

cout<<"请您输入您要转换的前缀表达式"<

cin>>in;

while(in!

='='){

a[i]=in;

cin>>in;

i++;

}

a[i]='=';

i=0;

while(a[i]!

='='){

cout<

i++;

}

}

inttrans(){

inti=0,j=0;

charch,e;

ch=a[i];

while(ch!

='='){

switch(ch){

case'(':

push(ch);

break;

case'+':

case'-':

case'/':

case'*':

{

while(!

isEmpty()&&gettop()!

='('&&gettop()<=ch){

e=gettop();

b[j]=e;

pop(e);

j++;

}

push(ch);

}

break;

case')':

if(!

isEmpty()){

while(gettop()!

='('){

e=gettop();

b[j]=e;

pop(e);

j++;

}

}

pop(e);

break;

default:

if(ch>='0'<='9')

b[j++]=ch;

}

ch=a[++i];

}

while(!

isEmpty()){

e=gettop();

b[j++]=e;

pop(e);

}

intk=0;

cout<

"<

for(;k

cout<

returnj;

}

};

boolCaculator(){//全局函数来计算后缀表达式

arrStacks(100);

intnewope,ope1,ope2,e;

charc;

cout<

"<

while(cin>>c,c!

='='){

switch(c){

case'+':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1+ope2);break;

case'-':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1-ope2);break;

case'*':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1*ope2);break;

case'/':

ope2=s.gettop();s.pop(e);ope1=s.gettop();s.pop(e);s.push(ope1/ope2);break;

default:

cin.putback(c);

cin>>newope;

s.push(newope);

break;

}

}

cout<

"<

e=s.gettop();

cout<

s.pop(e);

}

main(){

inti;

arrStackm(100);

m.input();

i=m.trans();

Caculator();

}

 

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

当前位置:首页 > 解决方案 > 解决方案

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

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