栈的应用

来源:互联网 发布:双口网络y参数矩阵 编辑:程序博客网 时间:2024/06/16 12:12
/* 括号匹配与检测 */#include <stack>#include <string>#include <iostream>using namespace std;int check(char c){    if(c=='[') return 1;    if(c=='(') return 1;    return 0;}int ok(char x , char y){    if(x=='(' && y==')') return 1;    if(x=='[' && y==']') return 1;    return 0;}int main (){    int f , i;    string str;    stack <char> s;    while(cin>>str)    {        while(!s.empty()) s.pop();        if(str.size()%2==1)        {//如果是合法情况必为偶数            cout<<"false"<<endl;            continue;        }        for(f=i=0 ; i<str.size() ; i++)        {//入栈与出栈操作            if(check(str[i])) s.push(str[i]);            else            {                if(s.empty()  && !check(str[i]))                {f=1 ; break;}                else if(!s.empty() && !ok(s.top() , str[i]))                {f=1;break;}                else  s.pop();            }        }        if(!s.empty() || f) cout<<"false"<<endl;        else cout<<"true"<<endl;    }    return 0;}


 

/* 行编辑程序 */#include <stack>#include <string>#include <iostream>#include <algorithm>using namespace std;int main (){int i;string str;stack <char> s;while(getline(cin,str)){//while(!s.empty()) s.pop();for(i=0 ; i<str.size() ; i++){if(str[i]!='@' && str[i]!='#') s.push(str[i]);else if(str[i]=='#') s.pop();else{while(!s.empty()) s.pop();}}str = "";if(s.empty()) cout<<"Empty"<<endl;else{while(!s.empty()) {str+=s.top();s.pop();}reverse(str.begin(),str.end());cout<<str<<endl;}}return 0;}


 

/* 迷宫求解 */#include <stack>#include <cstdio>#include <cstring>#include <iostream>#define    N    100using namespace std;int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};bool  v[N][N];int map[N][N],n,m;struct node{int x,y,dre;node(){x=y=dre=0;}}p,q;int ok(int x , int y){if(map[x][y] == 0 && v[x][y] == 0)return 1;return 0;}int isbound(int x , int y){if(x<0 || x>=n || y<0 || y>=m )return 0;return 1;}int main (){int r=0;int i,j,k;stack <node> s;//freopen("2.txt","r",stdin);while(scanf("%d%d",&n,&m) && n||m){//map[][]代表地图'1'为阻碍'0'为通路for(r=k=i=0 ; i<n ; i++){for(j=0 ; j<m ; j++){scanf("%d\n",&map[i][j]);v[i][j] = 0;//v[][]为标记数组}}while(!s.empty()) s.pop();p.x=p.y=p.dre=0;s.push(p);//采用深度优先搜索方式while(!s.empty()){if(p.x == n-1 && p.y== m-1){r=1;break;}for(i=p.dre ; i<4 ; i++){int x = p.x + d[i][0];int y = p.y + d[i][1];if(ok(x, y) && isbound(x , y)){//当前位置可以走  入栈 改变方向 且标记q.x = p.x = x;q.y = p.y = y;q.dre=i+1; s.push(q);v[x][y]=1;break;}}if(i>=4 && !s.empty()) {p = s.top(); s.pop();}}if(r) puts("成功逃离");else  puts("迷宫无解");}return 0;}/* 思路:1 用数组存储图2 设置一个结构体 保存访问的信息3 设置一个栈 用于保存访问过的点4 不需要回溯 5 默认(0 0)为起点 (n-1 m-1)为终点6 地图范例4 60 1 0 1 0 10 0 0 1 1 10 0 0 0 1 11 1 1 0 0 08 算法:1 如果达到直接跳出2 如果当前可以通 则入栈 且要改变下一次访问的方向3 如果当前不可通 则访问四个方向直到找到一个通路4 如果还是不通,则需要出栈 将出栈节点设置为新的访问节点*/


 

/* 保证输入式子有解 */#include <stack>#include <string>#include <cstdio>#include <cstring>#include <iostream>#define    N      7using namespace std;char H[N][N+1]={ {'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},{'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},{'<','<','<','<','<','=','='},{'>','>','>','>','>','=','='},{'<','<','<','<','<','=','='},};int Pow(int x , int y){    int t=1;for(int i=0 ; i<y ; i++) t*=x;return t;}int hci(char c){if(c=='+') return 0;if(c=='-') return 1;if(c=='*') return 2;if(c=='/') return 3;if(c=='(') return 4;if(c==')') return 5;if(c=='#') return 6;}int cti(string th){int i , s=0 , l=th.size();for(i=0 ; i<l ; i++)s+=(th[i]-48)*Pow(10,l-i-1);return s;}bool check(char c){if(c=='+' || c=='-' || c=='*' ||   c=='/' || c=='(' || c==')' || c=='#')   return 1;return 0;}char cp(char x , char y){return H[hci(x)][hci(y)];}int operate(int a ,int b , char c){if(c=='-') return a-b;if(c=='+') return a+b;if(c=='*') return a*b;if(c=='/') return a/b;}int isbound(char c){    if(c=='-' || c=='+'    || c=='*' || c=='/')       return 1;       return 0;}string deal(string str){    string th="";    int i , l=str.size();    for(i=1 ; i<=l ; i++)    {        th+=str[i-1];        if(isbound(str[i]) && str[i-1]=='(')           th+='0';    }    th+='#';    return th;}int main (){int i,j;string str;int a,b,res;stack <int> s1;stack <char> s2;//freopen("1.txt","r",stdin);while(getline(cin,str)){str=deal(str);        s2.push('#');if(str[0] == '0') break;for(i=0 ; i<str.size() ; i++){string th="";if('0'<=str[i] && str[i]<='9')//数字{for(j=i ; j<str.size() ; j++){if('0'<=str[j] && str[j]<='9')th+=str[j];else break;}i=j-1;s1.push(cti(th));}else if(check(str[i]))//运算符{char c=cp(s2.top(),str[i]); //比较优先级switch(c){case '<':s2.push(str[i]);break;case '>':a=s1.top();s1.pop();b=s1.top();s1.pop();res=operate(b , a , s2.top());s2.pop();i-=1;   //不直接入栈 而是下一次比较之后再做选择s1.push(res);break;case '=':s2.pop();break;}}else continue;}cout<<s1.top()<<endl;while(!s1.empty()) s1.pop();while(!s2.empty()) s2.pop();}return 0;}/*代码用STL实现 , 关键在于思想 代码实现时比较容易的*/
原创粉丝点击