执行命令来改变路径 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/。只有经过作者同意转载。