/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:曹莉萍
*完成时间:2014年12月2日
*版本号:v1.0
*/
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
const int num = 8;
int main()
{
int i=0, j, t;
string temp;
string name[num] =
{ "zhao", "qian", "sun", "li", "zhou", "wu", "zheng", "wang" };
double score[num];
// 打开文件
ifstream infile("score.txt",ios::in);
if (!infile)
{
cerr<<"没有找到文件!"<<endl;
exit (1);
}
while (i<num)
{
infile>>score[i];
i++;
}
infile.close();
// (1)按成绩排序并输出结果
for (j = 1; j <= num-1; j++)
for (i = 0; i < num - j; i++)
if (score[i] < score[i + 1])
{
t = score[i];
temp = name[i];
score[i] = score[i + 1];
name[i] = name[i + 1];
score[i + 1] = t;
name[i + 1] = temp;
}
cout << "按成绩排序后输出的结果为:" << endl;
for (i = 0; i < num; i++)
cout << name[i] << ":" << score[i] << endl;
// (2)按姓名排序并输出结果
for (j = 1; j <= num-1; j++)
for (i = 0; i < num - j; i++)
if (name[i] > name[i + 1])
{
t = score[i];
temp = name[i];
score[i] = score[i + 1];
name[i] = name[i + 1];
score[i + 1] = t;
name[i + 1] = temp;
}
cout << "按姓名排序后输出的结果为:" << endl;
for (i = 0; i < num; i++)
cout << name[i] << ":" << score[i] << endl;
return 0;
}
运行结果
学习心得
有点不明白怎么回事了,去问问