当然,请见下方给出的完整代码:
cCopy code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILES 100
#define MAX_NAME_LENGTH 50
#define MAX_CONTENT_LENGTH 100
struct File {
char name[MAX_NAME_LENGTH];
char content[MAX_CONTENT_LENGTH];
};
struct Folder {
struct File files[MAX_FILES];
int file_count;
};
void print_menu() {
printf("=== 文件管理系统 ===\n");
printf("1. 文件查找\n");
printf("2. 文件读取\n");
printf("3. 文件写入\n");
printf("4. 文件夹创建\n");
printf("5. 文件夹删除\n");
printf("6. 文件移动\n");
printf("7. 文件删除\n");
printf("8. 退出\n");
printf("==================\n");
}
void search_file(struct Folder folder, char* file_name) {
int i;
for (i = 0; i < folder.file_count; i++) {
if (strcmp(folder.files[i].name, file_name) == 0) {
printf("文件找到!\n");
printf("文件名: %s\n", folder.files[i].name);
printf("内容: %s\n", folder.files[i].content);
return;
}
}
printf("文件未找到。\n");
}
void read_file(struct Folder folder, char* file_name) {
int i;
for (i = 0; i < folder.file_count; i++) {
if (strcmp(folder.files[i].name, file_name) == 0) {
printf("文件内容: %s\n", folder.files[i].content);
return;
}
}
printf("文件未找到。\n");
}
void write_file(struct Folder* folder, char* file_name, char* content) {
if (folder->file_count >= MAX_FILES) {
printf("文件数量已达上限,无法写入新文件。\n");
return;
}
struct File new_file;
strcpy(new_file.name, file_name);
strcpy(new_file.content, content);
folder->files[folder->file_count] = new_file;
folder->file_count++;
printf("文件写入成功。\n");
}
void create_folder(struct Folder* folder) {
folder->file_count = 0;
printf("文件夹创建成功。\n");
}
void delete_folder(struct Folder* folder) {
folder->file_count = 0;
printf("文件夹删除成功。\n");
}
void move_file(struct Folder* source_folder, struct Folder* target_folder, char* file_name) {
int i;
for (i = 0; i < source_folder->file_count; i++) {
if (strcmp(source_folder->files[i].name, file_name) == 0) {
target_folder->files[target_folder->file_count] = source_folder->files[i];
target_folder->file_count++;
source_folder->file_count--;
printf("文件移动成功。\n");
return;
}
}
printf("文件未找到,移动失败。\n");
}
void delete_file(struct Folder* folder, char* file_name) {
int i;
for (i = 0; i < folder->file_count; i++) {
if (strcmp(folder->files[i].name, file_name) == 0) {
int j;
for (j = i; j < folder->file_count - 1; j++) {
folder->files[j] = folder->files[j + 1];
}
folder->file_count--;
printf("文件删除成功。\n");
return;
}
}
printf("文件未找到,删除失败。\n");
}
int main() {
struct Folder folder;
int choice;
char file_name[MAX_NAME_LENGTH];
char content[MAX_CONTENT_LENGTH];
create_folder(&folder);
do {
print_menu();
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入要查找的文件名:");
scanf("%s", file_name);
search_file(folder, file_name);
break;
case 2:
printf("请输入要读取的文件名:");
scanf("%s", file_name);
read_file(folder, file_name);
break;
case 3:
printf("请输入要写入的文件名:");
scanf("%s", file_name);
printf("请输入要写入的内容:");
scanf("%s", content);
write_file(&folder, file_name, content);
break;
case 4:
create_folder(&folder);
break;
case 5:
delete_folder(&folder);
break;
case 6:
printf("请输入要移动的文件名:");
scanf("%s", file_name);
move_file(&folder, &folder, file_name); // 移动到同一文件夹
break;
case 7:
printf("请输入要删除的文件名:");
scanf("%s", file_name);
delete_file(&folder, file_name);
break;
case 8:
printf("退出文件管理系统。\n");
break;
default:
printf("无效的选项,请重新输入。\n");
break;
}
printf("\n");
} while (choice != 8);
return 0;
}
您可以将上述代码保存到一个名为fruit_problem.c
的文件中,然后使用以下命令进行编译和运行:
gcc -o fruit_problem fruit_problem.c -lpthread
./fruit_problem
bashCopy code
gcc -o fruit_problem fruit_problem.c -lpthread ./fruit_problem
这样就可以执行模拟水果问题的实验了。