poj1028

简单栈模拟

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

stack<string>bstack, fstack;
string    current;

void visit()
{
    if (current != "")
        bstack.push(current);
    cin >> current;
    while (!fstack.empty())
        fstack.pop();
    cout << current << endl;
}

void forward()
{
    if (fstack.empty())
    {
        printf("Ignored\n");
        return;
    }
    bstack.push(current);
    current = fstack.top();
    fstack.pop();
    cout << current << endl;
}

void back()
{
    if (bstack.empty())
    {
        printf("Ignored\n");
        return;
    }
    fstack.push(current);
    current = bstack.top();
    bstack.pop();
    cout << current << endl;
}

int main()
{
    string    command;

    //freopen("t.txt", "r", stdin);
    current = "http://www.acm.org/";
    while (cin >> command && command != "QUIT")
    {
        if (command == "VISIT")
            visit();
        else if (command == "FORWARD")
            forward();
        else if (command == "BACK")
            back();
        getchar();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/rainydays/p/2820638.html