1028: Web Navigation: Stack pit operating point analysis +

Subject to the effect

Standard features include a Web browser can forward and backward between pages recently visited. One way to achieve these two functions are used to track the page stack by moving forward and backward can be reached. In this issue, it is required that you implement this action.

The need to support the following command:

BACK: The current page pushing forward top of the stack. Pop-up pages from the top of the back of the stack, making it the new current page. If the stack is empty back, ignore the command.

Forward: Push the current page back top of the stack. Pop-up pages from the top of the stack forward, making it the new current page. If the stack is empty before, it ignores the command.

Access: The current page pushing back the top of the stack, and the URL designated as the new current page. Forward stack is empty.

Exit: Exit browser.

Assuming that the browser initially loads the web page by URL http://www.acm.org/.

Ideas analysis

There are several pits point

  • BACK, FORWARD these two operations, if the stack is empty, the entire command is not executed! The current url will not change.
  • Note that the current url is the initial value.
#include<iostream>
#include<stack>
#include<string>
using namespace std;

stack<string> forward1, backward;

int main() {
	char s[10]; string str, curUrl("http://www.acm.org/");
	while (cin >> s) {
		int ignored = 0;
		if (s[0] == 'Q')break;
		if (s[0] == 'V') {
			backward.push(curUrl);
			cin >> str;
			curUrl = str;
			while (!forward1.empty()) forward1.pop();
		}
		else if (s[0] == 'B') {
			if (backward.empty()) ignored = 1;
			else {
				forward1.push(curUrl);
				curUrl = backward.top(); backward.pop();
			}
		}
		else if (s[0] == 'F') {
			if (forward1.empty()) ignored = 1;
			else {
				backward.push(curUrl); curUrl = forward1.top(); forward1.pop();
			}
		}

		if (!ignored)cout << curUrl << endl;
		else cout << "Ignored" << endl;
	}
}
Published 186 original articles · won praise 13 · views 9313

Guess you like

Origin blog.csdn.net/csyifanZhang/article/details/105185387