我已經嘗試過幾乎所有的東西。只是尋找一些提示。從文件讀取數據到結構中,排序數據並寫入文件
該項目是從一個文件[「racers2011.txt」]中讀取數據到一個結構中,並對男性的比賽時間進行排序,並對女性的比賽時間進行排序。他們將男性和女性分組並將他們的等級和比賽時間輸出,作爲他們最好的藍色比賽和最好的紅色比賽加起來。我已經讀入文件並將其輸出到新文件,但無法弄清如何對文件進行排序。
如果有人可以幫我一下,我將不勝感激。
這是我迄今(一些我不編譯代碼,所以我曾評論它)代碼:
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
struct Racer_struct
{
int bib;
char sex;
char fname[30];
char lname[30];
double b1, b2, r1, r2;
};
bool connectInFile(ifstream& fin, char infilename[]);
bool connectOutFile(ofstream& fout, char outfilename[]);
void readData(ifstream& fin, Racer_struct racers[], const int& MAX);
//void racerGender(ostream& fout, Racer_struct racers[], const int& MAX);
//double calcTotalTime(Racer_struct racers[], double total[], const int& MAX);
void writeData(ostream& fout, Racer_struct racers[], const int& MAX);
int main()
{
const int MAX = 38;
Racer_struct racers[MAX];
// double total[MAX];
ifstream fin;
ofstream fout;
char in_file[30], out_file[30];
bool opened;
char title[79];
opened = connectInFile(fin, in_file);
cout << opened << endl;
opened = connectOutFile(fout, out_file);
cout << opened << endl;
if(opened)
{
cout << "CONNECTED to: " << in_file << endl;
cout << "WRITING to: " << out_file << endl;
for(int i=0; i<=3; i++)
{
fin.getline(title, 80);
fout << title << "\n";
}
}
readData(fin, racers, MAX);
writeData(fout, racers, MAX);
fin.close();
fout.close();
cout << endl;
return 0;
}
bool connectInFile(ifstream& fin, char infilename[])
{
bool success = true;
cout << "Enter input filename: ";
cin >> infilename;
fin.open(infilename);
if(fin.fail())
success = false;
return success;
}
bool connectOutFile(ofstream& fout, char outfilename[])
{
bool opened = true;
cout << "Enter the filename you wish to write to: ";
cin >> outfilename;
fout.open(outfilename);
if(fout.fail())
opened = false;
return opened;
}
void readData(ifstream& fin, Racer_struct racers[], const int& MAX)
{
char ws;
for(int i=0; i<MAX && fin.peek()!= EOF; i++)
{
fin >> racers[i].bib >> racers[i].sex >> racers[i].fname >> racers[i].lname
>> racers[i].b1 >> racers[i].b2 >> racers[i].r1 >> racers[i].r2;
fin.get(ws);
}
}
/*
void racerGender(ostream& fout, Racer_struct racers[], const int& MAX)
{
for(int i=0; i<MAX; i++)
if(racers[i].sex == 'M')
{
calcTotalTime(racers, total, MAX);
writeData(fout, racers, MAX);
}
else
{
calcTotalTime(racers, total, MAX);
writeData(fout, racers, MAX);
}
}
double calcTotalTime(Racer_struct racers[], double total[], const int& MAX)
{
double total[MAX];
for(int i=0; i<MAX; i++)
if(racers[i].r1 > racers[i].r2 && racers[i].b1 > racers[i].b2)
total[i] = racers[i].r2 + racers[i].b2;
else if(racers[i].r2 > racers[i].r1 && racers[i].b2 > racers[i].b1)
total[i] = racers[i].r1 + racers[i].b1;
else if(racers[i].r1 > racers[i].r2 && racers[i].b2 > racers[i].b1)
total[i] = racers[i].r2 + racers[i].b1;
else
total[i] = racers[i].b2 + racers[i].r1;
return total[i];
}
*/
void writeData(ostream& fout, Racer_struct racers[], const int& MAX)
{
for(int i=0; i<MAX; i++)
{
fout << racers[i].bib << "\t" << racers[i].sex << "\t" << racers[i].fname
<< "\t" << racers[i].lname << "\t" << racers[i].b1 << "\t" << racers[i].b2
<< "\t" << racers[i].r1 << "\t" << racers[i].r2 /*<< "\t" << total[i]*/ << endl;
/* if((i+1)%5)
fout << "\t";
else
fout << endl;
*/
}
}
,這是家庭作業,學期結束了,我需要這個研究我的決賽。 「使用std :: sort」實際上並不是很有幫助。你能詳細闡述一下嗎?如果你可以看看我的代碼,並告訴我我的某些東西出了問題。那將是真棒。但是,任何幫助都比沒有幫助好,所以謝謝。 – 2011-04-25 00:17:46
嘗試在代碼中使用std :: sort。它應該是非常簡單的,你只需傳遞一些參數來顯示你的容器在哪裏,並給它一個函數來做比較。如果你使用了一個像clang(而不是g ++)的良好編譯器,你應該能夠很好地將它與文檔和編譯器的診斷結合起來。 – 2011-04-25 00:38:56
不會在我的情況下更容易使用交換功能?計算總的比賽時間並交換它們,直到男性和女性排名適當,然後以這種方式將它們放入文件中。 – 2011-04-25 06:21:24