首页 > 代码库 > 【POJ 1028】模拟浏览器

【POJ 1028】模拟浏览器

本题要求模拟浏览器的前进、后退等操作。

用两个栈实现,一个控制前进,一个控制后退。

在前进与后退操作中,从一个栈中弹出栈顶元素,压入另一个栈中。

当打开一个新网页时,将前进栈清空。

 

代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <stack>using namespace std;int top;char Cmd[100 + 5], URL[1000 + 5][100 + 5];stack<char *> backStack, foreStack;void solveVisit(){    scanf("%s", URL[++top]);    printf("%s\n", URL[top]);    while (!foreStack.empty()) foreStack.pop();    backStack.push(URL[top]);}void solveBack(){    if (backStack.size() == 1)     {        printf("Ignored\n");        return;    }    foreStack.push(backStack.top());    backStack.pop();    printf("%s\n", backStack.top());}void solveForward(){    if (foreStack.size() == 0)     {        printf("Ignored\n");        return;    }    printf("%s\n", foreStack.top());    backStack.push(foreStack.top());    foreStack.pop();}int main(){//    freopen("1.in", "r", stdin);    strcpy(URL[++top], "http://www.acm.org/");    backStack.push(URL[top]);    while (~scanf("%s", Cmd))    {        if (!strcmp(Cmd, "QUIT")) return 0;        if (!strcmp(Cmd, "VISIT")) solveVisit();        if (!strcmp(Cmd, "BACK")) solveBack();        if (!strcmp(Cmd, "FORWARD")) solveForward();    }    return 0;}