Advertisement
peHaBOT

Untitled

May 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // students
  4. //
  5. // Created by Chojnacky Karol on 15.05.2018.
  6. // Copyright © 2018 Chojnacky Karol. All rights reserved.
  7. //
  8. #include <fstream>
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <string>
  12. using namespace std;
  13. string imie,nazwisko,nr_albumu;
  14. fstream plik; //tworzymy liste
  15. struct student {
  16. string imie;
  17. string nazwisko;
  18. string nr_albumu;
  19. student *next;
  20. student();
  21. };
  22.  
  23. student::student(){
  24. next = 0;
  25. }
  26.  
  27. struct lista {
  28. student *first;
  29. void dodaj_studenta(string imie, string nazwisko, string nr_albumu);
  30. void usun_studenta(int nr);
  31. void wyswietl_studenta();
  32. void wyswietl_liste();
  33. void zapisz_liste();
  34. void odtworz_liste();
  35. lista();
  36. };
  37.  
  38. lista::lista(){
  39. first = 0;
  40. }
  41. lista *baza = new lista;
  42.  
  43. void lista::dodaj_studenta(string imie, string nazwisko, string nr_albumu) {
  44. student *nowa = new student; // tworzy nowy element listy
  45.  
  46. // wypełniamy naszymi danymi
  47. nowa->imie = imie;
  48. nowa->nazwisko = nazwisko;
  49. nowa->nr_albumu = nr_albumu;
  50.  
  51. if (first==0) // sprawdzamy czy to pierwszy element listy
  52. {
  53. // jeżeli tak to nowy element jest teraz początkiem listy
  54. first = nowa;
  55. }
  56.  
  57. else
  58. {
  59. // w przeciwnym wypadku wędrujemy na koniec listy
  60. student *temp = first;
  61.  
  62. while (temp->next)
  63. {
  64. // znajdujemy wskaźnik na ostatni element
  65. temp = temp->next;
  66. }
  67.  
  68. temp->next = nowa; // ostatni element wskazuje na nasz nowy
  69. nowa->next = 0; // ostatni nie wskazuje na nic
  70. }
  71. }
  72.  
  73.  
  74. void lista::wyswietl_studenta() {
  75. // wskaznik na pierszy element listy
  76. student *temp = first;
  77.  
  78. // przewijamy wskazniki na nastepne elementy
  79. while (temp)
  80. {
  81. cout << "imie: " << temp->imie << " nazwisko: " << temp->nazwisko << endl;
  82. temp=temp->next;
  83. }
  84. }
  85.  
  86. void lista::zapisz_liste() {
  87. // wskaznik na pierszy element listy
  88. student *temp = first;
  89.  
  90. // przewijamy wskazniki na nastepne elementy
  91. while (temp)
  92. {
  93. plik.open("plik.txt", ios::out | ios::app);
  94. if(plik.good() == true)
  95. {
  96. plik << temp->nr_albumu << "\n";
  97. plik << temp->imie << "\n";
  98. plik << temp->nazwisko << "\n";
  99. plik.close();
  100. }
  101. temp=temp->next;
  102. }
  103. }
  104.  
  105. void lista::odtworz_liste() {
  106. // wskaznik na pierszy element listy
  107. student *temp = first;
  108. string linia;
  109. string im, nazw, numer;
  110. ifstream plik("plik.txt");
  111. // przewijamy wskazniki na nastepne elementy
  112. int i = 0;
  113. plik.open("plik.txt", ios::out | ios::app);
  114. if(plik.good() == true)
  115. {
  116. do
  117. {
  118. i+=1;
  119. if ( i == 4 ) i = 0;
  120. getline(plik, linia); //pobierz linijkę
  121. if ( i == 1 ) numer = linia;
  122. if ( i == 2 ) im = linia;
  123. if ( i == 3 ) {
  124. nazw = linia;
  125. baza->dodaj_studenta(im, nazw, numer);
  126. }
  127. }
  128. while(linia != "");
  129. plik.close();
  130. }
  131. }
  132.  
  133. //~/Biurko/Studia/STUDIA - OWN/SII OWN/Programowanie/1 zajęcia/students/students
  134.  
  135.  
  136. void dodawanie(){
  137. //lista *root =
  138. cout << "DODAWANIE STUDENTA" << endl;
  139. cout << "Podaj imie studenta: "; cin >> imie;
  140. cout << "Podaj nazwisko studenta: "; cin >> nazwisko;
  141. cout << "Podaj numer albumu: "; cin >> nr_albumu;
  142. baza->dodaj_studenta(imie, nazwisko, nr_albumu);
  143. return;
  144. }
  145.  
  146. void lista::wyswietl_liste()
  147. {
  148. // wskaznik na pierszy element listy
  149. student *temp = first;
  150.  
  151. // przewijamy wskazniki na nastepne elementy
  152. while (temp)
  153. {
  154. cout << "imie: " << temp->imie << " nazwisko: " << temp->nazwisko << " nr_albumu: " << temp->nr_albumu << endl;
  155. temp=temp->next;
  156. }
  157. }
  158.  
  159. void szukaj(){
  160. cout << baza->first->imie << ", " << baza->first->nazwisko << ", " << baza->first->nr_albumu << endl;
  161. return;
  162. }
  163.  
  164. void menu(){
  165.  
  166. int wybor;
  167. cout << "BAZA STUDENTÓW\n1. Dodaj studenta\n2. Szukaj student\n3. Edytuj dane studenta\n4. Usuń studenta z bazy\n5. Wyswietl całą bazę\n6. Zamknij program\n=> "; cin >> wybor;
  168. switch(wybor){
  169. case 1:
  170. dodawanie();
  171. return menu();
  172. break;
  173. case 2:
  174. szukaj();
  175. return menu();
  176. break;
  177. case 3:
  178.  
  179. break;
  180. case 4:
  181.  
  182. break;
  183. case 5:
  184. baza->wyswietl_liste();
  185. return menu();
  186. break;
  187. case 6:
  188. baza->zapisz_liste();
  189. return;
  190. break;
  191. default:
  192. return ;
  193. break;
  194. }
  195. }
  196.  
  197. int main(int argc, const char * argv[]) {
  198. baza->odtworz_liste();
  199. menu();
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement