简化命令
cd创建文件夹
cf创建文件
st显示文件树
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//解决文件加入a->child,实现子文件
//解决文件打印竖线 0开始,%5=1打印,5倒数 +=5
//typedef struct paper{
// char name[50];
// int isfile;
// paper* next;
//}paper;
//
//
//typedef struct dictionary{
// char name[50];
// int isfile;
node* parent;
// dictionary* child;
// paper* pa;
//};
//由于一个节点下面,既要有文件,也要有文件夹,所以file->child既要能指向文件类型,也要指向文件夹类型。所以只能是这里同一个节点类型,因为兼容类型还不会写。
//同一个节点类型,但是区分不同使用,要在节点里加上标志位flag ——is_dictionary
int lenth = 10;
//文件类型后缀统一
typedef struct node {
char name[100];
int is_dictionary;
node* child;
node* parent;
node* bro;
} node;
node *root;
void init() {
root = (node*)malloc(sizeof(node));
strcpy(root->name, "root");
root->parent = NULL;
root->child = NULL;
root->bro = NULL;
root->is_dictionary = 1;
}
void addbro(node* parent, node* a) {
if (parent->bro == NULL) {
parent->bro = a;
} else {
node* bro = parent->bro;
while (bro->bro != NULL) {
bro = bro->bro;
}
bro->bro = a;
}
}
//void addChild(node* parent, node* a)
//跳转前的内存位置,取得改内存里跳转后的新内存地址
//void addChild(node* &parent, node* a)
//不进行跳转获取原位置,因为节点永远存在不为空
void addChild(node* parent, node* a) {
if (parent->child == NULL) {
parent->child = a;
} else {
node* bro = parent->child;
while (bro->bro != NULL) {
bro = bro->bro;
}
// bro->bro = NULL;
bro->bro = a;
}
}
void addFile(node* parent, char a[]) {
node* p = (node*)malloc(sizeof(node));
strcpy(p->name, a);
p->is_dictionary = 0;
p->bro = NULL;
p->child = NULL;
p->parent = NULL;
addbro(parent, p);
}
void addDictionary(node* parent, char a[]) {
node* p = (node*)malloc(sizeof(node));
strcpy(p->name, a);
p->is_dictionary = 1;
p->bro = NULL;
p->child = NULL;
p->parent = NULL;
// addbro(parent, p);
printf("add function is running\n");
addChild(parent, p);
printf("success creat dictionary\n");
}
//缩进输出文件类型名,只能减少,不能增加,增加就会都增加,减少可以选择性减少
//增加就会先算出最深的,然后算出距离差,然后再加上,不如一遍算缩进,还剩下多少,直接打印没有被占用的
//void format(node *a, int tab, int see) {
// printf("%-18s", a->name);
// for (int i = 0; i < see - tab; i++) {
// printf(" ");
// }
// printf("%-9s", "dictionary");
//
//}
//先就着基本有的直接敲上,然后打印递归的时候发现传进指针,一开始的root变量就要改成指针
void show(node* list, int floor, int head) {
node *a = list;
int tab = floor;
// printf("%d\n", floor);
while (a != NULL) {
for (int i = 0; i < head + 20; i++) {
printf("^");
}
floor = tab;
// 去除一开始的缩进,由之前的addbro改成addchild,文件夹的child不为NULL,->child才能进入,这样才能打开文件夹
while (floor > 0) {
// printf("%d\n", floor);
// printf("|");
if (floor % 5 == 1) {
// 从0开始,倒数到1
printf("|");
// 每下一层就是floor+5,对应一个竖,
} else {
printf(" ");
}
floor--;
}
if (a->is_dictionary) {
printf("%-18s", a->name);
for (int i = 0; i < lenth - tab; i++) {
printf(" ");
}
printf("%-9s\n", "dictionary");
// printf("-%-18s %-3s\n", a->name, "dictonary");
// 左对齐缩进
show(a->child, tab + 5, head);
} else {
// format(a->name,tab,lenth);
printf("%-18s", a->name);
for (int i = 0; i < lenth - tab; i++) {
printf(" ");
}
printf("%-9s\n", "<file>");
// printf("-%-7s %-s\n", a->name, "<file>");
}
a = a->bro;
}
}
//输出,查找,标记,替换,增加 族
//文件夹读取
//文件所有子项目读取
//文件写入.txt
//配合链表,实现代码命令创建
//选择文件传输
//在结合服务器实现云创建文件传送
//原版查找增加,现用于增加文件
void findandadd(node* list, int floor, char name[], char have[]) {
node *a = list;
// int tab = floor;
while (a != NULL) {
// floor = tab;
// while (floor) {
// printf(" ");
// floor--;
// }
if (a->is_dictionary) {
// printf("-%s\n", a->name);
if (strcmp(a->name, name) == 0) {
printf("FIND name\n");
addFile(a, have);
return ;
}
findandadd(a->child, 0, name, have);
// show(a->child, floor + 1);
} else {
// printf("|-%s\n", a->name);
if (strcmp(a->name, name) == 0) {
printf("FIND name\n");
addFile(a, have);
return ;
}
}
a = a->bro;
}
// printf("find error\n");
//需要控制只要查到,递归就不再做其他运算,直接退出,加结构体
}
//优化版本-增加文件夹
void findadd_dir(node* list, int floor, char name[], char have[]) {
node *a = list;
// int tab = floor;
while (a != NULL) {
// 之前版本注释掉了导致循环超时
// floor = tab;
// while (floor) {
// printf(" ");
// floor--;
// }
if (a->is_dictionary) {
printf("-checking dictionary-%s\n", a->name);
if (strcmp(a->name, name) == 0) {
printf("FIND name\n");
addDictionary(a, have);
return ;
}
findadd_dir(a->child, 0, name, have);
// findadd_dir(a->bro, 0, name, have);
// show(a->child, floor + 1);
} else {
// printf("|-%s\n", a->name);
// if (strcmp(a->name, name) == 0) {
// printf("FIND name\n");
// addDictionary(a, have);
// return ;
// }
return ;
}
a = a->bro;
}
}
int main() {
init();
char a[100] = "n";
char b[100] = "nwoevi";
addDictionary(root, a);
printf("dictionary ok!\n");
addFile(root, b);
printf("File b ok!\n");
char c[100] = "3894";
addFile(root, c);
printf("File 3849 ok!\n");
char d[100] = "floor2";
// findandadd(root, 0, a, d);
addDictionary(root, d);
char k[100] = "vnsn";
findadd_dir(root, 0, a, k);
char f[100] = "ij";
findandadd(root, 0, k, f);
char ch[100] = {};
// show(root, 0, 10);
//0导致一开始就先打印|,得最后打印|倒数到1
// 5不行,从4开始
// show(root, 4, 10);
show(root, 0, 10);
printf("input order\n");
while (1) {
// gets(ch);
scanf("%s", ch);
if (strcmp("close", ch) == 0) {
break;
} else if (strcmp("cf", ch) == 0) {
printf("Input file name:\n");
// gets(ch);
// gets()会把函数printf()打印出的回车吸收掉,所以用scanf,不受缓冲区影响
scanf("%s", ch);
printf("Input parent contest name:\n");
char tar[100];
// gets(tar);
scanf("%s", tar);
findandadd(root, 0, tar, ch);
printf("success creat file\n");
} else if (strcmp("cd", ch) == 0) {
printf("Input dictionary name:\n");
scanf("%s", ch);
char tar[100];
printf("Input parent contest name:\n");
// gets(tar);
scanf("%s", tar);
findadd_dir(root, 0, tar, ch);
// findadd_dir(root,0,"n",ch);
// printf("success creat dictionary\n"); 内置在子函数里了,解决查找失败仍提示创建成功问题
} else if (strcmp("st", ch) == 0) {
show(root, 0, 10);
// 无缩进
// show(root, 5, 10);
// 缩进前有|
// show(root, 4, 10);
// show(root, -1, 10);
printf("\ntree end\n");
} else {
printf("commond not found\n");
continue;
}
}
findandadd(root, 0, "n", "newnode");
show(root, 0, 30);
return 0;
}