要求用c语言实现一个笔记管理系统。要求实现以下功能: 打开笔记、关闭笔记、保存笔记、使用说明等内容; 支持笔记创建,允许用户自主创建笔记以及命名; 支持笔记编辑,允许用户对笔记内容进行编辑,文本输入; 支持笔记删除,允许用户删除不需要的笔记; 支持笔记分类,自动分类或允许用户对自己的笔记进行自 定义分类管理; 支持笔记查询,允许用户通过搜索功能查找所需的笔记; 支持笔记图文输入编辑; 支持数据分析,提供用户笔记记录频率分析、笔记使用习惯分析等内容; 要求使用多文件程序和工程管理、链表应用、文件的应用
时间: 2023-10-27 09:04:31 浏览: 246
以下是一个简单的笔记管理系统的示例程序,实现了打开笔记、关闭笔记、保存笔记、使用说明、笔记创建、笔记编辑、笔记删除、笔记分类、笔记查询等功能。使用了多文件程序和工程管理、链表应用、文件的应用等技术。
笔记管理系统的主函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "note.h"
int main() {
NoteList *noteList = createNoteList(); // 创建笔记列表
while (1) {
printMenu(); // 打印菜单
int choice = getChoice(); // 获取用户选择
switch (choice) {
case 1:
openNote(noteList); // 打开笔记
break;
case 2:
closeNote(noteList); // 关闭笔记
break;
case 3:
saveNote(noteList); // 保存笔记
break;
case 4:
printUsage(); // 使用说明
break;
case 5:
createNote(noteList); // 创建笔记
break;
case 6:
editNote(noteList); // 编辑笔记
break;
case 7:
deleteNote(noteList); // 删除笔记
break;
case 8:
classifyNote(noteList); // 分类笔记
break;
case 9:
searchNote(noteList); // 查询笔记
break;
case 10:
analyzeData(noteList); // 数据分析
break;
case 11:
destroyNoteList(noteList); // 销毁笔记列表
return 0; // 退出程序
default:
printf("Invalid choice!\n");
}
}
}
```
笔记管理系统的函数实现:
```c
// note.h
#ifndef NOTE_H
#define NOTE_H
#include <stdio.h>
typedef struct Note {
char name[50]; // 笔记名称
char category[50]; // 笔记分类
char content[1000]; // 笔记内容
struct Note *prev; // 上一个笔记
struct Note *next; // 下一个笔记
} Note;
typedef struct NoteList {
Note *head; // 第一个笔记
Note *tail; // 最后一个笔记
Note *curr; // 当前打开的笔记
} NoteList;
NoteList *createNoteList(); // 创建笔记列表
void destroyNoteList(NoteList *noteList); // 销毁笔记列表
void printMenu(); // 打印菜单
int getChoice(); // 获取用户选择
void openNote(NoteList *noteList); // 打开笔记
void closeNote(NoteList *noteList); // 关闭笔记
void saveNote(NoteList *noteList); // 保存笔记
void printUsage(); // 使用说明
void createNote(NoteList *noteList); // 创建笔记
void editNote(NoteList *noteList); // 编辑笔记
void deleteNote(NoteList *noteList); // 删除笔记
void classifyNote(NoteList *noteList); // 分类笔记
void searchNote(NoteList *noteList); // 查询笔记
void analyzeData(NoteList *noteList); // 数据分析
#endif
// note.c
#include "note.h"
NoteList *createNoteList() {
NoteList *noteList = (NoteList *) malloc(sizeof(NoteList));
noteList->head = NULL;
noteList->tail = NULL;
noteList->curr = NULL;
return noteList;
}
void destroyNoteList(NoteList *noteList) {
Note *p = noteList->head;
while (p != NULL) {
Note *q = p->next;
free(p);
p = q;
}
free(noteList);
}
void printMenu() {
printf("1. Open note\n");
printf("2. Close note\n");
printf("3. Save note\n");
printf("4. Usage\n");
printf("5. Create note\n");
printf("6. Edit note\n");
printf("7. Delete note\n");
printf("8. Classify note\n");
printf("9. Search note\n");
printf("10. Analyze data\n");
printf("11. Quit\n");
}
int getChoice() {
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
return choice;
}
void openNote(NoteList *noteList) {
char name[50];
printf("Enter the name of the note to open: ");
scanf("%s", name);
Note *p = noteList->head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
noteList->curr = p;
printf("Note \"%s\" opened.\n", name);
return;
}
p = p->next;
}
printf("Note \"%s\" not found.\n", name);
}
void closeNote(NoteList *noteList) {
if (noteList->curr == NULL) {
printf("No note opened.\n");
} else {
printf("Note \"%s\" closed.\n", noteList->curr->name);
noteList->curr = NULL;
}
}
void saveNote(NoteList *noteList) {
if (noteList->curr == NULL) {
printf("No note opened.\n");
} else {
char filename[50];
printf("Enter the name of the file to save to: ");
scanf("%s", filename);
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed to open file \"%s\".\n", filename);
} else {
fprintf(fp, "%s\n%s\n%s", noteList->curr->name, noteList->curr->category, noteList->curr->content);
fclose(fp);
printf("Note \"%s\" saved to file \"%s\".\n", noteList->curr->name, filename);
}
}
}
void printUsage() {
printf("Welcome to the note management system!\n");
printf("This program allows you to manage your notes with ease.\n");
printf("Please select an option from the menu to get started.\n");
}
void createNote(NoteList *noteList) {
char name[50];
printf("Enter the name of the note to create: ");
scanf("%s", name);
Note *p = noteList->head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
printf("Note \"%s\" already exists.\n", name);
return;
}
p = p->next;
}
Note *note = (Note *) malloc(sizeof(Note));
strcpy(note->name, name);
printf("Enter the category of the note: ");
scanf("%s", note->category);
printf("Enter the content of the note: ");
getchar(); // 消耗掉上一次的回车
fgets(note->content, 1000, stdin);
note->prev = NULL;
note->next = NULL;
if (noteList->head == NULL) {
noteList->head = note;
noteList->tail = note;
} else {
noteList->tail->next = note;
note->prev = noteList->tail;
noteList->tail = note;
}
printf("Note \"%s\" created.\n", name);
}
void editNote(NoteList *noteList) {
if (noteList->curr == NULL) {
printf("No note opened.\n");
} else {
printf("Enter the new content of the note: ");
getchar(); // 消耗掉上一次的回车
fgets(noteList->curr->content, 1000, stdin);
printf("Note \"%s\" edited.\n", noteList->curr->name);
}
}
void deleteNote(NoteList *noteList) {
char name[50];
printf("Enter the name of the note to delete: ");
scanf("%s", name);
Note *p = noteList->head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
if (p == noteList->curr) {
noteList->curr = NULL;
}
if (p == noteList->head && p == noteList->tail) {
noteList->head = NULL;
noteList->tail = NULL;
} else if (p == noteList->head) {
noteList->head = p->next;
noteList->head->prev = NULL;
} else if (p == noteList->tail) {
noteList->tail = p->prev;
noteList->tail->next = NULL;
} else {
p->prev->next = p->next;
p->next->prev = p->prev;
}
printf("Note \"%s\" deleted.\n", name);
free(p);
return;
}
p = p->next;
}
printf("Note \"%s\" not found.\n", name);
}
void classifyNote(NoteList *noteList) {
if (noteList->curr == NULL) {
printf("No note opened.\n");
} else {
printf("Enter the category of the note: ");
scanf("%s", noteList->curr->category);
printf("Note \"%s\" classified.\n", noteList->curr->name);
}
}
void searchNote(NoteList *noteList) {
char keyword[50];
printf("Enter the keyword to search for: ");
scanf("%s", keyword);
Note *p = noteList->head;
while (p != NULL) {
if (strstr(p->name, keyword) != NULL || strstr(p->category, keyword) != NULL || strstr(p->content, keyword) != NULL) {
printf("Name: %s\nCategory: %s\nContent: %s\n", p->name, p->category, p->content);
}
p = p->next;
}
}
void analyzeData(NoteList *noteList) {
int numNotes = 0;
int numChars = 0;
Note *p = noteList->head;
while (p != NULL) {
numNotes++;
numChars += strlen(p->content);
p = p->next;
}
printf("Number of notes: %d\n", numNotes);
printf("Average note length: %.2f characters\n", (float) numChars / numNotes);
}
```
在笔记管理系统中,每个笔记都是一个 Note 结构体,包含笔记名称、笔记分类、笔记内容以及上一个和下一个笔记的指针。笔记列表是一个 NoteList 结构体,包含第一个笔记、最后一个笔记和当前打开的笔记。根据用户的选择,笔记管理系统可以打开、关闭、保存、创建、编辑、删除、分类、查询笔记,还可以进行数据分析。笔记列表使用双向链表来管理笔记,支持在任意位置插入和删除笔记。
阅读全文