Advertisement
KAR98S

Shami's work

May 15th, 2020 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<sstream>
  4. #include<algorithm>
  5. #include<string>
  6. #include<vector>
  7. #include<map>
  8. #include<numeric>
  9.  
  10. using namespace std;
  11. typedef struct STUDENT_DATA {
  12.     int id;
  13.     string name;
  14.     int marks[3];
  15. }StudentData, *PStudentData;
  16.  
  17. void read(std::vector<StudentData> *pData)
  18. {
  19.     ifstream fin;
  20.     stringstream temp;
  21.     StudentData tmp;
  22.     string id, name, mark;
  23.     fin.open("course.csv");
  24.     string line;
  25.     int i=0;
  26.     while(getline(fin, line))
  27.     {
  28.         stringstream ss(line);
  29.         getline(ss, id, ',');
  30.         getline(ss, name, ',');
  31.         for(int j=0;j<3;j++){
  32.             getline(ss, mark, ',');
  33.             tmp.marks[j] = atoi(mark.c_str());
  34.         }
  35.         tmp.id = atoi(id.c_str());
  36.         tmp.name = name;
  37.         pData->push_back(tmp);
  38.         i++;
  39.  
  40.     }
  41.     fin.close();
  42. }
  43.  
  44. void print(StudentData data){
  45.     std::cout<<std::endl;
  46.     std::cout   << "\nid: "<< data.id
  47.                 << "\nname: "<< data.name
  48.                 << "\nmarks:";
  49.     for(int j=0;j<3;j++)
  50.         std::cout << data.marks[j] << ((j<2)?", ":"");
  51. }
  52.  
  53. void printAll(std::vector<StudentData> *pData){
  54.     for(auto data:(*pData))
  55.         print(data);
  56. }
  57.  
  58. void sum(std::vector<StudentData> *pData)
  59. {
  60.     int num;
  61.     cout << "enter id: ";
  62.     cin >> num;
  63.     for (auto i:(*pData)){
  64.         if (i.id == num) {
  65.             std::cout   << "name: "<< i.name
  66.                         << "\navg: "<< (float)std::accumulate(i.marks,i.marks + 3, 0) / 3;
  67.         }
  68.     }
  69. }
  70.  
  71. void avg(std::vector<StudentData> *pData)
  72. {
  73.     int exam;
  74.     float sum = 0;
  75.     cout << "enter exam no.: ";
  76.     cin >> exam;
  77.     for (auto i:(*pData)){
  78.         sum += i.marks[exam-1];
  79.     }
  80.     std::cout << "avg in exam " <<exam<< ": "<< sum / (float)(pData->size());
  81. }
  82.  
  83. void avgTotal(std::vector<StudentData> *pData)
  84. {
  85.     float sum = 0;
  86.     for (auto i:(*pData)){
  87.         sum += std::accumulate(i.marks,i.marks + 3, 0);
  88.     }
  89.     std::cout << "overall avg: " << sum / (float)(pData->size());
  90. }
  91.  
  92. void update(std::vector<StudentData> *pData){
  93.     int id,tmp;
  94.     char* tmpName;
  95.     std::cout<<"enter id: ";std::cin>>id;
  96.     for(int i=0;i<pData->size();i++){
  97.         if((*pData)[i].id == id){
  98.             //cout<<(*pData)[i].name;getch();
  99.             std::cout<<"enter new details:-\n";
  100.             std::cout<<"name: ";cin>>tmpName;
  101.             (*pData)[i].name = tmpName;
  102.             for(int j=0;j<3;j++){
  103.                 std::cout<<"marks in subject "<<j+1<<": ";cin>>(*pData)[i].marks[j];
  104.             }
  105.             break;
  106.  
  107.         }
  108.     }
  109. }
  110.  
  111. void store(std::vector<StudentData> *pData)
  112. {
  113.     ofstream fout;
  114.     //StudentData temp;
  115.     string tmp;
  116.     fout.open("tmp.csv",ios::trunc);
  117.     if(fout.is_open()){
  118.         for(auto i:(*pData)){
  119.             tmp = std::to_string(i.id) + "," + i.name;
  120.             for(int j=0;j<3;j++)
  121.                 tmp+= ("," + std::to_string(i.marks[j]));
  122.             fout<<tmp<<'\n';
  123.         }
  124.         fout.close();
  125.         std::cout<<"\nData saved!";
  126.     }
  127.     else{
  128.         std::cout<<"\nerror: file cannot be created";
  129.     }
  130. }
  131.  
  132. void reload(std::vector<StudentData> *pData){
  133.     read(pData);
  134. }
  135.  
  136. void displaySortedAvg(std::vector<StudentData> *pData){
  137.     int tmp;
  138.     std::vector<int> sortedSubscript;
  139.     for(int i=0;i<pData->size();i++)
  140.         sortedSubscript.push_back(i);
  141.     float highest = -1,avg1,avg2;
  142.     for(int i=0;i<pData->size();i++){
  143.         avg1 = (float)std::accumulate((*pData)[i].marks,(*pData)[i].marks + 3, 0) / 3;
  144.         cout<< avg1 << ' ';
  145.         for(int j=1;j<pData->size();j++){
  146.             avg2 = (float)std::accumulate((*pData)[j].marks,(*pData)[j].marks + 3, 0) / 3;
  147.             if(avg1 < avg2)
  148.                 highest = j;
  149.             else
  150.                 highest = i;
  151.         }
  152.         tmp = sortedSubscript[i];
  153.         sortedSubscript[i] = sortedSubscript[highest];
  154.         sortedSubscript[highest] = tmp;
  155.     }
  156.     for(auto i:sortedSubscript)
  157.         print((*pData)[i]);
  158. }
  159.  
  160.  void histogram(std::vector<StudentData> *pData){
  161.      std::map<int,int> sortedGraphData;
  162.      std::vector<int> x_axisPoints,y_axisValues;
  163.      int tmp,k, maxMarks = -1;
  164.  
  165.      for(auto i:(*pData)){
  166.         for(int j=0;j<3;j++){
  167.             sortedGraphData[i.marks[j]] += 1;
  168.         }
  169.      }
  170.      for(auto x:sortedGraphData)
  171.         if(x.first > maxMarks)
  172.             maxMarks = x.first;
  173.      maxMarks = maxMarks - maxMarks%10 + 10;
  174.      for(int j=10;j<=maxMarks;j+=10){
  175.         tmp = 0;
  176.         for(auto x:sortedGraphData){
  177.         if(x.first <= j && x.first > j-10){
  178.             tmp += x.second;
  179.             }
  180.         }
  181.         if(tmp){
  182.             x_axisPoints.push_back(tmp);
  183.         }
  184.     }
  185.  
  186.     y_axisValues = x_axisPoints;
  187.     std::sort(y_axisValues.begin(),y_axisValues.end(),[](int x,int y)->bool{return (x>y);});
  188.     y_axisValues.push_back(0);
  189.  
  190.     k=0;
  191.     for(auto i:y_axisValues){
  192.        std::cout<< i << ((i<10)?"  ":" ") << char(179) << char(179);
  193.        for(int j=10;j<=x_axisPoints.size()*10;j+=10){
  194.             cout<<"  "<<((x_axisPoints[(j/10) - 1] >= i)? (char)177:(char)32);
  195.  
  196.        }
  197.        std::cout<<std::endl;
  198.      }
  199.      for(int i=0;i<x_axisPoints.size()*3 + 6;i++)
  200.         cout<<((i == 3 || i == 4)? (char)216: (char)205);
  201.      cout<<endl<<"   "<<(char)179<<(char)179<<"  ";
  202.      for(int j=10;j<=x_axisPoints.size()*10;j+=10)
  203.         cout<<j<<' ';
  204.  }
  205.  
  206. int main()
  207. {
  208.         std::vector<StudentData> data;
  209.         std::vector<StudentData> *pData = &data;
  210.         int num;
  211.         std::string homeText[] = {  "1. print the content of the file on screen",
  212.                                     "2. print the total grade of a student in an exam",
  213.                                     "3. Print the average grade in a certain exam",
  214.                                     "4. Print the average of total grade for all students",
  215.                                     "5. Update student information",
  216.                                     "6. Store the updated content of the array into the file",
  217.                                     "7. Reload the content of the file",
  218.                                     "8. display records according to the total grade in descending order",
  219.                                     "9. draw a histogram for total grades",
  220.                                     "10. Exit"
  221.         };
  222.         void (*lookUpTable[])(std::vector<StudentData>*) = {
  223.             printAll,
  224.             sum,
  225.             avg,
  226.             avgTotal,
  227.             update,
  228.             store,
  229.             reload,
  230.             displaySortedAvg,
  231.             histogram
  232.         };
  233.         for(auto str:homeText){
  234.             cout<<str<<endl;
  235.         }
  236.         read(pData);
  237.         do {
  238.             cout << "\n\nchoose option:";
  239.             cin >> num;
  240.             if(num <= 10 && num > 0){
  241.                 cout << endl << (homeText[num-1].c_str() + 3) << endl << endl;
  242.                 lookUpTable[num-1](pData);
  243.             }
  244.         } while (num != 10);
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement