此道题就是考查基本数据结构堆栈的操作,之所以要在此写一下 ,是要总结一下做题的教训。以提醒自己少犯类似的错误。之所以这个简单的问题耗费了我一上午的时间,居然是因为我没有认真读题,只能说有点杯具了。:-(
代码
1 下面把代码贴出来,写的不好,别喷我。:-)
2 #include <iostream>
3 using namespace std;
4 class Stack
5 {
6 public:
7 void PushBack (char url[]);
8 void PopUp (char url[]);
9 inline bool IsEmpty (void) {return top == -1;}
10 inline bool IsFull (void) {return top == 100;}
11 int Top(){return top;}
12 inline void Reset (void){top = -1;}
13 inline void Print (void)
14 {
15 cout<<Url[top]<<endl;
16 }
17 Stack ();
18 ~Stack ();
19 private:
20 char Url[101][71];
21 int top;
22 };
23
24 Stack::~Stack()
25 {
26 }
27 Stack::Stack ()
28 {
29 top = -1;
30 }
31
32 void Stack::PushBack (char url[])
33 {
34 if (IsFull())
35 return;
36 ++ top;
37 strcpy(Url[top], url);
38 }
39
40 void Stack::PopUp (char url[])
41 {
42 if (IsEmpty())
43 {
44 strcpy (url, "");
45 return;
46 }
47 strcpy (url, Url[top]);
48 -- top;
49 }
50
51 int main ()
52 {
53 Stack backward, forward;
54 char command[10];
55 char url[71] = "http://www.acm.org/";
56 cin>>command;
57 while (strcmp (command, "QUIT") != 0)
58 {
59 if (strcmp (command, "VISIT") == 0)
60 {
61 backward.PushBack(url);
62 cin>>url;
63 cout<<url<<endl;
64 forward.Reset();
65 }
66 else if (strcmp (command, "BACK") == 0)
67 {
68 if (backward.IsEmpty())
69 {
70 cout<<"Ignored"<<endl;
71 goto input;
72 }
73 forward.PushBack(url);
74 backward.PopUp(url);
75 cout<<url<<endl;
76
77 }
78 else if (strcmp (command, "FORWARD") == 0)
79 {
80 if (forward.IsEmpty())
81 {
82 cout<<"Ignored"<<endl;
83 goto input;
84 }
85 backward.PushBack(url);
86 forward.PopUp(url);
87 cout<<url<<endl;
88 }
89 input: cin>>command;
90 }
91 return 0;
92 }
当然你也可以用stl中的stack那样更简单,代码更少。
最后总结一下教训吧,一定要认真读题。要不到时候真的是欲哭无泪啊。