用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; } ######[AI写代码神器 | 1505点数解答 | 2024-12-01 11:40:15]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- Java 实现仿 Windows 资源管理器:文件管理器功能全解析与代码示例(字节豆包 | 623点数解答 | 2025-01-01 14:59:04)247
- Java 创建 HTTP POST 请求实现文件上传:代码示例与注意要点 (字节豆包 | 300点数解答 | 2024-12-06 16:07:20)337
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)310
- Linux开发必备:物理机BIOS、型号、网卡驱动及虚拟机判断查询攻略(百度文心 | 328点数解答 | 2023-11-09 18:48:49)259
- 一招搞定!查询 Oracle 用户权限的 SQL 语句揭秘( | 129点数解答 | 2023-12-27 17:41:26)264
- Linux 系统组管理实战:创建、修改、添加用户与删除组操作全解析 (字节豆包 | 398点数解答 | 2024-10-18 09:14:34)216
- Linux系统:全面实操组管理与用户管理任务(阿里通义 | 634点数解答 | 2024-10-18 09:17:56)240
- Dev C++ 实现超多功能 C 语言资源管理器:创建、查看、管理文件一步到位! (字节豆包 | 1142点数解答 | 2024-12-01 11:36:27)354
- 用Dev C++实现C语言资源管理器:支持创建、查看、预览等功能!(GPT | 1505点数解答 | 2024-12-01 11:40:15)173
- 用 Dev C++ 实现 C 语言资源管理器:含代码,可创建、管理文件与文件夹 (GPT | 2709点数解答 | 2024-12-01 11:41:45)216
- Python三关挑战:文件与文件夹数量统计及路径获取(字节豆包 | 493点数解答 | 2024-12-11 13:29:49)220