之前没空写啦,现在补上,写的应该很详细了。
运行环境vs2019,其他环境运行请对scanf_s等输入函数进行修改,去掉_s。
真实吃土中,求求有钱的大佬打赏一下嘛,救救孩子。
//统计学生信息
#include <stdio.h>
#include<stdlib.h>
//创建信息结构体
struct StudentInfo {
long long int StudentID; //学生ID
char Name[20]; //姓名
int day, month, year; //出生年月
char Sex[5]; //性别
int Grade[5]; //成绩
double Avg; //平均分
};
void printinfo(const struct StudentInfo); //信息输出函数
struct StudentInfo* GetInfo(void); //信息获取函数
int main()
{
int StudentNumber = 0;
printf("Enter the number of student:");
scanf_s("%d", &StudentNumber);
//创建结构体指针和临时变量
struct StudentInfo* stu;
struct StudentInfo temp;
//创建指定数量的结构体
stu = (struct StudentInfo*)malloc(StudentNumber * sizeof(struct StudentInfo));
//获取信息
for (int i = 0; i < StudentNumber; i++) {
stu[i] = *GetInfo();
}
//对结构体平均成绩冒泡排序
for (int i = 0; i < StudentNumber; i++) {
for (int n = 0; n < StudentNumber - 1 - i; n++) {
if (stu[n].Avg < stu[n + 1].Avg) {
temp = stu[n];
stu[n] = stu[n + 1];
stu[n+1] = temp;
}
}
}
//输出行号以及排序后信息
for (int i = 0; i < StudentNumber; i++) {
printf("|%d\t", i+1);
printinfo(stu[i]);
}
free(stu);
}
struct StudentInfo* GetInfo(void) {
struct StudentInfo stu;
printf("Enter the studentid:");
scanf_s("%lld", &stu.StudentID);
getchar(); //去除缓冲区中的回车
printf("Enter the name:");
gets_s(stu.Name);
printf("Enter the brith(yyyy/mm/dd):");
scanf_s("%d/%d/%d", &stu.year, &stu.month, &stu.day);
getchar();
printf("Enter the sex:");
gets_s(stu.Sex);
printf("Enter the five grades:");
scanf_s("%d %d %d %d %d", &stu.Grade[0], &stu.Grade[1], &stu.Grade[2], &stu.Grade[3], &stu.Grade[4]);
stu.Avg = (stu.Grade[0] + stu.Grade[1] + stu.Grade[2] + stu.Grade[3] + stu.Grade[4]) / 5.0; //计算平均分
return &stu; //返回临时指针
}
void printinfo(const struct StudentInfo stu) {
printf("%lld\t%s\t", stu.StudentID, stu.Name);
printf("%d/%d/%d\t", stu.year, stu.month, stu.day);
printf("%s\t", stu.Sex);
for (int n = 0; n < 5; n++) {
printf("%d\t", stu.Grade[n]);
}
printf("\n");
}