完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include #include #include void print(struct student *head); struct student *insert(struct student *stu_2,struct student *head); void KeepMessage(struct student *head); //链表单元定义,链表相关变量 unsigned int Prin_n; struct student { int id; float score; char name[20]; char sex[20]; struct student *next; } *head,*pthis; //输入数据创建链表 void input() { struct student *tmp; FILE *fp; printf("nn请输入学生的信息以学号为0结束!!:n"); if(!(fp=fopen("student_list","wb")))//打开文本 { printf("cannotn"); return; } do { printf("IDt成绩t姓名t性别t以Tab或者回车输入n"); if ((tmp=(struct student *)malloc(sizeof(struct student)))==NULL)//申请内存 { printf("n错误!不能申请所需的内存!n"); exit(0); } scanf("%dt%ft%st%s",&tmp->id,&tmp->score,tmp->name,tmp->sex);//输入信息 fwrite(tmp,sizeof(struct student),1,fp); tmp->next=NULL; if (tmp->id!=0) { if (head==NULL) { head=tmp; pthis=head; } else { pthis->next=tmp; pthis=pthis->next; } } else { tmp->next=NULL; } } while (tmp->id!=0); fclose(fp); //关闭文件 free(tmp);//释放内存地址 } void KeepMessage(struct student *head)//保存数据函数存入二进制文件 { FILE *fp; struct student *bianl; bianl=head; if(!(fp=fopen("student_list","wb"))) { printf("cannotn"); return; } do{ fwrite(bianl,sizeof(struct student),1,fp); bianl=bianl->next; }while(bianl!=NULL); fclose(fp); // return temp; } //搜索链表找到第一个符合条件的项目输出 void search(int id) { FILE *fp; printf("nn查询结果n"); printf("IDt成绩n"); printf("-------------------------------n"); if(!(fp=fopen("student_list","r")))//防止无文件 { printf("不能读写n"); return ; } if (head==NULL) { printf("n错误!没有数据!n"); return; } pthis=head; while (pthis!=NULL) { if (pthis->id==id) { printf("%dt%.2f %s %sn",pthis->id,pthis->score,pthis->name,pthis->sex); return; } else { pthis=pthis->next; } } printf("n没有找到!n"); } //列表输出链表中的所有项排序 void list() { struct student *view_sort_chinese(struct student *h); FILE *fp; void print(struct student *head); if(!(fp=fopen("student_list","r"))) { printf("不能读写n"); return ; } printf("nn数据列表n"); printf("IDt成绩t姓名t性别tn"); printf("-------------------------------n"); printf("按学号排序如下n"); printf("-------------------------------n"); if (head==NULL) { printf("错误,没有数据!n"); return; } print (view_sort_chinese(head)); } struct student *view_sort_chinese(struct student *h) //冒泡排序 { //在调用函数里面判断head=NULL,这里不判断。 struct student *endpt,*u,*v,*p; u = (struct student *)malloc(sizeof(head)); u->next=h; h=u; for(endpt=NULL;endpt!=h;endpt=p) { for(p=u=h;u->next->next!=endpt;u=u->next) { if(u->next->id > u->next->next->id) { v=u->next->next; u->next->next=v->next; v->next=u->next; u->next=v; p=u->next->next; } } } u=h; h=h->next; head=h; return head; } void print(struct student *head)//打印文本 { struct student *p; p=head; if(head) { do { printf("%dt%.2f %s %sn",p->id,p->score,p->name,p->sex); p=p->next; }while(p!=NULL);//只打印大于0的数;&&(p->id>0) } else { printf("打印错误,文件为空n"); } } //追加数据 struct student *insert(struct student *hea,struct student *stu)// { struct student *p0,*p1; p1 = hea; p0 = stu; if(NULL == hea) { head=p0; p0->next=NULL; return head; } else { p0->next=hea; head=p0; } KeepMessage(head); return head; } //删除数据 struct student *del(int num) { struct student *p1,*p2; if(NULL==head) { printf("nthis list is null!n"); return head; } p1=head; while(p1->id != num && p1->next != NULL) { p2=p1; p1=p1->next; } if(num==p1->id) { if(p1==head) { head=p1->next; } else { p2->next=p1->next; } printf("n删除成员:%d 成功!n",num); } else { printf("%d没有找到!n",num); } KeepMessage(head); return head; } struct student *load1() { FILE *fp; struct student *temp; head=NULL; if(!(fp=fopen("student_list","r"))) { printf("不能读写,或无文件n"); return 0; } do { if ((temp=(struct student *)malloc(sizeof(struct student)))==NULL) { printf("n错误!不能申请所需的内存!n"); exit(0); } fread(temp,sizeof(struct student),1,fp); temp->next=NULL; if(!(!feof(fp)&&(temp->id>0))) { break; } if (head==NULL) { head=temp; pthis=head; } else { pthis->next=temp; pthis=pthis->next; } }while(!feof(fp)&&(temp->id>0)); fclose(fp); return head; } struct student *load() { FILE *fp; struct student *temp; head=NULL; if(!(fp=fopen("student_list","r"))) { printf("不能读写n"); return 0; } do { if ((temp=(struct student *)malloc(sizeof(struct student)))==NULL) { printf("n错误!不能申请所需的内存!n"); exit(0); } fread(temp,sizeof(struct student),1,fp); temp->next=NULL; if (temp->id!=0) { if (head==NULL) { head=temp; pthis=head; } else { pthis->next=temp; pthis=pthis->next; } } } while (temp->id!=0); fclose(fp); return head; } void Dere_list() { struct student *stu_2; char command=0; int id=0; int n; int i; do { printf("nnt 菜单n"); printf("-------------------------------n"); printf("ta,输入数据n"); printf("tc,排列顺序n"); printf("td,插入数据n"); printf("te,查询数据n"); printf("tf,删除记录n"); printf("th,载入文本数据n"); printf("tg,退出系统n"); printf("-------------------------------n"); printf("t请选择:"); command=getch(); printf("n"); //命令处理 switch (command) { case 'a': if (head==NULL) { input(); break; } else { printf("nn数据已经存在!n"); break; } case 'c': list(); break; case 'd': if ((stu_2=(struct student *)malloc(sizeof(struct student)))==NULL)//申请stu_2内存,可以连续输入,以占用上次。 { printf("n错误!不能申请所需的内存!n"); exit(0); } printf("nIDt成绩t姓名t性别t以Tab或者回车输入n"); scanf("%dt%ft%st%s",&stu_2->id,&stu_2->score,stu_2->name,stu_2->sex); if(stu_2->id<0) { printf("输入有误!n"); } print(insert(head,stu_2)); // free(stu_2); break; case 'e': scanf("%d",&i); search(i); //print(head); break; case 'f': printf("请输入学号IDn"); scanf("%d",&n); print(del(n)); break; case 'h': load1(); print(head); } } while (command!='g'); printf("按任意键退出n"); } //程序主函数 void main() { //主循环 Dere_list(); } |
|
相关推荐
2个回答
|
|
没释放内存、有malloc应该要delete (如果没delete,那我可以new多个对象)
|
|
|
|
是‘d’ 那个地方嘛?
|
|
|
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-4-25 06:06 , Processed in 0.576777 second(s), Total 74, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号