- (꒪ꇴ꒪ ),hello我是祐言
- 博客主页:C语言基础,Linux基础,软件配置领域博主🌍
- 快上🚘,一起学习!
- 送给读者的一句鸡汤🤔:
- 集中起来的意志可以击穿顽石!
- 作者水平很有限,如果发现错误,可在评论区指正,感谢🙏
我们来设计实现一个程序:对学生信息的存储、计算和打印。它可以方便地管理和处理多个学生的成绩数据,并根据平均成绩进行等级评定。
基础有功能:
(1)初始化了一个包含10条学生信息的结构体数组 str,每个学生有自己的姓名、年龄、学号以及语文、数学、英语成绩;
(2)调用 show_array 函数,逐行打印出学生的基本信息,包括姓名、年龄、学号、语文、数学和英语成绩;
(3)调用 average_sum 函数,计算每个学生的平均成绩,并打印出学生的姓名和对应的平均成绩;
(4) 调用 grade_average 函数,根据学生的平均成绩评定等级,并打印出学生的姓名和对应的评级。
效果:
源码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char name[10];
short int age;
int number;
float chinese;
float math;
float english;
float average;
char grade[2];
};
void show_array(struct student *str, int n)
{
for (int i=0; i<n; i++)
{
printf("姓名:%s,年龄:%d,学号:%d,语文:%.1f,数学:%.1f,英语:%.1f\n",
str[i].name, str[i].age, str[i].number, str[i].chinese, str[i].math, str[i].english);
}
}
float average_sum(struct student *str,int n)
{
for(int i = 0;i<n;i++){
float average = (str[i].chinese + str[i].math + str[i].english)/3;
str[i].average = average;
}
for (int i=0; i<n; i++)
{
printf("姓名:%s,对应平均分:%.1f \n", str[i].name, str[i].average);
}
}
char grade_average(struct student *str,int n)
{
for(int i = 0;i<n;i++){
if(str[i].average >= 90){
strcpy(str[i].grade, "A");
}
else if(str[i].average >= 80 && str[i].average < 90){
strcpy(str[i].grade, "B");
}
else if(str[i].average >= 70 && str[i].average < 80){
strcpy(str[i].grade, "C");
}
else if(str[i].average >= 60 && str[i].average < 70){
strcpy(str[i].grade, "D");
}
else{
strcpy(str[i].grade, "E");
}
}
for (int i=0; i<n; i++)
{
printf("姓名:%s,对应评级:%s \n", str[i].name, str[i].grade);
}
}
int main(void)
{
struct student str[50] ={
{.name = "小苏",.age = 22,.number = 7164101,.chinese = 88,.math = 94.5,.english = 77},
{.name = "小赵",.age = 21,.number = 7164102,.chinese = 76,.math = 89.5,.english = 95},
{.name = "小钱",.age = 20,.number = 7164103,.chinese = 95,.math = 91.5,.english = 78.5},
{.name = "小孙",.age = 23,.number = 7164104,.chinese = 67,.math = 75.5,.english = 75},
{.name = "小李",.age = 21,.number = 7164105,.chinese = 68,.math = 64.5,.english = 86},
{.name = "小王",.age = 22,.number = 7164106,.chinese = 77,.math = 75,.english = 83},
{.name = "小张",.age = 22,.number = 7164107,.chinese = 81,.math = 91.5,.english = 71.5},
{.name = "小陈",.age = 21,.number = 7164108,.chinese = 85,.math = 90,.english = 66},
{.name = "小杨",.age = 23,.number = 7164109,.chinese = 97,.math = 86.5,.english = 97},
{.name = "小吴",.age = 21,.number = 7164110,.chinese = 93,.math = 95,.english = 68},
};
printf("-----学生名单-----\n");
show_array(str, 10);
printf("-------均分-------\n");
average_sum(str, 10);
printf("-------评级-------\n");
grade_average(str, 10);
return 0;
}
📢写在最后
- 今天的分享就到这啦~
- 觉得博主写的还不错的烦劳
一键三连喔
~ - 🎉感谢关注🎉