zoukankan      html  css  js  c++  java
  • C语言之:结构体动态分配内存(利用结构体数组保存不超过10个学生的信息,每个学生的信息包括:学号、姓名和三门课(高数、物理和英语 )的成绩和平均分(整型)。)

    题目内容:

    利用结构体数组保存不超过10个学生的信息,每个学生的信息包括:学号、姓名和三门课(高数、物理和英语 )的成绩和平均分(整型)。

    编写程序,从键盘输入学生的人数,然后依次输入每个学生的学号、姓名和3门课的成绩

    然后计算每个学生的平均分

    最后按指定格式输出每个学生的平均分

    输入格式:

    先输入一个整数,表示学生个数

    然后每行输入一个学生的信息:学号、姓名和高数、物理及英语成绩

    输出格式:

    输出每个学生的平均分.printf中请用格式控制串"The average score of the %dth student is %d. "

    输入样例:

    5

    1001 Zhang 100 90 80

    1002 Wu 93 90 98

    1003 Zhu 89 88 87

    1004 Hu 90 98 98

    1005 Wang 90 98 97

    输出样例:

    The average score of the 1th student is 90.

    The average score of the 2th student is 93.

    The average score of the 3th student is 88.

    The average score of the 4th student is 95.

    The average score of the 5th student is 95.

    代码如下:

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct STU {
        char ID[20];
        char Name[20];
        float math;
        float physic;
        float english;
    };
    int main()
    {
    
    
        int n,i,j,k;
        float res;
        scanf("%d", &n);
        if (n > 10) {
            return 0;
        }
        struct STU *mySTU = (struct STU*)malloc(sizeof(struct STU) * n);   //动态内存分配关键 malloc
        struct STU* p = mySTU;
        for (i = 0; i < n; i++)
        {
            scanf("%s", &p->Name);
            scanf("%s", &p->ID);
            scanf("%f", &p->math);
            scanf("%f", &p->english);
            scanf("%f", &p->physic);
            *(p++);
        }
        struct STU *q = mySTU;
        for (j = 0; j < n; j++)
        {
            res = (q->math + q->physic + q->english)/3;
            k = (int)res;
            printf("The average score of the %dth student is %d.
    ", j+1,k );
            *(q++);
        }
    
            return 0;
    }
  • 相关阅读:
    样式
    读取网页图片(包含代理)
    C# 替换“换行符”写法
    Iframe中弹出消息问题
    程序list
    C#对象序列化
    软件工程——个人总结
    软件工程——团队作业4
    软件工程———团队答辩
    软件工程--团队作业三
  • 原文地址:https://www.cnblogs.com/Fzeng/p/14189475.html
Copyright © 2011-2022 走看看