20181207QAQ的小游戏,VISIT,BACK等等的浏览网址问题

Description
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”.
Input
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.
Output
If the website changes, output the new website, if not, output Ignored.
Sample Input 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
Sample Output 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/

思路不难,直接上代码:

1,最开始的思路分析代码:

#include<stdio.h>
#include<string.h>
int main()
{
    char I[1000][100];
    char L[1000][100];
    int lenn[1000]={0};
    I[0][0]='V';

    strcpy(L[0],"http://www.acm.org/");     //这一行strcpy的代码就相当于下面注释掉的功能。
    /*  L[0][0]='h';
    L[0][1]='t';
    L[0][2]='t';
    L[0][3]='p';
    L[0][4]=':';
    L[0][5]='/';
    L[0][6]='/';
    L[0][7]='w';
    L[0][8]='w';
    L[0][9]='w';
    L[0][10]='.';
    L[0][11]='a';
    L[0][12]='c';
    L[0][13]='m';
    L[0][14]='.';
    L[0][15]='o';
    L[0][16]='r';
    L[0][17]='g';
    L[0][18]='/';   */

    lenn[0]=19;
    char a[100];
    int i=0;
    while(scanf("%s",a))
    {
        if(a[0]=='V')
        {
            i += 1;
            I[i][0]='V';
            char shuru[100];
            scanf("%s",shuru);
            lenn[i]=strlen(shuru);

            strcpy(L[i],shuru);
            puts(L[i]);
            /*for(int j=0;j<lenn[i];j++)   开始想的循环的方法,真是,麻烦啊
            {
                L[i][j]=shuru[j];
                printf("%c",L[i][j]);
            }*/

            //printf("\n");  puts方法输出完字符串后会自动换行。
        }
        else if(a[0]=='B')
        {
            i -= 1;
            if(i<0)
            {
                i=0;
                printf("Ignored\n");
                //break;这里不加break
            }
            else if(I[i][0]=='V')
            {
                /*for(int j=0;j<lenn[i];j++)
                {
                    printf("%c",L[i][j]);
                }
                 printf("\n");*/
                puts(L[i]);//puts方法输出完字符串后会自动换行。

            }

        }
        else if(a[0]=='F')
        {
            i += 1;
            if(I[i][0]=='V')
            {
                /*for(int j=0;j<lenn[i];j++)
                {
                    printf("%c",L[i][j]);
                }
                 printf("\n");*/
                puts(L[i]);//puts方法输出完字符串后会自动换行。

            }
            else
            {
                printf("Ignored\n");   //就是puts("Ignored");
                i -= 1;
            }
        }
        else
        break;
    }
    return 0;
}

2,整理后的代码:

#include<stdio.h>
#include<string.h>
int main()
{
    char I[1000][100];
    char L[1000][100];
    int lenn[1000]={0};
    I[0][0]='V';
    strcpy(L[0],"http://www.acm.org/");
    lenn[0]=19;
    char a[100];
    int i=0;
    while(scanf("%s",a))
    {
        if(a[0]=='V')
        {
            i += 1;
            I[i][0]='V';
            char shuru[100];
            scanf("%s",shuru);
            lenn[i]=strlen(shuru);
            strcpy(L[i],shuru);
          	puts(L[i]);
        }
        else if(a[0]=='B')
        {
            i -= 1;
            if(i<0)
            {
                i=0;
                puts("Ignored");
            }
            else if(I[i][0]=='V')
                puts(L[i]);
        }
        else if(a[0]=='F')
        {
            i += 1;
            if(I[i][0]=='V')
                puts(L[i]);
            else
            {
                puts("Ignored");
                i -= 1;
            }
        }
        else
        break;
    }
    return 0;
}

附上学姐的switch分支代码:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值