这是我今天的编程题作业第一题,大家批评指点一下
/*
2.某班有5个学生,三门课。分别编写3个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
*/
#include <stdio.h>
typedef struct Student{
char name[20];
int scoreOne;
int scoreTwo;
int scoreThr;
int num;
}Stu;
/**
* @author pC, 15-06-02 19:06:32
*
* 求各门课的平均分
*
* @param st 学生结构体数组
* @param len 数组成员数
* @param avg 平均值指针
*/
void text(Stu *st,int len,float avg[])
{
int sum[3] = {0};
for (int i = 0; i < len; i++) {
sum[0] += (st+i) -> scoreOne;
sum[1] += (st+i) -> scoreTwo;
sum[2] += (st+i) -> scoreThr;
}
avg[0] = sum[0]/len;
avg[1] = sum[1]/len;
avg[2] = sum[2]/len;
}
/**
* @author pC, 15-06-02 18:06:48
*
* 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
*
* @param st 结构体数组
* @param len 数组成员数
*/
void text2(Stu *st,int len)
{
printf("--------------------\n");
printf("两门以上不及格的学生有:\n");
for (int i = 0; i<len; i++) {
// 定义判断标记
int flag = -2,score1 ,score2,score3;
score1 = score2 = score3 = 0;
if ( (st+i) -> scoreOne < 60) {
flag ++;
score1 = 1;
}
if ( (st+i) -> scoreTwo < 60) {
flag ++;
score2 = 1;
}
if ( (st+i) -> scoreThr < 60) {
flag ++;
score3 = 1;
}
while ( flag >= 0) {
printf("学号:%d\n",(st+i) -> num);
while ( score1 ) {
printf("scoreOne:%d\n",(st+i) -> scoreOne);
break;
}
while ( score2 ) {
printf("scoreTwo:%d\n",(st+i) -> scoreTwo);
break;
}
while ( score3 ) {
printf("scoreThr:%d\n",(st+i) -> scoreThr);
break;
}
break;
}
}
}
/**
* @author pC, 15-06-02 18:06:48
*
* 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
*
* @param st 结构体数组
* @param len 数组成员数
*/
void text3(Stu *st,int len)
{
printf("--------------------\n");
printf("三门课平均成绩在85-90分的学生有:\n");
int avg = 0;
for (int i = 0; i<len; i++) {
avg = ( (st+i) -> scoreOne + (st+i) -> scoreThr + (st+i) -> scoreTwo )/3;
if (avg >= 85 && avg <= 90) {
printf("%d--%s\n",(st+i) ->num,(st+i) ->name);
}
}
}
int main()
{
int len;
Stu stu[] ={
{ "Jim",85,97,83,1},
{ "Jack",85,87,87,2},
{ "Tom",59,79,66,3},
{ "Lily",55,97,57,4},
{ "Amy",43,69,58,5}
};
len = sizeof(stu)/sizeof(Stu);
float avg[3] = {0};
text(stu, len, avg);
printf("scoreOne:%.2f \nscoreTwo:%.2f \nscoreThr:%.2f\n",avg[0],avg[1],avg[2]);
text2(stu, len);
text3(stu, len);
return 0;
} |