北航 计算机软件基础 作业讲解

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1、编写一程序,输入全班40名同学的姓名(字符串)、学号、语文成绩、数学成绩、外语成绩;保存在结构体数组中。然后对此结构体数组排序,使得排序后的结构体数组中的元素按学生平均成绩非递增有序。#includestdio.h#includestring.h#defineNUM40//定义NUM代表全班人数structSTUDENT{charname[20];intnumber;floatchinese,math,english;floataverage;}student[NUM];//定义结构体数组typedefstructSTUDENTS;//声明一个结构体变量voidsort(Ss[NUM])//冒泡法进行排序{inti,j;chartemp1[20];inttemp2;floattemp3,temp4,temp5,temp6;for(i=0;iNUM;i++)for(j=0;jNUM-i;j++)if(s[j].averages[j+1].average){strcpy(temp1,s[j].name);strcpy(s[j].name,s[j+1].name);strcpy(s[j+1].name,temp1);temp2=s[j].number;s[j].number=s[j+1].number;s[j+1].number=temp2;temp3=s[j].chinese;s[j].chinese=s[j+1].chinese;s[j+1].chinese=temp3;temp4=s[j].math;s[j].math=s[j+1].math;s[j+1].math=temp4;temp5=s[j].english;s[j].english=s[j+1].english;s[j+1].english=temp5;temp6=s[j].average;s[j].average=s[j+1].average;s[j+1].average=temp6;}}voidmain(){inti;for(i=0;iNUM;i++){printf(请的输入第%d个学生学生的信息:\n,i+1);printf(name:);scanf(%s,&student[i].name);printf(number:);scanf(%d,&student[i].number);printf(chinese:);scanf(%f,&student[i].chinese);printf(math:);scanf(%f,&student[i].math);printf(english:);scanf(%f,&student[i].english);student[i].average=(student[i].chinese+student[i].math+student[i].english)/3;}printf(姓名学号语文数学英语平均\n);for(i=0;iNUM;i++){printf(%s%d%5.2f%5.2f%5.2f%5.2f\n,student[i].name,student[i].number,student[i].chinese,student[i].math,student[i].english,student[i].average);}sort(student);printf(排序结果为:\n);printf(姓名学号语文数学英语平均\n);for(i=0;iNUM;i++){printf(%s%d%5.2f%5.2f%5.2f%5.2f\n,student[i].name,student[i].number,student[i].chinese,student[i].math,student[i].english,student[i].average);}}图12、编写一程序,输入全班N名同学的姓名、学号、语文成绩、数学成绩、外语成绩。然后按照学生平均成绩非递增排序。程序需能方便地进行插入、删除等操作。#includestdio.h#includestdlib.h#includestring.htypedefstructnode//定义一个结构体并声明结构体类型{charname[20];intnumber;floatchinese,math,english;floataverage;structnode*Link;}Node;typedefNode*List;//声明结构类类型的指针Node*NewNode()//生成新节点{Node*p=(Node*)malloc(sizeof(Node));p-Link=NULL;returnp;}ListBuildList(intn)//建立空链表{Node*p,*r=NULL,*first=NULL;inti;for(i=0;in;i++){p=NewNode();if(first!=NULL)r-Link=p;elsefirst=p;r=p;}returnfirst;}ListSort(ListSL)//冒泡法进行排序{Listp,q;chartemp1[20];inttemp2;floattemp3,temp4,temp5,temp6;for(p=SL;p!=NULL;p=p-Link){for(q=p-Link;q!=NULL;q=q-Link){if(p-averageq-average){strcpy(temp1,q-name);strcpy(q-name,p-name);strcpy(p-name,temp1);temp2=q-number;q-number=p-number;p-number=temp2;temp3=q-chinese;q-chinese=p-chinese;p-chinese=temp3;temp4=q-math;q-math=p-math;p-math=temp4;temp5=q-english;q-english=p-english;p-english=temp5;temp6=q-average;q-average=p-average;p-average=temp6;}}}returnSL;}voidmain(){Node*LS,*p;intn;printf(thenumberofstudentis\n);scanf(%d,&n);LS=BuildList(n);p=LS;for(;p;p=p-Link){printf(thenameis);scanf(%s,&p-name);printf(thenumberis);scanf(%d,&p-number);printf(thechineseis);scanf(%f,&p-chinese);printf(themathis);scanf(%f,&p-math);printf(theenglishis);scanf(%f,&p-english);p-average=(p-chinese+p-math+p-english)/3;}for(p=LS;p;p=p-Link){printf(namenumberchinesemathenglishaverage\n);printf(%s%5d%5.2f%5.2f%5.2f%5.2f\n,p-name,p-number,p-chinese,p-math,p-english,p-average);}LS=Sort(LS);printf(排序后的结果为:\n);for(p=LS;p;p=p-Link){printf(namenumberchinesemathenglishaverage\n);printf(%s%5d%5.2f%5.2f%5.2f%5.2f\n,p-name,p-number,p-chinese,p-math,p-english,p-average);}}图23、实现约瑟夫环#includestdio.h#includestdlib.hstructnode//定义一个结构体{intnum;intcode;structnode*Link;};typedefstructnodeNODE;//声明一个结构体类型NODE*createlinklist(intn)//建立链表{NODE*head,*p,*q;inti=1;head=p=(structnode*)malloc(sizeof(structnode));p-num=i;p-code=rand()%100;p-Link=NULL;for(i=2;i=n;i++){q=(structnode*)malloc(sizeof(structnode));q-Link=NULL;if(q==0)return(0);p-Link=q;p=q;p-num=i;p-code=rand()%100;}p-Link=head;//使链表尾指向链表头形成循环链表returnhead;}voidprintlinklist(NODE*p,intn)//输出链表{inti;NODE*q=p;if(NULL==q-Link){printf(thelistisNULL!);return;}printf(所有玩家的信息列表:\n);for(i=1;i=n;i++){if(NULL==q){printf(thelistisNULL!);return;}printf(%d%d\n,p-num,p-code);p=p-Link;}}voidjoseph(NODE*p,intn,intm)//实现约瑟夫环{inti,j;NODE*q;for(i=1;in;i++){for(j=1;j=m-1;j++){p=p-Link;}q=p-Link;p-Link=q-Link;printf(%d%d\n,q-num,q-code);free(q);}printf(\n最后剩余的是第%d号的密码是%d.\n,p-num,p-code);p-Link=NULL;}voidmain(){NODE*head;intn,m;printf(请输入人数N:\n);scanf(%d,&n);printf(输入K:\n);scanf(%d,&m);head=createlinklist(n);printlinklist(head,n);printf(依次被选出的是:\n);joseph(head,n,m);}图34、实现多项式的表示与三则运算(+,-,×)#includestdio.h#includestdlib.h#includemath.htypedefstructnode{//定义并声明一个结构体变量intcoe;intexp;structnode*Link;}Node;typedefNode*List;//声明一个结构体指针Node*NewNode()//生成一个新节点{Node*p=(Node*)malloc(sizeof(Node));p-Link=NULL;returnp;}ListBuildList(intn)//生成链表{Node*p,*r=NULL,*first=NULL;inti;for(i=0;in;i++){p=NewNode();if(first!=NULL)r-Link=p;elsefirst=p;r=p;}returnfirst;}voidprintList(Node*p)//多项式显示{for(;p;p=p-Link){printf(%dx^%d,p-coe,p-exp);if(p-Link)printf(+);}printf(\n);}ListSort(ListSL)//多项式排序{Listp,q;inttemp1,temp2;for(p=SL;p!=NULL;p=p-Link){for(q=p-Lin

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功