POJ 1028 Web Navigation

题意:模拟一个网页浏览器的操作,BACK, FORWARD, VISIT, and QUIT。

思路:。。。

代码1:段。。

 

//模拟法
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
	int t=0;//当前网址位置
	int c[500];//c数组标记网址是否存在
	memset(c,0,sizeof(c));
	string a[500];//储存网址
	a[0]="http://www.acm.org/";
	c[0]=1;
	string b;
	while(cin>>b&&b[0]!='Q'){	
		if(b[0]=='V'){
			t++;
			cin>>a[t];
			cout<<a[t]<<endl;
			c[t]=1;
			c[t+1]=0;   //把当前访问的后面的抹掉
		}
		if(b[0]=='B'){
			if(t==0)cout<<"Ignored"<<endl;
			else{ 
				t--;cout<<a[t]<<endl;
			}
		}
		if(b[0]=='F'){
			if(c[t+1]==0)
				cout<<"Ignored"<<endl;
			else{
				t++; cout<<a[t]<<endl;
			}	
		}
	}
	return 0;
}

 

代码2:自己写的

 

#include<iostream>
#include<string>
using namespace std;

int main(){
    string b[500];//BACK栈
    b[0]="http://www.acm.org/";
    string f[500];//FORWARD栈
    int lb,lf;//BACK位置,FORWARD位置
    lb=0;lf=0;//初始位置为0
    string s;//命令
    string s2;//网址
    while(cin>>s&&s[0]!='Q'){
        if(s[0]=='B'){
            if(lb==0) cout<<"Ignored"<<endl;
            else{
                lf++;//forward加一
                f[lf]=b[lb];
                lb--;//back减一
                cout<<b[lb]<<endl;
            }
        }
        else if(s[0]=='F'){
            if(lf==0)  cout<<"Ignored"<<endl;
            else{
                lb++;//back加一
                b[lb]=f[lf];
                cout<<f[lf]<<endl;
                lf--;//forward减一
            }
        }
        else{
            lb++;//back加一
            lf=0;//forward变为0
            cin>>b[lb];
            cout<<b[lb]<<endl;
        }
    }
    return 0;
}


 

posted @ 2014-09-15 20:23  gongpixin  阅读(260)  评论(0编辑  收藏  举报