ZOJ1061 Web Navigation

原始版本:
#include <iostream>
#include 
<stack>
#include 
<string>
using namespace std;

void ClearStack(stack<string>& s)
{
//清空栈
    while (!s.empty())
    {
        s.pop();
    }
}
int main(void)
{
    
string command,newURL;
    
int n,cases,i;
    cin
>>n;
    
for (cases=1;cases<=n;++cases)
    {
        stack
<string> forwardStack,backwardStack;//前进栈,后退栈
        string curURL = "http://www.acm.org/";//当前访问页面
        if (cases!=1)
        {
            cout
<<endl;
        }
        
while(cin>>command)
        {
//读入一行命令
            if (command[0]=='Q')
            {
//退出
                break;
            }
            
else if(command[0]=='B')
            {
//退后
                if (!backwardStack.empty())
                {
//退后栈中有历史记录
                    forwardStack.push(curURL);//当前页面进入前进栈
                    
//从退出栈中栈顶取出页面为当前页面
                    curURL = backwardStack.top();
                    cout
<<curURL<<endl;
                    backwardStack.pop();
                }
                
else
                {
                    cout
<<"Ignored"<<endl;
                }
            }
            
else if(command[0]=='V')
            {
//访问新URL
                backwardStack.push(curURL);//当前页面进入后退栈
                cin>>curURL;
                cout
<<curURL<<endl;
                ClearStack(forwardStack);
//情况前进栈
            }
            
else if(command[0]=='F')
            {
//前进
                if (!forwardStack.empty())
                {
//前进栈中有历史记录
                    backwardStack.push(curURL);//当前页面进入后退栈
                    
//从前进栈中栈顶取出页面为当前页面
                    curURL = forwardStack.top();
                    cout
<<curURL<<endl;
                    forwardStack.pop();
                }
                
else
                {
                    cout
<<"Ignored"<<endl;
                }
            }
        }
    }
    
return 0;
}

重构后的版本:


#include 
<iostream>
#include 
<stack>
#include 
<string>
using namespace std;

class WebNavigation
{
private:
    
string curURL;//当前访问页面
    string command;//当前待处理的命令
    stack<string> forwardStack,backwardStack;//前进栈,后退栈
    bool isFinished;//是否处理完毕

    
void ClearStack(stack<string>& s);//清空栈
    void Back();//退后
    void Forward();//向前
    void Visit();//访问新URL
public:
    WebNavigation();
    
~WebNavigation();
    
bool IsFinished()const
    {
//是否处理完毕
        return this->isFinished;
    }
    
void Process();//实际处理
    friend istream& operator>> (istream& in,WebNavigation& webNavi);   
};

WebNavigation::WebNavigation()
{
    
this->curURL = "http://www.acm.org/";//默认首页
    this->isFinished = false;
}
WebNavigation::
~WebNavigation()
{
//确保退出时清空使用的栈
    ClearStack(this->forwardStack);
    ClearStack(
this->backwardStack);
}
void WebNavigation::Back()
{
//退后
    if (!backwardStack.empty())
    {
//退后栈中有历史记录
        forwardStack.push(curURL);//当前页面进入前进栈
        
//从退出栈中栈顶取出页面为当前页面
        curURL = backwardStack.top();
        cout
<<curURL<<endl;
        backwardStack.pop();
    }
    
else
    {
        cout
<<"Ignored"<<endl;
    }
}
void WebNavigation::ClearStack(stack<string>& s)
{
//清空栈
    while (!s.empty())
    {
        s.pop();
    }
}
void WebNavigation::Forward()
{
//向前
    if (!forwardStack.empty())
    {
//前进栈中有历史记录
        backwardStack.push(curURL);//当前页面进入后退栈
        
//从前进栈中栈顶取出页面为当前页面
        curURL = forwardStack.top();
        cout
<<curURL<<endl;
        forwardStack.pop();
    }
    
else
    {
        cout
<<"Ignored"<<endl;
    }
}
void WebNavigation::Visit()
{
//访问新URL
    backwardStack.push(curURL);//当前页面进入后退栈
    cin>>curURL;
    cout
<<curURL<<endl;
    ClearStack(forwardStack);
//清空前进栈
}
void WebNavigation::Process()
{
    
if (command[0]=='Q')
    {
//退出
        this->isFinished = true;
    }
    
else if(command[0]=='B')
    {
//退后
        this->Back();
    }
    
else if(command[0]=='V')
    {
//访问新URL
        this->Visit();
    }
    
else if(command[0]=='F')
    {
//前进
        this->Forward();
    }
}
istream
& operator>> (istream& in,WebNavigation& webNavi)
{
//输入当前命令
    in>>webNavi.command;
    
return in;
}

int main(void)
{
    
int n,cases,i;
    cin
>>n;
    
for (cases=1;cases<=n;++cases)
    {
        WebNavigation webNavior;
        
if (cases!=1)
        {
            cout
<<endl;
        }
        
while(cin>>webNavior)
        {
//读入一行命令
            webNavior.Process();
            
if (webNavior.IsFinished())
            {
//已经处理完毕
                break;
            }
        }
    }
    
return 0;
}


 

posted on 2008-10-28 11:38  Phinecos(洞庭散人)  阅读(1263)  评论(0编辑  收藏  举报

导航