#include <stdio.h>
#include <stdlib.h>
#include<Windows.h>
#include<iostream>
using namespace std;
typedef struct student
{
char number[20];
char name[20];
char sex[10];
char birth[20];
char province[20];
int score;
struct student *next;
}student;
void getSecond(student *s[],int sn);
void sort(student *s[],int sn);
int main()
{
//SetConsoleOutputCP(65001);
FILE* fd;
int flag = 0;
int sn = 10;//读取前面十个学生的信息
student * s[10];
//申请内存空间
for(;flag<sn;flag++)
{
s[flag] = (student *) malloc(sizeof(student));
}
//打开文件
if((fd = fopen("E:\\p.txt","w"))==NULL)
{
printf("文件打开失败.\n");
Sleep(80000);
return 0;
}
printf("文件打开写入成功.\n");
SetConsoleOutputCP(65001);
//输入数据
for( flag = 0; flag < sn; flag++)
{
cin>>s[flag]->number;
cin>>s[flag]->name;
cin>>s[flag]->sex;
cin>> s[flag]->birth;
cin>>s[flag]->province;
cin>>s[flag]->score;
cout<<endl;
}
//写入文件
for( flag = 0; flag < sn; flag++)
{
fprintf(fd,"%s%s%s%s%s%d",s[flag]->number,s[flag]->name,s[flag]->sex,
s[flag]->birth,s[flag]->province,s[flag]->score);
}
//关闭文件
fclose(fd);
//SetConsoleOutputCP(65001);
printf("\n");
printf("\n");
SetConsoleOutputCP(936);
printf("输出第二学生信息: ");
SetConsoleOutputCP(65001);
getSecond(s,sn);//输出成绩第二的学生信息
printf("\n");
printf("\n");
SetConsoleOutputCP(936);
printf("按照分数高低输出所有学生信息: ");
SetConsoleOutputCP(65001);
sort(s,sn);//按照分数高低输出所有学生信息
return 0;
}
void getSecond(student *s[],int sn)
{
//寻找次高分数的学生信息
int ranking = 1 ;
int i,j;
for(i = 0; i < sn; i++)
{
for(j = 0; j<sn; j++)
{
if(s[i]->score > s[j]->score) ranking++;
}
if(ranking == sn-1 ) break;//sn-1表示次高分数标志
else ranking = 1;
}
printf("\n");
SetConsoleOutputCP(936);
printf("%s%16s%8s%16s%16s%8d\n",s[i]->number,s[i]->name,s[i]->sex,
s[i]->birth,s[i]->province,s[i]->score);
}
void sort(student *s[],int sn)
{
student * temp;
int i=0,j=0;
SetConsoleOutputCP(65001);
for( ;i<sn-1; i++)
{
for(j = i; j<sn; j++)
{
if(s[i]->score < s[j]->score)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
printf("\n");
//888888888888888888
FILE* fp1;
if((fp1=fopen("E:\\e.txt","w"))==NULL)
{
printf("File open erroe!\n");
exit(0);
}
//888888888888888888
//输出所有排序后学生的信息
SetConsoleOutputCP(936);
for(i=0;i<sn;i++)
{
printf("%s%16s%8s%16s%16s%8d\n",s[i]->number,s[i]->name,s[i]->sex,
s[i]->birth,s[i]->province,s[i]->score);
}
//888888888888888888888888888
//输出所有排序后学生的信息
for(i=0;i<sn;i++)
{
SetConsoleOutputCP(936);
fprintf(fp1,"%s%16s%8s%16s%16s%8d\n",s[i]->number,s[i]->name,s[i]->sex,
s[i]->birth,s[i]->province,s[i]->score);
}
if(fclose(fp1))
{
printf("Cant not close the file! \n");
exit(0);
}
Sleep(10000000);
//888888888888888888888888888
}
输入:
10903070101 张三丰 男 19851009 河南省 79
10903070102 郭靖 男 19851009 江苏省 85
10903070103 黄蓉 女 19851009 江苏省 98
10903070104 杨康 男 19851009 河北省 60
10903070105 令狐冲 男 19851009 河北省 88
10903070106 任盈盈 女 19851009 河北省 77
10903070107 洪七公 男 19851009 河南省 64
10903070108 欧阳锋 男 19851009 福建省 67
10903070109 阿紫 女 19851009 广西省 82
10903070110 段誉 男 19851009 云南省 69
10903070111 梅超风 女 19851009 广西省 72
10903070112 穆念慈 女 19851009 陕西省 69
10903070113 霍青桐 女 19851009 四川省 81
10903070114 陈家洛 男 19851009 四川省 73
10903070115 香香公主 女 19851009 西藏自治区 85
10903070116 乔峰 男 19851009 山东省 78
10903070117 虚竹 男 19851009 山东省 85
10903070118 王语嫣 女 19851009 湖北省 83
10903070119 张无忌 男 19851009 湖北省 78
10903070120 张翠山 男 19851009 湖北省 79
10903070121 殷素素 女 19851009 湖南省 65
10903070122 谢逊 男 19851009 四川省 86
10903070123 杨逍 男 19851009 贵州省 71
10903070124 纪晓芙 女 19851009 四川省 65
10903070125 赵敏 女 19851009 辽宁省 64
10903070126 杨过 男 19851009 陕西省 76
10903070127 小龙女 女 19851009 江西省 81
10903070128 胡斐 男 19851009 湖南省 80
10903070129 袁紫衣 女 19851009 广东省 91
10903070130 凤天南 男 19851009 陕西省 76
结果如下: