codeforces C. Cd and pwd commands 执行命令行

执行命令来改变路径 cd

并显示路径命令 pwd

一个节目的

抽样:

input
7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd
output
/
/home/vasya/
/home/
/home/petya/
原题:

http://codeforces.com/problemset/problem/158/C


考点:

操作字符串 -- 使用C++特别难的,好像java和C#都特别easy。只是使用C++能够清楚每个细节的操作

这里直接使用string容器来实现了

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

void Cdandpwdcommands()
{
	int n;
	cin>>n;
	string path(1, '/');
	string command;
	while (n--)
	{
		cin>>command;
		if ("cd" == command)
		{
			cin>>command;
			if (command[0] == '/')//还要注意绝对路径
			{
				path = "/";
			}
			int i = 0;
			if (command[0] == '/') i++;
			while (i < (int)command.size())
			{
				for (; i < (int)command.size() && '.' != command[i]; i++)
					path.push_back(command[i]);

				if (i+1 < (int)command.size() && command[i+1] == '.')
				{
					if (path.size() > 1 && path.back() == '/') 
						path.pop_back();

					while (path.size() && path.back() != '/')
						path.pop_back();

					i += 2;
					if (i < (int)command.size() && command[i] == '/')
						i++;
				}
				else if (i < (int)command.size())
				{
					path.push_back(command[i++]);
				}
			}
			if (path.empty() || path.back() != '/') path.push_back('/');
		}
		else if ("pwd" == command)
		{
			cout<<path<<endl;
		}
	}
}



版权声明:笔者靖心脏,景空间地址:http://blog.csdn.net/kenden23/。只有经过作者同意转载。

转载于:https://www.cnblogs.com/hrhguanli/p/4865145.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值