两个栈来模拟浏览器的操作。
1 #include <iostream> 2 #include <string> 3 #include <stack> 4 using namespace std; 5 6 stack<string> forward; 7 stack<string> back; 8 9 int main () 10 { 11 string cur("http://www.acm.org/"), tmp; 12 while ( cin >> tmp ) 13 { 14 if ( tmp == "QUIT" ) 15 { 16 break; 17 } 18 else if ( tmp == "BACK" ) 19 { 20 if ( back.empty() ) 21 { 22 cout << "Ignored" << endl; 23 } 24 else 25 { 26 forward.push(cur); 27 cur = back.top(); 28 back.pop(); 29 cout << cur << endl; 30 } 31 } 32 else if ( tmp == "FORWARD" ) 33 { 34 if ( forward.empty() ) 35 { 36 cout << "Ignored" << endl; 37 } 38 else 39 { 40 back.push(cur); 41 cur = forward.top(); 42 forward.pop(); 43 cout << cur << endl; 44 } 45 } 46 else if ( tmp == "VISIT" ) 47 { 48 back.push(cur); 49 cin >> cur; 50 while ( !forward.empty() ) 51 { 52 forward.pop(); 53 } 54 cout << cur << endl; 55 } 56 } 57 return 0; 58 }