头文件:
#ifndef _STUDENT_H_
#define _STUDENT_H_
#define N 100
struct student
{
char name[20];
char sex;
int id;
int chinese;
int math;
};
typedef struct student st;
void Welcome();
void Show();
int Input(st *stu);
void Display(st *stu, int len);
void Sort(st *stu, int len);
#endif
主函数:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
int main()
{
st stu[N] = {0};
int num, len;
Welcome();
system("clear");
while(1)
{
Show();
scanf("%d", &num);
switch(num)
{
case 1:
len = Input(stu);
break;
case 2:
Display(stu, len);
break;
case 3:
Find(stu, len);
break;
case 4:
Sort(stu, len);
break;
default:
exit(1);
}
}
return 0;
}
各个函数模块:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"
void Welcome()
{
system("clear");
printf("*********************************\n");
printf(" WELCOME TO THE STUDENT SYSTEM! \n");
printf("*********************************\n");
sleep(2);
}
void Show()
{
printf("*********************************\n");
printf(" 1. INPUT! 2.SHOW INFO! \n");
printf(" 3. FIND! 4.SORT! \n");
printf("*********************************\n");
printf("Please input your choice:\n");
}
int Input(st *stu)
{
int i, len;
printf("Please input the information:\n");
for(i = 0; i < N; i++)
{
scanf("%s", stu[i].name);
if(strcmp(stu[i].name, "end") == 0)
{
len = i;
break;
}
scanf("%s", &stu[i].sex);
scanf("%d", &stu[i].id);
scanf("%d", &stu[i].chinese);
scanf("%d", &stu[i].math);
}
return len;
}
void Display(st *stu, int len)
{
int i;
for(i = 0; i < len; i++)
{
printf("name is %s\n", stu[i].name);
printf("sex is %c\n", stu[i].sex);
printf("id is %d\n", stu[i].id);
printf("chinese is %d\n", stu[i].chinese);
printf("math is %d\n", stu[i].math);
printf("\n");
}
}
void Find(st *stu, int len)
{
int i;
char target[100];
printf("Please input which student you want to find:\n");
scanf("%s", target);
for(i = 0; i < len; i++)
{
if(strcmp(stu[i].name, target) == 0)
{
printf("name is %s\n", stu[i].name);
printf("sex is %c\n", stu[i].sex);
printf("id is %d\n", stu[i].id);
printf("chinese is %d\n", stu[i].chinese);
printf("math is %d\n", stu[i].math);
}
}
}
void Sort(st *stu, int len)
{
int i, j;
int choice;
char tempname[100];
char tempsex;
int tempid, tempchinese, tempmath;
printf("Please input your choice: 1. Chinese 2. Math\n");
scanf("%d", &choice);
if(1 == choice)
{
for(i = 0; i < len - 1; i++)
{
for(j = 0; j < len - i - 1; j++)
{
if(stu[j].chinese > stu[j + 1].chinese)
{
strcpy(tempname, stu[j].name);
strcpy(stu[j].name, stu[j + 1].name);
strcpy(stu[j + 1].name, tempname);
tempsex = stu[j].sex;
stu[j].sex = stu[j + 1].sex;
stu[j + 1].sex = tempsex;
tempid = stu[j].id;
stu[j].id = stu[j + 1].id;
stu[j + 1].id = tempid;
tempchinese = stu[j].chinese;
stu[j].chinese = stu[j + 1].chinese;
stu[j + 1].chinese = tempchinese;
tempmath = stu[j].math;
stu[j].math = stu[j + 1].math;
stu[j + 1].math = tempmath;
}
}
}
for(i = 0; i < len; i++)
{
printf("name is %s\n", stu[i].name);
printf("sex is %c\n", stu[i].sex);
printf("id is %d\n", stu[i].id);
printf("chinese is %d\n", stu[i].chinese);
printf("math is %d\n", stu[i].math);
}
}
else if(2 == choice)
{
for(i = 0; i < len - 1; i++)
{
for(j = 0; j < len - i - 1; j++)
{
if(stu[j].math > stu[j + 1].math)
{
strcpy(tempname, stu[j].name);
strcpy(stu[j].name, stu[j + 1].name);
strcpy(stu[j + 1].name, tempname);
tempsex = stu[j].sex;
stu[j].sex = stu[j + 1].sex;
stu[j + 1].sex = tempsex;
tempid = stu[j].id;
stu[j].id = stu[j + 1].id;
stu[j + 1].id = tempid;
tempchinese = stu[j].chinese;
stu[j].chinese = stu[j + 1].chinese;
stu[j + 1].chinese = tempchinese;
tempmath = stu[j].math;
stu[j].math = stu[j + 1].math;
stu[j + 1].math = tempmath;
}
}
}
for(i = 0; i < len; i++)
{
printf("name is %s\n", stu[i].name);
printf("sex is %c\n", stu[i].sex);
printf("id is %d\n", stu[i].id);
printf("chinese is %d\n", stu[i].chinese);
printf("math is %d\n", stu[i].math);
}
}
else
{
;
}
}
这个系统比较low,用的数组和结构体完成的,实现了四种功能,第一个功能可以输入学生信息,第二个功能可以查看学生信息,第三个功能可以查找学生信息, 第四个功能可以排序。