#include
#include
#include
#include
#define N 40
//输入要求:学号必须为正规学号(位数在8以上)
//姓名需输入英文全拼,尽量不写汉字
//密码为yangjiantao
typedef struct//结构体
{
long num;//学号,长度要大于8位,
char name[15];//姓名需输入英文全拼,尽量不写汉字
unsigned int score[3];//成绩(百分制)
float sumS;//每个学生的总成绩
float aveS;//每个学生的平均分
} STU;
void input(STU person[],int m,int n);//输入学生的姓名,学号,成绩
void a(STU person[],int sumc[],float avec[],int m,int n);//计算学生的总分及平均分
void print(STU person[],int sumc[],float avec[],int m,int n);//输出学生的信息以及每门课的总分
void sort1(STU person[],int m,int n);//按成绩由小到大排序
void print1(STU person[],int m,int n,int min[]);//按成绩由小到大输出成绩
void print2(STU person[],int m,int n,int min[]);//按成绩由大到小输出成绩
void sort3(STU person[],int m,int n,int min[]);//按学号由小到大排序
void print3(STU person[],int m,int n,int min[]);//按学号由小到大输出成绩
void sort4(STU person[],int m,int n,int min[]);//按姓名字母顺序排序
void print4(STU person[],int m,int n,int min[]);//按姓名字母顺序输出成绩
int numsearch(STU person[],int m,long x);//x为学号
int namesearch(STU person[],int m,char y[]);//y为姓名
void writetofile(STU person[],int n);//向文件写入数据
void readfromfile(STU person[]);//从文件读出数据并打印出成绩
int main()
{
char s[100]="hello",ch[100];
printf("WHO ARE YOU?\n");
gets(ch);
system("cls");
if(strcmp(s,ch)==0)//密码设置
{
int m,n,sumc[N],min[N],y;//m,n为总人数与总科目数,n=3,sumc为每门课的总分,min为名次
float avec[N];//每门课的平均分
STU person[N];//定义结构体数组
printf("INPUT TOTAL NUMBER OF STUDENTS AND COURSES(courses==3):\n");
scanf("%d %d",&m,&n);
printf("INPUT YOUR NUMBER AND NAME\n");
printf("YOUR NUMBER MUST HAVE 8--14 DIGITS,AND YOUR NAME USE SPELLING\n");
input(person,m,n);
a(person,sumc,avec,m,n);
sort1(person,m,n);
sort3(person,m,n,min);
sort4(person,m,n,min);
printf("1.Sort in ascending order by total score of every of every student\n");//用户选项1--10
printf("2.Sort in descending order by total score of every of evedy student\n");
printf("3.Sort in ascending order by number\n");
printf("4.Sort in dictionary order by name\n");
printf("5.Search by number\n");
printf("6.search by name\n");
printf("7.Statics analysis for every course\n");
printf("9.Write to a file\n");
printf("10.Read from a file\n");
printf("0.Exit\n");//退出程序
while(1)
{
printf("Please enter your choice:");
scanf("%d",&y);//输入选择
if(y==1)
{
printf(" GRADE ASDENDING\n");
print1(person,m,n,min);
}
else if(y==2)
{
printf(" GRADE DESCENDING\n");
print2(person,m,n,min);
}
else if(y==3)
{
printf(" NUM ASDENDING\n");
print3(person,m,n,min);
}
else if(y==4)
{
printf(" NAME ASDENDING\n");
print4(person,m,n,min);
}
else if(y==5)
{
long x;
char ch;
do
{
printf("INPUT YOUR SEARCHING NUMBER\n");
scanf("%ld",&x);
int p;
p=numsearch(person,m,x);
if(p!=-1)
{
printf("%ld %s %u %u %u %.0f %d\n",person[p].num,person[p].name,person[p].score[0],
person[p].score[1],person[p].score[2],person[p].sumS,min[p]);
}
else printf("NO THIS STUDENT\n");
printf("Do you want do it again? Please enter y,Y,n,N\n");
scanf(" %c",&ch);//空格消去前面的回车
}
while(ch=='y'||ch=='Y');
}
else if(y==6)
{
char y[19];
int ch;
do
{
printf("INPUT YOUR SEARCHING NAME\n");
getchar();//消掉前面的回车
gets(y);
int r;
r=namesearch(person,m,y);
if(r!=-1)
{
printf("%ld %s %u %u %u %.0f %d\n",person[r].num,person[r].name,person[r].score[0],
person[r].score[1],person[r].score[2],person[r].sumS,min[r]);
}
else printf("NO THIS STUDENT\n");
printf("Do you want do it again? Please enter 1 or 0\n");
scanf("%d",&ch);
}while(ch==1);
}
else if(y==8)
{
printf(" TOTAL STATE\n");
print(person,sumc,avec,m,n);
}
else if(y==9)
{
writetofile(person,n);
}
else if(y==10)
{
readfromfile(person);
print1(person,m,n,min);
}
else if(y==0) exit(0);//退出程序
else printf(" NO THIS CHOICE!!!\n");
}
}
else
printf("you are a bad man\n"),exit(0);
return 0;
}
void input(STU person[],int m,int n)//输入学生的姓名,学号,成绩
{
int i,j;
for(i=0; i0;
scanf("%ld %s",&person[i].num,person[i].name);
int l=strlen(person[i].name);
if(l>=8&&l<=14)//判断姓名的长度
{
printf("INPUT THE GRADES OF COURSES as MT EN PH\n");
for(j=0; jscanf("%u",&person[i].score[j]);
person[i].sumS=person[i].sumS+person[i].score[j];
}
person[i].aveS=person[i].sumS/n;
}
else
{
printf("YOUR NAME IS INFORMAL!!!PLEASE ENTER IT AGAIN!!!\n");
printf("IT MUST HAVE 8--14 ALPHAS\n");
i--;
}
}
}
void a(STU person[],int sumc[],float avec[],int m,int n)//计算学生的总分及平均分
{
int i,j;
for(i=0; i0;
for(j=0; jvoid print(STU person[],int sumc[],float avec[],int m,int n)//输出学生的信息以及每门课的总分
{
int i,j;
for(i=0; iprintf("%13ld%15s ",person[i].num,person[i].name);
for(j=0; jprintf("%d\t",person[i].score[j]);
}
printf("%.0f\t%.1f\n",person[i].sumS,person[i].aveS);
}
printf("Sumofcourse\t\t\t");
for(j=0; jprintf("%d\t",sumc[j]);
}
printf("\nAveofcourse\t\t\t");
for(j=0; jprintf("%.1f\t",avec[j]);
}
printf("\n");
}
void sort1(STU person[],int m,int n)//按成绩由小到大排序
{
int i,j;
STU temp;//temp为中间变量
for(i=1; i//冒泡排序
{
for(j=0; j<=m-i-1; j++)
{
if(person[j].sumS>person[j+1].sumS)
{
temp=person[j+1];
person[j+1]=person[j];
person[j]=temp;
}
}
}
}
void print1(STU person[],int m,int n,int min[])//按成绩由小到大输出成绩
{
int i,j,k=m;
printf("Number\t Name\t MT EN PH SUM RANK\n");
for(i=0; iprintf("%.ld%13s ",person[i].num,person[i].name);
for(j=0; jprintf("%4d",person[i].score[j]);
}
printf(" %.0f\t",person[i].sumS);
min[i]=k;
k--;
printf("%d\n",min[i]);
}
}
void print2(STU person[],int m,int n,int min[])//按成绩由大到小输出成绩
{
int i,j;
printf("Number\t Name\t MT EN PH SUM RANK\n");
for(i=m-1; i>=0; i--)
{
printf("%.ld%13s ",person[i].num,person[i].name);
for(j=0; jprintf("%4d",person[i].score[j]);
}
printf(" %.0f\t",person[i].sumS);
printf("%d\n",min[i]);
}
}
void sort3(STU person[],int m,int n,int min[])//按学号由小到大排序
{
int i,j;//temp,temp1为中间变量
STU temp;
int temp1;
for(i=1; ifor(j=0; j<=m-i-1; j++)
{
if(person[j].num>person[j+1].num)
{
temp=person[j+1];
person[j+1]=person[j];
person[j]=temp;
temp1=min[j+1];
min[j+1]=min[j];
min[j]=temp1;
}
}
}
}
void print3(STU person[],int m,int n,int min[])//按学号由小到大输出成绩
{
int i,j;
for(i=0; iprintf("Number\t Name\t MT EN PH SUM RANK\n");
printf("%.ld%13s ",person[i].num,person[i].name);
for(j=0; jprintf("%4d",person[i].score[j]);
}
printf(" %.0f\t",person[i].sumS);
printf("%d\n",min[i]);
}
}
void sort4(STU person[],int m,int n,int min[])//按姓名字母顺序排序
{
int i,j,temp1;
STU temp;
for(j=1; jfor(i=0; i<=m-j-1; i++)
{
if(strcmp(person[i].name,person[i+1].name)>0)
{
temp=person[i+1];
person[i+1]=person[i];
person[i]=temp;
temp1=min[i+1];
min[i+1]=min[i];
min[i]=temp1;
}
}
}
}
void print4(STU person[],int m,int n,int min[])//按姓名字母顺序输出成绩
{
int i,j;
printf("Number\t Name\t MT EN PH SUM RANK\n");
for(i=0; iprintf("%.ld%13s ",person[i].num,person[i].name);
for(j=0; jprintf("%4d",person[i].score[j]);
}
printf(" %.0f\t",person[i].sumS);
printf("%d\n",min[i]);
}
}
//按学号查询学生信息
int numsearch(STU person[],int m,long x)//x为学号
{
int i;
for(i=0; iif(person[i].num==x)
return i;
}
return -1;
}
//按姓名查找
int namesearch(STU person[],int m,char y[])//x为姓名
{
int i;
for(i=0; iif(strcmp(y,person[i].name)==0)
return i;
}
return -1;
}
void writetofile(STU person[],int n)//将学生信息写入文件
{
FILE *fp;
if((fp=fopen("1.txt","w"))==NULL)
{
printf("Faliure to open file\n");
exit(0);
}
fwrite(person,sizeof(STU),n,fp);
fclose(fp);
}
void readfromfile(STU person[])//从文件中读取学生信息并打印
{
FILE *fp;
int i;
if((fp=fopen("1.txt","r"))==NULL)
{
printf("Faliure to open file\n");
exit(0);
}
for(i=0; !feof(fp); i++)
{
fread(&person[i],sizeof(STU),1,fp);
}
fclose(fp);
}