北邮oj--文件系统

 题不算难,但会出一些莫名其妙的错误,可能还是不扎实吧。

对于string和char*的转换:char*—>string:char buf[1000];string s(buf);

string—>char*:s.c_str();

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int TFILE=1;
const int TDIR=2;
struct Node{
	Node* next;
	string name;
	int type;
	Node(string s,int t){
		name=s;
		next=NULL;
		type=t;
	}
};
vector<Node*> tree;
void insertNode(Node* N,string s,int type){
	if(N->next==NULL){
		N->next=new Node(s,type);
	}else{
		insertNode(N->next,s,type);
	}
}
void createFile(string name,string dir){
	for(int i=0;i<tree.size();i++){
		if(tree[i]->name==dir){
			insertNode(tree[i],name,TFILE);
		}
	}
}
void createDir(string name,string dir){
	for(int i=0;i<tree.size();i++){
		if(tree[i]->name==dir){
			insertNode(tree[i],name,TDIR);
		}
	}
	tree.push_back(new Node(name,TDIR));
}
void listFD(string dir,int type){
	for(int i=0;i<tree.size();i++){
		if(tree[i]->name==dir){
			Node* temp=tree[i]->next;
			while(temp!=NULL){
				if(temp->type==type){
					printf("%s\n",temp->name.c_str());
				}
				temp=temp->next;
			}
			break;
		}
	}
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		Node* root=new Node("root",TDIR);
		tree.push_back(root);
		int N;
		scanf("%d",&N);
		while(N--){
			char buf1[1000];
			char buf2[1000];
			char buf3[1000];
			scanf("%s",buf1);
			string s1(buf1);
			if(s1=="CREATEFILE"){
				scanf("%s%s",buf2,buf3);
				string s2(buf2);
				string s3(buf3);
				createFile(s2,s3);
			}else if(s1=="CREATEDIR"){
				scanf("%s%s",buf2,buf3);
				string s2(buf2);
				string s3(buf3);
				createDir(s2,s3);
			}else if(s1=="LISTDIR"){
				scanf("%s",buf2);
				string s2(buf2);
				listFD(s2,TDIR);
			}else if(s1=="LISTFILE"){
				scanf("%s",buf2);
				string s2(buf2);
				listFD(s2,TFILE);
			}
		}
		tree.clear();
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值