第九章 使用结构体类型处理组合类型(习题)
1、定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
#include <stdio.h> struct Date{ int year; int month; int day; }; int main(){ struct Date date; printf("Please give date: "); scanf("%d%d%d", &date.year, &date.month, &date.day); int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int i, days = 0; for (i = 1; i < date.month; i++) { days += Days[i]; } days += date.day; //如果包含闰年的二月,天数加1 if(date.month > 2) { if (date.year%400 == 0 || (date.year%4 == 0 && date.year%100 != 0)){ ++days; } } printf("It's day %d in the year.\n", days); return 0; }
2、写一个函数days,实现第1 题的计算。由主函数将年、月、日传递给days函数,计算后将日子数传回主函数输出。
#include <stdio.h> struct Date{ int year; int month; int day; }; int Days(struct Date date) { static int Days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int i, days = 0; for (i = 1; i < date.month; i++) days += Days[i]; days += date.day; //如果包含闰年的二月,天数加1 if (date.month > 2) { if (date.year % 400 == 0 || (date.year % 4 == 0 && date.year % 100 !=0)) { ++days; } } return days; } int main(){ struct Date date; printf("Please give date: "); scanf("%d%d%d", &date.year, &date.month, &date.day); int days = Days(date); printf("It's day %d in the year.\n", days); return 0; }
3、编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输人这些记录,用print函数输出这些记录。
#include <stdio.h> #define N 5 struct student { int num; char name[20]; int score[3]; }students[N]; // students类型的数组 void print(struct student *p) // 定义函数,p作为形参接收students的首元素的地址,p和students指向同一内存空间 { int i; printf("学号\t姓名\t分数\t分数2\t分数3\n"); for(i=0;i<N;i++) { printf("%d\t%s\t%d\t%d\t%d\n", students[i].num, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]); } } int main() { int i; for(i=0;i<N;i++) { printf("请输入第%d个学生的信息:\n",i+1); scanf("%d%s%d%d%d", &students[i].num, &students[i].name, &students[i].score[0], &students[i].score[1], &students[i].score[2]); } print(students); // 将students数组首元素的地址作为函数实参 return 0; }
4、在第3题的基础上,编写一个函数input,用来输人5个学生的数据记录。
#include <stdio.h> #define N 5 struct student { int num; char name[20]; int score[3]; }students[N]; // students类型的数组 void input(struct student *p) // 定义函数,p作为形参接收students数组首元素的地址,p和students指向同一内存空间 { int i; for(i=0;i<N;i++) { printf("请输入第%d个学生的信息:\n",i+1); scanf("%d%s%d%d%d", &students[i].num, &students[i].name, &students[i].score[0], &students[i].score[1], &students[i].score[2]); } } void print(struct student *p) { int i; printf("学号\t姓名\t分数\t分数2\t分数3\n"); for(i=0;i<N;i++) { printf("%d\t%s\t%d\t%d\t%d\n", students[i].num, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]); } } int main() { input(students); print(students); // 将students数组首元素的地址作为函数实参 return 0; }
6、个人围成一圈,从第1个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子中的人原来的序号。要求用链表实现。
#include <stdio.h> #define NUM 13 typedef struct people { int num; struct people *next; } people; int main() { people p[NUM]; // 建立环状链表 people *head; head = p; // head 指向p[0] int i; for (i= 0; i < NUM; i++) // 1~13编号 { head->num = i + 1; head->next = &p[i + 1]; head = head->next; } p[NUM - 1].next = p; // 最后一个元素指向第一个元素 ,形成环 // 开始报数 int count = NUM; i = 1; head = p; while (count > 1) { //跳过已经被淘汰的节点 if (head->num == 0) { head = head->next; continue; } if (i == 3) { //被淘汰的节点,num置为0 printf("第 %d 位置被淘汰\n", head->num); head->num = 0; count--; } head = head->next; i++; if (i > 3) // 如果为数到3则该人退出并重新开始算 { i = 1; } } printf("--------------\n"); while (head->num == 0) { head = head->next; if (head->num != 0) //非0节点即为最后留下的 { printf("留到最后的是 %d \n", head->num); } } return 0; }
7、建立由3个学生数据结点构成的单向动态链表,向每个结点输人学生的数据(每个学生的数据包括学号、姓名、成绩)。然后逐个输出各结点中的数据。
#include <stdio.h> #include <malloc.h> #define LEN sizeof(struct student) struct student { int num; char name[20]; float score; struct student *next; }; int main() { struct student *head,*p,*s; head=p=(struct student*)malloc(LEN); scanf("%d%s%f",&p->num,&p->name,&p->score); p=(struct student*)malloc(LEN); scanf("%d%s%f",&p->num,&p->name,&p->score); s=(struct student*)malloc(LEN); scanf("%d%s%f",&s->num,&s->name,&s->score); head->next=p; p->next=s; s->next=NULL; p=head; printf("\n结点1:%d %s %6.2f\n",p->num,p->name,p->score); p=p->next; printf("结点2:%d %s %6.2f\n",p->num,p->name,p->score); p=p->next; printf("结点3:%d %s %6.2f\n",p->num,p->name,p->score); return 0; }

浙公网安备 33010602011771号