peHaBOT

Untitled

May 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 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. #include <iomanip>
  13. using namespace std;
  14. string imie,nazwisko,nr_albumu;
  15. fstream plik; //tworzymy liste
  16.  
  17. void clear(){
  18. for (int i = 0; i < 100; i++){
  19. cout << endl;
  20. }
  21. }
  22.  
  23. struct student {
  24. string imie;
  25. string nazwisko;
  26. string nr_albumu;
  27. student *next;
  28. student();
  29. };
  30.  
  31. student::student(){
  32. next = 0;
  33. }
  34.  
  35. struct lista {
  36. student *first;
  37. void dodaj_studenta(string imie, string nazwisko, string nr_albumu);
  38. void usun_studenta(int nr);
  39. void wyswietl_studenta();
  40. void wyswietl_liste();
  41. void zapisz_liste();
  42. void odtworz_liste();
  43. void szukaj_studenta();
  44. void edytuj_dane();
  45. void usun_studenta();
  46. lista();
  47. };
  48.  
  49. lista::lista(){
  50. first = 0;
  51. }
  52. lista *baza = new lista;
  53.  
  54. void lista::dodaj_studenta(string imie, string nazwisko, string nr_albumu) {
  55. student *nowa = new student; // tworzy nowy element listy
  56.  
  57. // wypełniamy naszymi danymi
  58. nowa->imie = imie;
  59. nowa->nazwisko = nazwisko;
  60. nowa->nr_albumu = nr_albumu;
  61.  
  62. if (first==0) // sprawdzamy czy to pierwszy element listy
  63. {
  64. // jeżeli tak to nowy element jest teraz początkiem listy
  65. first = nowa;
  66. }
  67.  
  68. else
  69. {
  70. // w przeciwnym wypadku wędrujemy na koniec listy
  71. student *temp = first;
  72.  
  73. while (temp->next)
  74. {
  75. // znajdujemy wskaźnik na ostatni element
  76. temp = temp->next;
  77. }
  78.  
  79. temp->next = nowa; // ostatni element wskazuje na nasz nowy
  80. nowa->next = 0; // ostatni nie wskazuje na nic
  81. }
  82. }
  83.  
  84. void lista::edytuj_dane() {
  85. // wskaznik na pierszy element listy
  86. student *temp = first;
  87. bool found = 0;
  88. string album;
  89. string imie,nazwis,nralb;
  90. cout << "Podaj nr albumu studenta => "; cin >> album;
  91. // przewijamy wskazniki na nastepne elementy
  92. while (temp)
  93. {
  94. if(temp->nr_albumu == album) {
  95. cout << "| " << temp->nr_albumu << " | " << temp->nazwisko << " " << temp->imie << endl;
  96. cout << "IMIE: "; cin >> imie;
  97. if(!imie.empty()) temp->imie = imie;
  98. cout << "NAZWISKO: "; cin >> nazwis;
  99. if(!nazwis.empty()) temp->nazwisko = nazwis;
  100. cout << "NR ALBUMU: "; cin >> nralb;
  101. if(!nralb.empty()) temp->nr_albumu = nralb;
  102. cout << "Rekord zostal edytowany" << endl;
  103. found = 1;
  104. }
  105. temp=temp->next;
  106. }
  107. if (!found) cout << "Brak podobnych rekordów w bazie" << endl;
  108. }
  109.  
  110. void lista::szukaj_studenta() {
  111. // wskaznik na pierszy element listy
  112. student *temp = first;
  113. bool found = 0;
  114. string nazwis;
  115. cout << "Podaj nazwisko studenta => "; cin >> nazwis;
  116. // przewijamy wskazniki na nastepne elementy
  117. while (temp)
  118. {
  119. if(temp->nazwisko == nazwis) {
  120. cout << "| " << temp->nr_albumu << " | " << temp->nazwisko << " " << temp->imie << endl;
  121. found = 1;
  122. }
  123. temp=temp->next;
  124. }
  125. if (!found) cout << "Brak podobnych rekordów w bazie" << endl;
  126. }
  127.  
  128. void lista::zapisz_liste() {
  129. // wskaznik na pierszy element listy
  130. student *temp = first;
  131. fstream plik("~/peHaBOT/desktop/Studia/STUDIA - OWN/SII OWN/Programowanie/Files CPP/plik.txt");
  132. plik.open("plik.txt", ios::out | ios::trunc);
  133. // przewijamy wskazniki na nastepne elementy
  134. while (temp)
  135. {
  136. if(plik.good() == true)
  137. {
  138. plik << temp->nr_albumu << "\n";
  139. plik << temp->imie << "\n";
  140. plik << temp->nazwisko << "\n";
  141. }
  142. temp=temp->next;
  143. }
  144. }
  145.  
  146. void lista::odtworz_liste() {
  147. string linia;
  148. string im, nazw, numer;
  149. ifstream plik("~/peHaBOT/desktop/Studia/STUDIA - OWN/SII OWN/Programowanie/Files CPP/plik.txt");
  150. // przewijamy wskazniki na nastepne elementy
  151. int i = 0;
  152. plik.open("plik.txt");
  153. if(plik.good())
  154. {
  155. cout << "Odtwarzam baze..." << endl;
  156. while(!plik.eof()){
  157. i+=1;
  158. if ( i == 4 ) i = 1;
  159. getline(plik, linia,'\n'); //pobierz linijkę
  160. if ( i == 1 ) numer = linia;
  161. if ( i == 2 ) im = linia;
  162. if ( i == 3 ) {
  163. nazw = linia;
  164. baza->dodaj_studenta(im, nazw, numer);
  165. }
  166. //cout << endl;
  167. }
  168. cout << "Baza odtworzona." << endl << endl;
  169. }
  170. plik.close();
  171. }
  172.  
  173.  
  174. //~/Biurko/Studia/STUDIA - OWN/SII OWN/Programowanie/1 zajęcia/students/students
  175.  
  176.  
  177. void dodawanie(){
  178. //lista *root =
  179. cout << "DODAWANIE STUDENTA" << endl;
  180. cout << "Podaj imie studenta: "; cin >> imie;
  181. cout << "Podaj nazwisko studenta: "; cin >> nazwisko;
  182. cout << "Podaj numer albumu: "; cin >> nr_albumu;
  183. baza->dodaj_studenta(imie, nazwisko, nr_albumu);
  184. cout << "Student zostal dodany do bazy." << endl;
  185. return;
  186. }
  187.  
  188. void lista::wyswietl_liste()
  189. {
  190. // wskaznik na pierszy element listy
  191. student *temp = first;
  192.  
  193. // przewijamy wskazniki na nastepne elementy
  194. while (temp)
  195. {
  196. cout << "| " << temp->nr_albumu << " | " << temp->nazwisko << " " << temp->imie << endl;
  197. temp=temp->next;
  198. }
  199. }
  200.  
  201.  
  202. void menu(){
  203.  
  204. int wybor;
  205. cout << "BAZA STUDENTÓW\n1. Dodaj studenta\n2. Szukaj studenta\n3. Edytuj dane studenta\n4. Usun studenta z bazy\n5. Wyswietl cala baze\n6. Zamknij program\n=> "; cin >> wybor;
  206. switch(wybor){
  207. case 1:
  208. dodawanie();
  209. return menu();
  210. break;
  211. case 2:
  212. baza->szukaj_studenta();
  213. return menu();
  214. break;
  215. case 3:
  216. baza->edytuj_dane();
  217. return menu();
  218. break;
  219. case 4:
  220.  
  221. break;
  222. case 5:
  223. baza->wyswietl_liste();
  224. return menu();
  225. break;
  226. case 6:
  227. baza->zapisz_liste();
  228. return;
  229. break;
  230. default:
  231. return ;
  232. break;
  233. }
  234. }
  235.  
  236. int main(int argc, const char * argv[]) {
  237. baza->odtworz_liste();
  238. menu();
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment