题目说的很明白了,按照题意做就行了
第一次用栈,wa了几次,注意弄清关系,别弄混当前页和栈中页的状态
就不多说了
#include<iostream>
#include<stack>
#include<string>
//#include<fstream>
using namespace std;
int main()
{
string p = "http://www.acm.org/";
stack<string> backward;
stack<string> forward;
string s;
backward.push(p);
//ifstream cin("in.txt");
while(cin>>s,s != "QUIT")
{
if(s == "VISIT")
{
cin>>s;
cout<<s<<endl;
backward.push(s);
while(!forward.empty())
forward.pop();
}
else if(s == "BACK")
{
// cout<<backward.size()<<endl;
if(backward.size() >= 2)
//if(!backward.empty())
{
forward.push(backward.top());
backward.pop();
cout<<backward.top()<<endl;
}
else
{
cout<<"Ignored"<<endl;
}
}
else if(s == "FORWARD")
{
if(!forward.empty())
{
cout<<forward.top()<<endl;
backward.push(forward.top());
forward.pop();
}
else
{
cout<<"Ignored"<<endl;
}
}
}
}