POJ - 1028(c++)

题目:

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/


不懂意思的就去百度查翻译

知识点:1、栈


我的代码:

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

int main(){
	stack<string>qz,hz;			//声明对象。前进栈、后退栈
	string cp="http://www.acm.org/",com;	//cp是当前网页,com是输入的
	while(cin>>com){
		if(com[0]=='B')			//BACK
		{
			if(hz.empty())		//后退栈空返回true 否则false
			{
				cout<<"Ignored"<<endl;	//后退栈空,被忽略,输出Ignored
			}
			else
			{
				qz.push(cp);	//将当前网页置于前进栈的栈顶
                cp=hz.top();	//当前网页等于后退栈的栈顶
                hz.pop();		//移除后退栈的栈顶
                cout<<cp<<endl;	//输出当前网页,也就是之前的前一个网页
			}
		}
		else if(com[0]=='F')	//FORWARD
		{
			if(qz.empty()){		//前进栈空为ture,
				cout<<"Ignored"<<endl;
			}
			else
			{
				hz.push(cp);	//将当前网页置于后退栈的顶部
				cp=qz.top();	//将当前网页等于前进栈的顶部
				qz.pop();		//移除前进栈的顶部
				cout<<cp<<endl;	//表达出当前网页
			}
			
		}
		else if(com[0]=='V')	//VISIT
        {
			hz.push(cp);		//将当前网页置于后退栈的栈顶
			cin>>cp;			//输入新网站
			cout<<cp<<endl;		//进入新网站
			while(!qz.empty())	//前进栈空返回false,否则true,直到清空前进栈
				qz.pop();		//移除前进栈栈顶元素			
		}
		else
		{
			break;				//都不是则跳出循环
		}
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值