poj1028 模拟

题意:模拟浏览器操作。
BACK:当前页面进入forward栈,backward栈出栈,如果backward栈为空,命令被忽略。
FORWARD:当前页面进入backward栈,forward栈出栈,如果forward栈为空,命令被忽略。
VISIT:当前页面进入backward栈,该元素为当前页面,清空forward栈。
QUIT:退出浏览器。 

算法:模拟 


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

int main()
{
	stack<string> forward;
	stack<string> backward;
	string command,currPage;
	while (cin >> command)
	{
		if (command == "QUIT")
		{
			break;
		}
		else if (command == "VISIT")
		{
			cin >> currPage;
			cout << currPage << endl;
			backward.push(currPage);
			while (!forward.empty())
			{
				forward.pop();
			}
		}
		else if (command == "BACK")
		{
			if (!backward.empty())
			{
				if (currPage != "http://www.acm.org/")
				{
					forward.push(currPage);
				}
				
				backward.pop();			// 因为backward栈的栈顶始终存放的当前显示的页面,因此BACK时要显示的是backward栈的第二个元素 
				
				(!backward.empty())?   currPage = backward.top() : currPage = "http://www.acm.org/";
				cout << currPage << endl ;
			}
			else
			{
				currPage = "http://www.acm.org/";
				cout << "Ignored" << endl; 
			}
		}
		else if (command == "FORWARD")
		{
			if (!forward.empty())
			{
				currPage = forward.top() ;
				backward.push(currPage);			// 只有当forward栈非空时,才需要将currPage放入backward栈 
				cout << currPage << endl;
				forward.pop();
			}
			else 
			{
				cout << "Ignored" << endl; 
			}
		}
	}
}




你可能感兴趣的:(模拟,poj)