Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<sstream>
- #include<algorithm>
- #include<string>
- #include<vector>
- #include<map>
- #include<numeric>
- using namespace std;
- typedef struct STUDENT_DATA {
- int id;
- string name;
- int marks[3];
- }StudentData, *PStudentData;
- void read(std::vector<StudentData> *pData)
- {
- ifstream fin;
- stringstream temp;
- StudentData tmp;
- string id, name, mark;
- fin.open("course.csv");
- string line;
- int i=0;
- while(getline(fin, line))
- {
- stringstream ss(line);
- getline(ss, id, ',');
- getline(ss, name, ',');
- for(int j=0;j<3;j++){
- getline(ss, mark, ',');
- tmp.marks[j] = atoi(mark.c_str());
- }
- tmp.id = atoi(id.c_str());
- tmp.name = name;
- pData->push_back(tmp);
- i++;
- }
- fin.close();
- }
- void print(StudentData data){
- std::cout<<std::endl;
- std::cout << "\nid: "<< data.id
- << "\nname: "<< data.name
- << "\nmarks:";
- for(int j=0;j<3;j++)
- std::cout << data.marks[j] << ((j<2)?", ":"");
- }
- void printAll(std::vector<StudentData> *pData){
- for(auto data:(*pData))
- print(data);
- }
- void sum(std::vector<StudentData> *pData)
- {
- int num;
- cout << "enter id: ";
- cin >> num;
- for (auto i:(*pData)){
- if (i.id == num) {
- std::cout << "name: "<< i.name
- << "\navg: "<< (float)std::accumulate(i.marks,i.marks + 3, 0) / 3;
- }
- }
- }
- void avg(std::vector<StudentData> *pData)
- {
- int exam;
- float sum = 0;
- cout << "enter exam no.: ";
- cin >> exam;
- for (auto i:(*pData)){
- sum += i.marks[exam-1];
- }
- std::cout << "avg in exam " <<exam<< ": "<< sum / (float)(pData->size());
- }
- void avgTotal(std::vector<StudentData> *pData)
- {
- float sum = 0;
- for (auto i:(*pData)){
- sum += std::accumulate(i.marks,i.marks + 3, 0);
- }
- std::cout << "overall avg: " << sum / (float)(pData->size());
- }
- void update(std::vector<StudentData> *pData){
- int id,tmp;
- char* tmpName;
- std::cout<<"enter id: ";std::cin>>id;
- for(int i=0;i<pData->size();i++){
- if((*pData)[i].id == id){
- //cout<<(*pData)[i].name;getch();
- std::cout<<"enter new details:-\n";
- std::cout<<"name: ";cin>>tmpName;
- (*pData)[i].name = tmpName;
- for(int j=0;j<3;j++){
- std::cout<<"marks in subject "<<j+1<<": ";cin>>(*pData)[i].marks[j];
- }
- break;
- }
- }
- }
- void store(std::vector<StudentData> *pData)
- {
- ofstream fout;
- //StudentData temp;
- string tmp;
- fout.open("tmp.csv",ios::trunc);
- if(fout.is_open()){
- for(auto i:(*pData)){
- tmp = std::to_string(i.id) + "," + i.name;
- for(int j=0;j<3;j++)
- tmp+= ("," + std::to_string(i.marks[j]));
- fout<<tmp<<'\n';
- }
- fout.close();
- std::cout<<"\nData saved!";
- }
- else{
- std::cout<<"\nerror: file cannot be created";
- }
- }
- void reload(std::vector<StudentData> *pData){
- read(pData);
- }
- void displaySortedAvg(std::vector<StudentData> *pData){
- int tmp;
- std::vector<int> sortedSubscript;
- for(int i=0;i<pData->size();i++)
- sortedSubscript.push_back(i);
- float highest = -1,avg1,avg2;
- for(int i=0;i<pData->size();i++){
- avg1 = (float)std::accumulate((*pData)[i].marks,(*pData)[i].marks + 3, 0) / 3;
- cout<< avg1 << ' ';
- for(int j=1;j<pData->size();j++){
- avg2 = (float)std::accumulate((*pData)[j].marks,(*pData)[j].marks + 3, 0) / 3;
- if(avg1 < avg2)
- highest = j;
- else
- highest = i;
- }
- tmp = sortedSubscript[i];
- sortedSubscript[i] = sortedSubscript[highest];
- sortedSubscript[highest] = tmp;
- }
- for(auto i:sortedSubscript)
- print((*pData)[i]);
- }
- void histogram(std::vector<StudentData> *pData){
- std::map<int,int> sortedGraphData;
- std::vector<int> x_axisPoints,y_axisValues;
- int tmp,k, maxMarks = -1;
- for(auto i:(*pData)){
- for(int j=0;j<3;j++){
- sortedGraphData[i.marks[j]] += 1;
- }
- }
- for(auto x:sortedGraphData)
- if(x.first > maxMarks)
- maxMarks = x.first;
- maxMarks = maxMarks - maxMarks%10 + 10;
- for(int j=10;j<=maxMarks;j+=10){
- tmp = 0;
- for(auto x:sortedGraphData){
- if(x.first <= j && x.first > j-10){
- tmp += x.second;
- }
- }
- if(tmp){
- x_axisPoints.push_back(tmp);
- }
- }
- y_axisValues = x_axisPoints;
- std::sort(y_axisValues.begin(),y_axisValues.end(),[](int x,int y)->bool{return (x>y);});
- y_axisValues.push_back(0);
- k=0;
- for(auto i:y_axisValues){
- std::cout<< i << ((i<10)?" ":" ") << char(179) << char(179);
- for(int j=10;j<=x_axisPoints.size()*10;j+=10){
- cout<<" "<<((x_axisPoints[(j/10) - 1] >= i)? (char)177:(char)32);
- }
- std::cout<<std::endl;
- }
- for(int i=0;i<x_axisPoints.size()*3 + 6;i++)
- cout<<((i == 3 || i == 4)? (char)216: (char)205);
- cout<<endl<<" "<<(char)179<<(char)179<<" ";
- for(int j=10;j<=x_axisPoints.size()*10;j+=10)
- cout<<j<<' ';
- }
- int main()
- {
- std::vector<StudentData> data;
- std::vector<StudentData> *pData = &data;
- int num;
- std::string homeText[] = { "1. print the content of the file on screen",
- "2. print the total grade of a student in an exam",
- "3. Print the average grade in a certain exam",
- "4. Print the average of total grade for all students",
- "5. Update student information",
- "6. Store the updated content of the array into the file",
- "7. Reload the content of the file",
- "8. display records according to the total grade in descending order",
- "9. draw a histogram for total grades",
- "10. Exit"
- };
- void (*lookUpTable[])(std::vector<StudentData>*) = {
- printAll,
- sum,
- avg,
- avgTotal,
- update,
- store,
- reload,
- displaySortedAvg,
- histogram
- };
- for(auto str:homeText){
- cout<<str<<endl;
- }
- read(pData);
- do {
- cout << "\n\nchoose option:";
- cin >> num;
- if(num <= 10 && num > 0){
- cout << endl << (homeText[num-1].c_str() + 3) << endl << endl;
- lookUpTable[num-1](pData);
- }
- } while (num != 10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement