zoukankan      html  css  js  c++  java
  • 201604-3 路径解析

    实现

    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    
    using namespace std;
    
    void handle_path(vector<string>& paths, string path) {
        stringstream ss(path);
        string dir;
        bool first_dir = true;
        while (getline(ss, dir, '/')) {
            if (dir.empty()) {
                if (first_dir) {
                    paths = vector<string> ();
                }        
            } else if (dir == ".."){
                if (!paths.empty()) {
                    paths.pop_back();
                }
            } else if (dir == ".") {
                // pass
            } else {
                paths.push_back(dir);
            }
            first_dir = false;
        }
        
    }
    
    void print_formalized_path(vector<string> paths) {
        if (paths.empty()) {
            cout << "/" << endl;
            return;
        }
    
        for (vector<string>::iterator ite = paths.begin();
            ite!=paths.end();++ite) {
            cout << "/" << *ite;
        }
        cout << endl;
    }
    
    int main() {
        int num;
        cin >> num;
        cin.ignore();
    
        string cur_path;
        getline(cin,cur_path);
    
        vector<string> cur_paths, paths;
        handle_path(cur_paths, cur_path);
    
        for (int i = 0;i < num;++i) {
            string path;
            getline(cin,path);
            paths = cur_paths;
            handle_path(paths, path);
            print_formalized_path(paths);
        }
    }
    

    参考

    Pretty9

  • 相关阅读:
    一些前台技巧
    javascript中的面向对象
    vs.net 常用快捷键
    js和C#中的进制转换方法
    MFC中CDC相关图形,文本的一些使用方法(转)
    poj1505
    poj1401
    poj2533
    poj1504
    poj1384
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13586450.html
Copyright © 2011-2022 走看看