Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <windows.h>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <string>
  7. using namespace std;
  8.  
  9. class Osoba //klasa osoba-klasa podstawowa
  10. {
  11. protected: //dane ukryte
  12. void pokaz_dane()
  13. {
  14. cout<<imie<<" "<<nazwisko<<" "<<wiek<<" lat"<<endl; //Wyœwietlanie imienia,nazwiska,wieku
  15.  
  16. }
  17. public:
  18. string imie;
  19. string nazwisko;
  20. int wiek;
  21. void wpisz_dane() //Wpisywanie z klawiatury imienia,nazwiska i wieku
  22. {
  23. cout<<"Wpisz swoje imie:"<<endl;
  24. cin >>imie;
  25. cout<<"Wpisz swoje nawisko:"<<endl;
  26. cin>>nazwisko;
  27. cout<<"Podaj swoj wiek:"<<endl;
  28. cin>>wiek;
  29. system("cls");
  30. pokaz_dane();//Wywołanie funkcji "pokaz_dane" ze składników prywatnych
  31. }
  32.  
  33. Osoba(); //konstruktor
  34.  
  35. };
  36.  
  37. Osoba::Osoba() //definicja konstruktora
  38. {
  39.  
  40. };
  41. class Student:public Osoba //klasa Student-klasa pochodna
  42. {
  43. private:
  44. void pokaz_dane() //Dane ukryte
  45. {
  46. cout <<grupa<<endl;
  47. }
  48. public:
  49. int grupa;
  50. void wpisz_dane()
  51. {
  52.  
  53. cout<<"Podaj numer grupy:"<<endl; //Wpisywanie numeru grupy z klawiatury
  54. cin>>grupa;
  55. system("cls");
  56. pokaz_dane();//Wywo³anie funkcji "pokaz_dane" ze sk³adników prywatnych
  57. }
  58.  
  59.  
  60. Student(); //Konstruktor
  61. };
  62. Student::Student() //Definicja konstruktora
  63.  
  64. {
  65.  
  66. }
  67.  
  68.  
  69. class Database
  70. {
  71. public:
  72. Database()
  73. {
  74. fstream plik;
  75. int i = 0;
  76. while(true)
  77. {
  78. plik.open("student_database.txt", ios::in);
  79. if(plik.good() == true)
  80. {
  81. Student st = Student();
  82. string dane;
  83. int nr_linii = 1;
  84. while(getline(plik, dane))
  85. {
  86. if(nr_linii==1)
  87. st.imie = dane;
  88. else if(nr_linii==2)
  89. st.nazwisko = dane;
  90. else if(nr_linii==3)
  91. {
  92. st.wiek = atoi(dane.c_str());
  93. }
  94.  
  95. else
  96. {
  97. st.grupa = atoi(dane.c_str());
  98. student_database.push_back(st);
  99. nr_linii=0;
  100. }
  101. nr_linii++;
  102. }
  103.  
  104. }
  105. else
  106. break;
  107. }
  108.  
  109. }
  110. vector<Student> student_database;
  111. void create_new_student()
  112. {
  113. Student st = Student();
  114. cout << "Imie: ";
  115. cin >> st.imie;
  116. cout << "Nazwisko: ";
  117. cin >> st.nazwisko;
  118. cout << "Wiek: ";
  119. cin >> st.wiek;
  120. cout << "Grupa: ";
  121. cin >> st.grupa;
  122. student_database.push_back(st);
  123. }
  124. void show_all_students()
  125. {
  126. cout<< "NUMER INDEKSU / IMIE / NAZWISKO" << endl;
  127. Student st = Student();
  128. for(int i=0;i<student_database.capacity();i++)
  129. {
  130. st = student_database[i];
  131. cout << i;
  132. cout << ". " + st.imie + " " + st.nazwisko<<endl;
  133. }
  134. system("pause");
  135. }
  136. void delete_student()
  137. {
  138. int id;
  139. cout << "Podaj ID studenta, ktorego zamierzasz usunac: ";
  140. cin >> id;
  141. student_database.erase(student_database.begin() + id);
  142. }
  143. void end_program()
  144. {
  145.  
  146. for (int i=0; i<student_database.capacity();i++)
  147. {
  148. string nazwa_pliku = i + ".txt";
  149. fstream plik("student_database.txt", ios::out);
  150. Student st = Student();
  151. st = student_database[i];
  152. plik << st.imie + "\n";
  153. plik << st.nazwisko+ "\n";
  154. plik << st.wiek;
  155. plik << "\n";
  156. plik << st.grupa;
  157. }
  158. exit(0);
  159.  
  160. }
  161. void program_controller()
  162. {
  163. system("cls");
  164. cout << "Co chcesz zrobić?" << endl;
  165. cout << "<L> Wylistowanie studentow" << endl;
  166. cout << "<D> Dodanie nowego studenta" << endl;
  167. cout << "<U> Usun studenta" << endl;
  168. cout << "<K> Zapisz i wyjdz" << endl;
  169. char wybor;
  170. cin >> wybor;
  171. switch(wybor)
  172. {
  173. case 'L':
  174. {
  175. system("cls");
  176. show_all_students();
  177. program_controller();
  178. }
  179. case 'D':
  180. {
  181. system("cls");
  182. create_new_student();
  183. program_controller();
  184. }
  185. case 'U':
  186. {
  187. system("cls");
  188. delete_student();
  189. program_controller();
  190. }
  191. case 'K':
  192. end_program();
  193. default:
  194. cout << "Nie ma takiego znaku" << endl;
  195. }
  196. }
  197.  
  198. };
  199.  
  200.  
  201. int main()
  202. {
  203. Database dt = Database();
  204. dt.program_controller();
  205.  
  206. return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement