-
Notifications
You must be signed in to change notification settings - Fork 0
/
operaterr.cpp
69 lines (55 loc) · 1.16 KB
/
operaterr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// operaterr.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
cout << item<<endl;
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
string simplifyPath(string path) {
string result;
vector<string> elems = split(path, '/');
int ignor = 0;
for (int i = elems.size() - 1; i >= 0; i--) {
if (elems[i] == "" || elems[i] == ".") {
continue;
}
if (elems[i] == "..") {
ignor++;
continue;
}
if (ignor > 0) {
ignor--;
continue;
}
if (result.size() == 0) {
result = "/" + elems[i];
}
else {
result = "/" + elems[i] + result;
}
}
return result.size() ? result : "/";
}
int main(int argc, char** argv)
{
string path("/a/./b/../../c/");
if (argc > 1) {
path = argv[1];
}
cout << path << " : " << simplifyPath(path) << endl;
}