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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
struct data{
string name;
int ID;
//string subjectNames[7];
int subjectNumbers[7];
double Average;
data(string n1, int a1, int a2[], double a3=0){
name = n1;
ID=a1;
Average=0;
for(int i=0; i<7 ; i++){
subjectNumbers[i] = a2[i];
Average += a2[i];
}
Average = Average/7; //Calculates Average, instead of Average = a3;
}
};
int main(){
ifstream in("input.txt");
ofstream out("output.txt");
vector<data> vec;
string s1, s2, name;
int id, k;
int numbers[7];
double Average;
while(getline(in,s1)){ //Name
name =s1; // or formatted
s2="";
getline(in,s1); //ID
for(auto x:s1) if(x >= '0' && x <= '9') s2+= x;
id = stoi(s2);
for(int i=0; i<7; i++){
getline(in,s1); // 7 Subjects
s2="";
for(auto x:s1) if(x >= '0' && x <= '9') s2+= x;
numbers[i]= stoi(s2);
}
getline(in,s1); // Average
getline(in,s1); // ";"
getline(in,s1); // "\n"
vec.emplace_back(data(name,id,numbers)); //without average, subjectName
}
for(int i=0; i<vec.size(); i++){ // selection sort by Average (descending)
int highestIndex = i;
for(int j=i+1; j<vec.size(); j++){
if(vec[i].Average < vec[j].Average ){
highestIndex = j;
}
}
if(i!=highestIndex) std::swap(vec[i], vec[highestIndex]);
}
for(auto x:vec){
out << x.name << "\n"
<< "ID:" << x.ID << "\n";
for(int i=0; i<7; i++){
switch(i){
case 0: out << "Kurdish:"; break;
case 1: out << "Arabic:"; break;
case 2: out << "English:"; break;
case 3: out << "Mathematics:"; break; // Mathematic in file
case 4: out << "Physics:"; break;
case 5: out << "Biology:"; break;
case 6: out << "Chemistry:"; break; // Chemical in file
}
out << x.subjectNumbers[i] << "\n";
}
out <<"Average=" << x.Average << "\n"
<< ";\n\n";
}
in.close();
out.close();
return 0;
}
|