#include <bits/stdc++.h>
#include <sstream>
using namespace std;

//Feng Yiyang A20EC4020// Tanshiba Naorin Prapti A20EC4051 // MD Sirajum Muneer A20EC4035//
//PT1 Group 2 
float grade_point(string grade){
    if ("A" == grade)
        return 4.0;
    if ("A-" == grade)
        return 3.67;
    if ("B+" == grade)
        return 3.33;
    if ("B" == grade)
        return 3.0;
    if ("B-" == grade)
        return 2.67;
    if ("C+" == grade)
        return 2.33;
    return 0.0;
}

//main function
int main()
{

    //filestream variable file
    fstream file;
    //data will store all the records
    string data[100][13], word;
    //t means total words, r means row
    //k means columns
    int k=0, r=-1, t=-1;

    //opening file
    file.open("student.txt");

    //extracting words from the file
    while (file >> word)
    {
        //storing the word in the 2D file
        t++;
        if (t%7 == 0){
            k=0;
            r++;
        }

        data[r][k++]=word;
    }
    //closing the file
    file.close();

    //total no. of records
    t = t/7+1;

    //to store the average GPA of the class
    float avg_GPA = 0;
    //for the students who score above 3.5
    string names[100];
    int l=-1;

    //iterating for the each record
    for (int i=0; i<t; i++){

        //temp for storing the grade points of the 5 subjects
        float GPA=0, temp[5];

        //calculating the GPA
        for (int j=0; j<5; j++){
            //calling the function to convert grade to points
            temp[j] = grade_point(data[i][2+j]);
            //storing the ans in the string 2D array
            std::ostringstream ss;
            ss<<temp[j];
            data[i][7+j] = ss.str();

            //if ans is 0, changing it to 0.0
            if (data[i][7+j] == "0")
                data[i][7+j] = "0.0";
        }

        //calculating the GPA
        GPA = (temp[0]*3 + temp[1]*3 + temp[2]*3 +
                temp[3]*2 + temp[4]*2)/(3+3+3+2+2);
        avg_GPA += GPA;

        if (GPA >= 3.5)
            names[++l] = data[i][1];

        //storing the GPA in the string format in the array
        std::ostringstream ss;
        ss<<GPA;
        data[i][12] = ss.str();

    }
    std::ostringstream ss;
    ss<<avg_GPA/t;
    string average_GPA = ss.str();

    //opening the file for writing
    ofstream ofile;
    ofile.open("Grades.txt");

    //writing in the file (heading)
    ofile<<"Matric no   Name                     ";
    ofile<<"Sub1\tSub2\tSub3\tSub4\tSub5\tGPA\n";
    //writing each line of the records in the file
    for (int i=0; i<t; i++){
        ofile<<data[i][0]<<"      ";
        ofile<<data[i][1];
        for(int j=0; j<25-data[i][1].size(); j++)
            ofile<<" ";
        for (int j=7; j<13; j++)
            ofile<<data[i][j]<<"\t";
        ofile<<"\n";
    }

    //write the Average GPA and students names getting GPA 
    //greater than and equal to 3.5
    ofile<<"\n\nAverage GPA  of all students: "<<average_GPA;
    ofile<<"\n\nList of Students who get GPA 3.5 and above:\n\n";
    for (int i=0; i<=l; i++){
        std::ostringstream ss;
        ss<<i+1;

        ofile<<ss.str()<<"."<<names[i];
    }
    //closing the file
    ofile.close();

    //returning 0 to the main function
    return 0;
}
