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