代码仓库-路径显示工具

说明

协助CMake学习

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
struct node
  {
    string name;
  	vector<shared_ptr<node>> son;
  	node(string name,vector<shared_ptr<node>> son = {})
  	  :name(name),son(son)
  	    {}
  	shared_ptr<node> insert(string name)
  	  {
  	  	auto son_node = make_shared<node>(name);
  	  	son.push_back(son_node);
  	  	return son_node;
		}
  };
bool cmp(shared_ptr<node> const& a,shared_ptr<node> const& b)
  {
  	return a -> name < b -> name;
  }
void show(shared_ptr<node> now,ll depth = 0)
  {
  	if (now == 0)
  	  return;
  	else
  	  {
  	  	cout << string(depth * 3,' ') << "+" << string(3,'-') << " " << now -> name << "\n";
  	  	if (now -> son.size())
  	  	  {
  	  	  	sort(now -> son.begin(),now -> son.end(),cmp);
  	  	  	cout << string((depth + 1) * 3,' ') << "|" << "\n";
			for (auto it : now -> son)
  	  	  	  show(it,depth + 1);
			  }
  	  	
		}
  }
void clear_screen()
  {
  	system("cls");
  }
string r()
  {
  	string ret;
  	cin >> ret;
  	return ret;
  }
void build_tree(shared_ptr<node> & root,shared_ptr<node> & tree_root,string path)
  {
  	clear_screen();
  	cout << path << "\n";
  	show(tree_root);
  	string commond = r();
  	if (commond == "exit" or commond == "quit")
  	  {
  	  	clear_screen();
  	  	throw int(0);
		}
	else if (commond == "up")
	  {
	  	return;
	  }
	else if (commond == "down")
	  {
	  	auto &v = root -> son;
	  	for (ll i = 0;i < v.size();i++)
	  	  cout << i << "." << v[i] -> name << "\n";
	  	ll son_id = stoll(r());
	  	build_tree(v[son_id],tree_root,path + "/" + v[son_id] -> name);
	  	build_tree(root,tree_root,path);
	  }
	else if (commond == "change")
	  {
	  	root -> name = r();
	  	build_tree(root,tree_root,path);
	  }
	else
	  {
	  	root -> insert(commond);
	  	build_tree(root,tree_root,path);
	  }
  }
void input(shared_ptr<node> & root,shared_ptr<node> & tree_root)
  {
  	cout << "input demo name : ";
  	root = make_shared<node>(r());
  	try
  	  {
  	  	build_tree(root,tree_root,"/" + root -> name);
		}
    catch(...)
      {
      	
	  }
  }
int main()
  {
  	shared_ptr<node> root;
  	input(root,root);
  	show(root);
  	
	return 0;
  }
posted @ 2021-10-01 00:21  XDU18清欢  阅读(47)  评论(0)    收藏  举报