本文主要是介绍⭐201604-3 路径解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
则这道题一开始没有搞懂的就是相对路径要怎么处理
其实当前路径+相对路径就是绝对路径了!
然后如果/…/出现在一开始,就直接删掉。
如果/…/出现在中间,就把前面的上一个目录删掉即可
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
using namespace std;
//路径解析
string str;//当前目录
string tempstr;//临时的需要正规化的路径
int main() {int n;scanf("%d", &n);getchar();getline(cin, str);for (int i = 0; i < n; i++){getline(cin, tempstr);//getline可以输入空字符串 if (tempstr.length() == 0)//如果是空字符串,直接输出当前路径{ tempstr = str;}else{if (tempstr[0] == '/');//说明是绝对路径,无需操作elsetempstr = str + "/" + tempstr;//是相对路径,将相对路径变成绝对路径int fd=0;while ((fd = tempstr.find("/../")) != -1){if (fd == 0)tempstr.replace(fd, 4, "/");else{int fd2 = tempstr.rfind("/", fd - 1);tempstr.erase(fd2, fd - fd2 + 3);}}fd = 0;while ((fd = tempstr.find("/./")) != -1){tempstr.replace(fd, 3, "/");} fd = 0;while ((fd = tempstr.find("//")) != -1){tempstr.replace(fd, 2, "/");} fd = 0; if(tempstr.length() > 1&&tempstr[tempstr.length() - 1] == '/')//如果最后一个是/,那么删掉{ tempstr.erase(tempstr.length() - 1, 1);//删掉最后一个}}cout << tempstr << endl;}return 0;
}
这篇关于⭐201604-3 路径解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!