#include<stdio.h>
typedef struct
{ char name[8];
char id[2];
int chinese;
int math;
int english;
int sum;
}*Student;
void Unitedfile()//合并1.txt和2.txt为3.txt
{ FILE *fp,*p;
Student studd;
fp=fopen("1.txt","r"); /*以读的方式打开1.txt*/
p=fopen("3.txt","w"); /*以写的方式打开3.txt*/
int i=0;
while( fscanf(fp,"%s%s%d%d%d",studd[i].name,&studd[i].id,studd[i].chinese,&studd[i].math,&studd[i].english )!=EOF)
{
fprintf(p,"%-6s %-6s %-6d %-6d %-6d\n",studd[i].name,studd[i].id,studd[i].chinese,studd[i].math,studd[i].english );
printf("%-6s %-6s %-6d %-6d %-6d\n",studd[i].name,studd[i].id,studd[i].chinese,studd[i].math,studd[i].english );
i++;
} /*读取1.txt的数据写入3.txt,继续读取直结束*/
fclose(fp); /*关闭文件1.txt*/
fp=fopen("2.txt","r"); /*以写的方式打开2.txt*/
i=0;
while( fscanf(fp,"%s%s%d%d%d",studd[i].name,studd[i].id,&studd[i].chinese,&studd[i].math,&studd[i].english )!=EOF)
{
fprintf(p,"%-6s %-6s %-6d %-6d %-6d\n",studd[i].name,studd[i].id,studd[i].chinese,studd[i].math,studd[i].english );
printf("%-6s %-6s %-6d %-6d %-6d\n",studd[i].name,studd[i].id,studd[i].chinese,studd[i].math,studd[i].english );
/*输出文本3.txt中的信息*/
i++;
}
fclose(fp); /*关闭文件2.txt*/
fclose(p); /*关闭文件3.txt*/
}
void findout() /*提取有补考学生的信息写入4.txt中*/
{
FILE *fp,*p;
Student studd;
fp=fopen("3.txt","r");
p=fopen("4.txt","w");
int i=0;
while( fscanf(fp,"%s%s%d%d%d",studd[i].name,studd[i].id,&studd[i].chinese,&studd[i].math,&studd[i].english )!=EOF)
{
if(studd[i].chinese<60||studd[i].math<60||studd[i].english<60)
{
fprintf(p,"%-6s %-6s %-6d %-6d %-6d\n",studd[i].name,studd[i].id,studd[i].chinese,studd[i].math,studd[i].english );
printf("%-6s %-6s %-6d %-6d %-6d\n",studd[i].name,studd[i].id,studd[i].chinese,studd[i].math,studd[i].english );
/*输出文本4.txt中的信息*/
}
i++;
}
fclose(fp);
fclose(p);
}
void Bsort(int R[],int n)
{
int t, flag=1,j; /*当flag为0时则停止排序*/
for( j=1;j<n;j++)
{
flag=0;
for(int k=n-1;k>=j;k--)
if(R[k]>R[k-1])
{
t=R[k];
R[k]=R[k-1];
R[k-1]=t;flag=1;
}
if(flag==0)
return;
} }
void maopao(Student studd)
{
int i,n;
FILE *fp;
fp=fopen("3.txt","r");
i=0;
while(fscanf(fp,"%s%s%d%d%d",studd[i].name,studd[i].id,&studd[i].chinese,&studd[i].math,&studd[i].english )!=EOF)
{
studd[i].sum=studd[i].english+studd[i].chinese+studd[i].math;
i++;
}
fclose(fp);
n=i-1;
Bsort(studd.sum,n); /*冒泡排序*/
fp=fopen("3.txt","w"); /*将信息写入3.txt中*/
for(int m=0;m<i;m++)
{
fprintf(fp,"%-8s %-8s %-8d %-8d %-8d %-8d\n",studd[m].name,studd[m].id,studd[m].chinese,studd[m].math,studd[m].english,studd[m].sum );
}
fclose(fp);
};
int Partition(int low,int high)
{ int pivotkey;
Student temp;
temp=studd[low];
pivotkey=studd[low].sum;
while(low<high)
{ while(low<high&&stud[high].sum<=pivotkey)--high;
stud[low]=studd[high];
while(low<high&&studd[low].sum>=pivotkey)++low;
studd[high]=studd[low]; }
studdivotloc;
if(low<high) {
privotloc=Partition(low,high);
QSort(low,privotloc-1);
QSort(privotloc+1,high); } }
void kuaisu() /*读取文件排序后将结果写入3.txt*/
{ int i,low,high;
FILE *fp;
fp=fopen("3.txt","r");
i=0;
while(fscanf(fp,"%s%s%d%d%d",stud[i].name,stud[i].id,&stud[i].chinese,&stud[i].math,&stud[i].english )!=EOF)
{ stud[i].sum=stud[i].english+stud[i].chinese+stud[i].math;
i++; }
fclose(fp); low=0; high=i-1;
QSort(low,high); /*快速排序*/
fp=fopen("3.txt","w"); /*将排序后的数据写入3.txt中*/
for(int j=0;j<i;j++)
{ fprintf(fp,"%-8s %-8s %-8d %-8d %-8d %-8d\n",stud[j].name,stud[j].id,stud[j].chinese,stud[j].math,stud[j].english,stud[j].sum );
} fclose(fp); }
int main()
{
int choice;
while(1)
{
/*主菜单*/
if(sign3==0)
{
printf("*******************************************************************************\n");
printf("***************************** 学生成绩管理系统 ******************************\n");
printf("********************* 1. 合并1和2得3 *****************\n");
printf("********************* 2. 把3中有成绩不及格的放入新文件4中 *****************\n");
printf("********************* 3. 把3中成绩总分按降序排列 *****************\n");
printf("********************* 4. 输入姓名,查找该成绩信息并输出结果 *****************\n");
printf("********************* 5. 退出系统 *****************\n");
printf("*******************************************************************************\n");
printf("请输入(1-5)选择要使用的功能:");
}
sign3=1;
scanf("%d",&choice);
switch(choice)
{
case 1:
Unitedfile();
sign=1;
cout<<"1和2合并成功!"<<endl;
cout<<"请选择下一步操作:"<<endl;
break;
case 2:
if(sign==0)
{
cout<<"请执行操作1后再执行此项操作!"<<endl;
cout<<"请选择下一步操作:"<<endl;
break;
}
cout<<"4.txt生成成功!"<<endl;;
findout();
cout<<"请选择下一步操作:"<<endl;
break;
case 3:
if(sign==0)
{
cout<<"请执行操作1后再执行此项操作!"<<endl;
cout<<"请选择下一步操作:"<<endl;
break;
}
sortfile();
cout<<"降序排列成功!"<<endl;
cout<<"请选择下一步操作:"<<endl;
sign1=1;
break;
case 4:
while(1)
{
char c;
findoutstudent();
cout<<"继续查找或返回主菜单请输入Y/N:"<<endl;
cin>>c;
if(c=='N'||c=='n')break;
}
system("cls");
sign3=0;
break;
case 5:
exit(0);
break;
}
}
return 0;
}