/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘畅
* 完成日期:2014 年 11 月 27 日
* 版 本 号:v1.0
*
* 问题描述:将上一个项目修改后做成了文件;
* 输入描述:···;
* 程序输出:···。
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int num = 100;
int main()
{
int i=0, j, t;
string temp;
string name[num];
double score[num];
ifstream infile("score.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while (infile>>name[i]>>score[i])
i++;
infile.close();
for (j = 1; j < num; ++j)
for (i = 0; i < num-j; ++i)
{
if (score[i] < score[i + 1])
{
t = score[i];
score[i] = score[i + 1];
score[i + 1] = t;
temp = name[i];
name[i] = name[i + 1];
name[i + 1] = temp;
}
}
ofstream outfile("score1.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout << "按成绩排序并输出结果:" << endl;
for (i=0; i<num; ++i)
{
outfile<<"第"<<i+1<<"名:"<<name[i]<<" 得分: "<<score[i]<<endl;
}
cout<<endl;
outfile.close();
for (j=1; j<num; ++j)
for (i=0; i<num-j; ++i)
{
if (name[i]>name[i+1])
{
temp = name[i];
name[i] = name[i + 1];
name[i + 1] = temp;
t = score[i];
score[i] = score[i + 1];
score[i + 1] = t;
}
}
ofstream outfile1("score2.txt",ios::out);
if(!outfile1)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"按姓名排序并输出结果:"<<endl;
for (i=0; i<num; ++i)
{
outfile1<<name[i]<<" 得分:"<<score[i]<<endl;
}
outfile1.close();
return 0;
}
运行结果:
按成绩排序并输出结果:
(2)按照姓名排序并输出结果:
学习心得:
还是有点小问题,有很多错误的行数,不知道怎么改正确,求教!