m0_74078058 2022-11-26 14:54 采纳率: 100%
浏览 76
已结题

C语言编程解决最大年龄问题?

这是我自己写的代码,有点问题,请大家帮忙修改一下,谢谢。

要求是这样的,程序开始,提示输入若干学生的姓名、性别、出生年、月、日(当姓名为#时结束输人)。结束输入后显示功能菜单,用户输入选项编号,进入不同功能。选项为1,显示学生信息。按合适的格式实现显示所有学生的信息。选项为2,显示最大女生。在所有学生中找出年龄最大的女学生,并将所找到的学生信息输出到屏幕。选项为3,显示最大男生。在所有学生中找出年龄最大的男学生,并将所找到的学生信息输出到屏幕。选项为4,显示最大学生。在所有学生中找出年龄最大的学生,并将所找到的学生信息输出到屏幕。选项为0,退出。

#include<stdio.h>
struct student                            //姓名、性别、出生年、月、日
{
    char name[20];
    char sex;
    int year;
    int month;
    int day;
}stu[100], t;
void age_oldest_student()
{
    int i;
    for (i = 0; i < 100; i++)
    {
        if ((stu[0].year != stu[i].year) && (stu[0].year > stu[i].year))
        {
            t = stu[0];
            stu[0] = stu[i];
            stu[i] = t;
        }
        if (stu[0].year == stu[i].year)//出生年份相同
        {
            if (stu[0].month > stu[i].month)
            {
                //找出年份相同时月份最小的那个学生信息
                t = stu[0];
                stu[0] = stu[i];
                stu[i] = t;
            }
        }
        if ((stu[0].year == stu[i].year) && (stu[0].month == stu[i].month))//出生年份和月份相同
        {
            //若俩个学生出生年份和月份都一样则进行出生日的比较找出出生日最小的那个
            if (stu[0].day > stu[i].day)
            {
                t = stu[0];
                stu[0] = stu[i];
                stu[i] = t;
            }
        }
        if (!strcmp(stu[i].name, "#"))
            break;
    }
    //输出年龄最大的学生的信息
    printf("姓名\t性别\t出生年月日\n");
    printf("%s\t%c\t%d-%d-%d\n", stu[0].name, stu[0].sex, stu[0].year, stu[0].month, stu[0].day);
}
int main()
{
    int i;
    printf("请输入学生姓名、性别、出生年、月、日(当输入为#时结束输入):\n");
    for ( i = 0; i < 100; i++)
    {
        scanf("%s%s%d%d%d",stu[i].name, &stu[i].sex, &stu[i].year, &stu[i].month, &stu[i].day);
        if (!strcmp(stu[i].name, "#"))
            break;
    }
    int x;
    x = i;
    printf("按1,显示学生信息\n按2,显示最大女生\n按3,显示最大男生\n按4,显示最大学生\n按0,退出\n");
    int z;
    scanf("%d", &z);
    switch (z)
    {
    case 0:
        break;
    case 1:
        printf("学生信息:\n姓名\t性别\t出生年\t月\t日\n");
        int y;
        for (y = 1; y <= x; y++)
        {
            printf("%s\t%s\t%d\t%d\t%d\n", stu[y - 1].name, &stu[y - 1].sex, stu[y - 1].year, stu[y - 1].month, stu[y - 1].day);
            if (!strcmp(stu[y].name, "#"))
                break;
        }
        break;
    case 2:
        for (y = 1; y <= x; y++)
        {
            if (strcmp(stu[y].sex, "w"))
            {
                getchar(stu[y]);
            }
        }

    }
    return 0;
}


  • 写回答

2条回答 默认 最新

  • 小园豆知识日记 移动开发领域新星创作者 2022-11-26 23:32
    关注

    主要是输入没有处理好,好久没写c了,改的很烂,只可供参考

    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    const int max=5;
    struct student                            //姓名、性别、出生年、月、日
    {
        char name[20];
        char sex[5];
        int year;
        int month;
        int day;
    }stu[max], t;
    void age_oldest_student()
    {
        int i;
        for (i = 0; i < max; i++)
        {
            //
            if(stu[i].year==0||stu[i].month==0||stu[i].day==0){
                continue;
            }else{
                if ((stu[0].year != stu[i].year) && (stu[0].year > stu[i].year))
                {
                    t = stu[0];
                    stu[0] = stu[i];
                    stu[i] = t;
                }
                if (stu[0].year == stu[i].year)//出生年份相同
                {
                    if (stu[0].month > stu[i].month)
                    {
                        //找出年份相同时月份最小的那个学生信息
                        t = stu[0];
                        stu[0] = stu[i];
                        stu[i] = t;
                    }
                }
                if ((stu[0].year == stu[i].year) && (stu[0].month == stu[i].month))//出生年份和月份相同
                {
                    //若俩个学生出生年份和月份都一样则进行出生日的比较找出出生日最小的那个
                    if (stu[0].day > stu[i].day)
                    {
                        t = stu[0];
                        stu[0] = stu[i];
                        stu[i] = t;
                    }
                }
                if (!strcmp(stu[i].name, "#"))
                    break;
            }
        }
        //输出年龄最大的学生的信息
        printf("姓名\t性别\t出生年月日\n");
        printf("%s\t%s\t%d-%d-%d\n", stu[0].name, stu[0].sex, stu[0].year, stu[0].month, stu[0].day);
    }
    
    void age_sex(char sex[]){
        int i;
        for (i = 0; i < max; i++)
        {
            //
            if(stu[i].year==0||stu[i].month==0||stu[i].day==0){
                continue;
            }else if(strcmp(stu[i].sex,sex)) {
                if ((stu[0].year != stu[i].year) && (stu[0].year > stu[i].year))
                {
                    t = stu[0];
                    stu[0] = stu[i];
                    stu[i] = t;
                }
                if (stu[0].year == stu[i].year)//出生年份相同
                {
                    if (stu[0].month > stu[i].month)
                    {
                        //找出年份相同时月份最小的那个学生信息
                        t = stu[0];
                        stu[0] = stu[i];
                        stu[i] = t;
                    }
                }
                if ((stu[0].year == stu[i].year) && (stu[0].month == stu[i].month))//出生年份和月份相同
                {
                    //若俩个学生出生年份和月份都一样则进行出生日的比较找出出生日最小的那个
                    if (stu[0].day > stu[i].day)
                    {
                        t = stu[0];
                        stu[0] = stu[i];
                        stu[i] = t;
                    }
                }
                if (!strcmp(stu[i].name, "#"))
                    break;
            }
        }
        //输出年龄最大的学生的信息
        printf("姓名\t性别\t出生年月日\n");
        printf("%s\t%s\t%d-%d-%d\n", stu[0].name, stu[0].sex, stu[0].year, stu[0].month, stu[0].day);
    }
    
    
    int main()
    {
        int i;
        printf("请输入学生姓名、性别、出生年、月、日(当输入为#时结束输入):\n");
        for ( i = 0; i < max; i++)
        {
            //输入有问题%c
            //libai 男 2002 04 11
            scanf("%s",stu[i].name);
            if (!strcmp(stu[i].name, "#"))break;
            scanf("%s",stu[i].sex);
            scanf("%d",&stu[i].year);
            scanf("%d",&stu[i].month);
            scanf("%d",&stu[i].day);
            printf("%8s %8s %8d %8d %8d\n",stu[i].name, stu[i].sex, stu[i].year, stu[i].month, stu[i].day);
        }
        int x;
        x = i;
        //接受回车
        getchar();
        while(true){
            printf("------------------------------------------------------------------------------\n");
            printf("按1,显示学生信息\n按2,显示最大女生\n按3,显示最大男生\n按4,显示最大学生\n按0,退出\n");
            int z;
            scanf("%d", &z);
            printf("z=======%d",z);
            switch (z)
            {
                case 0:
                    return 0;//直接return 0;退出程序break;
                case 1://输出学生信息
                    printf("学生信息:\n姓名\t性别\t出生年\t月\t日\n");
                    int y;
                    for (y = 1; y <= x; y++)
                    {
                        printf("%8s%8s%8d%8d%8d\n", stu[y - 1].name, &stu[y - 1].sex, stu[y - 1].year, stu[y - 1].month, stu[y - 1].day);
                        if (!strcmp(stu[y].name, "#"))
                            break;
                    }
                        break;
                case 4://显示最大xue生
                    age_oldest_student();
                        break;
                case 2://显示最大女生
                    age_sex("女");
                        break;
                case 3://显示最大男生
                    age_sex("男");
                        break;
            }
        }
    }
    /**
    李白 男 2002 01 01
    杜甫 男 2022 02 02
    李清照 女 1997 01 01
    蔡文姬 女 1889 01 01
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月6日
  • 已采纳回答 11月28日
  • 创建了问题 11月26日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分