为了更好的看懂,我们可以从一个简单的问题循序渐进(代码里都有很多必要的注释)
问题描述:
1编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输入这些记录,用print函数输出这些记录。
第一题代码实现
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include<stdio.h> struct Stu //创建一个结构体类型 { int num; char name[20]; float score[3]; }; void print(struct Stu stu[], int size);//函数声明 int main() { struct Stu stu[5]; //定义一个结构体数组 int i; for (i = 0; i < 5; i++) //接收用户输入的信息 { printf("请按学号,姓名,三门成绩得分顺序输入第%d个学生的信息:n", i + 1); scanf("%d%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]); } print(stu, 5); //调用函数 return 0; } void print(struct Stu stu[], int size)//函数实现 { int i; for (i = 0; i < size; i++) { printf("第%d个学生的信息:", i + 1); printf("学号:%d 姓名:%s 成绩得分:%f %f %fn", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]); } }
运行结果:
2.在第1题的基础上,编写一个函数input,用来输入5个学生的数据记录。
其实就是比上面多了一个函数
代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include<stdio.h> struct Stu //创建一个结构体类型 { int num; char name[20]; float score[3]; }; void print(struct Stu stu[], int size);//函数声明 void input(struct Stu stu[], int size);//函数声明 int main() { struct Stu stu[5];//定义一个结构体数组 input(stu, 5);//调用函数 print(stu, 5);//调用函数 return 0; } void print(struct Stu stu[], int size)//打印函数实现 { int i; for (i = 0; i < size; i++) { printf("第%d个学生的信息:", i + 1); printf("学号:%d 姓名:%s 成绩得分:%f %f %fn", stu[i].num, stu[i].name, stu[i].score[0],stu[i].score[1],stu[i].score[2]); } } void input(struct Stu stu[], int size)//输入函数实现 { int i; for (i = 0; i < size; i++) { printf("请按学号,姓名,三门成绩得分顺序输入第%d个学生的信息:n", i + 1); scanf("%d%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); } }
运行结果:
![]()
3.有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程成绩、平均分数)。
第三就要用到上面两个的结合知识,如果上面两个没问题,那这一个问题也就变得简单
代码实现如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68#include<stdio.h> #define N 10 //宏定义 struct Stu //创建一个结构体类型 { int num; char name[20]; float score[3]; }; float Aversc1, Aversc2, Aversc3,Aversc4;//定义全局用来存储平均分 int n;//用来记录最大的学号 void input(struct Stu stu[], int size);//函数声明 void PutMax(struct Stu stu[], int size);//函数声明 void print(struct Stu stu[]);//函数声明 int main() { struct Stu stu[N];//定义一个结构体数组 input(stu, N);//调用函数 PutMax(stu, N); print(stu); return 0; } void PutMax(struct Stu stu[], int size)//实现求值的主要函数 { float Max = stu[0].score[0] + stu[0].score[1] + stu[0].score[2];//先把第一个学生的成绩总和赋值给Max float tem;//借助tem为中间变量找到最大值 int i; for (i = 0; i < size; i++) { Aversc1 += (stu[i].score[0]) / size; //求第一科平均值 Aversc2 += ( stu[i].score[1]) / size; //求第二科平均值 Aversc3 += (stu[i].score[2]) / size; //求第三科平均值 tem = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];//把一个一个学生的总成绩赋值给tem if (Max < tem) //Max与tem比较,找到总分最大值 { Max = tem; n = i;//找到最大值序数然后赋值给n } Aversc4 = (stu[n].score[0] + stu[n].score[1] + stu[n].score[2]) / 3;//求总分最大的学生的的平均成绩 } } void input(struct Stu stu[], int size)//输入函数实现 { int i; for (i = 0; i < size; i++) { printf("请按学号,姓名,三门成绩得分顺序输入第%d个学生的信息:n", i + 1); scanf("%d%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]); } } void print(struct Stu stu[]) //输出函数实现 { printf("第一门成绩的平均分为%fn", Aversc1); printf("第二门成绩的平均分为%fn", Aversc2); printf("第三门成绩的平均分为%fn", Aversc3); printf("最高分的学生的信息为n学号:%d n姓名:%sn三门成绩分别是:%f %f %fn", stu[n].num, stu[n].name,stu[n].score[0], stu[n].score[1], stu[n].score[2]); printf("三门成绩平均分为:%fn", Aversc4); }
供个人学习使用!
最后
以上就是现实哈密瓜最近收集整理的关于C语言——结构体和结构体数组的定义和使用的全部内容,更多相关C语言——结构体和结构体数组内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复