问题十六:每位同学的信息学号、姓名、C++、高数、英语成绩,定义一个学生成绩的结构体数组。

/***************************************************************
                          C语言
   
                                         AUTHOR:liuyongshui
 问题来源:http://blog.csdn.net/sxhelijian/article/details/8620846
 ***************************************************************/
/*
    问题十六:每位同学的信息学号、姓名、C++、高数、英语成绩,定义一个学生成绩的结构体数组,其中的数据成员包括学号(char num[12])、姓名(name)、三门课的成绩(grade)、总分(score)、均分(average))。
            (1)从键盘上输入5名学生的信息;
            (2)求出每名同学的总分和均分,并存储在结构体数组中(可以读入过程中“顺便”计算);
            (3)输出求出每位同学的信息学号、姓名、总分和均分。
*/

#include <stdio.h>    

 struct stu
 {
       char  num[12];
       char  name[15];
       float cpp;
       float english;
       float math;
       float score;
       float average;
 };
void calculate (struct stu scholastic[] );  //计算学生成绩总分和平均分

int main()
{
     int i;
     struct stu student[5];
     
     printf("请输入五个同学信息.......\n");
     printf("学号\t姓名\t英语分数\t高数\tC++分数\n");
     for(i=0; i<5; i++)
     {
         scanf("%s %s %f %f %f", student[i].num,
             student[i].name, &student[i].english,
             &student[i].math, &student[i].cpp);
     }
     calculate(student);
       
     return 0;
}


// 函数的定义
 
void calculate (struct stu scholatic[] )
{
      int i;

      for(i=0; i<5; i++)
      {
          scholatic[i].score=scholatic[i].average=0.0;

          scholatic[i].score= scholatic[i].math+
                              scholatic[i].english+
                              scholatic[i].cpp;

          scholatic[i].average=scholatic[i].score/3;
      }
      printf("输出每个同学的总分和平均分:\n");
      for(i=0; i<5; i++)
      {
          printf("第%d人: 学号:%s 姓名:%s 总分:%f 平均分:%f\n", i+1, scholatic[i].num,
                                          scholatic[i].name,
                                          scholatic[i].score,
                                          scholatic[i].average);
      }
}

以下是用C语言定义一个学生类型的结构体,包括学号姓名成绩,定义五个学生变量并需要定义结构体数组,循环输入学生信息,循环输出学生信息的代码示例: ```c #include <stdio.h> #define MAX_STUDENTS 5 struct student { int id; char name[20]; double score; }; int main() { struct student students[MAX_STUDENTS]; // Input student information for (int i = 0; i < MAX_STUDENTS; i++) { printf("Enter student %d information:\n", i+1); printf("ID: "); scanf("%d", &students[i].id); printf("Name: "); scanf("%s", students[i].name); printf("Score: "); scanf("%lf", &students[i].score); } // Output student information printf("Student Information:\n"); for (int i = 0; i < MAX_STUDENTS; i++) { printf("ID: %d, Name: %s, Score: %.2lf\n", students[i].id, students[i].name, students[i].score); } return 0; } ``` 这个程序定义了一个名为`student`的结构体,包含了三个成员:`id`表示学号(整型),`name`表示姓名(字符数组),`score`表示成绩(浮点型)。然后定义了一个长度为5的结构体数组`students`用来存储五个学生的信息。 在输入学生信息时,通过一个循环来逐一输入每个学生的学号姓名成绩。在输出学生信息时,同样也通过一个循环来逐一输出每个学生的信息。 注意,在输入姓名时使用了`scanf("%s", students[i].name)`,这里没有使用`&`符。这是因为字符数组名本身就是一个指针,不需要再取地址。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值