用栈模拟浏览器c++

#pragma   warning (disable: 4786)
#include <iostream>
#include <stack>
#include <string>
using namespace std;
//要注意当前访问的页面就是backset栈顶的元素,切记
//理解浏览器前进后退的含义自己编
int main(){
	
	stack<string> back,forward;
	string command,url;
	back.push("http://www.acm.org/");
	while (cin>>command,command!="QUIT")
	{
		if (command == "VISIT")
		{
			cin>>url;
			back.push(url);
			cout<<url<<endl;
		    while(!forward.empty()) forward.pop();
		} 
		else if (command == "BACK")
		{
			if (back.size()==1)//等于1表示就是当前的页面,已经不能够弹栈了
			{
				cout<<"Ignored"<<endl;
			} 
			else
			{
				url = back.top();
				forward.push(url);	
				back.pop();
				url = back.top();
				cout<<url<<endl;
			}
			
		}
		else if (command == "FORWARD")
		{
			if (forward.size()==0)//forward的大小可以为零,这点要与backset区分开来
			{
				cout<<"Ignored"<<endl;
			} 
			else
			{	
				url= forward.top();
				back.push(url);
				forward.pop();
				cout<<url<<endl;
			}
		}
	}
	return 0;
}


 

posted @ 2012-08-16 20:03  gitran  阅读(318)  评论(0编辑  收藏  举报