Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #pragma region Struktury
  5. typedef struct ElementTelefon {
  6. int unikalnyNumer;
  7. char* marka;
  8. char* model;
  9. int masa;
  10. int czasCzuwania;
  11. char* stan;
  12. int wartosc;
  13. struct ElementTelefon* nast;
  14.  
  15. }Telefon;
  16.  
  17.  
  18.  
  19. typedef struct ElementKlient {
  20. char* imie;
  21. char* nazwisko;
  22. int* tablicaTelefonow;
  23. int kwotaZakopow;
  24. struct ElementKlient* nast;
  25.  
  26. }Klient;
  27. #pragma endregion
  28.  
  29. #pragma region ZmienneGlobalne
  30.  
  31. Telefon* telefonyPocz;
  32. Klient* klienciPocz;
  33. int najwyzszyUnikalnyNumer = 0;
  34. #pragma endregion
  35.  
  36.  
  37. #pragma region ZarzadzanieTelefony
  38. int getIloscTelefonow() {
  39.  
  40. Telefon* tmp = telefonyPocz;
  41. int iloscTel = 0;
  42. while (tmp != NULL)
  43. {
  44. tmp = tmp->nast;
  45. iloscTel++;
  46. }
  47. return iloscTel;
  48. }
  49.  
  50. Telefon* getTelefon(int unikalnyNr) {
  51.  
  52. Telefon* tmp = telefonyPocz;
  53.  
  54. while (tmp != NULL && tmp->unikalnyNumer != unikalnyNr)
  55. {
  56. tmp = tmp->nast;
  57. }
  58.  
  59. return tmp;
  60. }
  61.  
  62. Telefon* dodajTelefonDoListy(Telefon* nowy) {
  63.  
  64. if (nowy->unikalnyNumer != NULL && nowy->unikalnyNumer >= najwyzszyUnikalnyNumer)
  65. {
  66. najwyzszyUnikalnyNumer = nowy->unikalnyNumer + 1;
  67. }
  68.  
  69. if (nowy->unikalnyNumer == NULL)
  70. {
  71. nowy->unikalnyNumer = ++najwyzszyUnikalnyNumer;
  72. }
  73.  
  74. if (telefonyPocz == NULL)
  75. {
  76. nowy->nast = NULL;
  77. return nowy;
  78. }
  79.  
  80. nowy->nast = telefonyPocz;
  81. return nowy;
  82. }
  83.  
  84. void usunTelefonZListy(int unikalnyNr) {
  85.  
  86. Telefon* usuwany = getTelefon(unikalnyNr);
  87. if (usuwany == telefonyPocz)
  88. {
  89. telefonyPocz = usuwany->nast;
  90. }
  91. else
  92. {
  93. Telefon* poprzedni = telefonyPocz;
  94. while (poprzedni->nast != usuwany)
  95. {
  96. poprzedni = poprzedni->nast;
  97. }
  98. poprzedni->nast = usuwany->nast;
  99. }
  100. free(usuwany);
  101. }
  102.  
  103. void edytujTelefonZListy(Telefon* edytowany) {
  104. if (telefonyPocz == NULL)
  105. {
  106. return;
  107. }
  108.  
  109. Telefon* tmp = telefonyPocz;
  110.  
  111. while (tmp != edytowany)
  112. {
  113. tmp = tmp->nast;
  114. }
  115.  
  116. strcpy(tmp->marka, edytowany->marka);
  117. strcpy(tmp->model, edytowany->model);
  118. tmp->masa = edytowany->masa;
  119. tmp->czasCzuwania = edytowany->czasCzuwania;
  120. strcpy(tmp->stan, edytowany->stan);
  121. tmp->wartosc = edytowany->wartosc;
  122. }
  123.  
  124. #pragma endregion
  125.  
  126. #pragma region SortowanieTelefony
  127.  
  128. int comparatorMarka(const void* v1, const void* v2)
  129. {
  130. const Telefon* const* pp = v1;
  131. const Telefon* const* qq = v2;
  132. return strcmp((*pp)->marka, (*qq)->marka);
  133. }
  134.  
  135. void sortujTelefonMarka() {
  136. int iloscTelefonow = getIloscTelefonow();
  137. Telefon** tablicaTelefonow = malloc(iloscTelefonow * sizeof(Telefon*));
  138.  
  139. Telefon* tmp = telefonyPocz;
  140. for (int i = 0; i < iloscTelefonow; i++)
  141. {
  142. tablicaTelefonow[i] = tmp;
  143. tmp = tmp->nast;
  144. }
  145. qsort(tablicaTelefonow, iloscTelefonow, sizeof(Telefon*), comparatorMarka);
  146.  
  147. telefonyPocz = tablicaTelefonow[0];
  148. tmp = telefonyPocz;
  149. for (int i = 1; i < iloscTelefonow; i++)
  150. {
  151. tmp->nast = tablicaTelefonow[i];
  152. tmp = tmp->nast;
  153. }
  154. tmp->nast = NULL;
  155.  
  156. free(tablicaTelefonow);
  157. }
  158.  
  159. int comparatorModel(const void* v1, const void* v2)
  160. {
  161. const Telefon* const* pp = v1;
  162. const Telefon* const* qq = v2;
  163. return (*pp)->wartosc > (*qq)->wartosc;
  164. }
  165.  
  166. void sortujTelefonModel() {
  167. int iloscTelefonow = getIloscTelefonow();
  168. Telefon** tablicaTelefonow = malloc(iloscTelefonow * sizeof(Telefon*));
  169.  
  170. Telefon* tmp = telefonyPocz;
  171. for (int i = 0; i < iloscTelefonow; i++)
  172. {
  173. tablicaTelefonow[i] = tmp;
  174. tmp = tmp->nast;
  175. }
  176. qsort(tablicaTelefonow, iloscTelefonow, sizeof(Telefon*), comparatorModel);
  177.  
  178. telefonyPocz = tablicaTelefonow[0];
  179. tmp = telefonyPocz;
  180. for (int i = 1; i < iloscTelefonow; i++)
  181. {
  182. tmp->nast = tablicaTelefonow[i];
  183. tmp = tmp->nast;
  184. }
  185. tmp->nast = NULL;
  186. free(tablicaTelefonow);
  187. }
  188.  
  189.  
  190. int comparatorCena(const void* v1, const void* v2)
  191. {
  192. const Telefon* const* pp = v1;
  193. const Telefon* const* qq = v2;
  194. return (*pp)->wartosc > (*qq)->wartosc;
  195. }
  196.  
  197. void sortujTelefonCena() {
  198. int iloscTelefonow = getIloscTelefonow();
  199. Telefon** tablicaTelefonow = malloc(iloscTelefonow * sizeof(Telefon*));
  200.  
  201. Telefon* tmp = telefonyPocz;
  202. for (int i = 0; i < iloscTelefonow; i++)
  203. {
  204. tablicaTelefonow[i] = tmp;
  205. tmp = tmp->nast;
  206. }
  207. qsort(tablicaTelefonow, iloscTelefonow, sizeof(Telefon*), comparatorCena);
  208.  
  209. telefonyPocz = tablicaTelefonow[0];
  210. tmp = telefonyPocz;
  211. for (int i = 1; i < iloscTelefonow; i++)
  212. {
  213. tmp->nast = tablicaTelefonow[i];
  214. tmp = tmp->nast;
  215. }
  216. tmp->nast = NULL;
  217. free(tablicaTelefonow);
  218. }
  219.  
  220. #pragma endregion
  221.  
  222.  
  223. #pragma region WyswietlanieTelefony
  224.  
  225.  
  226. void wyswietlTelefony() {
  227.  
  228. Telefon* tmp = telefonyPocz;
  229. printf("unikalnyNumer marka model masa czasCzuwania stan wartosc\n");
  230. while (tmp != NULL)
  231. {
  232. printf("%d %s %s %d %d %s %d\n", tmp->unikalnyNumer, tmp->marka, tmp->model, tmp->masa, tmp->czasCzuwania, tmp->stan, tmp->wartosc);
  233. tmp = tmp->nast;
  234. }
  235. }
  236.  
  237. #pragma endregion
  238.  
  239.  
  240. #pragma region ZarzadzanieKlienci
  241. int getIloscKlientow() {
  242.  
  243. Klient* tmp = klienciPocz;
  244. int iloscKlientow = 0;
  245. while (tmp != NULL)
  246. {
  247. tmp = tmp->nast;
  248. iloscKlientow++;
  249. }
  250. return iloscKlientow;
  251. }
  252.  
  253.  
  254. void dodajTelefonDoKlienta(Klient* klient, int numerTelefonu) {
  255. if (klient->tablicaTelefonow == NULL)
  256. {
  257. klient->tablicaTelefonow = malloc(sizeof(int));
  258. klient->tablicaTelefonow[0] = numerTelefonu;
  259. return;
  260. }
  261.  
  262. int sizeNewTable = sizeof(klient->tablicaTelefonow) / sizeof(int)+1;
  263.  
  264. int* nowaTablicaTelefonow = NULL;
  265.  
  266. nowaTablicaTelefonow = (int*)realloc(klient->tablicaTelefonow, sizeNewTable * sizeof(int));
  267. nowaTablicaTelefonow[sizeNewTable - 1] = numerTelefonu;
  268. return;
  269.  
  270. }
  271.  
  272. Klient* dodajKlientaDoListy(Klient* nowy) {
  273. nowy->tablicaTelefonow = NULL;
  274. if (klienciPocz == NULL)
  275. {
  276. nowy->nast = NULL;
  277. return nowy;
  278. }
  279.  
  280. nowy->nast = klienciPocz;
  281. return nowy;
  282. }
  283.  
  284. void usunKlientaZListy(Klient* usuwany) {
  285.  
  286. if (usuwany == klienciPocz)
  287. {
  288. klienciPocz = usuwany->nast;
  289. free(usuwany);
  290. }
  291. else
  292. {
  293. Klient* poprzedni = usuwany;
  294. while (poprzedni->nast != usuwany)
  295. {
  296. poprzedni = poprzedni->nast;
  297. }
  298. if (poprzedni->nast == usuwany)
  299. {
  300. poprzedni->nast = usuwany->nast;
  301. free(usuwany);
  302. }
  303. }
  304. }
  305.  
  306. void edytujKlientaZListy(Klient* edytowany) {
  307. if (klienciPocz == NULL)
  308. {
  309. return;
  310. }
  311.  
  312. Klient* tmp = klienciPocz;
  313.  
  314. while (tmp != edytowany)
  315. {
  316. tmp = tmp->nast;
  317. }
  318.  
  319. if (tmp == edytowany)
  320. {
  321. strcpy(tmp->imie, edytowany->imie);
  322. strcpy(tmp->nazwisko, edytowany->nazwisko);
  323. }
  324.  
  325. }
  326. #pragma endregion
  327.  
  328. #pragma region wyswietlanieKlienci
  329.  
  330. void wyswietlKlienta(Klient* tmp) {
  331. printf("Imie: %s\nNazwisko: %s\nKwota zakopow: %d\n", tmp->imie, tmp->nazwisko, tmp->kwotaZakopow);
  332.  
  333. }
  334.  
  335. void wyswietlKlienci() {
  336.  
  337. Klient* tmp = klienciPocz;
  338. printf("LP imie nazwisko kwota\n");
  339. int numer = 1;
  340. while (tmp != NULL)
  341. {
  342. printf("%d %s %s %d\n", numer++, tmp->imie, tmp->nazwisko, tmp->kwotaZakopow);
  343. tmp = tmp->nast;
  344. }
  345. }
  346. #pragma endregion
  347.  
  348.  
  349.  
  350. #pragma region menu
  351.  
  352. void menuKlient()
  353. {
  354. wyswietlKlienci();
  355.  
  356. }
  357.  
  358.  
  359.  
  360. void menuGlowne() {
  361.  
  362. while (1 == 1)
  363. {
  364.  
  365. printf("1 Wyswietl Klientow\n");
  366. printf("2 Wyswietl Telefony\n\n");
  367. printf("Wybierz akcje");
  368.  
  369. int liczba = scanf("%d");
  370.  
  371. switch (liczba)
  372. {
  373. case 1:menuKlient(); break;
  374. case 2: wyswietlTelefony(); break;
  375. default:
  376. break;
  377. }
  378.  
  379.  
  380. system("cls");
  381. }
  382.  
  383. }
  384.  
  385. #pragma endregion
  386.  
  387.  
  388.  
  389. #pragma region Pliki
  390.  
  391. void dodajTelefonZlinii(char* liniaTelefon) {
  392.  
  393. char* pt = strtok(liniaTelefon, ",");
  394.  
  395. Telefon* nowy = malloc(sizeof(Telefon));
  396. nowy->unikalnyNumer = atoi(pt);
  397. pt = strtok(NULL, ",");
  398. printf("%s", pt);
  399. nowy->marka = pt;
  400. pt = strtok(NULL, ",");
  401. nowy->model = pt;
  402. pt = strtok(NULL, ",");
  403. nowy->stan = pt;
  404. pt = strtok(NULL, ",");
  405. nowy->czasCzuwania = atoi(pt);
  406. pt = strtok(NULL, ",");
  407. nowy->masa = atoi(pt);
  408. pt = strtok(NULL, ",");
  409. nowy->wartosc = atoi(pt);
  410. pt = strtok(NULL, ",");
  411.  
  412. telefonyPocz = dodajTelefonDoListy(nowy);
  413. }
  414.  
  415.  
  416. void czytajTelefonyZPliku() {
  417. FILE* fp = fopen("telefony.txt", "r");
  418. char* liniaTMP;
  419. char chunk[128];
  420.  
  421. // Store the chunks of text into a line buffer
  422. size_t len = sizeof(chunk);
  423. char* line = malloc(len);
  424. if (line == NULL) {
  425. perror("Unable to allocate memory for the line buffer.");
  426. exit(1);
  427. }
  428.  
  429. line[0] = '\0';
  430.  
  431. while (fgets(chunk, sizeof(chunk), fp) != NULL) {
  432. size_t len_used = strlen(line);
  433. size_t chunk_used = strlen(chunk);
  434.  
  435. if (len - len_used < chunk_used) {
  436. len *= 2;
  437. if ((line = realloc(line, len)) == NULL) {
  438. perror("Unable to reallocate memory for the line buffer.");
  439. free(line);
  440. exit(1);
  441.  
  442. }
  443.  
  444. }
  445. strncpy(line + len_used, chunk, len - len_used);
  446. len_used += chunk_used;
  447.  
  448. if (line[len_used - 1] == '\n') {
  449.  
  450. liniaTMP = malloc(len_used * sizeof(char));
  451. strcpy(liniaTMP, line);
  452.  
  453. dodajTelefonZlinii(liniaTMP);
  454. // "Empty" the line buffer
  455. line[0] = '\0';
  456.  
  457. }
  458.  
  459. }
  460. fclose(fp);
  461. free(line);
  462. }
  463.  
  464.  
  465.  
  466. void dodajKlientaZlinii(char* liniaKlient) {
  467. char* pt = strtok(liniaKlient, ",");
  468.  
  469. Klient* nowy = malloc(sizeof(Klient));
  470. nowy->imie = pt;
  471. pt = strtok(NULL, ",");
  472. nowy->nazwisko = pt;
  473. pt = strtok(NULL, ",");
  474. nowy->nazwisko = pt;
  475. klienciPocz = dodajKlientaDoListy(nowy);
  476. }
  477.  
  478.  
  479.  
  480. void czytajKlienciZPliku() {
  481. FILE* fp = fopen("klienci.txt", "r");
  482. char* liniaTMP;
  483. char chunk[128];
  484.  
  485. // Store the chunks of text into a line buffer
  486. size_t len = sizeof(chunk);
  487. char* line = malloc(len);
  488. if (line == NULL) {
  489. perror("Unable to allocate memory for the line buffer.");
  490. exit(1);
  491. }
  492.  
  493. line[0] = '\0';
  494.  
  495. while (fgets(chunk, sizeof(chunk), fp) != NULL) {
  496. size_t len_used = strlen(line);
  497. size_t chunk_used = strlen(chunk);
  498.  
  499. if (len - len_used < chunk_used) {
  500. len *= 2;
  501. if ((line = realloc(line, len)) == NULL) {
  502. perror("Unable to reallocate memory for the line buffer.");
  503. free(line);
  504. exit(1);
  505.  
  506. }
  507.  
  508. }
  509. strncpy(line + len_used, chunk, len - len_used);
  510. len_used += chunk_used;
  511.  
  512. if (line[len_used - 1] == '\n') {
  513.  
  514. liniaTMP = malloc(len_used * sizeof(char));
  515. strcpy(liniaTMP, line);
  516.  
  517. dodajKlientaZlinii(liniaTMP);
  518.  
  519. line[0] = '\0';
  520. }
  521. }
  522. fclose(fp);
  523. free(line);
  524. }
  525.  
  526.  
  527.  
  528. void czytajTelefonyKlientowZPliku() {
  529. FILE* fp = fopen("telefonyKlient.txt", "r");
  530. char* liniaTMP;
  531. char chunk[128];
  532.  
  533. // Store the chunks of text into a line buffer
  534. size_t len = sizeof(chunk);
  535. char* line = malloc(len);
  536. if (line == NULL) {
  537. perror("Unable to allocate memory for the line buffer.");
  538. exit(1);
  539. }
  540.  
  541. line[0] = '\0';
  542.  
  543. Klient* tmp = klienciPocz;
  544. while (fgets(chunk, sizeof(chunk), fp) != NULL) {
  545. size_t len_used = strlen(line);
  546. size_t chunk_used = strlen(chunk);
  547.  
  548. if (len - len_used < chunk_used) {
  549. len *= 2;
  550. if ((line = realloc(line, len)) == NULL) {
  551. perror("Unable to reallocate memory for the line buffer.");
  552. free(line);
  553. exit(1);
  554.  
  555. }
  556.  
  557. }
  558. strncpy(line + len_used, chunk, len - len_used);
  559. len_used += chunk_used;
  560.  
  561. if (line[len_used - 1] == '\n') {
  562.  
  563. liniaTMP = malloc(len_used * sizeof(char));
  564. strcpy(liniaTMP, line);
  565.  
  566.  
  567. char* pt = strtok(liniaTMP, ",");
  568.  
  569.  
  570. while (pt != NULL)
  571. {
  572. dodajTelefonDoKlienta(tmp, atoi(pt));
  573. pt = strtok(NULL, ",");
  574. }
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581. line[0] = '\0';
  582. }
  583. }
  584. fclose(fp);
  585. free(line);
  586. }
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595. #pragma endregion
  596.  
  597.  
  598.  
  599.  
  600.  
  601. int main()
  602. {
  603. czytajTelefonyZPliku();
  604. czytajKlienciZPliku();
  605. czytajTelefonyKlientowZPliku();
  606. menuGlowne();
  607. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement