c语言动态分配输出学生成绩,C语言之:结构体动态分配内存(利用结构体数组保存不超过10个学生的信息,每个学生的信息包括:学号、姓名和三门课(高数、物理和英语 )的成绩和平均分(整型)。)...

题目内容:

利用结构体数组保存不超过10个学生的信息,每个学生的信息包括:学号、姓名和三门课(高数、物理和英语 )的成绩和平均分(整型)。

编写程序,从键盘输入学生的人数,然后依次输入每个学生的学号、姓名和3门课的成绩

然后计算每个学生的平均分

最后按指定格式输出每个学生的平均分

输入格式:

先输入一个整数,表示学生个数

然后每行输入一个学生的信息:学号、姓名和高数、物理及英语成绩

输出格式:

输出每个学生的平均分.printf中请用格式控制串"The average score of the %dth student is %d.\n"

输入样例:

5

1001 Zhang 100 90 80

1002 Wu 93 90 98

1003 Zhu 89 88 87

1004 Hu 90 98 98

1005 Wang 90 98 97

输出样例:

The average score of the 1th student is 90.

The average score of the 2th student is 93.

The average score of the 3th student is 88.

The average score of the 4th student is 95.

The average score of the 5th student is 95.

代码如下:

#define _CRT_SECURE_NO_WARNINGS#include#include#include

structSTU {char ID[20];char Name[20];floatmath;floatphysic;floatenglish;

};intmain()

{intn,i,j,k;floatres;

scanf("%d", &n);if (n > 10) {return 0;

}struct STU *mySTU = (struct STU*)malloc(sizeof(struct STU) *n); //动态内存分配关键 mallocstruct STU* p =mySTU;for (i = 0; i < n; i++)

{

scanf("%s", &p->Name);

scanf("%s", &p->ID);

scanf("%f", &p->math);

scanf("%f", &p->english);

scanf("%f", &p->physic);*(p++);

}struct STU *q =mySTU;for (j = 0; j < n; j++)

{

res= (q->math + q->physic + q->english)/3;

k= (int)res;

printf("The average score of the %dth student is %d.\n", j+1,k );*(q++);

}return 0;

}

原文:https://www.cnblogs.com/Fzeng/p/14189475.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值