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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#include<iostream>
#include<string>
#include<vector>
#include <fstream>
#include <algorithm>
#include <functional>
#include <numeric>
using namespace std;
# pragma warning (disable:4996)
// score_data class
class score_data
{
private:
string Lastname;
string Firstname;
int First_Score;
int Second_Score;
int Third_Score;
int Fourth_Score;
int Fifth_Score;
int Sixth_Score;
int Seventh_Score;
int Eighth_Score;
int Ninth_Score;
int Tenth_Score;
public:
//constructors
score_data()
{
Lastname = "";
Firstname = "";
First_Score = 0;
Second_Score = 0;
Third_Score = 0;
Fourth_Score = 0;
Fifth_Score = 0;
Sixth_Score = 0;
Seventh_Score = 0;
Eighth_Score = 0;
Ninth_Score = 0;
Tenth_Score = 0;
}
score_data(string lastname, string firstname, int first_score, int second_score, int third_score, int fourth_score, int fifth_score, int sixth_score, int seventh_score, int eighth_score, int ninth_score, int tenth_score)
{
this->Lastname = lastname;
this->Firstname = firstname;
this->First_Score = first_score;
this->Second_Score = second_score;
this->Third_Score = third_score;
this->Fourth_Score = fourth_score;
this->Fifth_Score = fifth_score;
this->Sixth_Score = sixth_score;
this->Seventh_Score = seventh_score;
this->Eighth_Score = eighth_score;
this->Ninth_Score = ninth_score;
this->Tenth_Score = tenth_score;
}
operator double() { return (double)First_Score; }
//accessor
string getLastname()
{
return Lastname;
}
void display() { cout << Lastname << " " << Firstname << " " << First_Score << endl; }
//destructor
~score_data(){}
friend score_data operator + (const score_data& sd_1)
{
return score_data(sd_1.Lastname, sd_1.Firstname, sd_1.First_Score + sd_1.Second_Score + sd_1.Third_Score + sd_1.Fourth_Score + sd_1.Fifth_Score + sd_1.Sixth_Score + sd_1.Seventh_Score + sd_1.Eighth_Score + sd_1.Ninth_Score + sd_1.Tenth_Score, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
};
template <typename T>
class score_data_container
{
private:
vector<T*> Score_DataVector;
public:
score_data_container() {}
void InsertData(T* score)
{
Score_DataVector.push_back(score);
double average_score = accumulate(Score_DataVector.begin(), Score_DataVector.end(), 0.0, plus<score_data>())/3;
cout << average_score << endl;
}
bool rule(const score_data fir, const score_data sec)
{
if (fir.Lastname > sec.Lastname)
{
return true;
}
else
{
return false;
}
}
sort(Score_DataVector.begin(), Score_DataVector.end(), rule); // not work
void show()
{
int a = 3;
int b = 0;
do
{
//Score_DataVector[b]->display();
b++;
} while (b < a);
}
~score_data_container()
{
vector<T*>().swap(Score_DataVector);
}
};
//Function that reads input from file.
bool readinput(score_data_container<score_data> *container, ifstream &infile)
{
infile.open("Scores.csv");
if (infile)
{
string line;
string Lastname;
string Firstname;
int First_Score;
int Second_Score;
int Third_Score;
int Fourth_Score;
int Fifth_Score;
int Sixth_Score;
int Seventh_Score;
int Eighth_Score;
int Ninth_Score;
int Tenth_Score;
while (getline(infile, line))
{
char *c_line = new char[line.length() + 1];
strcpy(c_line, line.c_str());
string str(c_line);
char *lastname = strtok(c_line, ",");
char *firstname = strtok(NULL, ",");
char *first_score = strtok(NULL, ",");
char *second_score = strtok(NULL, ",");
char *third_score = strtok(NULL, ",");
char *fourth_score = strtok(NULL, ",");
char *fifth_score = strtok(NULL, ",");
char *sixth_score = strtok(NULL, ",");
char *seventh_score = strtok(NULL, ",");
char *eighth_score = strtok(NULL, ",");
char *ninth_score = strtok(NULL, ",");
char *tenth_score = strtok(NULL, ",");
Lastname.assign(lastname);
Firstname.assign(firstname);
First_Score = atoi(first_score);
Second_Score = atoi(second_score);
Third_Score = atoi(third_score);
Fourth_Score = atoi(fourth_score);
Fifth_Score = atoi(fifth_score);
Sixth_Score = atoi(sixth_score);
Seventh_Score = atoi(seventh_score);
Eighth_Score = atoi(eighth_score);
Ninth_Score = atoi(ninth_score);
Tenth_Score = atoi(tenth_score);
score_data *Score_Data = new score_data(Lastname, Firstname, First_Score, Second_Score, Third_Score, Fourth_Score, Fifth_Score, Sixth_Score, Seventh_Score, Eighth_Score, Ninth_Score, Tenth_Score);
container->InsertData(Score_Data);
}
}
else
return false;
}
int main()
{
ifstream infile;
score_data_container<score_data> *container = new score_data_container < score_data > ;
if (!readinput(container, infile))
{
cout << "Error opening the file!";
return -1;
}
container->show();
return 0;
}
|