题意:现对浏览器进行4种操作:后退、前进、转到某一网址、退出。要求对退出以外的每种操作,输出该操作执行后浏览器当前网站的网址。其中定义浏览器首页为http://www.acm.org/。
思路:用字符串的一维数组即可解决。但是注意每拜访一个网站,此后的网站就作废了,此网站就是最后一个,即在后退与前进操作中时刻改变最后一个网站的下标。在输入网站的操作中,用一个变量标记输入网站的下标,以便在前进中判断,即代码中的m。其他都挺容易了,不废话了……
代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a[120],b;
int i=1,n=0,m;
a[0]="http://www.acm.org/";
while(cin>>b&&b!="QUIT")
{
if(b=="VISIT")
{
cin>>a[i];
n=m=i;
i++;
cout<<a[n]<<endl;
}
else if(b=="BACK")
{
if(n-1>=0)
{
n--;
i--;
cout<<a[n]<<endl;
}
else cout<<"Ignored"<<endl;
}
else
{
if(n+1<=m)
{
n++;
i++;
cout<<a[n]<<endl;
}
else cout<<"Ignored"<<endl;
}
}
return 0;
}