|
发表于 2024-12-19 12:55:59
|
显示全部楼层
本楼为最佳答案
 - #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define PAGE_SIZE 10 // 每页占用 10 个字符
- #define MEMORY_SIZE 5 // 内存页最大数量
- #define MAX_FILES 100 // 最大文件数量
- typedef struct {
- char filename[50]; // 文件名
- char creator[50]; // 创建者
- int size; // 文件大小(字符数)
- char *content; // 文件内容
- } File;
- typedef struct {
- File *files[MAX_FILES]; // 文件表
- int file_count; // 当前文件数量
- } FileSystem;
- typedef struct {
- char memory[MEMORY_SIZE][PAGE_SIZE + 1]; // 模拟内存页(+1 用于存储 '\0')
- int page_usage[MEMORY_SIZE]; // 每页是否被占用(1:占用,0:空闲)
- } Memory;
- typedef struct {
- char external_storage[MAX_FILES][PAGE_SIZE + 1]; // 外存模拟
- int storage_usage[MAX_FILES]; // 外存使用状态
- } ExternalStorage;
- // 全局变量
- FileSystem fs; // 文件系统
- Memory memory; // 模拟内存
- ExternalStorage ext; // 模拟外存
- // 初始化文件系统
- void init_file_system() {
- fs.file_count = 0;
- for (int i = 0; i < MEMORY_SIZE; i++) memory.page_usage[i] = 0;
- for (int i = 0; i < MAX_FILES; i++) ext.storage_usage[i] = 0;
- }
- // 创建文件
- void create_file(const char *filename, const char *creator, const char *content) {
- if (fs.file_count >= MAX_FILES) {
- printf("文件系统已满,无法创建更多文件。\n");
- return;
- }
- File *new_file = (File *)malloc(sizeof(File));
- strcpy(new_file->filename, filename);
- strcpy(new_file->creator, creator);
- new_file->size = strlen(content);
- new_file->content = strdup(content);
- fs.files[fs.file_count++] = new_file;
- printf("文件 '%s' 已创建,大小为 %d 字节。\n", filename, new_file->size);
- }
- // 内存换入
- int load_to_memory(const char *content, int start_page) {
- int pages_needed = (strlen(content) + PAGE_SIZE - 1) / PAGE_SIZE; // 需要的页数
- int pages_loaded = 0;
- for (int i = start_page; i < MEMORY_SIZE && pages_loaded < pages_needed; i++) {
- if (memory.page_usage[i] == 0) {
- strncpy(memory.memory[i], content + pages_loaded * PAGE_SIZE, PAGE_SIZE);
- memory.memory[i][PAGE_SIZE] = '\0'; // 确保字符串以 '\0' 结尾
- memory.page_usage[i] = 1;
- pages_loaded++;
- }
- }
- return pages_loaded == pages_needed ? 1 : 0; // 如果成功加载返回 1,否则 0
- }
- // 内存换出
- void unload_to_external_storage(int page, int ext_index) {
- if (ext.storage_usage[ext_index] == 0) {
- strcpy(ext.external_storage[ext_index], memory.memory);
- memory.page_usage= 0; // 清空内存页
- ext.storage_usage[ext_index] = 1;
- printf("内存页 %d 已换出到外存位置 %d。\n", page, ext_index);
- } else {
- printf("外存位置 %d 已被占用。\n", ext_index);
- }
- }
- // 显示文件内容
- void display_file(const char *filename) {
- for (int i = 0; i < fs.file_count; i++) {
- if (strcmp(fs.files[i]->filename, filename) == 0) {
- printf("文件名: %s\n创建者: %s\n大小: %d 字节\n内容:\n%s\n",
- fs.files[i]->filename, fs.files[i]->creator, fs.files[i]->size, fs.files[i]->content);
- return;
- }
- }
- printf("文件 '%s' 未找到。\n", filename);
- }
- // 删除文件
- void delete_file(const char *filename) {
- for (int i = 0; i < fs.file_count; i++) {
- if (strcmp(fs.files[i]->filename, filename) == 0) {
- free(fs.files[i]->content);
- free(fs.files[i]);
- fs.files[i] = fs.files[--fs.file_count]; // 用最后一个文件代替删除的文件
- printf("文件 '%s' 已删除。\n", filename);
- return;
- }
- }
- printf("文件 '%s' 未找到。\n", filename);
- }
- // 显示外存内容
- void display_external_storage() {
- printf("外存内容:\n");
- for (int i = 0; i < MAX_FILES; i++) {
- if (ext.storage_usage[i]) {
- printf("位置 %d: %s\n", i, ext.external_storage[i]);
- }
- }
- }
- int main() {
- init_file_system();
- // 接收用户输入
- create_file("test.txt", "user1", "This is a test file content.");
- create_file("example.txt", "user2", "Example file content for testing.");
- // 文件操作
- display_file("test.txt");
- // 模拟内存加载
- if (!load_to_memory("This is a test file content.", 0)) {
- printf("内存不足,无法加载文件。\n");
- }
- // 模拟内存换出
- unload_to_external_storage(0, 0);
- // 显示外存内容
- display_external_storage();
- // 删除文件
- delete_file("test.txt");
- return 0;
- }
复制代码 |
|