QAQ的小游戏

描述
Recently,QAQ fell in love a small game,which simulates browser browsing web pages.
It has three kind of operations:
1.BACK: Back to the previous page
2.FORWARD:Go to the next page
3.VISIT URL:Access to the web page
Now,QAQ has visited http://www.acm.org/, and wants to know the website after each operation,
if the page not change, output “Ignored”.

输入
There is only one test data, and the number of operations does not exceed 1000,
end flag when input “QUIT”
Attion: every website length no more then 100.

输出
If the website changes, output the new website, if not, output Ignored.

输入样例 1
VISIT http://oj.51ac.club/
VISIT http://maojunjie666.top/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

输出样例 1
http://oj.51ac.club/
http://maojunjie666.top/
http://oj.51ac.club/
http://www.acm.org/
Ignored
http://oj.51ac.club/
http://www.ibm.com/
http://oj.51ac.club/
http://www.acm.org/
http://oj.51ac.club/
http://www.ibm.com/
Ignored

这题看着挺水的,但还是要注意要用两个变量控制下标。都是为了防止越界,一个是为了防止下标小于0,另一个是为了防止下标超出最大长度。

下面是我的代码:

#include<stdio.h>
#include<string.h>
int main()
{
    char l[1005][105],m[10],a;
    int index=0,all=0;
    while(scanf("%s",m)!=EOF){
        if(strcmp(m,"QUIT")==0) break;//用strcmp函数判断每一行第一个单词
        else if(strcmp(m,"VISIT")==0){
            index+=1;
            all=index;
            scanf("%s",l[index]);
            printf("%s\n",l[index]);
        }
        else if(strcmp(m,"BACK")==0){
            if(index==1){
                index-=1;
                printf("http://www.acm.org/\n");
            }
            else if(index==0) printf("Ignored\n");//如果index是0就代表已经访问过最初的网站,即输出Ignored
            else{
                index-=1;
                printf("%s\n",l[index]);
            }
        }
        else if(strcmp(m,"FORWARD")==0){
                index+=1;
            if(index>all){//如果下标越界,就带表已经访问过此网站
                index=all;
                printf("Ignored\n");}
            else{
                printf("%s\n",l[index]);}
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42891420/article/details/84940432