设计要求:
1.可录入学生信息,包含(姓名、学号、数学成绩、语文成绩);
2.输入学生姓名,输出该生相关信息;
3.排序,按照数学成绩或者语文成绩排序,排序依据由用户指定;
4.更改学生信息;
5.输出所有记录;
6.退出;
以下附本人自己写的代码,程序不够精简,尚可完善,还望赐教。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int num = 0;
struct student
{
char name[10];
int id;
int chinese;
int math;
};
typedef struct student STU;
void InsertInfo(STU *s[])
{
printf("Please input Information:\n");
while(1)
{
s[num] = (STU *)malloc(sizeof(STU));
if(NULL == s[num])
{
return;
}
scanf("%s",s[num]->name);
if(strcmp(s[num]->name, "end") == 0)
{
break;
}
scanf("%d%d%d", &s[num]->id, &s[num]->chinese, &s[num]->math);
num++;
}
}
void SearchInfo(STU *s[])
{
char tar_name[10] = {0};
int j;
printf("please input the target name:\n");
scanf("%s", tar_name);
for(j = 0;j < num;j++)
{
if(strcmp(s[j]->name, tar_name) == 0)
{
printf("The Information of the student is:\n");
printf("%s %d %d %d\n", s[j]->name, s[j]->id, s[j]->chinese, s[j]->math);
break;
}
}
if(j == num)
{
printf("Not find!!\n");
}
}
void SortInfo(STU *s[])
{
printf("************************************\n\n");
printf("******please choose the sort by:****\n\n");
printf("****1.chinese 2.Math****\n\n");
printf("************************************\n\n");
int choice = 0;
int i, j, k;
STU *tmp;
scanf("%d", &choice);
if(1 == choice)
{
for(i = 0;i < num;i++)
{
for(j = i + 1;j < num ;j++)
{
if(s[i]->chinese > s[j]->chinese)
{
tmp = s[j];
s[j] = s[i];
s[i] = tmp;
}
}
}
for(k = 0;k < num;k++)
{
printf("%s %d %d %d\n", s[k]->name, s[k]->id, s[k]->chinese,s[k]->math);
}
}
if(2 == choice)
{
for(i = 0;i < num ;i++)
{
for(j = i + 1;j < num ;j++)
{
if(s[i]->math > s[j]->math)
{
tmp = s[j];
s[j] = s[i];
s[i] = tmp;
}
}
}
for(k = 0;k < num;k++)
{
printf("%s %d %d %d\n", s[k]->name, s[k]->id, s[k]->chinese,s[k]->math);
}
}
}
void ShowInfo(STU *s[])
{
int i;
for(i = 0;i < num;i++)
{
printf("%s %d %d %d\n", s[i]->name, s[i]->id, s[i]->chinese, s[i]->math);
}
}
void ChangeInfo(STU *s[])
{
char tar_name[10] = {0};
int i = 0;
printf("**********BEFORE CHANGE*********\n");
ShowInfo(s);
printf("*********************************\n");
printf("Please input the student you want to change:\n");
scanf("%s",tar_name);
printf("Please input NEW Information:\n");
while(s[i] != NULL)
{
if(strcmp(s[i]->name, tar_name) == 0)
{
scanf("%s",s[i]->name);
scanf("%d",&s[i]->id);
scanf("%d",&s[i]->chinese);
scanf("%d",&s[i]->math);
printf("Change Success!\n");
break;
}
i++;
}
printf("~~~~~~~~~~AFTER CHANGE~~~~~~~~~~\n");
ShowInfo(s);
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
void show()
{
printf("************************************\n\n");
printf("*****WELCOME TO TEACHER SYSTEM******\n\n");
printf("************************************\n\n");
sleep(1);
system("clear");
}
void PrintInfo()
{
printf("************************************\n");
printf("***1.InsertInfo 2.SearchInfo\n");
printf("***3.SortInfo 4.ChangeInfo\n");
printf("***5.ShowInfo 6.Quit \n");
printf("************************************\n");
printf("Please input your choice:\n");
}
int main()
{
STU *stu[100] = {0};
char choice[10];
show();
while(1)
{
PrintInfo();
scanf("%s", choice);
switch(atoi(&choice[0]))
{
case 1:
InsertInfo(stu);
break;
case 2:
SearchInfo(stu);
break;
case 3:
SortInfo(stu);
break;
case 4:
ChangeInfo(stu);
break;
case 5:
ShowInfo(stu);
break;
case 6:
exit(1);
default:
printf("Unkown choice!\n");
break;
}
}
return 0;
}