关于成绩排名问题的程序

/*
说明:
[1]运行:
程序有一点小问题,运行输出的时候会出现重复。
*/
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NUM 100

class Information{
public:
char id[8], name[12];
int score;

public:
Information & operator=(Information &b)
{ strcpy(id,b.id);
strcpy(name,b.name);
score=b.score;
return *this;
}
};

void main()
{

char filename1[256],filename2[256];

cout<<"输入源文件全路径:";
cin>>filename1;

cout<<"输入目的文件全路径:";
cin>>filename2;

ifstream infile(filename1,ios::in|ios::nocreate);
ofstream outfile(filename2);

if(! infile){
cout<<"不能打开输入文件:"<<filename1<<’/n’;
exit(1);
}
if(! outfile){
cout<<"不能打开目的文件:"<<filename2<<’/n’;
exit(2);
}

Information student[MAX_NUM],temp;

char t_id[8],t_name[12];

int t_score,count=0,i;

while(infile>>t_id)
{
infile>>t_name>>t_score;
strcpy(student[count].id,t_id);
strcpy(student[count].name,t_name);
student[count].score=t_score;
count++;
}

for( i=0;i<count;i++)
for(int j=i+1;j<count;j++)
if(student[i].score<student[j].score)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}

for(i=0;i<count;i++)
outfile<<student[i].id<<"(A)"<<student[i].name<<"(B)"<<student[i].score<<’

’;
cout<<"已将排序后成绩写入"<<filename2<<"中。"<<endl;

infile.close();
outfile.close();

}
源文件内容:
45991901 王成 89
45991902 李名 75
45991903 张三 95
45991904 赵五 87
45991905 王立 88
45991906 孙羽政 86
45991907 周小国 85
45991908 范预 98
45991909 刘二 92
45991910 张宗 74
45991911 苏栏 73
45991912 赵新 71
45991913 曾梅每 70
45991914 李开创 69
45991915 姚为国 79
45991916 武彬 68
45991917 马三轮 61
45991918 左右 65

运行结果:
45991908范预(A)范预(B)98
45991903张三(A)张三(B)95
45991909刘二(A)刘二(B)92
45991901王成(A)王成(B)89
45991905王立(A)王立(B)88
45991904赵五(A)赵五(B)87
45991906孙羽政(A)孙羽政(B)86
45991907周小国(A)周小国(B)85
45991915姚为国(A)姚为国(B)79
45991902李名(A)李名(B)75
45991910张宗(A)张宗(B)74
45991911苏栏(A)苏栏(B)73
45991912赵新(A)赵新(B)71
45991913曾梅每(A)曾梅每(B)70
45991914李开创(A)李开创(B)69
45991916武彬(A)武彬(B)68
45991918左右(A)左右(B)65
45991917马三轮(A)马三轮(B)61

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注