• PKU 1024 Web Navigation 解法及教训总结


    此道题就是考查基本数据结构堆栈的操作,之所以要在此写一下 ,是要总结一下做题的教训。以提醒自己少犯类似的错误。之所以这个简单的问题耗费了我一上午的时间,居然是因为我没有认真读题,只能说有点杯具了。:-(

    代码
    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那样更简单,代码更少。
    最后总结一下教训吧,一定要认真读题。要不到时候真的是欲哭无泪啊。
  • 相关阅读:
    第02周学习提升建议:【python安装、变量、输入输出、流程、循环】--【第五篇】流程、循环
    向gitlab上传本地项目
    [jenkins+gitlab+postman] 持续集成
    linux 上安装newman
    【python】读取cfg/ini/txt配置文件
    【CI/CD】docker部署gitlab,并且本地拉取gitlab代码成功
    【CI/CD】docker部署Jenkins
    【TCP知识】03_Linux查看TCP连接状态
    【nginx知识】02_【转载】nginx反向代理时保持长连接
    【TCP/IP知识】02_【转载】TCP 半连接队列和全连接队列
  • 原文地址:https://www.cnblogs.com/ghl_carmack/p/1811181.html
Copyright © 2020-2023  润新知