C语言|C语言 学生信息增删改查

#include #include #include #include #include #include #define LEN sizeof(struct student)typedef struct student { long int StudentID; //学号 int age; //年龄 int enrolment; //入学年份 char sex[3]; //性别 char name[10]; //姓名 char major[10]; //专业 char tell[15]; //电话号码 char nation[10]; //民族 char province[10]; //省份 char birthday[15]; //出生年月 char IDnumber[20]; //身份证号 struct student *next; }node; void gotoxy(int x, int y) { COORD coord = {x, y}; /*COORD是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标。其定义为: typedef struct _COORD { SHORT X; // horizontal coordinate SHORT Y; // vertical coordinate } COORD; */ SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }void menu(node *head) //菜单 { void display(node *head); void input(node *head); void outputall(node *head); node *subtruct(node *head); node *change(node *head); int a; gotoxy(47,4); printf("欢迎来到学生信息管理系统!\n"); gotoxy(38,6); printf("|----------------------------------------|\n"); gotoxy(38,7); printf("|--------- 1、输出指定学生信息 ----------|\n"); gotoxy(38,8); printf("|--------- 2、增加指定学生信息 ----------|\n"); gotoxy(38,9); printf("|--------- 3、删除指定学生信息 ----------|\n"); gotoxy(38,10); printf("|--------- 4、改动指定学生信息 ----------|\n"); gotoxy(38,11); printf("|--------- 5、输出所有学生信息 ----------|\n"); gotoxy(38,12); printf("|----------------------------------------|\n"); gotoxy(38,14); printf("请选择操作:\n"); gotoxy(50,14); scanf("%d",&a); if(a<1||a>5) { gotoxy(38,16); printf("请重新输入!\n"); gotoxy(50,16); scanf("%d",&a); } system("cls"); switch(a) { case 1: display(head); break; case 2: input(head); break; case 3: subtruct(head); break; case 4: change(head); break; case 5: outputall(head); break; } }node *init() //初始化链表 { node *head; head=(node *)malloc(LEN); head->next=NULL; return head; }void save(node *head) //将链表中的信息存入磁盘 { node *p; p=head->next; FILE *rf; if((rf=fopen("xuesheng.txt","w+"))==NULL) printf("不能打开文件!\n"); else while(p) { fwrite(p,sizeof(student),1,rf); p=p->next; } fclose(rf); }void input(node *head) //按照学号顺序增加学生信息 { int number,a; node *p,*s,*pre; pre=head; s=head->next; gotoxy(20,2); printf("增加人数:"); gotoxy(30,2); scanf("%d",&number); for(int i=0; iname,&p->age,p->sex,p->nation); gotoxy(20,8); printf("出生年月,籍贯(1997.7.23 山西省):\n"); gotoxy(20,10); scanf("%s%s",p->birthday,p->province); gotoxy(20,12); printf("身份账号:\n"); gotoxy(20,14); scanf("%s",p->IDnumber); gotoxy(20,16); printf("电话号码:\n"); gotoxy(20,18); scanf("%s",p->tell); gotoxy(20,20); printf("入学年份,专业,学号(2015 计算机 1507064101):\n"); gotoxy(20,22); scanf("%d%s%ld",&p->enrolment,p->major,&p->StudentID); p->next=NULL; if(!s) head->next=p; else { while(s&&(p->StudentID>s->StudentID)) { pre=s; s=s->next; } if(s) { pre->next=p; p->next=s; } else pre->next=p; system("cls"); } } save(head); gotoxy(20,2); printf("还要继续添加吗(1要0不要)?\n"); gotoxy(20,4); scanf("%d",&a); system("cls"); if(a==1)input(head); if(a==0)menu(head); }void output(node *head,long int StudentID0) //输出指定学号的学生信息 { node *p; p=head->next; while(p&&p->StudentID!=StudentID0) p=p->next; gotoxy(20,2); printf("学号为 %ld 的学生所有信息:\n",p->StudentID); gotoxy(20,4); printf("%s\n","姓名,年龄,性别,民族:"); gotoxy(20,6); printf("%-10s%-5d%-5s%-5s\n",p->name,p->age,p->sex,p->nation); gotoxy(20,8); printf("%s\n","出生年月,籍贯:"); gotoxy(20,10); printf("%-15s%-10s\n",p->birthday,p->province); gotoxy(20,12); printf("%s\n","身份证号:"); gotoxy(35,12); printf("%-20s\n",p->IDnumber); gotoxy(20,14); printf("%s\n","电话号码:"); gotoxy(35,14); printf("%-20s\n",p->tell); gotoxy(20,16); printf("%s\n","入学年份,专业,学号:"); gotoxy(20,18); printf("%-10d%-20s%-20ld\n",p->enrolment,p->major,p->StudentID); p=p->next; gotoxy(20,20); printf("按任意键继续!\n"); getch(); }void outputall(node *head) //输出所有学生信息 { node *write(node *head); head=write(head); node *p; p=head->next; if(!(head->next)) { gotoxy(38,4); printf("学生信息为空!按任意键返回主菜单!\n"); getch(); } else { while(p) { printf("学号为 %ld 的学生所有信息:\n\n",p->StudentID); printf("%s\n\n","姓名,年龄,性别,民族:"); printf("%-10s%-5d%-5s%-5s\n\n",p->name,p->age,p->sex,p->nation); printf("%s\n\n","出生年月,籍贯:"); printf("%-15s%-10s\n\n",p->birthday,p->province); printf("%s\n\n","身份证号:"); printf("%-20s\n\n",p->IDnumber); printf("%s\n\n","电话号码:"); printf("%-20s\n\n",p->tell); printf("%s\n\n","入学年份,专业,学号:"); printf("%-10d%-20s%-20ld\n\n",p->enrolment,p->major,p->StudentID); p=p->next; } printf("按任意键返回主菜单!\n"); } getch(); system("cls"); menu(head); }void display(node *head) //查询指定学生信息 { int a; node *p; long int StudentID0; if(!(head->next)) { gotoxy(38,4); printf("学生信息为空!按任意键返回主菜单!\n"); getch(); } else { gotoxy(20,2); printf("请输入要查询学生的学号:\n"); gotoxy(20,4); scanf("%ld",&StudentID0); p=head->next; while(p&&p->StudentID!=StudentID0) p=p->next; if(p==NULL) { gotoxy(20,6); printf("没有这位同学的信息,是否要录入?(1是0否):"); gotoxy(65,6); scanf("%d",&a); system("cls"); if(a==1)input(head); if(a==0)menu(head); } else { system("cls"); output(head,StudentID0); } } system("cls"); menu(head); }void subtruct(node *head) //删除指定学生信息 { int a; node *p,*pre; long int StudentID0; gotoxy(20,2); printf("请输入要删除学生的学号:\n"); gotoxy(20,4); scanf("%ld",&StudentID0); p=head->next; pre=head; while(p&&p->StudentID!=StudentID0) { pre=p; p=p->next; } if(p==NULL) { gotoxy(20,6); printf("没有这位同学的信息!按任意键返回主菜单!\n"); getch(); system("cls"); menu(head); } else { system("cls"); output(head,StudentID0); system("cls"); gotoxy(20,2); printf("是否确认删除?(1是0否):"); gotoxy(50,2); scanf("%d",&a); pre->next=p->next; p->next=NULL; save(head); gotoxy(20,4); printf("删除成功!按任意键返回主菜单!\n"); free(p); getch(); } system("cls"); menu(head); }node *change(node *head) //改变指定学生信息 { long int StudentID0; char info[20]; node *p; int d,a; long int ld; char char0[20]; p=head->next; printf("请输入要改动的学生学号:\n"); scanf("%ld",&StudentID0); output(head,StudentID0); while(p&&p->StudentID!=StudentID0) p=p->next; printf("请输入改动项目:\n"); scanf("%s",info); if(strcmp("姓名",info)==0)a=1; if(strcmp("年龄",info)==0)a=2; if(strcmp("性别",info)==0)a=3; if(strcmp("民族",info)==0)a=4; if(strcmp("籍贯",info)==0)a=5; if(strcmp("专业",info)==0)a=6; if(strcmp("学号",info)==0)a=7; if(strcmp("出生年月",info)==0)a=8; if(strcmp("身份证号",info)==0)a=9; if(strcmp("电话号码",info)==0)a=10; if(strcmp("入学年份",info)==0)a=11; printf("请输入改动内容:\n"); switch(a) { case 1: scanf("%s",char0); strcpy(p->name,char0); break; case 2: scanf("%d",&d); p->age=d; break; case 3: scanf("%s",char0); strcpy(p->sex,char0); break; case 4: scanf("%s",char0); strcpy(p->nation,char0); break; case 5: scanf("%s",char0); strcpy(p->province,char0); break; case 6: scanf("%s",char0); strcpy(p->major,char0); break; case 7: scanf("%ld",&ld); p->StudentID=ld; break; case 8: scanf("%s",char0); strcpy(p->birthday,char0); break; case 9: scanf("%s",char0); strcpy(p->IDnumber,char0); break; case 10: scanf("%s",char0); strcpy(p->tell,char0); break; case 11: scanf("%d",&d); p->enrolment=d; break; } printf("是否继续改动?(1是0否):"); scanf("%d",&a); if(a==1){system("cls"); head=change(head); } printf("改动成功!按任意键查看该生信息!\n"); getch(); system("cls"); save(head); output(head,StudentID0); system("cls"); menu(head); return head; }node *write(node *head) //从磁盘读入信息并形成单链表 { node *p,*s,stu; p=head; FILE *rf; if((rf=fopen("xuesheng.txt","r+"))==NULL) printf("不能打开文件!\n"); else { while(fread(&stu,sizeof(node),1,rf)!=0) { s=(node *)malloc(LEN); s->age=stu.age; s->enrolment=stu.enrolment; s->StudentID=stu.StudentID; strcpy(s->birthday,stu.birthday); strcpy(s->IDnumber,stu.IDnumber); strcpy(s->major,stu.major); strcpy(s->name,stu.name); strcpy(s->nation,stu.nation); strcpy(s->province,stu.province); strcpy(s->sex,stu.sex); strcpy(s->tell,stu.tell); s->next=NULL; p->next=s; p=s; } } fclose(rf); return head; }int main() { system("color F5"); node *head; head=init(); head=write(head); menu(head); return 0; }


    推荐阅读