使用结构体数组存储数据,每个学生包含学号、姓名、C语言成绩、web成绩、高数成绩、英语成绩、总分,实现添加(初始化可连续添加),删除,查找,排序(根据各科成绩或总分排序),修改等功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char no[12];
char name[20];
int cgrade;
int webgrade;
int math;
int english;
int total;
};
struct Student s[50];
int n=0;
void init()
{
printf("请输入学生的学号,姓名,C语言成绩,web成绩,数学成绩和英语成绩\n");
printf("当输入学生学号为0时停止输入\n");
while(n<=50)
{
printf("请输入学生学号:\n");
scanf("%s",s[n].no);
if(strcmp(s[n].no,"0")==0)
break;
printf("请输入学生的姓名:\n");
scanf("%s",s[n].name);
printf("请输入会员的C语言成绩,web成绩,数学成绩,英语成绩:\n");
scanf("%d%d%d%d",&s[n].cgrade,&s[n].webgrade,&s[n].math,&s[n].english);
s[n].total = s[n].cgrade + s[n].webgrade + s[n].math + s[n].english;
n++;
}
}
void print() //输出
{
int j;
printf("学生信息记录如下:\n");
printf("\t|===============================================================================================|\n");
printf("\t| 学号\t姓名\t总分\t C语言成绩\t web成绩\t数学成绩\t英语成绩\t|\n");
printf("\t|-----------------------------------------------------------------------------------------------|\n");
for(j=0;j<n;j++)
{
printf("\t|%s\t%s\t%d\t %d\t\t %d\t\t %d\t\t %d\t\t|\n",s[j].no,s[j].name,s[j].total,s[j].cgrade,s[j].webgrade,s[j].math,s[j].english);
if(j<(n-1))
printf("\t|-----------------------------------------------------------------------------------------------|\n");
}
printf("\t|===============================================================================================|\n");
}
struct Student gete() //输入
{
struct Student e;
printf("请输入学号:\n");
scanf("%s",e.no);
printf("请输入姓名:\n");
scanf("%s",&e.name);
printf("请输入学生的C语言成绩,web成绩,数学成绩,英语成绩:\n");
scanf("%d%d%d%d",&e.cgrade,&e.webgrade,&e.math,&e.english);
e.total = e.cgrade + e.english + e.math + e.webgrade;
return e;
}
void add1()
{
struct Student e = gete();
int i;
printf("请输入要插入的位置(1-%d):\n",n);
scanf("%d",&i);
while(i<1||i>n+1)
{
printf("您输入的位置有误!\n");
printf("请重新输入要插入的位置(1-%d):\n",n);
scanf("%d",&i);
}
int j;
for(j=n-1;j>=i-1;j--)
{
s[j+1]=s[j];
}
s[i-1]=e;
n++;
}
void del()
{
int i,j;
char num[12];
int found=1;
char choice;
while(1)
{
printf("请输入要删除的学生的账号:\n");
scanf("%s",num);
getchar();
for(i=0;i<n&&found==1;i++) //5 1-5
{
if(strcmp(s[i].no,num)==0)
{
found=0;
printf("\t|===============================================================================================|\n");
printf("\t| 学号\t姓名\t总分\t C语言成绩\t web成绩\t数学成绩\t英语成绩\t|\n");
printf("\t|-----------------------------------------------------------------------------------------------|\n");
printf("\t|%s\t%s\t%d\t %d\t\t %d\t\t %d\t\t %d\t\t|\n",s[i].no,s[i].name,s[i].total,s[i].cgrade,s[i].webgrade,s[i].math,s[i].english);
printf("\t|===============================================================================================|\n");
for(j=i;j<n-1;j++)
{
s[j]=s[j+1];
}
n--;
}
}
if(found==1)
{
printf("未找到!\n");
}
found=1;
printf("是否继续(Y/N)\n");
choice=getchar();
getchar();
if(choice=='N'|| choice=='n')
break;
}
}
void findbyno()
{
int i;
char num[12];
int found=1;
char choice;
while(1)
{
printf("请输入要查找的学生的学号:\n");
scanf("%s",&num);
getchar();
for(i=0;i<n&&found==1;i++)
{
if(strcmp(s[i].no,num)==0)
{
found=0;
printf("\t|===============================================================================================|\n");
printf("\t| 学号\t姓名\t总分\t C语言成绩\t web成绩\t数学成绩\t英语成绩\t|\n");
printf("\t|-----------------------------------------------------------------------------------------------|\n");
printf("\t|%s\t%s\t%d\t %d\t\t %d\t\t %d\t\t %d\t\t|\n",s[i].no,s[i].name,s[i].total,s[i].cgrade,s[i].webgrade,s[i].math,s[i].english);
printf("\t|===============================================================================================|\n");
}
}
if(found==1)
{
printf("未找到!\n");
}
found=1;
printf("是否继续查询(Y/N)\n");
choice=getchar();
if(choice=='N'|| choice=='n')
break;
}
}
void findbyname()
{
char name[20];
char choice;
int found=1;
int i;
while(1)
{
printf("输入要查询学生的姓名:\n");
scanf("%s",name);
getchar();
for(i=0;i<n&&found==1;i++)
{
if(strcmp(s[i].name,name)==0)
{
found=0;
printf("\t|===============================================================================================|\n");
printf("\t| 学号\t姓名\t总分\t C语言成绩\t web成绩\t数学成绩\t英语成绩\t|\n");
printf("\t|-----------------------------------------------------------------------------------------------|\n");
printf("\t|%s\t%s\t%d\t %d\t\t %d\t\t %d\t\t %d\t\t|\n",s[i].no,s[i].name,s[i].total,s[i].cgrade,s[i].webgrade,s[i].math,s[i].english);
printf("\t|===============================================================================================|\n");
}
}
if(found==1)
{
printf("未找到!\n");
}
found=1;
printf("是否继续查询(Y/N)\n");
choice=getchar();
if(choice=='N'|| choice=='n')
break;
}
}
void mod()
{
int i;
char num[12];
int found=1;
char choice;
int opt;
while(1)
{
printf("请输入要修改的学生的学号(学号无法修改):\n");
scanf("%s",num);
getchar();
for(i=0;i<n&&found==1;i++)
{
if(strcmp(s[i].no,num)==0)
{
found=0;
printf("\t|===============================================================================================|\n");
printf("\t| 学号\t姓名\t总分\t C语言成绩\t web成绩\t数学成绩\t英语成绩\t|\n");
printf("\t|-----------------------------------------------------------------------------------------------|\n");
printf("\t|%s\t%s\t%d\t %d\t\t %d\t\t %d\t\t %d\t\t|\n",s[i].no,s[i].name,s[i].total,s[i].cgrade,s[i].webgrade,s[i].math,s[i].english);
printf("\t|===============================================================================================|\n");
printf("--请选择--1.修改C语言成绩 2.修改web成绩 3.修改数学成绩 4.修改英语成绩 5.修改姓名\n");
scanf("%d",&opt);
getchar();
switch(opt)
{
case 1:
printf("请输入C语言成绩新值:\n");
scanf("%d",&s[i].cgrade);
getchar();
break;
case 2:
printf("请输入web成绩新值:\n");
scanf("%d",&s[i].webgrade);
getchar();
break;
case 3:
printf("请输入数学成绩新值:\n");
scanf("%d",&s[i].math);
getchar();
break;
case 4:
{
printf("请输入英语成绩新值:\n");
scanf("%d",&s[i].english);
getchar();
break;
}
case 5:
printf("请输入姓名新值:\n");
scanf("%s",s[i].name);
getchar();
break;
}
s[i].total = s[i].cgrade + s[i].english + s[i].math + s[i].webgrade;
break;
}
}
if(found==1)
{
printf("未找到!\n");
}
found=1;
printf("是否继续修改(Y or N)\n");
choice=getchar();
getchar();
if(choice=='N'|| choice=='n')
break;
}
}
void sort()
{
int i,j;
struct Student temp;
for(i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if (s[j].total<s[j+1].total)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
printf("\n排序后的学生信息:\n");
}
void cgradesort()
{
int i,j;
struct Student temp;
for(i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if (s[j].cgrade<s[j+1].cgrade)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
printf("\n排序后的学生信息:\n");
}
void webgradesort()
{
int i,j;
struct Student temp;
for(i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if (s[j].webgrade<s[j+1].webgrade)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
printf("\n排序后的学生信息:\n");
}
void mathsort()
{
int i,j;
struct Student temp;
for(i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if (s[j].math<s[j+1].math)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
printf("\n排序后的学生信息:\n");
}
void englishsort()
{
int i,j;
struct Student temp;
for(i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if (s[j].english<s[j+1].english)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
printf("\n排序后的学生信息:\n");
}
int main()
{
int opt;
printf(" |*============================================*|\n");
printf(" |* 欢迎使用学生管理系统 *|\n");
printf(" |*============================================*|\n");
printf(" |* 1.初始化 *|\n");
printf(" |* 2.添加新的学生记录 *|\n");
printf(" |* 3.删除已有学生记录 *|\n");
printf(" |* 4.根据姓名查询已有会员记录 *|\n");
printf(" |* 5.根据学号查询已有会员记录 *|\n");
printf(" |* 6.修改已有学生记录 *|\n");
printf(" |* 7.根据总分对已有学生记录进行排序 *|\n");
printf(" |* 8.根据C语言成绩对已有学生记录进行排序 *|\n");
printf(" |* 9.根据web成绩对已有学生记录进行排序 *|\n");
printf(" |* 10.根据数学成绩对已有学生记录进行排序 *|\n");
printf(" |* 11.根据英语成绩对已有学生记录进行排序 *|\n");
printf(" |* 0.退出会员信息管理系统 *|\n");
printf(" |*============================================*|\n");
while(1)
{
printf("请输入要完成的功能:0-10:");
scanf("%d",&opt);
getchar();
switch(opt)
{
case 1:init();print();break;
case 2:add1();print();break;
case 3:del();print();break;
case 4:findbyname();print();break;
case 5:findbyno();print();break;
case 6:mod();print();break;
case 7:sort();print();break;
case 8:cgradesort();print();break;
case 9:webgradesort();print();break;
case 10:mathsort();print();break;
case 11:englishsort();print();break;
case 0:system("cls");exit(0);break;
default:printf("请输入0-11中的数!\n");break;
}
}
}