Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this.
The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/
Input
Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.
Output
For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.
Sample Input
VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD QUIT
Sample Output
http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ Ignored http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ Ignored
让你用两个栈模拟网站的浏览情况;
BACK:将当前页面推到前向堆栈的顶部。从后向堆栈的顶部弹出页面,使其成为新的当前页面。如果后向堆栈为空,则忽略该 命令。
FORWARD:将当前页面推到后向堆栈的顶部。从前向堆栈的顶部弹出页面,使其成为新的当前页面。如果前向堆栈为空,则忽 略该命令。
VISIT:将当前页面推送到后向堆栈的顶部,并将URL指定为新的当前页面。清空前向堆栈。
QUIT:退出浏览器。
这道题千万不要想的太复杂,只要单纯的按照题目的要求,一步一步的用栈模拟就好了,想的复杂容易累;
还是想说一下,看清楚题,是输出Ignored,首字母大写,说多了都是泪啊。。。。。。
#include<stdio.h>
#include<string.h>
#include<stack>
#include<string>
#include<iostream>
using namespace std;
string s,jj,t="http://www.acm.org/";//jj代表当前的页面;t代表最开始的初始页面;
stack<string>ff,bb;//代表前进和后退的两个栈;
string v="VISIT",b="BACK",f="FORWARD",q="QUIT",i="Ignored";
int main()
{
ff.push(t);
jj=t;
while(cin>>s)
{
if(s==q)
break;
else if(s==b)
{
if(bb.empty())
cout<<i<<endl;
else
{
ff.push(jj);
jj=bb.top();
cout<<jj<<endl;
bb.pop();
}
}
else if(s==f)
{
if(ff.empty())
cout<<i<<endl;
else
{
bb.push(jj);
jj=ff.top();
cout<<jj<<endl;
ff.pop();
}
}
else if(s==v)
{
cin>>ss;
bb.push(jj);
while(!ff.empty())
ff.pop();
cout<<ss<<endl;
jj=ss;
}
}
return 0;
}