酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

用dev c++c语言数据结构设计并实现一个资源管理器使其至少能够实现创建,查看,管理文件和文件夹,快速预览文件和文件夹,复制删除移动文件等功能使其可以实现用户交互可以直接编译运行(适当利用树状文件系统结构)

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 50 #define MAX_CHILDREN 100 typedef struct Node { char name[MAX_NAME_LEN]; struct Node *children[MAX_CHILDREN]; int num_children; } Node; Node *root = NULL; Node* create_node(char *name) { Node *new_node = (Node*)malloc(sizeof(Node)); strcpy(new_node->name, name); new_node->num_children = 0; for (int i = 0; i < MAX_CHILDREN; i++) { new_node->children[i] = NULL; } return new_node; } Node* find_child(Node *parent, char *name) { for (int i = 0; i < parent->num_children; i++) { if (strcmp(parent->children[i]->name, name) == 0) { return parent->children[i]; } } return NULL; } void create_file(char *path) { char *token = strtok(path, "/"); Node *current = root; while (token != NULL) { Node *child = find_child(current, token); if (child == NULL) { child = create_node(token); current->children[current->num_children++] = child; } current = child; token = strtok(NULL, "/"); } } void print_node(Node *node, int level) { for (int i = 0; i < level; i++) { printf("\t"); } printf("%s\n", node->name); for (int i = 0; i < node->num_children; i++) { print_node(node->children[i], level + 1); } } void preview_files() { if (root == NULL) { printf("No files or directories created yet.\n"); } else { printf("Files and directories:\n"); print_node(root, 0); } } int main() { root = create_node("root"); char input[100]; char command[10]; char path[50]; while (1) { printf("Enter command: "); fgets(input, sizeof(input), stdin); sscanf(input, "%s %s", command, path); if (strcmp(command, "create") == 0) { create_file(path); printf("File created successfully.\n"); } else if (strcmp(command, "preview") == 0) { preview_files(); } else if (strcmp(command, "exit") == 0) { break; } else { printf("Invalid command. Please try again.\n"); } } return 0; } [2024-12-01 11:40:15 | AI写代码神器 | 1505点数解答]

相关提问