用C语言编写程序:有五个学生的三门课程的成绩,求每门课程的平均成绩

如题所述

程序设计思路:首先我们需要定义一个学生的结构体,用于存放学生信息;接着是3个方法,一个输入学生信息的方法,一个是计算学生每门课程平均成绩的,最后一个是输出学生所有信息,包括计算好的平均成绩,具体实现代码如下:

#include <stdio.h>

#include <stdlib.h>

#define ARRAY_LEN 100   /*数组长度*/

/*定义学生结构体*/

typedef struct {

int no; /*学号*/

float score1; /*成绩1*/

float score2; /*成绩2*/

float score3; /*成绩3*/

float totalScore; /*总分*/

float averageScore; /*平均分*/

} student;

/*输入学生信息*/

void inputInfo (student stu[], int stuIndex) {

int i;

printf ("第%d名学生↓\n", stuIndex+1);

printf ("学号:");

scanf ("%d",&stu[stuIndex].no);

printf ("成绩1:");

scanf ("%f",&stu[stuIndex].score1);

printf ("成绩2:");

scanf ("%f",&stu[stuIndex].score2);

printf ("成绩3:");

scanf ("%f",&stu[stuIndex].score3);

putchar ('\n');

}

/*计算平均成绩*/

void calculationScore (student stu[], int stuIndex) {

stu[stuIndex].totalScore=stu[stuIndex].score1+stu[stuIndex].score2+stu[stuIndex].score3;

stu[stuIndex].averageScore=stu[stuIndex].totalScore/3; 

} /*输出学生成绩*/

void printInfo (student stu[], int stuIndex) {

int i;

printf ("%d\t",stu[stuIndex].no);

printf ("%.2f\t",stu[stuIndex].score1);

printf ("%.2f\t",stu[stuIndex].score2);

printf ("%.2f\t",stu[stuIndex].score3);

printf ("%.2f",stu[stuIndex].averageScore);

putchar ('\n');

}

int main (void) {

int stuNum=5,i;

student stu[ARRAY_LEN];

/*输入、计算*/

puts ("请输入学生信息:");

putchar ('\n');

for (i=0; i<stuNum; i++) {

inputInfo (stu,i);

calculationScore (stu,i);

}

putchar ('\n');

printf ("%d名学生成绩输入完毕!", stuNum);

putchar ('\n');

puts ("================================================\n");

/*输出*/

puts ("学号\t成绩1\t成绩2\t成绩3\t平均成绩");

for (i=0; i<stuNum; i++)

printInfo (stu,i);

getch (); /*屏幕暂留*/

return 0;

}

程序的运行结果:

扩展资料:

具有相同数据类型的数据我们可以用数组来存放,但对于上面的学生信息,包含多种数据类型,所以只能使用结构体来存放。

结构体的定义形式为:

struct 结构体名{

结构体所包含的变量或数组

};

结构体是一种集合,它里面包含了多个变量或数组,它们的类型可以相同,也可以不同,每个这样的变量或数组都称为结构体的成员(Member)。

结构体成员的定义方式与变量和数组的定义方式相同,只是不能初始化。注意大括号后面的分号;不能少,这是一条完整的语句。结构体也是一种数据类型,它由程序员自己定义,可以包含多个其他类型的数据。

像 int、float、char 等是由C语言本身提供的数据类型,不能再进行分拆,我们称之为基本数据类型;而结构体可以包含多个基本类型的数据,也可以包含其他的结构体,我们将它称为复杂数据类型或构造数据类型。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-28
#include <stdio.h>
#include <stdlib.h>
 
#define ARRAY_LEN 100 /*数组长度*/

typedef struct {
    int no; /*学号*/
    float score1; /*成绩1*/
    float score2; /*成绩2*/
    float score3; /*成绩3*/
float totalScore; /*总分*/
float averageScore; /*平均分*/
} student;

/*输入学生信息*/
void inputInfo (student stu[], int stuIndex) {
    int i;

    printf ("第%d名学生↓\n", stuIndex+1);

    printf ("学号:");
    scanf ("%d",&stu[stuIndex].no);

    printf ("成绩1:");
    scanf ("%f",&stu[stuIndex].score1);
    printf ("成绩2:");
    scanf ("%f",&stu[stuIndex].score2);
    printf ("成绩3:");
    scanf ("%f",&stu[stuIndex].score3);

    putchar ('\n');
}
 
/*计算平均成绩*/
void calculationScore (student stu[], int stuIndex) {

    stu[stuIndex].totalScore =  stu[stuIndex].score1+
                                stu[stuIndex].score2+
                                stu[stuIndex].score3;
    stu[stuIndex].averageScore = stu[stuIndex].totalScore/3; 
}

/*输出学生成绩*/
void printInfo (student stu[], int stuIndex) {
    int i;

    printf ("%d\t",stu[stuIndex].no);

    printf ("%.2f\t",stu[stuIndex].score1);
    printf ("%.2f\t",stu[stuIndex].score2);
    printf ("%.2f\t",stu[stuIndex].score3);
    printf ("%.2f",stu[stuIndex].averageScore);

    putchar ('\n');
}

int main (void) {
    int stuNum=5,i;
    student stu[ARRAY_LEN];
    
    /*输入、计算*/
    puts ("请输入学生信息:");
    putchar ('\n');
    for (i=0; i<stuNum; i++) {
        inputInfo (stu,i);
        calculationScore (stu,i);
    }
    putchar ('\n');
    printf ("%d名学生成绩输入完毕!", stuNum);
    putchar ('\n');
    puts ("================================================================\n");

    /*输出*/
    puts ("学号\t成绩1\t成绩2\t成绩3\t平均成绩");
    for (i=0; i<stuNum; i++)
        printInfo (stu,i);

    getch (); /*屏幕暂留*/
    return 0;
}

运行结果

以下图示改为2名学生,上方源代码为题主要求的5名学生

第2个回答  推荐于2018-02-27
#include <stdio.h>
/*定义结构体*/
struct student
{
int a;
int b;
int c;
float ave;
};
int main()
{
struct student sco[5];
printf("输入5个学生的3们成绩:\n");
for(i=0;i<5;i++)
{
scanf("%d%d%d",&sco[i].a,&sco[i].b,&sco[i].c);
sco[i].ave=(float)(sco[i].a+sco[i].b+sco[i].c)/3;
}
printf("5名学生平均成绩分别为:\n");
for(i=0;i<5;i++)
{
printf("%.2f\n",sco[i].ave);
}
return 0;
}
————————————————
如果以上出现“%”符号,那是为何防止系统误识我把半角符号写成全角符号了,注意改回来。
————————————————
亲爱的LZ,如果我的回答能够帮你解决问题,或是对你有帮助,或是对你今后的发展造成积极的影响,那么请您采纳我的回答吧,同时更迫切地希望您能够在采纳的时候帮我打上“能解决”和“原创”,然后把两行的五颗五角星分别点亮,点亮五角星就是点亮你我的希望。 我冲11级了,需要很多综合声望,感谢您的帮助,衷心祝愿您快乐每一天~本回答被网友采纳
第3个回答  2017-07-28
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int s[3][5];
    int avg1,avg2,avg3;
    long sum[3]={0,0,0};
    int i,j;
    for(j=0;j<3;j++)
    {
         for(i=0;i<5;i++)
         {
            scanf("%d",&s[j][i]);
            sum[j] +=  s[j][i];
         }
    }

    for(j=0;j<3;j++)
        printf("科目%d平均:%d\n",j+1,sum[j]/5);
    return 0;
}

第4个回答  2017-09-13
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
float a;
float b;
float c;
} STUDENT; 

int main()
{
STUDENT s[5];
int i;

for(i=0;i<5;i++)
{
printf("请输入第%d (%d/5)个学生的3门课程成绩,各科成绩间用空格分隔:\n",i+1,i+1); 
fflush(stdin);
scanf("%f %f %f",&s[i].a,&s[i].b,&s[i].c);
}
float avg_a=0.0,avg_b=0.0,avg_c=0.0;
for(i=0;i<5;i++)
{
avg_a+=s[i].a;
avg_b+=s[i].b;
avg_c+=s[i].c;
}
printf("\na课程的平均分为:%.2f   b课程的平均分为:%.2f   c课程的平均分为:%.2f",avg_a/5,avg_b/5,avg_c/5);
return 0;
}

相似回答