[Codeforces] 158C - Cd and pwd commands

题意:模拟cmd或终端中的几种目录操作命令。

易错的地方:

1、cd /a/b/c/d/../e 输出的目录是 /a/b/c/e/

2、假设当前目录在/a/b/下,此时输入 cd ../c/d 输出的目录是 /a/c/d/

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int main()
{
    #ifdef _1Test
        freopen("test0.in", "r", stdin);
        freopen("test0.out", "w", stdout);
        srand(time(NULL));
    #endif
    int n, cnt;
    char ch[1000][201], tmp[205], com[205];
    while(~scanf("%d", &n))
    {
        cnt = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%s", com);
            if(!strcmp(com, "pwd"))
            {
                printf("/");
                for(int i = 0; i < cnt; i++)
                {
                    printf("%s/", ch[i]);
                }
                puts("");
            }
            else
            {
                scanf("%s", com);
                int len = strlen(com);
                if(!strcmp(com, ".."))
                {
                    --cnt;
                }
                else if(com[0] == '/')
                {
                    cnt = 0;
                    int s = 0;
                    for(int i = 1; i < len; i++)
                    {
                        ch[cnt][s++] = com[i];
                        if(com[i+1] == '/' || i == len - 1)
                        {
                            ch[cnt][s] = '\0';
                            s = 0;
                            strcmp(ch[cnt], "..") ? cnt++ : cnt--;
                            i++;
                        }
                    }
                }
                else
                {
                    int s = 0;
                    for(int i = 0; i < len; i++)
                    {
                        ch[cnt][s++] = com[i];
                        if(com[i+1] == '/' || i == len - 1)
                        {
                            ch[cnt][s] = '\0';
                            s = 0;
                            strcmp(ch[cnt], "..") ? cnt++ : cnt--;
                            i++;
                        }
                    }
                }
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值