第九章 (答案仅供参考),有问题大家可以在评论区一起讨论。
9.1//定义一个结构体变量,包括年、月、日数据。计算该日在本年是第几天,注意闰年问题; #includestruct Data { int year; int month; int day; }; int main() { struct Data a; printf("please input year/month/day:"); scanf("%d/%d/%d",&a.year,&a.month,&a.day); int m[]={31,28,31,30,31,30,31,31,30,31,30,31}; int sum=a.day,i;//a.day表示本月的天数,所以在后边要算总月数时要-1; if(a.year%4==0&&a.year%100!=0||a.year%400==0) { m[1]++; } for(i=0;i
9.2//做一个针对10个学生的简易成绩管理系统。学生信息包括学号、姓名、年龄、三门课成绩。 //功能包括统计不及格的名单并显示,对平时成绩进行从高到底的排序。 #include#include #define N 3 struct STU { char num[11];//学号; char name[21];//姓名; int age;//年龄; float score[3];//三门课成绩; }; void PrintMenu(); void Input(); void fun1();//统计不及格名单; void swap(int i,int j);//对成绩进行由高到底的排序; void fun2();//对成绩进行由高到低的排序;并输出; int flag=0; struct STU stu[N]; int main() { while(1) { PrintMenu();//主菜单; int choose; scanf("%d",&choose); if(!choose)//相当于choose==0; { printf("感谢使用,欢迎下次使用,再见!"); exit(0);//退出程序; } else if(choose==1) { Input(); } else if(choose==2) { fun1(); } else if(choose==3) { fun2(); } else { printf("没有该选项!"); } } return 0; } void PrintMenu() { printf("n欢迎使用简易学生管理系统,请对应下列序号输入你想要的功能,enter结束n"); printf("1 录入学生信息n"); printf("2 统计不及格学生信息n"); printf("3 平时成绩排序n"); printf("0 退出系统n"); } void Input() { int i; for(i=0;i 9.3//有十个学生的信息,包括学号,姓名,年龄,组成结构体数组。将该数组的10个学生数据读出形成链表; #include#include #include #define N 2 struct STU { char num[11];//学号 char name[21];//姓名 int age;//年龄 struct STU *next;//链表指针域 }stu[N]; int main() { int i; struct STU *p,*head; for(i=0;i num,p->name,p->age); p=p->next;//移动指针指向它的下一个指针; } return 0; } 9.4//给定一个链表,每个链表中的结点包括学号,成绩,在其中查找某个学号的学生结点,将成绩替换成指定新成绩; #include#include #include struct STU { char num[11];//学号 float score;//成绩 struct STU *next;//链表指针域; }; struct STU *Create(int n);//创建链表存放学生信息; void print(struct STU *head);//输出; void find(struct STU *head,struct STU stu);//查找某个学号的学生结点; int main() { struct STU *head=NULL,stu; int n; printf("please input student number:"); scanf("%d",&n); // char a[11]; head=Create(n);//创建链表存放学生信息 printf("学号 成绩n"); print(head); printf("please input you want change num:"); scanf("%s %f",stu.num,&stu.score); find(head,stu); printf("修改后的信息n"); print(head); return 0; } struct STU *Create(int n) { int i=0; struct STU *head=NULL,*p1,*p2; while(i next=NULL;//为链表开辟空间;最后一个结点指向空; printf("请输入第%d个学生的信息:",i+1); scanf("%s %f",p1->num,&p1->score); if(head==NULL)//链表为空的情况 { head=p1;//产生第一个结点; p2=p1;//p2指向当前链表的尾结点; } else { p2->next=p1;//p2的指针域指向p1,链接到尾结点p2后; p2=p1;//p2指向新的尾结点; } i++; } return head; } void print(struct STU *head) { struct STU *p; p=head; while(p!=NULL)//当链表存在时,输出; { printf("%s %.2fn",p->num,p->score); p=p->next;//下一个结点; } } void find(struct STU *head,struct STU stu) { struct STU *p; p=head; while(p!=NULL)//链表存在 { //printf("%s %fn",p->num,p->score); // printf("%s %fn",stu.num,stu.score); if(strcmp(p->num,stu.num)==0) { //strcpy(p->num,stu.num);//将要修改的学号复制到指针p所指的结点中; p->score=stu.score; printf("%s %fn",p->num,p->score); } p=p->next; } if(p==NULL) { printf("无该学号n"); } }
可以收藏方便后续观看查找。



