Guest User

Untitled

a guest
Dec 24th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. class StudentInfo
  9. {
  10.  
  11. private:
  12. string fileName;
  13. int const static size = 5;
  14. string lastname;
  15. double grades[size];
  16. map<string, double> students;
  17. double AvarageGrade(double const arr[]); // liczenie sredniej
  18.  
  19. public:
  20. StudentInfo(string name);
  21. void DisplayResults(); // wyswietlanie wynikow
  22. map<string, double> LoadStudents(); // obsluga pliku, zapisywanie do kolekcji
  23.  
  24. };
  25.  
  26. StudentInfo::StudentInfo(string name) {
  27. fileName = name;
  28. LoadStudents();
  29. };
  30.  
  31. map<string, double> StudentInfo::LoadStudents() {
  32.  
  33. ifstream file;
  34.  
  35. file.open(fileName + ".txt", ios::in);
  36. if(!file.good()) {
  37. string wyjatek = "Wystapil problem z otwarciem pliku.";
  38. throw wyjatek;
  39. }
  40.  
  41. while( file >> lastname >> grades[0] >> grades[1] >> grades[2] >> grades[3] >> grades[4] )
  42. {
  43. double k = AvarageGrade(grades);
  44.  
  45. students[lastname] = k;
  46. }
  47.  
  48. return students;
  49. }
  50.  
  51.  
  52. void StudentInfo::DisplayResults() {
  53.  
  54. for( const auto &student : students){
  55. cout<<student.first << " ma srednia " << student.second<<endl;
  56. }
  57.  
  58. }
  59.  
  60. double StudentInfo::AvarageGrade(double const grades[]) {
  61. double tmp;
  62. double avarage;
  63. string wyjatek = "Zla ocena, poza zakrezem 0 - 6.";
  64. for(int i = 0; i < size; i++){
  65. if(grades[i] > 6 || grades[i]<0)
  66. throw wyjatek;
  67. tmp += grades[i];
  68. }
  69. avarage = tmp/size;
  70. return avarage;
  71. }
  72.  
  73. int main() {
  74.  
  75. StudentInfo foo("studenci");
  76.  
  77. try {
  78. foo.DisplayResults();
  79. }
  80. catch(string e) {
  81. cout<< e;
  82. }
  83. return 1;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment