首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj-1028 -网页导航

poj-1028 -网页导航

作者头像
瑾诺学长
发布2018-09-21 16:12:56
6420
发布2018-09-21 16:12:56
举报
文章被收录于专栏:专注研发专注研发专注研发

Description

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
题目大意:

标准的web浏览器中包含特性向后和向前移动页面最近访问了。实现这些特性的一种方法是使用两个堆栈跟踪的页面,可以达成的向后和向前移动。这个问题,你问来实现这一点。

以下命令需要支持:

:把当前页面顶部的堆栈。流行的页面的顶端向后堆栈,使其成为新的当前页面。如果向后栈为空,命令将被忽略。

转发:把当前页面向后堆栈的顶部。流行的页面的顶部堆栈,使其成为新的当前页面。如果栈是空,命令将被忽略。

访问:把当前页面顶部的向后堆栈,并使新的当前页面指定的URL。远期栈为空。

退出:退出浏览器。

假定浏览器最初加载web页面URL http://www.acm.org

        说的是栈的模拟,但是我还不能很好的理解运用栈,所以我就这样做了,调试了好半天
#include<iostream>
#include<string>
using namespace std;
int main()
{
   string str[100];
    string s1="http://www.acm.org/",s2;
str[0]=s1;
int q=0,ed=0;
    while(1)
    {cin>>s1;
        if(s1=="QUIT"){break;}

        if(s1=="VISIT")
        {
        cin>>s2;
    q++;
      str[q]=s2;
      cout<<str[q]<<endl;
ed=q;    //每次添加新的url时都要让ed=q;q相当于一直指向当前所指的url下标; 而ed是最后一个输入的url下标。
        }
        if(s1=="BACK")
        {if(q<=0){ cout<<"Ignored"<<endl;}//若把if 和 else 的条件互换,过程会变得更加繁琐;
          else {cout<<str[--q]<<endl;}  

        }
        if(s1=="FORWARD")
        {
         if(q>=ed){  cout<<"Ignored"<<endl;}
            else {cout<<str[++q]<<endl;}

        }

    }


    return 0;
和我思路一样的代码,总觉的人家的简单清晰很多
并且我最后一个bug还是看这个代码找到的

#include<stdio.h>
 char str[200][71]={"http://www.acm.org/"};
int point=0,end=0;
void forword()
{
    if(point>=end) printf("Ignored\n");//我在参考这儿的写法
    else printf("%s\n",str[++point]);
}
void back()
{
    if(point<=0) printf("Ignored\n");
    else printf("%s\n",str[--point]);
}
void vist()
{
    scanf("%s",str[++point]);
    printf("%s\n",str[point]);
    end=point;                                 //在此处更新栈的最终指针
}
int main()
{
    char com[10];
    int g=1;
    while(g)
    {
        scanf("%s",com);
        switch(com[0])
        {
            case 'V':vist();break;
            case 'B':back();break;
            case 'F':forword();break;
            default : g=0;break;
        }
    }
    return 0;
}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档