作业:
程序代码:
#include<stdio.h>
#include<string.h>//定义全局变量
char name[][1000] = {""};//存放姓名
int grade[1000] = {0};//存放分数
int num = 0;//用于输入学生数//定义菜单函数
void print_menu()
{
printf("\t\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*-----------0.退出----------------------*\n");
printf("\t\t*****************************************\n");
}//定义信息录入函数
void enter()
{
printf("请输入学生个数:");
scanf("%d",&num);
for (int i = 0; i < num; i++)
{
printf("请输入第%d个学生的姓名和分数:",i+1);
scanf("%s %d",name[i],&grade[i]);
getchar();
}
}//定义查看信息函数
void check()
{
printf("学生信息如下:\n");
for (int i = 0; i < num; i++)
{
printf("%s:%d\t",name[i],grade[i]);
}
putchar(10);
}//定义求最大值函数
void max()
{
int max = 0;
int maxi = 0;
max = grade[0];
maxi = 0;
for (int i = 0; i < num; i++)
{
if (max < grade[i])
{
max = grade[i];
maxi = i;
}
}
printf("成绩最好的学生为:%s:%d\n",name[maxi],max);
}//定义按姓名升序排序函数
void nam()
{
char temp[100] = "";
for (int i = 1; i < num; i++)
{
for (int j = 0; j < num-i; j++)
{
if (strcmp(name[j],name[j+1]) > 0)
{
//交换
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);int temp = grade[j];
grade[j] = grade[j+1];
grade[j+1] = temp;
}
}
}
printf("排序后的结果为:\n");
for (int i = 0; i < num; i++)
{
printf("%s:%d\t",name[i],grade[i]);
}
puts("");
}//定义按成绩升序排序函数
void gra()
{
char temp[100] = "";
for (int i = 1; i < num; i++)
{
for (int j = 0; j < num-i; j++)
{
if (grade[j] > grade[j+1])
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);int temp = grade[j];
grade[j] = grade[j+1];
grade[j+1] = temp;}
}
}
printf("排序后的结果为:\n");
for (int i = 0; i < num; i++)
{
printf("%s:%d\t",name[i],grade[i]);
}
puts("");
}//*************主函数***************
int main(int argc, char const *argv[])
{
int menu = 0;
while (1)
{
print_menu();printf("请输入>>>");
scanf("%d",&menu);
switch (menu)
{
case 1:
{
enter();
}
break;
case 2:
{
check();
}
break;
case 3:
{
max();
}
break;
case 4:
{
nam();
}
break;
case 5:
{
gra();
}
break;
case 0:goto END;
default:printf("您输入的功能有误,请重新输入\n");
}
}
END:
return 0;
}运行结果:
参考代码: