#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
struct student
{
int num;
char name[50];
char sex;
int age;
};
int n=0;
void input(struct student ss[]);
void del(struct student deln[]);
void max(struct student maxx[]);
float avg(struct student avgn[]);
void sort(struct student minn[]);
void display(struct student dis[]);
void main()
{
struct student ss[N];
int change,x=0;
float savg;
do
{
printf("\t ╭────────────────────────────╮\n");
printf("\t ∣ 请根据菜单选择功能 ∣\n");
printf("\t |————————————————————————————|\n");
printf("\t | \t 1---录入学员信息 |\n");
printf("\t | \t 2---删除学员信息 |\n");
printf("\t | \t 3---显示最大年龄的学员信息 |\n");
printf("\t | \t 4---显示学员的平均年龄 |\n");
printf("\t | \t 5---对学员年龄降序排列 |\n");
printf("\t | \t 6---退出程序 |\n");
printf("\t ╰────────────────────────────╯\n");
printf("请输入功能编号:");
scanf("%d",&change);
switch(change)
{
case 1: input(ss);break;
case 2: del(ss);break;
case 3: max(ss);break;
case 4: savg=avg(ss);
printf("%d名学员的平均年龄是:%.3f\n",n,savg);break;
case 5: sort(ss);break;
case 6: exit(0);break;
default: printf("输入错误,请重新输入。\n");break;
}
}while(x<6);
}
void input(struct student sa[])
{
char answer;
do
{
printf("\n请输入学员信息\n");
printf("\n姓名:");
fflush(stdin);
gets(sa[n].name);
printf("性别(m or f):");
fflush(stdin);
sa[n].sex=getchar();
printf("年龄:");
scanf("%d",&sa[n].age);
n++;
printf("\n^^^^^^^^^^^^^^^^^^^^\n");
printf("是否继续录入?(y or n):");
fflush(stdin);
scanf("%c",&answer);
}while(answer=='y'||answer=='Y');
printf("\n本次录入的所有学员的信息如下\n");
display(sa);
}
void del(struct student deln[])
{
int i,num;
printf("请输入要删除学员的学号:");
scanf("%d",&num);
for(i=num-1;i<n;i++)
{
deln[i].num=deln[i+1].num;
strcpy(deln[i].name,deln[i+1].name);
deln[i].sex=deln[i+1].sex;
deln[i].age=deln[i+1].age;
}
printf("\n剩余学员的信息如下:\n");
printf("\n学号\t姓名\t性别\t年龄\t\n");
for(i=0;i<n-1;i++)
printf("\n%d\t%s\t%c\t%d\n",deln[i].num,deln[i].name,deln[i].sex,deln[i].age);
n--;
}
void max(struct student maxx[])
{
int i,maxage;
maxage=maxx[0].age;
printf("年龄最大的学员是:");
for(i=0;i<n;i++)
{
if(maxage<maxx[i].age)
maxage=maxx[i].age;
}
printf("\n学号\t姓名\t性别\t年龄\t\n");
printf("================================\n");
for(i=0;i<n;i++)
if(maxage==maxx[i].age)
printf("\n%d\t%s\t%c\t%d\n",i+1,maxx[i].name,maxx[i].sex,maxx[i].age);
}
float avg(struct student avgn[])
{
int i,sum=0;
float aavg;
for(i=0;i<n;i++)
sum+=avgn[i].age;
aavg=(float)sum/n;
return aavg;
}
void sort(struct student minn[])
{
int i,j,t;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(minn[i].age<minn[j].age)
{
t=minn[i].age;
minn[i].age=minn[j].age;
minn[j].age=t;
}
printf("学员年龄从大到小排列顺序为:");
printf("\n学号\t姓名\t性别\t年龄\t\n");
printf("================================\n");
for(i=0;i<n;i++)
printf("\n%d\t%s\t%c\t%d\n",minn[i].num,minn[i].name,minn[i].sex,minn[i].age);
display(minn);
}
void display(struct student dis[])
{
int i;
printf("\n学号\t姓名\t性别\t年龄\t\n");
printf("================================\n");
for(i=0;i<n;i++)
{
dis[i].num=i+1;
printf("\n%d\t%s\t%c\t%d\n",dis[i].num,dis[i].name,dis[i].sex,dis[i].age);
}
}
搞定了~~~~跟大家分享一下。由于是出学,有不明白的地方多见谅~