希望我的回答能够帮助到你。
#include<stdio.h>#include<malloc.h>
struct Student
{
int num //学号
char name[20] //姓名
struct Score score
}
struct Score
{
float politic
float maths
float english
float special
}
int main()
{
int M,N,i,j
printf("Please input M:")
scanf("%d",&M)
struct Student *stu = NULL
stu = (struct Student*)malloc(sizeof(struct Student)*M) //创建一个结构体含有N个数据
for(i = 0 i < M i++)
{
printf("Please input the No%d student's number:",i+1)
scanf("%d",&stu[i].num)
printf("Please input the No%d student's name:",i+1)
scanf("%s",stu[i].name)
stu[i].score.politic = 0.0 //初始化float 老版本编译器如果不初始化可能会报错
printf("Please input the No%d student' score of",i+1)
scanf("%f",&stu[i].score.politic)
stu[i].score.maths = 0.0
scanf("%f",&stu[i].score.maths)
stu[i].score.english = 0.0
scanf("%f",&stu[i].score.english)
stu[i].score.special = 0.0
scanf("%f",&stu[i].score.special)
}
printf("Please input N:")
scanf("%d",&N)
for(i = 0 i < M i++)
{
if(i+1 == N)
{
peinrf("%d %s %.2f %.2f %.2f %.2f\n",stu[i].num,stu[i].name,
stu[i].score.politic,
stu[i].score.maths,
stu[i].score.english,
stu[i].score.special)
}
}
free(stu) //释放
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)