Advertisement
Guest User

Untitled

a guest
May 20th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <string>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9. void wymagajEnter();
  10. void dodajOsobe();
  11. void pokazOsoby();
  12. void zapiszOsobyDoPliku();
  13. void wczytajOsobyzPliku();
  14. void PrzeszukajBaze();
  15. void UsunOsobe();
  16. struct Czlowiek
  17. {
  18. string imie;
  19. string nazwisko;
  20. short wiek;
  21. string telefon;
  22. };
  23. short ileDodanoOsob;
  24. Czlowiek ludzie[20];
  25. int main()
  26. {
  27. char tescior;
  28. do
  29. {
  30. cout << "OS w bazie :" << ileDodanoOsob;
  31. cout << " MENU: " << endl;
  32. cout << " 1. Dodaj odobe" << endl;
  33. cout << " 2. Pokaz osoby" << endl;
  34. cout << " 3. Zapisz Osoby do pliku" << endl;
  35. cout << " 4. Wczyataj Osoby z pliku" << endl;
  36. cout << "5. Przeszukaj baze" << endl;
  37. cout << "6. Usun osobe z bazy danych" << endl;
  38. tescior = _getch();
  39. switch (tescior)
  40. {
  41. case '1':
  42. dodajOsobe();
  43. break;
  44. case '2':
  45. pokazOsoby(); break;
  46. case '3':
  47. zapiszOsobyDoPliku();
  48. break;
  49. case '4' :
  50. wczytajOsobyzPliku(); break;
  51. case '5' :
  52. PrzeszukajBaze(); break;
  53. case '6':
  54. UsunOsobe(); break;
  55. }
  56. wymagajEnter();
  57.  
  58.  
  59. system("cls");
  60. } while (tescior != 27);
  61. return 0;
  62. }
  63. void wymagajEnter()
  64. {
  65. cout << "Nacisnij ENTER aby kontnyowac" << endl;
  66. while (_getch() != 13);
  67. }
  68. void dodajOsobe()
  69. {
  70. cout << "Podaj imie:";
  71. cin >> ludzie[ileDodanoOsob].imie;
  72. cout << "Podaj nazwisko:";
  73. cin >> ludzie[ileDodanoOsob].nazwisko;
  74. cout << "Podaj wiek:";
  75. cin >> ludzie[ileDodanoOsob].wiek;
  76. cout << "Podaj nr_tel:";
  77. cin >> ludzie[ileDodanoOsob].telefon;
  78. ileDodanoOsob++;
  79. }
  80. void pokazOsoby()
  81. {
  82. if (ileDodanoOsob > 0)
  83. {
  84. for (int j = 0; j < ileDodanoOsob; j++)
  85. {
  86. cout << "Osoba nr:" << (j+1) << endl;
  87. cout << "Imie: " << ludzie[j].imie << endl;
  88. cout << "Nazwisko:" << ludzie[j].nazwisko << endl;
  89. cout << "Wiek: " << ludzie[j].wiek << endl;
  90. cout << "Nr_tel:" << ludzie[j].telefon << endl << endl;
  91.  
  92. }
  93. }
  94. }
  95. void zapiszOsobyDoPliku()
  96. {
  97. ofstream file("baza.txt");
  98. if (file.is_open())
  99. {
  100. file << ileDodanoOsob << endl;
  101. for (int j = 0; j < ileDodanoOsob; j++)
  102. {
  103. file << ludzie[j].imie << endl;
  104. file << ludzie[j].nazwisko << endl;
  105. file << ludzie[j].wiek << endl;
  106. file << ludzie[j].telefon << endl;
  107. }
  108. file.close();
  109. }
  110. else
  111. cout << "Plik nie zostal otwarty poprawnie" << endl;
  112.  
  113. }
  114. void wczytajOsobyzPliku()
  115. {
  116. ifstream file("baza.txt");
  117. if (file.is_open())
  118. {
  119. file >> ileDodanoOsob;
  120. for (int j = 0; j < ileDodanoOsob; j++)
  121. {
  122. file >> ludzie[j].imie;
  123. file >> ludzie[j].nazwisko;
  124. file >> ludzie[j].wiek;
  125. file >> ludzie[j].telefon ;
  126. }
  127. file.close();
  128. }
  129. else cout << "Nie udalo sie otworzyc pliku";
  130. }
  131. void PrzeszukajBaze()
  132. {
  133. if (ileDodanoOsob != 0)
  134. {
  135. string imie;
  136. cout << "Podaj imie, a podam ci wiecej danych uzytkownika";
  137. cin >> imie;
  138. for (int j = 0; j < ileDodanoOsob; j++)
  139. {
  140. if (imie == ludzie[j].imie)
  141. {
  142. cout << "Osoba nr: " << (j + 1) << endl;
  143. cout << "Imie: " << ludzie[j].imie << endl;
  144. cout << "Nazwisko: " << ludzie[j].nazwisko << endl;
  145. cout << "Wiek: " << ludzie[j].wiek << endl;
  146. cout << "Telefon: " << ludzie[j].telefon << endl;
  147. }
  148. }
  149. }
  150. else cout << "Baza danych jest pusta";
  151. }
  152. void UsunOsobe()
  153. {
  154. if (ileDodanoOsob != 0)
  155. {
  156. short indeks;
  157. cout << "Podaj indeks osoby ktorej chcesz usunac" << endl;
  158. cin >> indeks;
  159. for (short k = indeks; k > ileDodanoOsob; k++)
  160. {
  161. ludzie[k - 1].imie = ludzie[k].imie;
  162. ludzie[k - 1].nazwisko = ludzie[k].nazwisko;
  163. ludzie[k - 1].wiek = ludzie[k].wiek;
  164. ludzie[k - 1].telefon = ludzie[k].telefon;
  165. }
  166. ileDodanoOsob--;
  167. zapiszOsobyDoPliku();
  168. cout << "Uzytkownik o indeksie" << indeks << "zostala usunieta" << endl;
  169. }
  170. else
  171. cout << "Pusta baza danych" << endl;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement