作业
#include <stdio.h>
#include <string.h>
#define MAX 10
//菜单函数
void me_nu(void)
{
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======0:退出\n");
}
//学生信息录入函数
void student_1(char stu_dent[][100],int m,int gread[],int n)
{
for(int i=0;i<n;i++)
{
printf("请输入第%d名学生姓名\n",i+1);
scanf("%s",stu_dent[i]);
printf("请输入第%d名学生成绩\n",i+1);
scanf("%d",&gread[i]);
}
}
//学生信息输出
void student_2(char stu_dent[][100],int m,int gread[],int n)
{
for(int i=0;i<n;i++)
{
printf("学生姓名:%s",stu_dent[i]);
printf("学生成绩:%d\n",gread[i]);
}
}
//成绩最好的学生成绩
int student_3(int gread[],int n)
{
int max_gre = 0;
int max = gread[0];
for(int i=0;i<n;i++)
{
if(max<gread[i])
{
max_gre = i;
}
}
return max_gre;
}
//按姓名排序
void student_4(char stu_dent[][100],int m,int gread[],int n)
{
char temp1[100] ={0};
for(int i = 1;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(strcmp(stu_dent[j],stu_dent[+1])>0)
{
strcpy(temp1,stu_dent[j]);
int temp2 =gread[j];
strcpy(stu_dent[j],stu_dent[j+1]);
gread[j] = gread[j+1];
strcpy(stu_dent[j+1],temp1);
gread[j+1] = temp2;
}
}
}
for(int i = 0;i<n;i++)
{
printf("姓名:%s",stu_dent[i]);
printf("成绩:%d\t",gread[i]);
}
printf("\n");
}
//按成绩排序
void student_5(int gread[],int m,char stu_dent[][100],int n)
{
char temp1[100] ={0};
for(int i = 1;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(gread[j]>gread[j+1])
{
strcpy(temp1,stu_dent[j]);
strcpy(stu_dent[j],stu_dent[j+1]);
strcpy(stu_dent[j+1],temp1);
int temp2 =gread[j];
gread[j] = gread[j+1];
gread[j+1] = temp2;
}
}
}
for(int i = 0;i<n;i++)
{
printf("姓名:%s",stu_dent[i]);
printf("成绩:%d\t",gread[i]);
}
printf("\n");
}
int main(int argc, const char *argv[])
{
int menu = 0;
static char stu_dent[MAX][100] = {0};
static int gread[MAX] = {0};
static int n = 0;
int max_stu=0,max_gre=0;
int max = 0;
while(1)
{
me_nu();
printf("请输入:");
scanf("%d",&menu);
getchar();
switch(menu)
{
case 1:
{
printf("请输入本班有多少名学生:");
scanf("%d",&n);
student_1(stu_dent,n,gread,n);
}
break;
case 2:
{
student_2(stu_dent,n,gread,n);
}
break;
case 3:
{
max = student_3(gread,n);
printf("成绩最好的学生是:%s",stu_dent[max]);
printf("该生的成绩为:%d\n",gread[max]);
}
break;
case 4:
{
student_4(stu_dent,n,gread,n);
}
break;
case 5:
{
student_5(gread,n,stu_dent,n);
}
break;
case 0:
{
goto END;
}
default:
printf("您输入的内容有误,请重新输入\n");
}
}
END:
return 0;
}