要求用c语言实现一个笔记管理系统,要求有基于libgraphics/ simpleGUI 图形库实现的界面,要求编写代码
时间: 2024-02-12 10:05:13 浏览: 21
好的,笔记管理系统的功能包括新建笔记、编辑笔记、删除笔记、查看笔记等。需要用到文件操作和图形界面库。
以下是一个简单的实现:
```
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NOTE_NUM 100
#define MAX_NOTE_LENGTH 1000
struct note {
char title[50];
char content[MAX_NOTE_LENGTH];
};
struct note notes[MAX_NOTE_NUM];
int note_num = 0;
void draw_main_window();
void draw_note_list();
void draw_note_editor(int note_id);
void save_notes();
void load_notes();
int main() {
initwindow(800, 600, "Note Manager");
load_notes();
while (1) {
draw_main_window();
draw_note_list();
if (ismouseclick(WM_LBUTTONDOWN)) {
int x = mousex();
int y = mousey();
if (x > 10 && x < 110 && y > 50 && y < 80) { // new note button
note_num++;
strcpy(notes[note_num - 1].title, "Untitled");
strcpy(notes[note_num - 1].content, "");
draw_note_list();
draw_note_editor(note_num - 1);
} else if (x > 10 && x < 110 && y > 90 && y < 120) { // save button
save_notes();
} else {
for (int i = 0; i < note_num; i++) {
if (x > 130 && x < 330 && y > 50 + i * 40 && y < 90 + i * 40) { // note item
draw_note_editor(i);
break;
}
}
}
}
delay(50);
}
return 0;
}
void draw_main_window() {
setcolor(WHITE);
settextstyle(BOLD_FONT, HORIZ_DIR, 24);
outtextxy(10, 10, "Note Manager");
settextstyle(BOLD_FONT, HORIZ_DIR, 16);
outtextxy(10, 50, "New Note");
outtextxy(10, 90, "Save");
}
void draw_note_list() {
cleardevice();
draw_main_window();
settextstyle(BOLD_FONT, HORIZ_DIR, 18);
for (int i = 0; i < note_num; i++) {
outtextxy(130, 50 + i * 40, notes[i].title);
}
}
void draw_note_editor(int note_id) {
cleardevice();
settextstyle(BOLD_FONT, HORIZ_DIR, 16);
outtextxy(10, 10, "Title:");
outtextxy(10, 50, "Content:");
settextstyle(BOLD_FONT, HORIZ_DIR, 18);
outtextxy(130, 10, notes[note_id].title);
char temp_content[MAX_NOTE_LENGTH];
strcpy(temp_content, notes[note_id].content);
char* content_ptr = strtok(temp_content, "\n");
int line_num = 0;
while (content_ptr != NULL) {
outtextxy(130, 50 + line_num * 30, content_ptr);
content_ptr = strtok(NULL, "\n");
line_num++;
}
char c;
int index = strlen(notes[note_id].content);
while (1) {
if (kbhit()) {
c = getch();
if (c == 13) { // enter key
strcat(notes[note_id].content, "\n");
line_num++;
outtextxy(130, 50 + (line_num - 1) * 30, "");
} else if (c == 8 && index > 0) { // backspace key
index--;
notes[note_id].content[index] = '\0';
cleardevice();
draw_note_editor(note_id);
break;
} else if (c >= 32 && c <= 126 && index < MAX_NOTE_LENGTH - 1) {
notes[note_id].content[index] = c;
notes[note_id].content[index + 1] = '\0';
index++;
outtextxy(130, 50 + (line_num - 1) * 30, notes[note_id].content + strlen(notes[note_id].content) - strlen(content_ptr));
content_ptr = strtok(NULL, "\n");
if (content_ptr != NULL) {
line_num++;
}
}
}
if (ismouseclick(WM_LBUTTONDOWN)) {
int x = mousex();
int y = mousey();
if (x > 10 && x < 110 && y > 50 && y < 80) { // delete button
for (int i = note_id; i < note_num - 1; i++) {
notes[i] = notes[i + 1];
}
note_num--;
draw_note_list();
break;
} else if (x > 10 && x < 110 && y > 90 && y < 120) { // save button
save_notes();
break;
}
}
delay(50);
}
}
void save_notes() {
FILE* fp = fopen("notes.txt", "w");
if (fp == NULL) {
return;
}
fprintf(fp, "%d\n", note_num);
for (int i = 0; i < note_num; i++) {
fprintf(fp, "%s\n%s\n", notes[i].title, notes[i].content);
}
fclose(fp);
}
void load_notes() {
FILE* fp = fopen("notes.txt", "r");
if (fp == NULL) {
return;
}
fscanf(fp, "%d\n", ¬e_num);
for (int i = 0; i < note_num; i++) {
fgets(notes[i].title, 50, fp);
if (notes[i].title[strlen(notes[i].title) - 1] == '\n') {
notes[i].title[strlen(notes[i].title) - 1] = '\0';
}
fgets(notes[i].content, MAX_NOTE_LENGTH, fp);
if (notes[i].content[strlen(notes[i].content) - 1] == '\n') {
notes[i].content[strlen(notes[i].content) - 1] = '\0';
}
}
fclose(fp);
}
```
以上代码仅供参考,可能还需要优化和改进。