parent
7517a3bed1
commit
320ec0bf9b
@ -0,0 +1,264 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
struct Student//定义学生结构体
|
||||
{
|
||||
char id[20];
|
||||
char name[20];
|
||||
int class;
|
||||
double score1;
|
||||
double score2;
|
||||
double score3;
|
||||
double score;
|
||||
}student[10];
|
||||
int numstudents=0;
|
||||
void PrintMenu()//打印菜单以供选择
|
||||
{
|
||||
printf(" 1.input\n");
|
||||
printf(" 2.delete\n");
|
||||
printf(" 3.select\n");
|
||||
printf(" 4.order\n");
|
||||
printf(" 5.output\n");
|
||||
printf(" 6.update\n");
|
||||
printf(" 7.quit\n");
|
||||
}
|
||||
void Input()//输入学生信息
|
||||
{
|
||||
char a;
|
||||
do
|
||||
{
|
||||
printf("Please input information:\n");
|
||||
scanf("%s",&student[numstudents].id);
|
||||
scanf("%s",&student[numstudents].name);
|
||||
scanf("%d",&student[numstudents].class);
|
||||
scanf("%lf",&student[numstudents].score1);
|
||||
scanf("%lf",&student[numstudents].score2);
|
||||
scanf("%lf",&student[numstudents].score3);
|
||||
student[numstudents].score=student[numstudents].score1+student[numstudents].score2+student[numstudents].score3;
|
||||
numstudents++;
|
||||
printf("continue?(y or n)");//选择是否要继续输入学生信息
|
||||
fflush(stdin);
|
||||
scanf("%c",&a);
|
||||
}while(a=='y');
|
||||
}
|
||||
void Printinformation()//打印学生信息
|
||||
{
|
||||
for(int i=0;i<numstudents;i++)
|
||||
{
|
||||
printf("学号:%s,姓名:%s,班级:%d,成绩1:%lf,成绩2:%lf,成绩3:%lf,总成绩:%lf\n",student[i].id,student[i].name,student[i].class,student[i].score1,student[i].score2,student[i].score3,student[i].score);
|
||||
}
|
||||
}
|
||||
void Delete()//删除学生信息
|
||||
{
|
||||
char a;
|
||||
do
|
||||
{
|
||||
int choice;
|
||||
printf("delete by?\n1.id\n2.name\n");//询问用什么方式选中要删除的学生
|
||||
scanf("%d",&choice);
|
||||
if(choice==1)
|
||||
{
|
||||
char cid[20]={0};
|
||||
printf("请输入学号:\n");
|
||||
scanf("%s",&cid);
|
||||
int i;
|
||||
for( i=0;i<numstudents;i++)
|
||||
{
|
||||
if(strcmp(student[i].id,cid)==0)
|
||||
{
|
||||
for(int j=i;j<numstudents;j++)
|
||||
{
|
||||
student[j]=student[j+1];
|
||||
}
|
||||
numstudents--;
|
||||
printf("已删除!\n");
|
||||
break;
|
||||
}
|
||||
if(i==numstudents-1)
|
||||
{
|
||||
printf("未找到该学生信息!\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else if(choice==2)
|
||||
{
|
||||
char cname[20]={0};
|
||||
printf("请输入姓名:\n");
|
||||
scanf("%s",&cname);
|
||||
int i;
|
||||
for( i=0;i<numstudents;i++)
|
||||
{
|
||||
if(strcmp(student[i].name,cname)==0)
|
||||
{
|
||||
for(int j=i;j<numstudents;j++)
|
||||
{
|
||||
student[j]=student[j+1];
|
||||
}
|
||||
numstudents--;
|
||||
printf("已删除!\n");
|
||||
break;
|
||||
}
|
||||
if(i==numstudents-1)
|
||||
{
|
||||
printf("未找到该学生信息!\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Printinformation();
|
||||
printf("continue?(y or n)\n");
|
||||
fflush(stdin);
|
||||
scanf("%c",&a);//询问是否继续
|
||||
}while(a=='y');
|
||||
}
|
||||
void Select()//查询学生信息
|
||||
{
|
||||
char a;
|
||||
do
|
||||
{
|
||||
printf("search by?\n1.id\n2.name\n3.class\n");//询问以什么方式检索学生信息
|
||||
int choice;
|
||||
scanf("%d",&choice);
|
||||
if(choice==1)
|
||||
{
|
||||
char sid[20]={0};
|
||||
printf("请输入查询学生学号:\n");
|
||||
scanf("%s",&sid);
|
||||
for(int i=0;i<numstudents;i++)
|
||||
{
|
||||
if(strcmp(student[i].id,sid)==0)
|
||||
{
|
||||
printf("学号:%s,姓名:%s,班级:%d,成绩1:%lf,成绩2:%lf,成绩3:%lf,总成绩:%lf\n",student[i].id,student[i].name,student[i].class,student[i].score1,student[i].score2,student[i].score3,student[i].score);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else if(choice==2)
|
||||
{
|
||||
char sname[20]={0};
|
||||
printf("请输入查询学生姓名:\n");
|
||||
scanf("%s",&sname);
|
||||
for(int i=0;i<numstudents;i++)
|
||||
{
|
||||
if(strcmp(student[i].name,sname)==0)
|
||||
{
|
||||
printf("学号:%s,姓名:%s,班级:%d,成绩1:%lf,成绩2:%lf,成绩3:%lf,总成绩:%lf\n",student[i].id,student[i].name,student[i].class,student[i].score1,student[i].score2,student[i].score3,student[i].score);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(choice==3)
|
||||
{
|
||||
int sclass;
|
||||
printf("请输入查询学生班级:\n");
|
||||
scanf("%d",&sclass);
|
||||
for(int i=0;i<numstudents;i++)
|
||||
{
|
||||
if(sclass==student[i].class)
|
||||
{
|
||||
printf("学号:%s,姓名:%s,班级:%d,成绩1:%lf,成绩2:%lf,成绩3:%lf,总成绩:%lf\n",student[i].id,student[i].name,student[i].class,student[i].score1,student[i].score2,student[i].score3,student[i].score);
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("continue?(y or n)\n");
|
||||
fflush(stdin);
|
||||
scanf("%c",&a);
|
||||
|
||||
} while(a=='y');
|
||||
}
|
||||
void Order()//将学生信息按照班级以及成绩排序
|
||||
{
|
||||
struct Student temp;
|
||||
for(int i=0;i<numstudents-1;i++)//将学生按照班级排序
|
||||
{
|
||||
for(int j=0;j<numstudents-1-i;j++)
|
||||
{
|
||||
if(student[j].class>student[j+1].class)
|
||||
{
|
||||
temp=student[j];
|
||||
student[j]=student[j+1];
|
||||
student[j+1]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
int start=0;//将按照班级排好序的学生按照成绩在班级内部排序
|
||||
for(int i=0;i<numstudents-1;i++)
|
||||
{
|
||||
if(student[i].class != student[i+1].class)
|
||||
{
|
||||
int end=i;
|
||||
for (int k=start;k<end;k++)
|
||||
{
|
||||
for(int l=start;l<end-k;l++)
|
||||
{
|
||||
if(student[l].score<student[l+1].score)
|
||||
{
|
||||
temp=student[l];
|
||||
student[l]=student[l+1];
|
||||
student[l+1]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
start=i+1;
|
||||
}
|
||||
}
|
||||
Printinformation();
|
||||
}
|
||||
void Update()//修改学生信息
|
||||
{
|
||||
char a;
|
||||
do
|
||||
{
|
||||
printf("请输入修改学生学号:\n");
|
||||
char choice[20]={0};
|
||||
scanf("%s",&choice);
|
||||
for(int i=0;i<numstudents;i++)
|
||||
{
|
||||
if(strcmp(student[i].id,choice)==0)
|
||||
{
|
||||
printf("请输入修改后的学号,姓名,班级,成绩1,成绩2,成绩3\n");
|
||||
scanf("%s",&student[i].id);
|
||||
scanf("%s",&student[i].name);
|
||||
scanf("%d",&student[i].class);
|
||||
scanf("%lf",&student[i].score1);
|
||||
scanf("%lf",&student[i].score2);
|
||||
scanf("%lf",&student[i].score3);
|
||||
student[i].score=student[i].score1+student[i].score2+student[i].score3;
|
||||
}
|
||||
}
|
||||
Printinformation();
|
||||
printf("continue(y or n)?\n");
|
||||
fflush(stdin);
|
||||
scanf("%c",&a);
|
||||
} while (a=='y');
|
||||
|
||||
}
|
||||
void Run()
|
||||
{
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
PrintMenu();
|
||||
printf("please input your option\n");
|
||||
scanf("%d",&choice);
|
||||
switch(choice)
|
||||
{
|
||||
case 1:Input();break;
|
||||
case 2:Delete();break;
|
||||
case 3:Select();break;
|
||||
case 4:Order();break;
|
||||
case 5:Printinformation();break;
|
||||
case 6:Update();break;
|
||||
case 7:printf("thanks for using!");break;
|
||||
default:printf("Wrong Input!\n");
|
||||
}
|
||||
}while(choice!=7);
|
||||
|
||||
}
|
||||
int main()
|
||||
{
|
||||
PrintMenu();
|
||||
Run();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue