class StudenR { string Name; int NumGroup; string Sex; int Marks[5]; int AVM; public: StudenR(); StudenR(ifstream &fin); bool Read(ifstream &fin); void Write(ofstream &fout); int AvMark(); void SetMarks(int &marks); int GetMarks(int i) { return Marks[i]; } int GetAvMark() {return AVM;} ~StudenR(); }; #include "stdafx.h" #include "StudenR.h" using namespace std; StudenR::StudenR() { Name = "<unknown>"; NumGroup = 0; Sex = 'x'; for (int i = 0; i < 5; i++) Marks[i] = 0; } StudenR::StudenR(ifstream &fin) { fin >> Name; fin >> NumGroup; fin >> Sex; for (int i = 0; i < 5; i++) { fin >> Marks[i]; AVM = AvMark(); } } bool StudenR::Read(ifstream &fin) { if (!(fin >> Name))return false; if (!(fin >> NumGroup))return false; if (!(fin >> Sex))return false; for (int i = 0; i < 5; i++) if (!(fin >> Marks[i]))return false; return true; } int StudenR::AvMark() { int AvMark = 0; for (int i = 0; i < 5; i++) { AvMark += Marks[i]; } AvMark /= 5; return AvMark; } void StudenR::SetMarks(int &marks) { for (int i = 0; i<5; i++) this->Marks[i] = marks; } void StudenR::Write(ofstream &fout) { fout << Name << " " << NumGroup << " " << Sex << " "; for (int i = 0; i < 5; i++) { fout << Marks[i] << " "; } fout << endl; } StudenR::~StudenR() { } #include "stdafx.h" #include "StudenR.h" using namespace std; void Read1(StudenR*students, ifstream &fin, int &n); void Write(StudenR*students, ofstream &fout, int n); void WriteAvM(ofstream&fout, StudenR*students, int n); void Sort(StudenR*&studets, int n); int main() { ifstream fin("Text.txt"); ofstream fout("res.txt"); StudenR *students = new StudenR[100]; int n = 0; Read1(students, fin, n); Write(students, fout, n); fout << endl << endl << endl; WriteAvM(fout, students, n); fout << endl << endl << endl; Sort(students, n); Write(students, fout, n); fin.close(); fout.close(); delete[]students; return 0; } void Read1(StudenR *students, ifstream &fin, int &n) { n = 0; for (; !fin.eof(); n++) { if (!students[n].Read(fin))return; } } void Write(StudenR *students, ofstream &fout, int n) { for (int i = 0; i < n; i++) students[i].Write(fout); } void WriteAvM(ofstream &fout, StudenR *students, int n) { for (int i = 0; i < n; i++) { students[i].Write(fout); int AvMark = students[i].AvMark(); fout << "AvMark" << i + 1 << "=" << AvMark << endl; } } void Sort(StudenR*&students, int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (students[j].GetAvMark()<students[j + 1].GetAvMark()) { StudenR s = students[j]; students[j] = students[j + 1]; students[j + 1] = s; } } } } |
1 answer
You need to write this:
for (int i = 0; i < n; ++i) { for (int j = i; j < n - i; ++j) { if (students[j].GetAvMark()<students[j + 1].GetAvMark()) //... So you sort by kill, and if you want to sort in ascending order, change the comparison sign
- The function itself does not sort in any way - ANA
- Well, I wrote what needs to be changed, and you compare with what you wrote - AR Hovsepyan
- doesn't sort anyway - ANA
- and from what you took that does not sort? Take any array of integers and insert the element of this array instead of students [j] .GetAvMark (). If the array is sorted, then check if the GetAvMark () returns the desired number? .. I don't know what you were trying to do. Maybe your file does not have the appropriate data to get fin >> Marks [i]; - AR Hovsepyan
- In the output file, only the average rating is performed, but not sorted by descending average rating. - ANA
|
i, since the leading element is already the smallest. - VTT