07-串应用-
题目描述
标准Web浏览器包含在最近访问过的页面之间前后移动的功能。 实现这些功能的一种方法是使用两个堆栈来跟踪可以通过前后移动到达的页面。 在此问题中,系统会要求您实现此功能。
需要支持以下命令:
BACK:将当前页面推到前向堆栈的顶部。 从后向堆栈的顶部弹出页面,使其成为新的当前页面。 如果后向堆栈为空,则忽略该命令。
FORWARD:将当前页面推到后向堆栈的顶部。 从前向堆栈的顶部弹出页面,使其成为新的当前页面。 如果前向堆栈为空,则忽略该命令。
访问:将当前页面推送到后向堆栈的顶部,并将URL指定为新的当前页面。 清空前向堆栈。
退出:退出浏览器。
假设浏览器最初在URL http://www.acm.org/上加载网页
输入
测试数据只有一组
输入是一系列命令。 命令关键字BACK,FORWARD,VISIT和QUIT都是大写的。 URL没有空格,最多包含70个字符。 任何时候堆栈都不会超过100个元素。 输入结束由QUIT命令指示。
输出
对于除QUIT之外的每个命令,如果不忽略该命令,则在执行命令后输出当前页面的URL。否则,打印“Ignored”。 每个命令的输出应该在对应行上输出。 QUIT命令无输出。
输入样例
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
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
要做什么题目都写了
#include<iostream>
#include<stack>
using namespace std;
int main()
{
string command,wweb;
stack<string> back;
stack<string> forward;
string web="http://www.acm.org/";
while(cin>>command)//约等于scanf()!=EOF
{
if(command=="BACK")
{
if(!back.empty())
{
forward.push(web);
web=back.top();
back.pop();
cout<<web<<endl;
}
else
cout<<"Ignored"<<endl;
}
if(command=="FORWARD")
{
if(!forward.empty())
{
back.push(web);
web=forward.top();
forward.pop();
cout<<web<<endl;
}
else
cout<<"Ignored"<<endl;
}
if(command=="VISIT")
{
cin>>wweb;
back.push(web);
web=wweb;
while(!forward.empty())
forward.pop();
cout<<web<<endl;
}
if(command=="QUIT")
break;
}
return 0;
}
另附 自己写stack的c语言写法
#include<iostream>
using namespace std;
struct node
{
string data;
node *prior;
};
class stack_node
{
public:
node *base;
node *top;
stack_node()
{
base=top=new node;
top->prior=NULL;
}
bool empty()
{
if(base==top)
return true;
else
return false;
}
void push(string e)
{
node *p=new node;
p->data=e;
p->prior=top;
top=p;
}
void pop()
{
if(base==top)
return ;
node *p=top;
top=p->prior;
delete p;
}
string gettop()
{
if(base!=top)
return top->data;
else
return 0;
}
};
int main()
{
string command,wweb;
stack_node back;
stack_node forward;
string web="http://www.acm.org/";
while(cin>>command)//约等于scanf()!=EOF
{
if(command=="BACK")
{
if(!back.empty())
{
forward.push(web);
web=back.gettop();
back.pop();
cout<<web<<endl;
}
else
cout<<"Ignored"<<endl;
}
if(command=="FORWARD")
{
if(!forward.empty())
{
back.push(web);
web=forward.gettop();
forward.pop();
cout<<web<<endl;
}
else
cout<<"Ignored"<<endl;
}
if(command=="VISIT")
{
cin>>wweb;
back.push(web);
web=wweb;
while(!forward.empty())
forward.pop();
cout<<web<<endl;
}
if(command=="QUIT")
break;
}
return 0;
}