#include <stdio.h> #include <windows.h> #include <malloc.h> #include <conio.h> FILE *sco; FILE *bsc; void print_(){ //修饰 for( int i=0; i<110; i++ ) { printf("*"); Sleep(10); //间隔输出 } printf("\n"); } void home_page() //主界面 { system ( "color 5b" ); //背景颜色 print_(); //调用修饰函数 printf("%48c 学生管理系统\n",' '); //%45c ,' '输入空格 printf("%45c 1 查找\n",' '); printf("%45c 2 添加\n",' '); printf("%45c 3 删除\n",' '); printf("%45c 4 修改\n",' '); printf("%45c 5 浏览成绩\n",' '); printf("%45c 6 浏览信息\n",' '); printf("%45c 7 保存\n",' '); printf("%45c 8 文件读取\n",' '); printf("%45c 9 退出\n",' '); print_(); printf("请选择:"); } typedef char Str[100]; typedef struct Student_message //学生信息结构体 { Str Name; //char Name[20]; //姓名 Str Number; //char Number[8]; //学号 Str Phone; //char Phone[12]; //电话 Str Scholarship; //char Scholarship[7]; //奖学金情况 struct Student_message *Next; }Student_message,*_message; typedef struct Student_score //学生成绩结构体 { Str name; //姓名 Str Adv; //char Adv[4]; //高数 Str Lin; //char Lin[4]; //线代 Str Eng; //char Eng[4]; //大英 Str Pro; //char Pro[4]; //程序 struct Student_score *Next; }Student_score,*_score; // struct Student_score *Sco=0; char *Filename1="学生信息.txt"; char *Filename2="学生成绩.txt"; _message Read_file1(char *filename1) { _message stu_temp; _message stu_head = NULL; bsc=fopen(filename1,"r"); if(!bsc) { printf("%s文件不存在!!!\n",filename1); return stu_head; } while(!feof(bsc)) { stu_temp = (_message)calloc(1,sizeof(Student_message)); fscanf(bsc,"%s %s %s %s\n",stu_temp->Name,stu_temp->Number,stu_temp->Phone,stu_temp->Scholarship); stu_temp->Next=stu_head; stu_head = stu_temp; } fclose(bsc); //关闭文件 printf("读取完毕\n"); return stu_head; } _score Read_file2(char *filename2) { _score stu_temp; _score stu_head = NULL; sco=fopen(filename2,"r"); if(!sco) { printf("%s文件不存在!!!\n",filename2); return stu_head; } while(!feof(sco)) { stu_temp = (_score)calloc(1,sizeof(Student_score)); fscanf(sco,"%s %s %s %s %s\n",stu_temp->name,stu_temp->Adv,stu_temp->Eng,stu_temp->Lin,stu_temp->Pro); stu_temp->Next=stu_head; stu_head = stu_temp; } fclose(sco); //关闭文件 printf("读取完毕\n"); return stu_head; } void Save_file1(char *filename1, _message mes_head) //保存信息至文件 { _message stu_cur = mes_head; if(!mes_head) { return; } if(!(bsc = fopen(filename1,"w"))) { bsc = fopen(filename1,"a+"); } while(stu_cur) { fprintf(bsc,"%s %s %s %s\n",stu_cur->Name,stu_cur->Number,stu_cur->Phone,stu_cur->Scholarship); stu_cur = stu_cur->Next; } fclose(bsc); printf("保存完毕\n"); } void Save_file2(char *filename2, _score sco_head) //保存信息至文件 { _score stu_cur = sco_head; if(!sco_head) { return; } if(!(sco = fopen(filename2,"w"))) { sco = fopen(filename2,"a+"); } while(stu_cur) { fprintf(sco,"%s %s %s %s %s\n",stu_cur->name,stu_cur->Adv,stu_cur->Eng,stu_cur->Lin,stu_cur->Pro); stu_cur = stu_cur->Next; } fclose(sco); printf("保存完毕\n"); } void Search_message(_message mes_head) //查找学生信息 { _message stu_cur = mes_head; Str msg; if(!mes_head) { printf("\n当前无任何信息记录!!!\n"); return; } printf("\n输入要查找学生姓名: "),scanf("%s",msg); while(stu_cur) { if(!strcmp(stu_cur->Name,msg)) { printf("姓名: %s\n",stu_cur->Name); printf("学号: %s\n",stu_cur->Number); printf("电话: %s\n",stu_cur->Phone); printf("奖学金信息: %s\n",stu_cur->Scholarship); return; } stu_cur = stu_cur->Next; } printf("输入的信息不存在!!!\n"); } void Search_score(_score sco_head) //查找学生信息 { _score stu_cur = sco_head; Str sco; if(!sco_head) { printf("\n当前无任何信息记录!!!\n"); return; } printf("\n输入要查找学生姓名: "),scanf("%s",sco); while(stu_cur) { if(!strcmp(stu_cur->name,sco)) { printf("姓名: %s\n",stu_cur->name); printf("高数: %s\n",stu_cur->Adv); printf("大英: %s\n",stu_cur->Eng); printf("线代: %s\n",stu_cur->Lin); printf("程序: %s\n",stu_cur->Pro); return; } stu_cur = stu_cur->Next; } printf("输入的信息不存在!!!\n"); } _message Add_message(_message mes_head) //添加学生信息 { _message stu_temp = (_message)calloc(1,sizeof(Student_message)); printf("\n姓名: "),scanf("%s",stu_temp->Name); printf("学号: "),scanf("%s",stu_temp->Number); printf("电话: "),scanf("%s",stu_temp->Phone); printf("奖学金信息(若无请输入\"无\"): "),scanf("%s",stu_temp->Scholarship); stu_temp->Next = mes_head; return stu_temp; } _score Add_score(_score sco_head) //添加学生信息 { _score stu_temp = (_score)calloc(1,sizeof(Student_score)); printf("\n姓名: "),scanf("%s",stu_temp->name); printf("高数: "),scanf("%s",stu_temp->Adv); printf("大英: "),scanf("%s",stu_temp->Eng); printf("线代: "),scanf("%s",stu_temp->Lin); printf("程序: "),scanf("%s",stu_temp->Pro); stu_temp->Next = sco_head; return stu_temp; } _message Delete_message(_message mes_head) //删除学生信息 { _message stu_temp,stu_cur = mes_head; Str msg; if(!mes_head) { printf("\n当前无任何信息记录!!!\n"); return mes_head; } printf("\n请输入要删除的学生姓名: "),scanf("%s",msg); if(!strcmp(mes_head->Name,msg)) { stu_temp = mes_head; mes_head = stu_temp->Next; free(stu_temp); printf("删除成功!!!\n"); return mes_head; } while(stu_cur->Next) { if(!strcmp(stu_cur->Next->Name,msg)) { stu_temp = stu_cur->Next; stu_cur->Next = stu_temp->Next; free(stu_temp); printf("删除成功!!!\n"); return mes_head; } stu_cur = stu_cur->Next; } printf("输入的信息不存在!!!\n"); return mes_head; } _score Delete_score(_score sco_head) //删除学生信息 { _score stu_temp,stu_cur = sco_head; Str sco; if(!sco_head) { printf("\n当前无任何信息记录!!!\n"); return sco_head; } printf("\n请输入要删除的学生姓名: "),scanf("%s",sco); if(!strcmp(sco_head->name,sco)) { stu_temp = sco_head; sco_head = stu_temp->Next; free(stu_temp); printf("删除成功!!!\n"); return sco_head; } while(stu_cur->Next) { if(!strcmp(stu_cur->Next->name,sco)) { stu_temp = stu_cur->Next; stu_cur->Next = stu_temp->Next; free(stu_temp); printf("删除成功!!!\n"); return sco_head; } stu_cur = stu_cur->Next; } printf("输入的信息不存在!!!\n"); return sco_head; } void Modify_message(_message mes_head) //修改学生信息 { _message stu_cur = mes_head; Str msg; if(!mes_head) { printf("\n当前无任何信息记录!!!\n"); return; } printf("\n请输入要修改的学生姓名: "),scanf("%s",msg); while(stu_cur) { if(!strcmp(stu_cur->Name,msg)) { printf("请输入信息替换:\n\n"); printf("\n姓名: "),scanf("%s",stu_cur->Name); printf("学号: "),scanf("%s",stu_cur->Number); printf("电话: "),scanf("%s",stu_cur->Phone); printf("奖学金信息(若无请输入\"无\"): "),scanf("%s",stu_cur->Scholarship); return; } stu_cur = stu_cur->Next; } printf("输入的信息不存在!!!\n"); return; } void Modify_score(_score sco_head) //修改学生信息 { _score stu_cur = sco_head; Str sco; if(!sco_head) { printf("\n当前无任何信息记录!!!\n"); return; } printf("\n请输入要修改的学生姓名: "),scanf("%s",sco); while(stu_cur) { if(!strcmp(stu_cur->name,sco)) { printf("请输入信息替换:\n\n"); printf("\n姓名: "),scanf("%s",stu_cur->name); printf("高数: "),scanf("%s",stu_cur->Adv); printf("大英: "),scanf("%s",stu_cur->Eng); printf("线代: "),scanf("%s",stu_cur->Lin); printf("程序: "),scanf("%s",stu_cur->Pro); return; } stu_cur = stu_cur->Next; } printf("输入的信息不存在!!!\n"); return; } void Display_message(_message mes_head) //显示学生信息 { _message mes_cur = mes_head; if(!mes_head) { printf("\n当前无任何信息记录!!!\n"); return; } while(mes_cur) { printf("\n姓名: %s\n",mes_cur->Name); printf("学号: %s\n",mes_cur->Number); printf("电话: %s\n",mes_cur->Phone); printf("奖学金信息: %s\n",mes_cur->Scholarship); mes_cur = mes_cur->Next; } } void Display_score(_score sco_head) //显示学生信息 { _score sco_cur = sco_head; if(!sco_head) { printf("\n当前无任何信息记录!!!\n"); return; } while(sco_cur) { printf("\n姓名: %s\n",sco_cur->name); printf("高数: %s\n",sco_cur->Adv); printf("大英: %s\n",sco_cur->Eng); printf("线代: %s\n",sco_cur->Lin); printf("程序: %s\n",sco_cur->Pro); sco_cur = sco_cur->Next; } } int main( int argc, char const *argv[] ) { char choice; _message mes_head = NULL; _score sco_head = NULL; while(1) { home_page(); //进入主页面 choice = getch(); system("cls"); switch(choice) //进行选择 { case '1': Search_message(mes_head); Search_score(sco_head); break; case '2': mes_head = Add_message(mes_head); sco_head = Add_score(sco_head); break; case '3': mes_head = Delete_message(mes_head); sco_head = Delete_score(sco_head); break; case '4': Modify_message(mes_head); Modify_score(sco_head); break; case '5': Display_score(sco_head); break; case '6': Display_message(mes_head); break; case '7': Save_file1(Filename1,mes_head); Save_file2(Filename2,sco_head); break; case '8': mes_head = Read_file1(Filename1); sco_head = Read_file2(Filename2); break; case '9': exit(0); break; default: break; } } return 0; }室友程序
主要功能:查找修改删除添加学生成绩,并能储存文件中。
大一期末大作业,学习编程以来第一次编这种稍微有点长的程序。从中也知道了码代码格式的重要性(看的时候比较舒服,也比较清晰);以及中括号先打全的重要性,避免之后还要慢慢找;函数之类的拼写也同样需要注意。
在编程过程中要时刻保持头脑清晰,以免突然逻辑混乱不知道这个循环或者判断有什么作用。就像是写作文一样,要先构思,并且条理清晰。这时候注释就起了很大的作用,有时候回过头看前面的代码时,有些过程已经不知道起了什么作用,及时做好注释既可以方便自己也能让别人理解容易一点(虽然我目前没有这种习惯,只是偶尔注释一下自定义函数的作用)。
编程还是需要靠积累(偶尔码代码偶尔爽,一直码代码一直爽!),同时还要关注新技术的出现,不然很快就会像过气的电脑硬件一样被淘汰掉。