本帖最后由 我叫MD 于 2019-6-5 22:58 编辑
#include <stdio.h>
#include <stdlib.h>
/*
12.编写程序,用一个数组存放学生信息,每个学生信息包括学号,姓名,三门课程的成绩。从键盘输入10个数据,要求:
1.输出每门课程的平均成绩
2.输出每个学生的总分和平均成绩
3.输出平均分最高的学生数据
4.输出每门课程高于平均成绩的学生人数
5.输出每个学生第一门课程与平均成绩的分差
6.输出所有课程分数最高的学生
7.输出每门课程分数最高的学生
8.输出第一门课程不及格的学生
*/
//先创建学生的结构体
typedef struct Student
{
unsigned int stu_num;
int language;
int math;
int english;
char name[20];
}Student;
//先录入10个学生的信息
void inputinfo(Student *student)
{
for (int i = 0; i < 10; i++)
{
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &student[i].stu_num);
printf("姓名:");
scanf("%s", student[i].name);
printf("语文成绩:");
scanf("%d", &student[i].language);
printf("数学成绩:");
scanf("%d", &student[i].math);
printf("外语成绩:");
scanf("%d", &student[i].english);
}
}
//返回语文课程的平均成绩
float language_sco(Student *student)
{
float language = 0;
for (int i = 0; i < 10; i++)
{
language += student[i].language;
}
return language / 10;
}
//返回数学课程的平均成绩
float math_sco(Student *student)
{
float math = 0;
for (int i = 0; i < 10; i++)
{
math += student[i].math;
}
return math / 10;
}
//返回英语课程的平均成绩
float english_sco(Student *student)
{
float english = 0;
for (int i = 0; i < 10; i++)
{
english += student[i].english;
}
return english / 10;
}
//返回学生的总分
float* stu_all_soc(Student *student)
{
float student_allsocre[10] = { 0 };
for (int i = 0; i < 10; i++)
{
student_allsocre[i] = student[i].language + student[i].math + student[i].english;
}
return student_allsocre;
}
//返回学生的平均分
float* stu_ave_sco(Student *student)
{
float student_avesocre[10] = { 0 };
float *student_avesoc = stu_all_soc(student);
for (int i = 0; i < 10; i++)
{
student_avesocre[i] = *(student_avesoc + i) / 3;
}
return student_avesocre;
}
//3.输出平均分最高的学生数据
void max_ave_sco(Student *student)
{
float *ave_sco = stu_ave_sco(student);
int max = 0;
for (int j = 1; j < 10; j++)
{
if (ave_sco[max] < ave_sco[j])
{
max = j;
}
}
//ave_sco[max];
printf("平均分最高的学生\n");
printf("姓名:%s\n",student[max].name);
printf("语文成绩:%d\n", student[max].language);
printf("数学成绩:%d\n", student[max].math);
printf("英语成绩:%d\n", student[max].english);
}
//4.输出每门课程高于平均成绩的学生人数
void four(Student *student)
{
int language_ave = language_sco(student);
int math_ave = math_sco(student);
int english_ave = english_sco(student);
int language_num = 0, math_num = 0, english_num = 0;
for (int i = 0; i < 10; i++)
{
if (student[i].language > language_ave)
{
language_num++;
}
if (student[i].math > math_ave)
{
math_num++;
}
if (student[i].english > english_ave)
{
english_num++;
}
}
printf("语文高于平均成绩的学生人数是:%d\n", language_num);
printf("数学高于平均成绩的学生人数是:%d\n", math_num);
printf("英语高于平均成绩的学生人数是:%d\n", english_num);
}
int main()
{
Student student[10];
inputinfo(student);
max_ave_sco(student);
four(student);
system("pause");
return 0;
}
我把目前的所有代码赋值上来了,不好意思,新手,写代码杂乱不规范,可能不利于阅读,主要看下返回学生的平均分那段代码就好了,其他的代码和问的问题不关联 |