解题思路:就是用栈来模拟浏览器的前进、后退操作。
代码:
#includestruct stack {public: stack() : top(0), bottom(0) {} void push(const char *page); void pop(char *page); bool is_empty(); void clear();private: void str_copy(char *des, const char *src); char url[101][71]; int top, bottom;};void stack::str_copy(char *des, const char *src) { while ((*des++ = *src++));}void stack::push(const char *page) { str_copy(url[top++], page);}void stack::pop(char *page) { str_copy(page, url[--top]);}bool stack::is_empty() { return top == bottom;}void stack::clear() { top = bottom = 0;}int main() { char cmd[8]; char cur_page[71] = "http://www.acm.org/"; stack fw_stack, bw_stack; while (scanf("%s", cmd) && cmd[0] != 'Q') { switch (cmd[0]) { case 'V': bw_stack.push(cur_page); fw_stack.clear(); scanf("%s", cur_page); printf("%s\n", cur_page); break; case 'B': if (bw_stack.is_empty()) { printf("Ignored\n"); } else { fw_stack.push(cur_page); bw_stack.pop(cur_page); printf("%s\n", cur_page); } break; case 'F': if (fw_stack.is_empty()) { printf("Ignored\n"); } else { bw_stack.push(cur_page); fw_stack.pop(cur_page); printf("%s\n", cur_page); } break; } } return 0;}