#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#define MaxSize 100
struct student{
int num;
char name [10];
int computer,math,english,sum;
float average;
};
struct student s[MaxSize];
struct student;
int count=0;
void welcome()
{
printf (" +-------------------------------------+\n");
printf (" | 欢迎使用学生信息管理系统 |\n");
printf (" | |\n");
printf (" | |\n");
printf (" | |\n");
printf (" +-------------------------------------+\n");
}
void login()
{
char a[10],key[10];
int i=0;
printf("\n\n\n\n\n\t\t请输入用户名:");
gets(a);
printf("\n\n\t\t请输入密码:");
gets(key);
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t\t登录中");
for(i=0;i<3;i++)
{
printf(". ");
Sleep(500);
}
if(strcmp("HL",a)==0 && strcmp("0312",key)==0)
{
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t\t登录成功,欢迎进入学生管理系统界面\n");
}
else
{
system("cls");
printf("\n\n\n\t\t您输入的用户名或密码不正确,请重新输入。\n");
login();
} }
void mainmenu()
{
printf ("\n");
printf ("\t\t\t\t <学生信息管理系统>\n");
printf ("\t\t1.录入学生信息\t");
printf ("2.显示学生信息\t");
printf ("3.添加学生信息\t");
printf ("4.删除学生信息\n");
printf ("\t\t5.修改学生信息\t");
printf ("6.查找学生信息\t");
printf ("7.按总成绩降序\t");
printf ("8.按总成绩升序\n");
printf ("\t\t9.按学号降序\t");
printf ("10.按学号升序\t");
printf ("11.写入文件\t");
printf ("12.读出文件\n");
printf ("\t\t0.退出\n");
printf ("\t\t输入你的选择:");
}
void new_student (struct student s[])
{
int i,n;
if (count==MaxSize)
{
printf ("The array is full! \n");
return;
}
printf ("输入学生人数:");
scanf ("%d",&n);
for (i=0;i<n;i++)
{
printf ("输入学生学号:\n");
scanf ("%d",&s[i].num);
printf ("输入学生姓名:\n");
scanf ("%s",&s[i].name);
printf ("输入计算机成绩:\n");
scanf ("%d",&s[i].computer);
printf ("输入数学成绩:\n");
scanf ("%d",&s[i].math);
printf ("输入英语成绩\n");
scanf ("%d",&s[i].english);
s[i].sum=s[i].computer+s[i].english+s[i].math;
s[i].average=(s[i].computer+s[i].english+s[i].math)/3.0;
count++;
}
}
void out_putstudent (struct student s[])
{
int i;
if (count==0)
{
printf ("count of students is zero!");
return;
}
printf ("学号\t姓名\t计算机\t数学\t英语\t总分\t平均分\n");
for (i=0;i<count;i++){
s[i].sum=s[i].computer+s[i].english+s[i].math;
s[i].average=(s[i].computer+s[i].english+s[i].math)/3.0;
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s[i].num,s[i].name,s[i].math,s[i].english,s[i].computer,s[i].sum,s[i].average);}
}
void _delete()
{
int i,flag=0,j;
char n[100];
printf("请输入要删除的学生姓名:\n");
scanf("%s",n);
for(i=0;i<count;i++)
if(strcmp(s[i].name,n)==0)
{
flag=1;
break;
}
if(flag==1)
{for(j=i;j<count-1;j++)
s[j]=s[j+1];
count--;
}
else
printf("没有找到抱歉。");
return;
}
void add()
{
int i,num,j,k;
printf("输入添加学生的学号:");
scanf ("%d",&num);
for(i=0;i<count;i++)
{
if(s[i].num>num)
{
break;
}
}
k=i;
for(j=count-1;j>=k;j--)
{
s[j+1]=s[j];
}
printf ("再次输入学生学号:");
scanf("%d",&s[k].num);
printf("学生姓名:");
scanf("%s",s[k].name);
printf("学生数学分数:");
scanf("%d",&s[k].math);
printf("学生英语分数:");
scanf("%d",&s[k].english);
printf("学生计算机分数:");
scanf("%d",&s[k].computer);
count++;
}
void modify (struct student *p)
{
int num,course,score,i;
printf ("Input the number of updated student:");
scanf ("%d",&num);
printf ("Choice the course: 1.math 2.english 3.computer:");
scanf ("%d",&course);
printf ("Input the new score:");
scanf ("%d",&score);
for (i=0;i<count;i++,p++)/*按学号查找*/
if (p->num==num)
break;
if (i<count) /*找到,修改成绩*/
switch (course)
{
case 1:p->math=score;break;
case 2:p->english=score;break;
case 3:p->computer=score;break;
}
}
void search_student(struct student s[])
{
int i,flag,num;
printf ("Input num:");
scanf ("%d",&num);
if (count==0)
{
printf ("count of students is zero!\n");
return;
}
for (i=0;i<count;i++)
if (s[i].num==num)
{
flag=1;
break;
}
if (flag)
{
printf ("学号\t姓名\t计算机\t数学\t英语\t总分\t平均分\n");
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s[i].num,s[i].name,s[i].computer,s[i].english,s[i].math,s[i].sum,s[i].average);
}
else
printf ("Not found!");
}
void num_d(struct student s[])
{
struct student t;
int i,j,index;
for (i=0;i<count-1;++i)
{
index=i;
for (j=i+1;j<count;j++)
if (s[j].num>s[index].num)
index=j;
t=s[index];
s[index]=s[i];
s[i]=t;
}
printf ("学号\t姓名\t计算机\t数学\t英语\t总分\t平均分\n");
for (i=0;i<count;i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s[i].num,s[i].name,s[i].computer,s[i].english,s[i].math,s[i].sum,s[i].average);
}
void num_a(struct student s[])
{
struct student t;
int i,j,index;
for (i=0;i<count-1;++i)
{
index=i;
for (j=i+1;j<count;j++)
if (s[j].num<s[index].num)
index=j;
t=s[index];
s[index]=s[i];
s[i]=t;
}
printf ("学号\t姓名\t计算机\t数学\t英语\t总分\t平均分\n");
for (i=0;i<count;i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s[i].num,s[i].name,s[i].computer,s[i].english,s[i].math,s[i].sum,s[i].average);
}
void sum_d(struct student s[])
{
struct student t;
int i,j,index;
for (i=0;i<count-1;++i)
{
index=i;
for (j=i+1;j<count;j++)
if (s[j].sum>s[index].sum)
index=j;
t=s[index];
s[index]=s[i];
s[i]=t;
}
printf ("学号\t姓名\t计算机\t数学\t英语\t总分\t平均分\n");
for (i=0;i<count;i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s[i].num,s[i].name,s[i].computer,s[i].english,s[i].math,s[i].sum,s[i].average);
}
void sum_a(struct student s[])
{
struct student t;
int i,j,index;
for (i=0;i<count-1;++i)
{
index=i;
for (j=i+1;j<count;j++)
if (s[j].sum<s[index].sum)
index=j;
t=s[index];
s[index]=s[i];
s[i]=t;
}
printf ("学号\t姓名\t计算机\t数学\t英语\t总分\t平均分\n");
for (i=0;i<count;i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s[i].num,s[i].name,s[i].computer,s[i].english,s[i].math,s[i].sum,s[i].average);
}
void save()
{
FILE *fp;
int i=0;
fp=fopen("D:\\student.txt","w");
if(fp==NULL){
printf ("File open error!");
exit(0);
}
while(i<count)
{
if(fwrite((s+i),sizeof(student),1,fp)==1)
i++;
else
break;
}
fclose(fp);
}
void read( )
{
FILE *fp;
int i=0;
fp=fopen("D:\\student.txt","r");
if(fp!=NULL)
{
while(!feof(fp))
{
if(fread((s+i),sizeof(student),1,fp)==1)
i++;
}
fclose(fp);
}
count=i;
}
int main ()
{
welcome();
login();
Sleep(1000);
system("cls");
int number;
do{
mainmenu();
scanf ("%d",&number);
switch (number)
{
case 1: new_student(s);break;
case 2: out_putstudent (s);break;
case 3: add();break;
case 4: _delete();break;
case 5: modify (s);break;
case 6: search_student(s);break;
case 7: sum_d(s);break;
case 8: sum_a(s);break;
case 9: num_d(s);break;
case 10: num_a(s);break;
case 11:save();break;
case 12:read();out_putstudent(s);break;
case 0: exit(0);
}
}while (number!=0);
return 0;
}