定义描述学生信息(学号、姓名、性别、出生日期、4门课程成绩和平均分)的结构体类型如下: struct date{ int month; int day;

文章描述了一个C语言程序,用于管理学生信息。程序定义了包含学号、姓名、性别、出生日期和成绩的结构体类型,并实现了输入、输出单个及一批学生信息的函数,以及按姓名查找和按姓名排序的功能。程序能存储最多50人的信息,并能按姓名从小到大排序后输出所有信息。
摘要由CSDN通过智能技术生成

定义描述学生信息(学号、姓名、性别、出生日期、4门课程成绩和平均分)的结构体类型如下: 
struct date
{        int month;
        int day;
        int year;
 };
struct stu
{
        int num;
        char name[20];
        char sex;
        struct date birthday;
        float score[4];
         float ave;
    };
1)定义输入单个学生信息的函数Input(struct stu *a) ;
2)定义输出单个学生信息的函数Output(struct stu a) ;
3)定义输入一批学生信息的函数Inputarray(struct stu a[ ],int n);
4)定义输出一批学生信息的函数Outputarray(struct stu a[ ],int n));
5)请编写按姓名进行查找的函数Searchname,若找到,返回表示该学生的信息,不排除有有同名同姓的情况;否则,返回 查无此人。
6)请编写函数Sortname,它的功能是:按每个学生的名字由小到大输出学生的记录。
编程建立的结构体数组通过输入输入存放全班(最多50人)学生信息,按学生的姓名从到大排序,输出学生的所有信息。

#include <stdio.h>
#include <string.h>
char str10[] = "查无此人";
char str11[] = "查找到该学生的信息";
struct date {
    int month;
    int day;
    int year;
};

struct stu {
    int num;
    char name[20];
    char sex;
    struct date birthday;
    float score[4];
    float ave;
};

void Input(struct stu* a) {
    printf("请输入学号:");
    scanf("%d", &a->num);
    printf("请输入姓名:");
    scanf("%s", a->name);
    printf("请输入性别:");
    scanf(" %c", &a->sex);
    printf("请输入出生日期(格式为:月 日 年):");
    scanf("%d %d %d", &a->birthday.month, &a->birthday.day, &a->birthday.year);
    printf("请输入4门课程成绩:"); a->ave = 0;
    for (int i = 0; i < 4; i++) {
        scanf("%f", &a->score[i]);
        a->ave += a->score[i];
    }
    a->ave /= 4.0;
}

void Output(struct stu a) {
    printf("学号:%d\n", a.num);
    printf("姓名:%s\n", a.name);
    printf("性别:%c\n", a.sex);
    printf("出生日期:%d年%d月%d日\n", a.birthday.year, a.birthday.month, a.birthday.day);
    printf("4门课程成绩:%.2f %.2f %.2f %.2f\n", a.score[0], a.score[1], a.score[2], a.score[3]);
    printf("平均分:%.2f\n", a.ave);
}

void Inputarray(struct stu a[], int n) {
    for (int i = 0; i < n; i++) {
        printf("请输入第%d个学生的信息:\n", i + 1);
        Input(&a[i]);
    }
}

void Outputarray(struct stu a[], int n) {
    for (int i = 0; i < n; i++) {
        printf("第%d个学生的信息:\n", i + 1);
        Output(a[i]);
        printf("\n");
    }
}

char* Searchname(struct stu a[], int n, char name[]) {
    for (int i = 0; i < n; i++) {
        if (strcmp(a[i].name, name) == 0) {
            char* p2 = str11;
            return p2;
        }
    }char* p1 = str10;
    return p1;
}

void Sortname(struct stu a[], int n) {
    struct stu temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (strcmp(a[i].name, a[j].name) > 0) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
}

int main() {
    struct stu class1[50];
    int n;

    printf("请输入班级人数(最多50人):");
    scanf("%d", &n);

    Inputarray(class1, n);

    Sortname(class1, n);

    Outputarray(class1, n);

    char name[20];
    printf("请输入要查找的姓名:");
    scanf("%s", name);
    printf("%s\n", Searchname(class1, n, name));

    return 0;
}
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值