首页 > 代码库 > 课程设计备份

课程设计备份

个人备份,勿抄袭。



#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct link { char num[10]; char name[9]; char sex[3]; char old[3]; char cla[5]; char col[15]; char maj[20]; struct link *next; struct link *front; }stu; void wind(void); /*窗口*/ stu *invent(void); /*创建*/ stu *sort(stu *head); /*排序*/ stu *add(stu *head); /*添加*/ stu *find(stu *head); /*查询(粗略和精确)*/ stu *findAccurate(stu *head); /*精确查询 */ stu *modify(stu *head); /*修改*/ stu *exclude(stu *head); /*删除*/ void show(stu *head); /*显示全部 */ void shouone(stu *head);/*显示一个 */ void out(void); /*退出*/ void inf(stu *head); /*将数据写入文本*/ stu *outf (void); /*从文本读入数据*/ int main() { stu *head,*p; while(1) { int a; wind(); scanf("%d",&a); system("cls"); switch(a) { case 1:head=invent();break; case 2:head=outf(); getchar(); add(head); break; case 3:head=outf(); find(head); break; case 4:head=outf(); modify(head); break; case 5:head=outf(); getchar(); exclude(head); break; case 6:head=outf(); show(head); break; case 7:out();break; default: printf("功能不存在。\n"); } printf("按任意键继续。\n"); fflush(stdin); getchar(); system("cls"); } return 0; } void wind(void) { printf("***************************************************\n"); printf("* *\n"); printf("* *\n"); printf("* 欢迎使用学生基本信息管理系统 *\n"); printf("* 1.创建数据 *\n"); printf("* 2.添加数据 *\n"); printf("* 3.查询数据 *\n"); printf("* 4.修改数据 *\n"); printf("* 5.删除数据 *\n"); printf("* 6.显示全部数据 *\n"); printf("* 7.退出 *\n"); printf("* *\n"); printf("***************************************************\n"); } stu *invent (void) /*输入数据*/ { stu *head=NULL,*tail,*new; int icount=1,n; printf("请输入学生的总数:"); scanf("%d",&n); getchar(); /*清缓存*/ while(icount<=n) { new=(stu *)malloc(sizeof(stu)); printf("请输入第%d个学生的学号(不超过九位):",icount); /*fgets安全,但是对读入回车。*/ gets(new->num); printf("请输入第%d个学生的姓名:",icount); gets(new->name); printf("请输入第%d个学生的性别:",icount); gets(new->sex); printf("请输入第%d个学生的年龄:",icount); gets(new->old); printf("请输入第%d个学生的班级:",icount); gets(new->cla); printf("请输入第%d个学生的学院:",icount); gets(new->col); printf("请输入第%d个学生的专业:",icount); gets(new->maj); new->next =NULL; if(icount == 1) { head=tail=new; head->front=NULL; } else { new->front=tail; tail->next =new; tail=new; } icount++; } sort(head); inf(head); return head; } stu *add(stu *head) /*添加数据*/ { stu *new,*tou,*wei,*p; int n,i; new=(stu *)malloc(sizeof(stu)); new->next=NULL; printf("请输入学生的学号(不超过九位):"); /*fgets安全,但是对读入回车。*/ gets(new->num ); printf("请输入学生的姓名:"); gets(new->name); printf("请输入学生的性别:"); gets(new->sex); printf("请输入学生的年龄:"); gets(new->old); printf("请输入学生的班级:"); gets(new->cla); printf("请输入学生的学院:"); gets(new->col); printf("请输入学生的专业:"); gets(new->maj); head->front=new; new->next=head; head=new; head->front=NULL; head=sort(head); inf(head); return head; } void show(stu *head) /*显示数据*/ { printf("学生数据如下:\n"); int icount=1; while(head!=NULL) { printf("第%d个学生的学号为:",icount); puts(head->num ); printf("第%d个学生的姓名为:",icount); puts(head->name ); printf("第%d个学生的性别为:",icount); puts(head->sex ); printf("第%d个学生的年龄为:",icount); puts(head->old ); printf("第%d个学生的班级为:",icount); puts(head->cla ); printf("第%d个学生的学院为:",icount); puts(head->col ); printf("第%d个学生的专业为:",icount); puts(head->maj ); putchar(\n); icount++; head=head->next ; } } void shouone(stu *head) { printf("学生的学号为:"); puts(head->num ); printf("学生的姓名为:"); puts(head->name ); printf("学生的性别为:"); puts(head->sex ); printf("学生的年龄为:"); puts(head->old ); printf("学生的班级为:"); puts(head->cla ); printf("学生的学院为:"); puts(head->col ); printf("学生的专业为:"); puts(head->maj ); } stu *find(stu *head) { int select=0; int flag=1; char inc1[20],inc2[20]; printf("请选择以下两种查询方式(输入序号):\n"); printf("1.粗略查询(知道专业和班级).\n"); printf("2.精确查询(知道姓名或者学号)\n."); scanf("%d",&select); while(1) { if(select==1) { getchar(); printf("请输入专业:"); gets(inc1); printf("请输入班级:"); gets(inc2); while(head!=NULL) { if((strcmp(head->maj,inc1))==0&&(strcmp(head->maj,inc2))) { shouone(head); flag=0; } head=head->next; } if(flag) { printf("数据不存在。"); } break; } else if(select==2) { getchar(); printf("请输入姓名或者学号:"); gets(inc1); while(head!=NULL) { if(strcmp(inc1,head->name)==0||strcmp(inc1,head->num )==0) { break; } head=head->next ; } if(head!=NULL) { shouone(head); } else { printf("数据不存在。"); } break; } else { printf("选择不存在,请重新选择。"); } } return head; } stu *modify(stu *head) { stu *new; printf("请首先查询你所要修改的数据。(请使用精确查找)\n"); new=find(head); if(new!=NULL) { printf("请输入修改后学生的学号(不超过九位):"); /*fgets安全,但是对读入回车。*/ gets(new->num ); printf("请输入修改后学生的姓名:"); gets(new->name); printf("请输入修改后学生的性别:"); gets(new->sex); printf("请输入修改后学生的年龄:"); gets(new->old); printf("请输入修改后学生的班级:"); gets(new->cla); printf("请输入修改后学生的学院:"); gets(new->col); printf("请输入修改后学生的专业:"); gets(new->maj); } inf(head); return head; } stu *exclude(stu *head) { stu *q,*p; printf("请首先查询你所要删除的数据。\n"); q=findAccurate(head); if(q!=NULL) { if(q->front==NULL) { head=q->next; head->front=NULL; free(q); } else { p=q->front; q->next->front=p; p->next=p->next->next; free(q); } } inf(head); return head; } stu *findAccurate(stu *head) { char inc[20]; printf("请输入姓名或者学号:"); gets(inc); while(head!=NULL) { if(strcmp(inc,head->name)==0||strcmp(inc,head->num )==0) { break; } head=head->next ; } if(head!=NULL) { shouone(head); } else { printf("数据不存在。"); } return head; } void inf(stu *head) { FILE *fp; if((fp=fopen("学生数据.txt","w"))==NULL) { printf("文件打开失败.\n"); exit(1); } while(head!=NULL) { fwrite(head,sizeof(stu),1,fp); head=head->next; } fclose(fp); } stu *outf (void) /*从文本读入数据*/ { stu *head=NULL,*tail,*new; int icount=1; FILE *fp; if((fp=fopen("学生数据.txt","r"))==NULL) { printf("数据文件不存在.\n"); exit(1); } while(!feof(fp)) { new=(stu *)malloc(sizeof(stu)); fread(new,sizeof(stu),1,fp); new->next =NULL; if(icount == 1) { head=tail=new; head->front=NULL; } else { new->front=tail; tail->next =new; tail=new; } icount++; } tail->front->next=NULL; /*多走一个字节*/ free(tail); return head; } stu *sort(stu *head) { stu *tail,*temp,*tou,*shou[100],*wei[100]; char a[30]; int i,j; tail=head; while(tail->next !=NULL) { tail=tail->next ; } while(head!=tail) { temp=head; while(temp!=tail) { if((strcmp(temp->col,temp->next->col))>0) { strcpy(a,temp->num ); strcpy(temp->num ,temp->next->num ); strcpy(temp->next->num,a ); strcpy(a,temp->name ); strcpy(temp->name ,temp->next->name ); strcpy(temp->next->name,a ); strcpy(a,temp->sex ); strcpy(temp->sex ,temp->next->sex ); strcpy(temp->next->sex,a ); strcpy(a,temp->old ); strcpy(temp->old ,temp->next->old ); strcpy(temp->next->old,a ); strcpy(a,temp->cla ); strcpy(temp->cla ,temp->next->cla ); strcpy(temp->next->cla,a ); strcpy(a,temp->col ); strcpy(temp->col ,temp->next->col ); strcpy(temp->next->col,a ); strcpy(a,temp->maj ); strcpy(temp->maj ,temp->next->maj ); strcpy(temp->next->maj,a ); } temp=temp->next ; } tail=tail->front ; } temp=head; i=0; j=0; shou[i]=head; strcpy(a,temp->col ); while(temp!=NULL) { if((strcmp(a,temp->col))==0) { wei[j]=temp; } else if((strcmp(a,temp->col))<0) { strcpy(a,temp->col ); shou[++i]=temp; wei[++j]=temp; } temp=temp->next ; } shou[++i]=NULL; i=0; while(shou[i]!=NULL) { tou=shou[i]; tail=wei[i]; while(tou!=tail) { temp=tou; while(temp!=tail) { if((strcmp(tou->maj,tou->next->maj))>0) { strcpy(a,temp->num ); strcpy(temp->num ,temp->next->num ); strcpy(temp->next->num,a ); strcpy(a,temp->name ); strcpy(temp->name ,temp->next->name ); strcpy(temp->next->name,a ); strcpy(a,temp->sex ); strcpy(temp->sex ,temp->next->sex ); strcpy(temp->next->sex,a ); strcpy(a,temp->old ); strcpy(temp->old ,temp->next->old ); strcpy(temp->next->old,a ); strcpy(a,temp->cla ); strcpy(temp->cla ,temp->next->cla ); strcpy(temp->next->cla,a ); strcpy(a,temp->col ); strcpy(temp->col ,temp->next->col ); strcpy(temp->next->col,a ); strcpy(a,temp->maj ); strcpy(temp->maj ,temp->next->maj ); strcpy(temp->next->maj,a ); } temp=temp->next ; } tail=tail->front ; } i++; } return head; } void out(void) { printf("谢谢使用本系统,再见。(按任意键退出)\n"); getchar(); exit(0); }

 

#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct link{char num[10];char name[9];char sex[3];char old[3];char cla[5];char col[15];char maj[20];struct link *next;struct link *front; }stu;void wind(void); /*窗口*/ stu *invent(void); /*创建*/ stu *sort(stu *head); /*排序*/ stu *add(stu *head); /*添加*/ stu *find(stu *head); /*查询(粗略和精确)*/ stu *findAccurate(stu *head); /*精确查询 */stu *modify(stu *head); /*修改*/ stu *exclude(stu *head); /*删除*/void show(stu *head); /*显示全部 */void shouone(stu *head);/*显示一个 */void out(void); /*退出*/void inf(stu *head); /*将数据写入文本*/stu *outf (void); /*从文本读入数据*/ int main(){ stu *head,*p;while(1){int a;wind();scanf("%d",&a);system("cls");switch(a){case 1:head=invent();break;case 2:head=outf();getchar();   add(head);   break;case 3:head=outf();   find(head);   break;case 4:head=outf();   modify(head);   break;case 5:head=outf();getchar();   exclude(head);   break;case 6:head=outf();   show(head);   break;case 7:out();break; default: printf("功能不存在。\n");}printf("按任意键继续。\n");fflush(stdin);getchar(); system("cls");} return 0; }  void wind(void){printf("***************************************************\n");printf("*                                                 *\n");printf("*                                                 *\n");printf("*         欢迎使用学生基本信息管理系统            *\n");printf("*         1.创建数据                              *\n");printf("*         2.添加数据                              *\n");printf("*         3.查询数据                              *\n");printf("*         4.修改数据                              *\n");printf("*         5.删除数据                              *\n");printf("*         6.显示全部数据                          *\n");printf("*         7.退出                                  *\n");printf("*                                                 *\n");printf("***************************************************\n");}stu *invent (void) /*输入数据*/ {stu *head=NULL,*tail,*new;int icount=1,n;printf("请输入学生的总数:");scanf("%d",&n);getchar(); /*清缓存*/ while(icount<=n){new=(stu *)malloc(sizeof(stu));printf("请输入第%d个学生的学号(不超过九位):",icount);  /*fgets安全,但是对读入回车。*/ gets(new->num);printf("请输入第%d个学生的姓名:",icount);gets(new->name);printf("请输入第%d个学生的性别:",icount);gets(new->sex);printf("请输入第%d个学生的年龄:",icount);gets(new->old);printf("请输入第%d个学生的班级:",icount);gets(new->cla);printf("请输入第%d个学生的学院:",icount);gets(new->col);printf("请输入第%d个学生的专业:",icount);gets(new->maj);new->next =NULL;if(icount == 1){head=tail=new;head->front=NULL;}else{new->front=tail;tail->next =new;tail=new;}icount++;}sort(head);inf(head);return head;}stu *add(stu *head)  /*添加数据*/ {stu *new,*tou,*wei,*p;int n,i;new=(stu *)malloc(sizeof(stu));new->next=NULL;printf("请输入学生的学号(不超过九位):");  /*fgets安全,但是对读入回车。*/ gets(new->num );printf("请输入学生的姓名:");gets(new->name);printf("请输入学生的性别:");gets(new->sex);printf("请输入学生的年龄:");gets(new->old);printf("请输入学生的班级:");gets(new->cla);printf("请输入学生的学院:");gets(new->col);printf("请输入学生的专业:");gets(new->maj);head->front=new;new->next=head;head=new;head->front=NULL;head=sort(head);inf(head);return head;}void show(stu *head)  /*显示数据*/ {printf("学生数据如下:\n");int icount=1;while(head!=NULL){printf("第%d个学生的学号为:",icount);puts(head->num );printf("第%d个学生的姓名为:",icount);puts(head->name );printf("第%d个学生的性别为:",icount);puts(head->sex  );printf("第%d个学生的年龄为:",icount);puts(head->old  );printf("第%d个学生的班级为:",icount);puts(head->cla  ); printf("第%d个学生的学院为:",icount);puts(head->col  );printf("第%d个学生的专业为:",icount);puts(head->maj  );putchar(‘\n‘);icount++;head=head->next ;}}void shouone(stu *head){printf("学生的学号为:");puts(head->num );printf("学生的姓名为:");puts(head->name );printf("学生的性别为:");puts(head->sex  );printf("学生的年龄为:");puts(head->old  );printf("学生的班级为:");puts(head->cla  ); printf("学生的学院为:");puts(head->col  );printf("学生的专业为:");puts(head->maj  );} stu *find(stu *head){int select=0;int flag=1;char inc1[20],inc2[20];printf("请选择以下两种查询方式(输入序号):\n");printf("1.粗略查询(知道专业和班级).\n");printf("2.精确查询(知道姓名或者学号)\n.");scanf("%d",&select);while(1){if(select==1){getchar();printf("请输入专业:");gets(inc1);printf("请输入班级:");gets(inc2);while(head!=NULL){if((strcmp(head->maj,inc1))==0&&(strcmp(head->maj,inc2))){shouone(head);flag=0;}head=head->next;}if(flag){printf("数据不存在。");}break;}else if(select==2){getchar();printf("请输入姓名或者学号:");gets(inc1);while(head!=NULL){if(strcmp(inc1,head->name)==0||strcmp(inc1,head->num )==0){break;}head=head->next ;}if(head!=NULL){shouone(head);}else{printf("数据不存在。");}break;}else{printf("选择不存在,请重新选择。");}}return head; }
stu *modify(stu *head){stu *new;printf("请首先查询你所要修改的数据。(请使用精确查找)\n");new=find(head);if(new!=NULL){printf("请输入修改后学生的学号(不超过九位):");  /*fgets安全,但是对读入回车。*/ gets(new->num );printf("请输入修改后学生的姓名:");gets(new->name);printf("请输入修改后学生的性别:");gets(new->sex);printf("请输入修改后学生的年龄:");gets(new->old);printf("请输入修改后学生的班级:");gets(new->cla);printf("请输入修改后学生的学院:");gets(new->col);printf("请输入修改后学生的专业:");gets(new->maj);}inf(head);return head;}stu *exclude(stu *head){stu *q,*p;printf("请首先查询你所要删除的数据。\n");q=findAccurate(head);if(q!=NULL){if(q->front==NULL){head=q->next;head->front=NULL;free(q);}else{p=q->front;q->next->front=p;p->next=p->next->next;free(q);}}inf(head);return head;}stu *findAccurate(stu *head){char inc[20];printf("请输入姓名或者学号:");gets(inc);while(head!=NULL){if(strcmp(inc,head->name)==0||strcmp(inc,head->num )==0){break;}head=head->next ;}if(head!=NULL){shouone(head);}else{printf("数据不存在。");}return head;}
void inf(stu *head) {FILE *fp;if((fp=fopen("学生数据.txt","w"))==NULL){printf("文件打开失败.\n");exit(1); }while(head!=NULL){fwrite(head,sizeof(stu),1,fp);head=head->next;}fclose(fp);}stu *outf (void) /*从文本读入数据*/ {stu *head=NULL,*tail,*new;int icount=1;FILE *fp;if((fp=fopen("学生数据.txt","r"))==NULL){printf("数据文件不存在.\n");exit(1); }while(!feof(fp)){new=(stu *)malloc(sizeof(stu));fread(new,sizeof(stu),1,fp);new->next =NULL;if(icount == 1){head=tail=new;head->front=NULL;}else{new->front=tail;tail->next =new;tail=new;}icount++;}tail->front->next=NULL; /*多走一个字节*/ free(tail);return head;}stu *sort(stu *head){stu *tail,*temp,*tou,*shou[100],*wei[100];char a[30];int i,j;tail=head;while(tail->next !=NULL){tail=tail->next ;}while(head!=tail){temp=head;while(temp!=tail){if((strcmp(temp->col,temp->next->col))>0){strcpy(a,temp->num );strcpy(temp->num ,temp->next->num );strcpy(temp->next->num,a );strcpy(a,temp->name );strcpy(temp->name ,temp->next->name );strcpy(temp->next->name,a );strcpy(a,temp->sex );strcpy(temp->sex ,temp->next->sex );strcpy(temp->next->sex,a );strcpy(a,temp->old );strcpy(temp->old ,temp->next->old );strcpy(temp->next->old,a );strcpy(a,temp->cla );strcpy(temp->cla ,temp->next->cla );strcpy(temp->next->cla,a );strcpy(a,temp->col );strcpy(temp->col ,temp->next->col );strcpy(temp->next->col,a );strcpy(a,temp->maj );strcpy(temp->maj ,temp->next->maj );strcpy(temp->next->maj,a );}temp=temp->next ;}tail=tail->front ;}temp=head;i=0;j=0;shou[i]=head;strcpy(a,temp->col );while(temp!=NULL){if((strcmp(a,temp->col))==0){wei[j]=temp;}    else if((strcmp(a,temp->col))<0){strcpy(a,temp->col );shou[++i]=temp;wei[++j]=temp;}temp=temp->next ;}shou[++i]=NULL;i=0;while(shou[i]!=NULL){tou=shou[i];tail=wei[i];while(tou!=tail){temp=tou;while(temp!=tail){if((strcmp(tou->maj,tou->next->maj))>0){strcpy(a,temp->num );strcpy(temp->num ,temp->next->num );strcpy(temp->next->num,a );strcpy(a,temp->name );strcpy(temp->name ,temp->next->name );strcpy(temp->next->name,a );strcpy(a,temp->sex );strcpy(temp->sex ,temp->next->sex );strcpy(temp->next->sex,a );strcpy(a,temp->old );strcpy(temp->old ,temp->next->old );strcpy(temp->next->old,a );strcpy(a,temp->cla );strcpy(temp->cla ,temp->next->cla );strcpy(temp->next->cla,a );strcpy(a,temp->col );strcpy(temp->col ,temp->next->col );strcpy(temp->next->col,a );strcpy(a,temp->maj );strcpy(temp->maj ,temp->next->maj );strcpy(temp->next->maj,a );}temp=temp->next ;}tail=tail->front ;}i++;} return head;}void out(void){printf("谢谢使用本系统,再见。(按任意键退出)\n");getchar();exit(0);}

课程设计备份