当前位置:编程学习 > C/C++ >>

求一c语言大作业 不少于300行

答案:#include <stdio.h> #include <string.h> #include <stdlib.h> #define SN 2 typedef struct student { char num[10], name[10]; float score[SN], sum, avg; struct student *next; }STU; /**********输入链表单元内容************/ void input(STU *p) { int i; printf("please input number:\n"); scanf("%s",p->num); printf("please input name:\n"); scanf("%s",p->name); printf("please input %d scores:\n",SN); p->sum=0; for(i=0;i<SN;i++) { scanf("%f",&p->score[i]); p->sum+=p->score[i]; } p->avg=p->sum/SN; } /**********创建一个链表单元**********/ STU *creat_node() { STU *p; p=(STU *)malloc(sizeof(STU)); if(p == NULL) { printf("No enough memory !"); exit(0); } input(p); p->next=NULL; return p; } /**********创建一个链表**********/ STU *creat_list() { STU *head=NULL,*tail=NULL,*p; char str[4]; printf("List creating...\n"); do { printf("Do you want to continue (yes/no) :"); scanf("%s",str); if(strcmp(str,"yes")==0) { p=creat_node(); if(head==NULL){head=tail=p;continue;} tail->next=p; tail=p; } if(strcmp(str,"yes")!=0&&strcmp(str,"no")!=0) { printf("You must input 'yes' or 'no'.\n"); //getchar(); continue; } if(strcmp(str,"no")==0)break; //getchar(); }while(1); printf("List create end...\n\n"); return head; } /************输出一个链表单元**********/ void print_a_node(STU *fin) { int i; printf("%s;\t%s;\t%0.2f;\t%0.2f\t",fin->num,fin->name,fin->avg,fin->sum); for(i=0;i<SN;i++) printf("%0.2f\t",fin->score[i]); putchar(10); } /************输出一个链表头部**********/ void print_a_head() { int i; printf("number\tname\tavg\tsum\t"); for(i=0;i<SN;i++) printf("score%d\t",i+1); putchar(10); } /************输出操作菜单**********/ void print_menu_list() { printf("======the operation menu list======\n"); printf("[0]-->exit\n[1]-->creat a list\n[2]-->print the list\n[3]-->insert a list node\n[4]-->select by number\n[5]-->select by name\n"); printf("[6]-->delete a list node\n[7]-->update a list node\n[8]-->order the list by score\n[9]-->print the operation menu list\n"); printf("======the operation menu list======\n"); putchar(10); } /************输出链表**********/ int print_list(STU *stu) { STU *p=stu; if(stu==NULL) { printf("no records!!!\n"); return (0); } print_a_head(); while(p!=NULL) { print_a_node(p); p=p->next; } putchar(10); return (0); } /************插入链表单元************/ void insert(STU *stu) { STU *tail=stu,*p; printf("now insert a list node...\n"); while(tail->next!=NULL) { tail=tail->next; } p=creat_node(); tail->next=p; printf("Insert end...\n\n"); } /**********查找链表num**********/ STU *find_num(STU *stu, char num[]) { STU *p=stu,*pr=NULL; while(p!=NULL) { if(strcmp(p->num,num)==0){pr=p;break;} p=p->next; } return pr; } /**********查找链表name**********/ STU *find_name(STU *stu, char name[]) { STU *p=stu,*pr=NULL; while(p!=NULL) { if(strcmp(p->name,name)==0){pr=p;break;} p=p->next; } return pr; } /************删除链表单元************/ STU * delet(STU *stu, char name[]) { STU *p=stu,*front=stu; if((p=find_name(stu,name))!=NULL) { printf("the delete record:\n"); print_a_head(); print_a_node(p); } else { printf("can not find the student!\n"); return stu; } p=stu; while(p!=NULL&&strcmp(p->name,name)!=0) { front=p; p=p->next; } if(p==stu&&front==stu)stu=NULL; else front->next=p->next; if(p!=NULL)p->next=NULL; free(p); printf("delete end...\n\n"); return stu; } /**********更新链表单元**********/ void update(STU *stu, char name[]) { STU *fin; if((fin=find_name(stu,name))!=NULL) { printf("before update:\n"); print_a_head(); print_a_node(fin); } else { printf("can not find the student!\n"); exit(0); } printf("please input the new records now...\n"); input(fin); printf("update end...\n\n"); } /**********链表单元排序**********/ void order(STU *stu) { STU *pi,*pj,*max,temp; int i; if(stu!=NULL&&stu->next!=NULL) { for(pi=stu;pi!=NULL;pi=pi->next) { max=pi; for(pj=pi->next;pj!=NULL;pj=pj->next) { if(max->sum<pj->sum) max=pj; } if(max!=pi) { strcpy(temp.num,max->num); strcpy(max->num,pi->num); strcpy(pi->num,temp.num); strcpy(temp.name,max->name); strcpy(max->name,pi->name); strcpy(pi->name,temp.name); temp.sum=pi->sum; pi->sum=max->sum; max->sum=temp.sum; temp.avg=max->avg; max->avg=pi->avg; pi->avg=temp.avg; for(i=0;i<SN;i++) { temp.score[i]=max->score[i]; max->score[i]=pi->score[i]; pi->score[i]=temp.score[i]; } } } printf("order end...\n\n"); } else printf("do not need to order...\n\n"); } /************释放链表**********/ void fre(STU *stu) { STU *p=stu,*pf; if(stu==NULL) { printf("the list is NULL!\n"); exit(0); } while(p!=NULL) { pf=p; p=p->next; stu=p; pf->next=NULL; free(pf); } if(stu==NULL) printf("free the list.\n"); } STU * menu(STU *stu,int cas) { STU *fin=NULL; char a[10]; switch(cas) { //创建链表 case 1: if(stu!=NULL)fre(stu); stu=creat_list(); break; //输出链表 case 2: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} print_list(stu); break; //插入链表单元 case 3: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} insert(stu); break; //查找输出number case 4: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} printf("please input the 'number' you want to find:\n"); scanf("%s",a); if((fin=find_num(stu,a))!=NULL) { print_a_head(); print_a_node(fin); } else printf("no found!\n"); break; //查找输出name case 5: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} printf("please input the 'name' you want to find:\n"); scanf("%s",a); if((fin=find_name(stu,a))!=NULL) { print_a_head(); print_a_node(fin); putchar(10); } else printf("no found!\n"); break; //删除链表单元 case 6: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} printf("please input the 'name' you want to delete:\n"); scanf("%s",a); stu=delet(stu,a); break; //更新链表单元 case 7: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} printf("please input the 'name' you want to update:\n"); scanf("%s",a); update(stu,a); break; //链表单元排序 case 8: if(stu==NULL){printf("can not do this operation!\n");putchar(10);break;} printf("order by score\n"); order(stu); break; //打印链表操作菜单 case 9: print_menu_list(); break; default: printf("can not do this operation!\n");putchar(10);break; } return stu; } void main() { STU *stu=NULL; int cas; //打印操作提示 print_menu_list(); //用户操作 do { printf("press 0~9 to choose operation!\n"); scanf("%d",&cas); if(cas<0||cas>9){printf("you must press 0 to 9 !\n");continue;} if(cas!=0)stu=menu(stu,cas); if(cas==0){printf("operation end !\n");putchar(10);} }while(cas!=0); //释放链表 fre(stu); }

上一个:哪位高手能告诉我怎么用c/c++写一个桌面飘雪的程序,或者是QQ宠物那样的小东西
下一个:c语言程序设计。。。。。不能正常读取

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,