mituri

[C++](p) klasa student, wczytywanie pojedynczo slow z pliku

Jan 12th, 2016
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // zad1_Hawryl.cpp : Defines the entry point for the console application.
  2. //
  3. /*
  4. Dany jest plik z ocenami studentów. Każdy student opisany jest za pomocą nazwiska i ocen z LICZBA_OCEN przedmiotów.
  5. Dane studenta są wczytywane z pliku. Napisz program, który obliczy dla każdego studenta średnią ocenę i wyświetli wykaz
  6. tych ocen posortowanych według nazwisk. Opracuj potrzebną do tego zadania klasę StudentInfo.
  7. Program zgłasza wyjątki, gdy plik nie istnieje lub ocena wykracza poza zakres (0-6).
  8. */
  9. #include "stdafx.h"
  10. #include "string"
  11. #include <iostream>
  12. #include <cstdlib>
  13. #include <fstream>
  14. #include <sstream>
  15. #include <vector>
  16. #include <algorithm> // std::sort
  17.  
  18. #define LICZBA_OCEN 5
  19.  
  20. using namespace std;
  21.  
  22.  
  23. class Student{
  24. private:
  25. string nazwisko;
  26. int oceny[LICZBA_OCEN];
  27. public:
  28. void pobierzDaneStudenta(string studentLinia)
  29. {
  30. istringstream is(studentLinia);
  31. is >> nazwisko >> oceny[0] >> oceny[1] >> oceny[2] >> oceny[3] >> oceny[4] >> oceny[5];
  32. for(int i = 0; i < LICZBA_OCEN; i++)
  33. {
  34. if( !(oceny[i] > 0 && oceny[i] < 7) )
  35. {
  36. throw 0;
  37. }
  38. }
  39. }
  40.  
  41. double obliczSrednia()
  42. {
  43. double srednia = 0;
  44. for(int i = 0; i < LICZBA_OCEN; i++)
  45. {
  46. srednia += oceny[i];
  47. }
  48. srednia/=LICZBA_OCEN; // srednia/= LICZBA_OCEN to to samo co srednia = srednia/ LICZBA_OCEN
  49. return srednia;
  50. }
  51.  
  52. void pokazStudenta()
  53. {
  54. cout << "Nazwisko: " << nazwisko << " " << ", Oceny: ";
  55. for(int i = 0 ; i < LICZBA_OCEN; i++)
  56. {
  57. cout << oceny[i]<< " ";
  58. }
  59. }
  60.  
  61. friend bool operator < (const Student s1, const Student s2)
  62. {
  63. return (s1.nazwisko < s2.nazwisko);
  64. }
  65. };
  66.  
  67.  
  68. bool mojaFunkcja(Student s1, Student s2)
  69. {
  70. return (s1<s2);
  71. }
  72.  
  73.  
  74. class studentInfo
  75. {
  76. private:
  77. vector<Student> studentVec;
  78. public:
  79. studentInfo(fstream& file){
  80. while(!file.eof())
  81. {
  82. string linia;
  83. getline(file, linia);
  84. Student sTmp;
  85. try
  86. {
  87. sTmp.pobierzDaneStudenta(linia);
  88. }
  89. catch(int e)
  90. {
  91. cerr << "Zly Zakres Oceny" << endl;
  92. }
  93. studentVec.push_back(sTmp);
  94. }
  95. sort(studentVec.begin(), studentVec.end(), mojaFunkcja);
  96. }
  97.  
  98. void pokazStudentow()
  99. {
  100. if(!studentVec.empty())
  101. {
  102. for(int i = 0; i < studentVec.size(); i++)
  103. {
  104. studentVec[i].pokazStudenta();
  105. cout << ",srednia: " << studentVec[i].obliczSrednia() << endl;
  106. }
  107. }
  108. }
  109. };
  110.  
  111. int _tmain(int argc, _TCHAR* argv[])
  112. {
  113. string nazwaPliku = "text.txt";
  114. fstream file;
  115. file.open( nazwaPliku.c_str() , std::ios::in);
  116.  
  117. try
  118. {
  119. if( !file.good() )
  120. {
  121. string wBrakPliku;
  122. throw 0;
  123. }
  124. }
  125. catch(int e)
  126. {
  127. cout<< "Brak pliku!!!" << endl;
  128. }
  129.  
  130. studentInfo sInfo(file);
  131. sInfo.pokazStudentow();
  132.  
  133. getchar();
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment