##实现功能
- 学生成绩输入
- 显示学生成绩
- 插入学生成绩
- 统计学生总人数
- 删除学生成绩
- 按照学号查询学生成绩
- 按照学号快速排序
1.头文件与结构体的创建
#include
#include
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
#define OVERFLOW -2
#define MAXSIZE 200
typedef int status;
typedef int ElemType;
typedef struct {
char name[20];
int number;
int math;
int C;
int English;
}student;
typedef struct {
student *elem;
int Length;
}Sqlist;
1.主程序代码
int main(void)
{
Sqlist L;
InitList(L);
int quit;
printf("******学生成绩管理系统******\n");
printf("1.学生成绩输入\n");
printf("2.显示学生成绩\n");
printf("3.插入学生成绩\n");
printf("4.统计学生总人数\n");
printf("5.删除学生成绩\n");
printf("6.按照学号查询学生成绩\n");
printf("7.按照学号快速排序\n");
printf("0.退出系统");
while(quit!=0)
{
printf("\n\n请选择相应操作:");
scanf("%d",&quit);
if(quit==1)
{
int n;
printf("\n请输入学生的总人数:");
scanf("%d",&n);
ListImport(L,n);
}
else if(quit==2)
{
ListOutput(L,L.Length);
}
else if(quit==3)
{
int t;
printf("输入要插入的位置:");
scanf("%d",&t);
ListInsert(L,t);
}
else if(quit==4)
{
int t;
t=ListLenght(L);
printf("学生的总人数为:%d人",t);
}
else if(quit==5)
{
int t,s;
printf("输入要删除的位置:");
scanf("%d",&t);
s=ListDelete(L,t);
if(s!=0)
{
printf("删除成功!!!");
}
else
{
printf("删除失败!!!");
}
}
else if(quit==6)
{
QuickSort(L,1,L.Length);
int t,i;
printf("输入要查询学生的学号:");
scanf("%d",&t);
i=Search(L,t,0,L.Length);
if(i==0)
{
printf("无此学号的学生");
}
else
{
printf("姓名:%s\n",L.elem[i].name);
printf("学号:%d\n",L.elem[i].number);
printf("数学成绩:%d\n",L.elem[i].math);
printf("英语成绩:%d\n",L.elem[i].English);
printf("C语言成绩:%d\n",L.elem[i].C);
}
}
else if(quit==7)
{
QuickSort(L,1,L.Length);
printf("排序成功!!!");
}
else if(quit==0)
{}
else
{
printf("输入错误,请重新输入");
}
}
printf("\n\n***退出成功***\n");
printf("***欢迎下次使用***\n");
}
2.子函数代码
status InitList(Sqlist& L)
{
L.elem = (student*)malloc(MAXSIZE * sizeof(student));
if (!L.elem)exit(-2);
L.Length = 0;
return OK;
}
status ListImport(Sqlist& L, int n)
{
int i;
if (!L.elem)return ERROR;
for (i = 1; i <= n; i++)
{
printf("\n请输入第%d个学生的信息:\n",i);
printf("姓名:");
scanf("%s", L.elem[i].name);
printf("学号:");
scanf("%d", &L.elem[i].number);
printf("数学成绩:");
scanf("%d", &L.elem[i].math);
printf("英语成绩:");
scanf("%d", &L.elem[i].English);
printf("C语言成绩:");
scanf("%d", &L.elem[i].C);
++L.Length;
}
return OK;
}
status ListOutput (Sqlist& L,int n)
{
int i;
if(!L.elem)return ERROR;
for(i=1;i<=n;i++)
{
printf("姓名:%s\n",L.elem[i].name);
printf("学号:%d\n",L.elem[i].number);
printf("数学成绩:%d\n",L.elem[i].math);
printf("英语成绩:%d\n",L.elem[i].English);
printf("C语言成绩:%d\n",L.elem[i].C);
}
}
status ListInsert(Sqlist& L, int i)
{
int j;
if (i<1 || i>L.Length + 1)return ERROR;
if (L.Length == MAXSIZE)return ERROR;
for (j = L.Length; j >= i; j--)
{
L.elem[j+1]=L.elem[j];
}
printf("请输入第%d个学生的信息:\n",i);
printf("姓名:");
scanf("%s", L.elem[i].name);
printf("学号:");
scanf("%d", &L.elem[i].number);
printf("数学成绩:");
scanf("%d", &L.elem[i].math);
printf("英语成绩:");
scanf("%d", &L.elem[i].English);
printf("C语言成绩:");
scanf("%d", &L.elem[i].C);
++L.Length;
return OK;
}
status ListDelete(Sqlist& L, int i)
{
int j;
if (i<1 || i>L.Length)return ERROR;
for (j = i; j <= L.Length; j++)
{
L.elem[j] = L.elem[j+1];
}
--L.Length;
return OK;
}
status ListLenght(Sqlist L)
{
return L.Length;
}
void InsertSort(Sqlist &L)
{
int i,j;
student s;
for(i=1;i<L.Length;i++)
{
if(L.elem[i].name<L.elem[i-1].name)
{
s=L.elem[i];
L.elem[i]=L.elem[i-1];
for(j=i-1;s.name<L.elem[j].name;j--)
L.elem[j+1]=L.elem[j];
L.elem[j+1]=s;
}
}
}
int Partition(Sqlist &L,int first,int end)
{
int i,j;
student temp;
i=first;
j=end;
while(i<j)
{
while(i<j&&L.elem[i].number<L.elem[j].number)j--;
if(i<j)
{
temp=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=temp;
i++;
}
while(i<j&&L.elem[i].number<=L.elem[j].number)i++;
if(i<j)
{
temp=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=temp;
j--;
}
}
return i;
}
void QuickSort(Sqlist &L,int first,int end)
{
int pivot;
if(first<end)
{
pivot=Partition(L,first,end);
QuickSort(L,first,pivot-1);
QuickSort(L,pivot+1,end);
}
}
int Search(Sqlist L,int key,int low,int high)
{
while(low<=high)
{
int mid;
mid=(low+high)/2;
if(key==L.elem[mid].number)
{
return mid;
}
else if(key<L.elem[mid].number)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return ERROR;
}