// TheChapterOfEleven.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
//1、
struct stu_data {
int num;
char name[20];
float chinese_score;
float math_score;
float english_score;
};
void print_stu_data();
//2、
typedef struct date{
int year;
int month;
int day;
}my_date;
void get_the_day();
int main()
{
//1、
//print_stu_data();
//2、
//get_the_day();
}
/*1、有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘上输入5个学生的数据
* 要求输出3门课程的总平均成绩,以及最高分的学生数据(包括学号、姓名,3门课程成绩,平均分数)。
*/
void print_stu_data() {
struct stu_data stu[5];
printf("请输入5个学生的数据:\n");
for (int i = 0; i < 5; i++) {
printf("第%d个学生,依次输入学号 姓名 语文成绩 数学成绩 英语成绩:\n", i+1);
scanf_s("%d", &stu[i].num);
scanf_s("%s", &stu[i].name, 20);
scanf_s("%f", &stu[i].chinese_score);
scanf_s("%f", &stu[i].math_score);
scanf_s("%f", &stu[i].english_score);
}
printf("学号\t姓名\t语文\t数学\t英语\n");
for (int i = 0; i < 5; i++) {
printf("%d\t%s\t%.2f\t%.2f\t%.2f\n", stu[i].num, stu[i].name,
stu[i].chinese_score, stu[i].math_score, stu[i].english_score);
}
float chinese_average = 0, math_average = 0, english_average = 0;
float max_score = 0;
int max_index = 0;
for (int i = 0; i < 5; i++) {
chinese_average += stu[i].chinese_score;
math_average += stu[i].math_score;
english_average += stu[i].english_score;
float temp_score = stu[i].chinese_score + stu[i].math_score + stu[i].english_score;
if (max_score < temp_score) {
max_score = temp_score;
max_index = i;
}
}
chinese_average = chinese_average / 5;
math_average = math_average / 5;
english_average = english_average / 5;
printf("语文平均分:%.2f\t数学平均分:%.2f\t英语平均分:%.2f\n", chinese_average,
math_average, english_average);
printf("最高分的学生信息:\n学号\t姓名\t语文\t数学\t英语\t平均分\n");
printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", stu[max_index].num, stu[max_index].name,
stu[max_index].chinese_score, stu[max_index].math_score, stu[max_index].english_score,
max_score/3);
}
//2、定义一个结构体变量(包括年月日),计算某日在这一年中是第几天。(注意闰年问题)
//通常所说:四年一闰,百年不闰,四百年再闰。 例如:2000年是闰年,2100年则是平年。
void get_the_day() {
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
my_date date;
date.year = 2021;
date.month = 11;
date.day = 19;
//如果是闰年,则二月为29天
if ((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0) {
days[1] = 29;
}
int the_day = date.day;
for (int i = 0; i < date.month - 1; i++) {
the_day += days[i];
}
printf("%d年%d月%d日是这一年中的第%d天。", date.year, date.month, date.day, the_day);
}
课后练习--结构体
最新推荐文章于 2023-06-01 11:54:23 发布