| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5194 人关注过本帖
标题:如何使用堆栈法???
只看楼主 加入收藏
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
想不出来

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-05 09:56
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
幻幻帮一忙

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-07 14:10
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
一个逆波兰式的生成程序VC & C++
你学过C,应该能看懂吧?
#include<iostream.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#define maxbuffer 64
void main()
\{
char display_out(char out_ch[maxbuffer], char ch[32]);
//int caculate_array(char out_ch[32]);
static int i=0;
static int j=0;
char ch[maxbuffer],s[maxbuffer],out[maxbuffer];
cout<<"请输入中缀表达式: ";
cin>>ch;
for(i=0;i<maxbuffer;i++)
\{out[i]=ch[i];}
cout<<"请确认您输入的表达式: ";
while(out[j]!=‘#‘)
\{
cout<<out[j];
j++;
}
cout<<‘#‘<<endl;
display_out(s,out);
//caculate_array;
}

char display_out(char out_ch[32],char ch[])
\{
int top=-1;
int i=0,data[maxbuffer],n;
int j=0;

char sta[20];
while(ch[i]!=‘#‘)
\{
if(isalnum(ch[i]))
\{
while(isalnum(ch[i]))
\{
out_ch[j]=ch[i];
j++;
i++;
}out_ch[j]=‘ ‘;j++;
}
else\{

switch(ch[i])
\{

case ‘+‘:
case ‘-‘: if(sta[top]==‘(‘||top==-1)
\{
top++;
sta[top]=ch[i];
i++;
}

else
\{
//j--;
out_ch[j]=sta[top];
j++;
top--;
//i++;
}
break;
//break;
case ‘*‘:
case ‘/‘:if(sta[top]==‘*‘&&sta[top]==‘/‘)
\{
out_ch[j]=sta[top];
j++;
//i++;
top--;
}

else
\{
top++;
sta[top]=ch[i];
i++;
}
break ;
//break;
case ‘(‘:top++;
sta[top]=ch[i];
i++;
break;
case ‘)‘:if(sta[top]==‘(‘)
\{
top--;
i++;
}
if(top==-1)
\{
cout<<"错误: 第"<<j<<"个位置的“)”没有找到与之匹配的“(”";
ch[i]=‘#‘;j=0;
}
else
\{
//while(sta[top]!=‘(‘)\{

out_ch[j]=sta[top];
top--;
j++;
//}break;

}
break;
/*case ‘#‘: out_ch[j]=‘#‘;
j++;
break;*/
default:
cout<<" your input is error"<<endl;
ch[i]=‘#‘;
j=0;
break;
}
}
}while(top!=-1)
\{

out_ch[j]=sta[top];j++;
top--;
}

out_ch[j]=‘#‘;
n=0;

cout<<"逆波兰表达式为: ";

while(out_ch[n]!=‘#‘)
\{
cout<<out_ch[n];
n++;
}
cout<<endl;
j=0;
/*while(ch[j]!=‘#‘)
\{
top++;
data[top]=ch[j];
}
cout<<data[top];
*/
return out_ch[maxbuffer];


}

[此贴子已经被作者于2005-4-7 14:33:16编辑过]



2005-04-07 14:24
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 

逆波兰表达仍例子: (2+3)*4 就写成2,3,+,4,* 所以eval()只要有两个栈,一个存表达式本身,一个存中间计算过程. 从表达式中pop(),遇到数就push()到过程栈中,遇到算符就从过程栈中pop()给出此步值再压push()回栈中,直到结束.

比如上例:

表达式栈 过程栈 2,3,+,4,* 3,+,4,* 2 +,4,* 2,3 4,* 5 * 4,5 -- 20


2005-04-07 14:28
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 

3 32+ 5 3* -

12 34 2- * 8 /

乍一看上面两个式子很奇怪,是吗?它们就是这里所要讲到的一种表达式的记法——逆波兰表达式。

现在,准备一个很窄的圆筒,筒是有底的,像一个细长的杯子,粗细刚好和一枚硬币相当。再做几个和硬币一样大的小圆纸片,在纸片上依次写上“3”“32”“+”“5”“3”“*”“-”,记住,每个纸片上要么只写一个数,要么只写一个运算符号,把它们按上面的顺序排好。好,现在仔细听我说,按顺序一个接一个地拿起小圆纸片,反复执行以下几个规则:

1. 如果你拿着的是一个数,不多说,直接把它放进圆筒;

2. 如果你拿着的是一个运算符号,不要把它放进去。先从圆筒里取出两个数(当然是先取最上而的啦,筒很细的),然后处这两个数作运算符号指定的运算,并把结果写在一张新的纸片上,然后放进筒里。比如你拿着的是“+”,你要依次取出“32”和“3”,让它们相加,得“35”,把“35”写在一张新纸片上(现在“34”和“12”可以扔掉了),并把这张新纸片放进圆筒。

当圆筒里只有一个数时,你就可以停下来了,我猜这个数是20,没错,这就是这个表达式的值!

我们刚才操作的,其实就是一个“栈”,栈是一种数据结构,具有一个性质——后进先出(LIFO——Last Input First Output),你已经深有体会了,就像一摞盘子,你只能从最上面的开始取,放的时候也只能放在最上面。放进去的动作叫做“入栈”,取出来叫做“弹出”。以后你就可以把栈想像成一摞盘子,或是上面说的小圆筒和小纸片,栈就是这么简单!

逆波兰表达式虽然看起来比较繁琐,其实在计算机中很有用。计算机可不知道先乘除后加减,先括号内后括号外,它要把你输入的式子变成逆波兰表达式,它就可以不断地执行上面两个规则,直至把结果算出来告诉你。现在你可以亲自在计算机上试试,试着编一个程序,让它读入一个逆波兰表达式,然后让它计算!

成功了吗?如果你回答“是”,你可以接着思考另一个问题,对于7个元素的序列1 2 3 4 5 6 7,通过一个栈的处理后(比如1 2 3入栈,3 2弹出,4入栈,4弹出……如此等等),能得到多少种不同的排列?能得到4 3 5 2 1 7 6吗?能得到3 2 4 5 7 1 6吗?


2005-04-07 14:31
eastsnake
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2005-3-8
收藏
得分:0 
没学过数据结构?
这是数据结构的知识

程序员是男孩,语言是女孩; 每个男孩都希望能交往更多的女孩; 但是却没有一个男孩真正了解一个女孩; 男孩总是不能专心一个女孩,而女孩却总是在变~
2005-04-07 15:24
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
谢谢幻幻。
数据结构。不知道学过没。忘了。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-07 15:36
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
以下是引用yushengou在2005-4-7 15:36:38的发言: 谢谢幻幻。 数据结构。不知道学过没。忘了。
我发现你很像我哈

2005-04-07 15:37
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
以下是引用幻风幻云在2005-4-7 15:37:51的发言: 我发现你很像我哈
真的吗。这可不是个好习惯啊 我要改,不和你同流和污

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-07 15:40
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
以下是引用yushengou在2005-4-7 15:40:34的发言: 真的吗。这可不是个好习惯啊 我要改,不和你同流和污
这是天性 不信你能改得了

2005-04-07 15:47
快速回复:如何使用堆栈法???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013304 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved