第十一次作业
心得:这两道题都是结构体中的题,第六题中还用到了调用函数,所以说各部分的知识都是紧密相连的,第五题中输入的时候需要注意题目中说的顺序问题,排序的问题中用到了交换,第六题我没有得出最后的正确答案,对i相应的知识掌握的还不够完善,需要在后续继续学习相关知识的内容。
#include<stdio.h>
#include<string.h>
struct contact
{
char name[11];
int birth;
char phone[18];
};
int main()
{
struct contact person[10] = { '\0' };
int i, j, N;
struct contact temp;
scanf("%d\n", &N);
for (i = 0; i<N; i++)
{
scanf("%s%d%s", person[i].name, &person[i].birth, person[i].phone);
}
for (i = 0; i < N - 1; i++)
{
for (j = 0; j < N - i - 1; j++)
{
if (person[j].birth > person[j + 1].birth)
{
temp = person[j];
person[j] = person[j + 1];
person[j + 1] = temp;
}
}
}
for (i = 0; i < N; i++)
{
printf("%s %d %s\n", person[i].name, person[i].birth, person[i].phone);
}
return 0;
}第五题:通讯录排序,建立一个通讯录,结构包括:姓名,生日,电话号码,生日又包括年月日,定义一个嵌套结构,输入小于十个人的信息,再按他们的年龄从大到小输入信息
#include <stdio.h>
#define MAXN 5 struct student
{
int num;
char name[5];
int score;
char grade;
};
int set_grade( struct student *p, int n );
int main()
{
struct student stu[MAXN], *ptr;
int n, i, count;
ptr = stu;
scanf("%d\n", &n);
for(i = 0; i < n; i++)
{
scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
}
count = set grade(ptr, n);
printf("The count for failed (<60): %d\n", count);
printf("The grades:\n");
for(i = 0; i < n; i++)
printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
return 0;
}第六题:按等级统计学生的成绩,输入10个学生的学号,姓名,成绩,输出学生的等级和不及格人数,要求调用函数set-grade()