C //习题 8.15 有一个班4个学生,5门课程。1. 求第1门课程的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩

C程序设计 (第四版) 谭浩强 习题8.15

习题 8.15 有一个班4个学生,5门课程。1. 求第1门课程的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

 

代码块
方法1:使用指针,函数指针,动态分配内存
#include <stdio.h>
#include <stdlib.h>

#define M 4
#define N 5

//初始化学生成绩和平均成绩
void initialStu(float ***stu, int m, int n, float **aver){
	*stu = (float**)malloc(m * sizeof(float*));
	for(int i = 0; i < m; i++){
		(*stu)[i] = (float*)malloc(n * sizeof(float));
	}
	*aver = (float*)malloc(m * sizeof(float));
}

//输入学生成绩
void inputStu(float **stu, int m, int n, float *aver){
	printf("Enter the scores of %d students in %d courses:\n\n", n, m);
	for(int i = 0; i < m; i++){
		for(int j = 0; j < n; j++){
			printf("Enter No.%d student No.%d course score: ", i + 1, j + 1);
			scanf("%f", &stu[i][j]);
			while(stu[i][j] < 0 || stu[i][j] > 100){
				printf("Score Error! Retry!\nEnter No.%d student No.%d course score: ", i + 1, j + 1);
				scanf("%f", &stu[i][j]);
			}
		}
	}
}

//计算每名学生平均成绩并存入aver数组
void averageScoreStu(float **stu, int m, int n, float *aver){
	for(int i = 0; i < m; i++){
		float sum = 0.0;
		for(int j = 0; j < n; j++){
			sum += stu[i][j];
		}
		aver[i] = sum / n;
	}
}

//第一门课程平均成绩
void averageFirstCourse(float **stu, int m, int n, float *aver){
	float sum = 0.0;
	for(int i = 0; i < m; i++){
		sum += stu[i][0];
	}
	printf("\nThe average score of No.1 course is %.2f\n\n", sum / m);
}

//两门以上课程不及格
void twoCourseFailed(float **stu, int m, int n, float *aver){
	int sign = 0;
	printf("Students who have failed more than two courses are:\n");
	for(int i = 0; i < m; i++){
		int count = 0;
		for(int j = 0; j < n; j++){
			if(stu[i][j] < 60){
				count++;
			}
		}
		if(count >= 2){
			sign = 1;
			printf("No.%d student: ", i + 1);
			for(int k = 0; k < n; k++){
				printf("%-6.2f ", stu[i][k]);
			}
			printf("Average Score: %.2f\n", aver[i]);
		}
	}
	if(sign == 0){
		printf("None!\n");
	}
	printf("\n");
}

//平均成绩在90分以上或全部课程成绩在85分以上
void highScoreStu(float **stu, int m, int n, float *aver){
	printf("Students with average scores above 90 and scores above 85 in all courses are:\n");
	for(int i = 0; i < m; i++){
		int count = 0;
		for(int j = 0; j < n; j++){
			if(stu[i][j] > 85){
				count++;
			}
		}
		if(aver[i] > 90 || count == n){
			printf("No.%d student.\n", i + 1);
		}
	}
	printf("\n");
}

//释放分配内存
void freeStu(float ***stu, int m, int n, float **aver){
	for(int i = 0; i < m; i++){
		free((*stu)[i]);
	}
	free(*stu);
	free(*aver);
}

//通过函数指针调用不同函数
void function(float **stu, int m, int n, float *aver, void (*fun[])(float**, int, int, float*)){
	for(int i = 0; i < 5; i++){
		fun[i](stu, m, n, aver);
	}
}

int main(){
	float **stu = NULL;
	float *aver = NULL;

	void (*fun[5])(float**, int, int, float*) = {inputStu, averageScoreStu, averageFirstCourse, twoCourseFailed, highScoreStu};

	initialStu(&stu, M, N, &aver);
	function(stu, M, N, aver, fun);
	freeStu(&stu, M, N, &aver);

	system("pause");
    return 0;
}
方法2:使用结构体
#include <stdio.h>
#include <stdlib.h>

#define M 4
#define N 5

struct Student{
	int number;
	float score[N];
	float aver;
	int twoCourseFailedSign;
	int highScoreSign;
};

void initialStu(Student **stu, int m){
	*stu = (Student*)malloc(m * sizeof(Student));
}

void inputStu(Student *stu, int m, int n){
	printf("Enter the scores of %d students in %d courses:\n", n, m);
	for(int i = 0; i < m; i++){
		int countFailed = 0;
		int countHigh = 0;
		float sum = 0.0;
		printf("\nEnter No.%d student number: ", i + 1);
		scanf("%d", &stu[i].number);
		while(stu[i].number < 100 || stu[i].number > 999){
			printf("Number Error! Retry!\nEnter No.%d student number: ", i + 1);
			scanf("%d", &stu[i].number);
		}
		for(int j = 0; j < n; j++){
			printf("Enter No.%d student No.%d course score: ", i + 1, j + 1);
			scanf("%f", &stu[i].score[j]);
			while(stu[i].score[j] < 0 || stu[i].score[j] > 100){
				printf("Score Error! Retry!\nEnter No.%d student No.%d course score: ", i + 1, j + 1);
				scanf("%f", &stu[i].score[j]);
			}
		}
	}
}

void averageScoreAndTwoFailedAndHighScore(Student *stu, int m, int n){
	for(int i = 0; i < m; i++){
		int countFailed = 0;
		int countHigh = 0;
		float sum = 0.0;
		for(int j = 0; j < n; j++){
			sum += stu[i].score[j];
			if(stu[i].score[j] < 60){
				countFailed++;
			}
			if(stu[i].score[j] > 85){
				countHigh++;
			}
		}
		stu[i].aver = sum / n;
		if(countFailed >= 2){
			stu[i].twoCourseFailedSign = 1;
		}
		else{
			stu[i].twoCourseFailedSign = 0;
		}
		if(countHigh == n || stu[i].aver > 90){
			stu[i].highScoreSign = 1;
		}
		else{
			stu[i].highScoreSign = 0;
		}
	}
}

void averageFirstCourse(Student *stu, int m, int n){
	float sum = 0.0;
	for(int i = 0; i < m; i++){
		sum += stu[i].score[0];
	}
	printf("\nThe average score of No.1 course is %.2f\n\n", sum / m);
}

void twoCourseFailed(Student *stu, int m, int n){
	printf("Students who have failed more than two courses are:\n");
	int sign = 0;
	for(int i = 0; i < m; i++){
		if(stu[i].twoCourseFailedSign == 1){
			sign = 1;
			printf("Student Number: %d Course Score: ", stu[i].number);
			for(int j = 0; j < n; j++){
				printf("%-5.2f ", stu[i].score[j]);
			}
			printf("Average Score: %.2f\n", stu[i].aver);
		}
	}
	if(sign == 0){
		printf("None!\n");
	}
	printf("\n");
}

void highScoreStu(Student *stu, int m, int n){
	printf("Students with average scores above 90 and scores above 85 in all courses are:\n");
	int sign = 0;
	for(int i = 0; i < m; i++){
		if(stu[i].highScoreSign == 1){
			sign = 1;
			printf("Student Number: %d\n", stu[i].number);
		}
	}
	if(sign == 0){
		printf("None!\n");
	}
	printf("\n");
}


void function(Student *stu, int m, int n, void (*fun[])(Student*, int, int)){
	for(int i = 0; i < 5; i++){
		fun[i](stu, m, n);
	}
}

int main(){
	Student *stu = NULL;

	void (*fun[5])(Student*, int, int) = {inputStu, averageScoreAndTwoFailedAndHighScore, averageFirstCourse, twoCourseFailed, highScoreStu};

	initialStu(&stu, M);
	function(stu, M, N, fun);
	free(stu);

	system("pause");
    return 0;
}
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值