1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int ROW = 10, CLM = 3;
void readfile(string names[], double stats[][CLM], int row);
void sort(string names[], double stats[][CLM], int row);
int main()
{
int i, j;
string names[10];
double stats[ROW][CLM];
readfile(names, stats, ROW);
sort(names, stats, ROW);
for (i=0; i < ROW; i++)
{
cout << names[i] << endl;
cout << stats[i][0] << endl;
cout << stats[i][1] << endl;
cout << stats[i][2] << endl << endl;
}
cin.get();
return 0;
}
void readfile(string names[], double stats[][CLM], int row)
{
int i, j;
ifstream inFile;
inFile.open("p1data.txt");
if (!inFile)
{
cout << "Cannot open input file. "
<< "Program terminates." << endl;
}
else
cout << "Open Successful\n\n";
for (i = 0; i < row; ++i)
{
inFile >> names[i];
for ( j = 0; j < CLM - 1; ++j)
{
inFile >> stats[i][j];
}
stats[i][CLM - 1] = 0;
}
inFile.close();
}
void sort(string names[], double stats[][CLM], int row)
{
string temp;
int iteration;
int index;
double temp2, temp3, temp4;
for (iteration = 1; iteration < row; iteration++)
{
for (index = 0; index < row -iteration; index++)
{
if (names[index] > names[index + 1])
{
temp = names[index];
temp2 = stats[index][0];
temp3 = stats[index][1];
temp4 = stats[index][2];
names[index] = names[index + 1];
stats[index][0] = stats[index + 1][0];
stats[index][1] = stats[index + 1][1];
stats[index][2] = stats[index + 1][2];
names[index + 1] = temp;
stats[index + 1][0] = temp2;
stats[index + 1][1] = temp3;
stats[index + 1][2] = temp4;
}
}
}
}
|