Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //struktura z danymi klienta
  5. typedef struct DANE
  6. {
  7. char *nazwa;
  8. char *adres;
  9. int *id;
  10. char *kontakt;
  11. }dane;
  12.  
  13. //struktura z lista
  14. typedef struct KLIENT klient;
  15.  
  16. struct KLIENT
  17. {
  18. dane informacje;
  19. klient *nast;
  20. };
  21. // struktura dotyczaca transakcji
  22. typedef struct TRANSAKCJE
  23. {
  24. char *nazwa_tr;
  25. int *ilosc_zamowionego_towaru;
  26. int *kwota;
  27. int *id_klienta;
  28. }transakcje;
  29.  
  30. typedef struct TR tr;
  31. struct TR
  32. {
  33. transakcje trans;
  34. tr *next;
  35. };
  36.  
  37. //zapisanie do pliku
  38. void zapisz_do_pliku(klient *head)
  39. {
  40. FILE *plik1=NULL;
  41. klient *tmp;
  42. if(head==NULL)
  43. printf("Nie ma elementow do zapisania w pliku");
  44. else
  45. {
  46. plik1=fopen("lista_klientow.txt", "w");
  47. if(plik1==NULL)
  48. printf("Wystapil blad");
  49. else
  50. {
  51. tmp=head;
  52. while(tmp!=NULL)
  53. {
  54. fprintf(plik1,"id klienta %d\n",tmp->informacje.id);
  55. fprintf(plik1, "nazwa: %s\n", tmp->informacje.nazwa);
  56. fprintf(plik1, "adres: %s\n", tmp->informacje.adres);
  57. fprintf(plik1, "kontakt: %s\n", tmp->informacje.kontakt);
  58. tmp = tmp->nast;
  59. fprintf(plik1, "----------------\n");
  60. }
  61.  
  62. }
  63. fclose(plik1);
  64. }
  65. }
  66. //zapisanie do pliku
  67. void zapisz_do_pliku_tr(tr *head1)
  68. {
  69. FILE *plik2=NULL;
  70. tr *tmp;
  71. if(head1==NULL)
  72. printf("Nie ma elementow do zapisania w pliku");
  73. else
  74. {
  75. plik2=fopen("lista_transakcji.txt", "w");
  76. if(plik2==NULL)
  77. printf("Wystapil blad");
  78. else
  79. {
  80. tmp=head1;
  81. while(tmp!=NULL)
  82. {
  83. fprintf(plik2, "nazwa towaru: %s\n", tmp->trans.nazwa_tr);
  84. fprintf(plik2, "ilosc: %d\n", tmp->trans.ilosc_zamowionego_towaru);
  85. fprintf(plik2, "kwota: %d\n", tmp->trans.kwota);
  86. fprintf(plik2, "id klienta %d\n", tmp->trans.id_klienta);
  87. tmp = tmp->next;
  88. fprintf(plik2,"-----------------\n");
  89.  
  90. }
  91. }
  92. fclose(plik2);
  93. }
  94. }
  95. transakcje* nowa_tr()
  96. {
  97. tr *nowy;
  98. char bufor[30+1];
  99. char *nazwa_tr;
  100. int ilosc_zamowionego_towaru,kwota,id_klienta;
  101.  
  102. system("cls");
  103. printf("Podaj nazwe towaru \n");
  104. gets(bufor);
  105. nazwa_tr = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
  106. strcpy(nazwa_tr, bufor);
  107. printf("Podaj ilosc zamowionego towaru\n");
  108. scanf("%d",&ilosc_zamowionego_towaru);
  109. printf("Podaj kwote\n");
  110. scanf("%d", &kwota);
  111. printf("Podaj id klienta\n");
  112. scanf("%d", &id_klienta);
  113. nowy=(tr*)malloc(sizeof(tr));
  114.  
  115. nowy->next=NULL;
  116. nowy->trans.nazwa_tr=nazwa_tr;
  117. nowy->trans.ilosc_zamowionego_towaru=ilosc_zamowionego_towaru;
  118. nowy->trans.kwota=kwota;
  119. nowy->trans.id_klienta=id_klienta;
  120. return nowy;
  121.  
  122. }
  123.  
  124. //funkcja tworzaca nowy element listy
  125. int id=1;
  126.  
  127. klient* nowy_element()
  128. {
  129. klient *nowy;
  130. char bufor[30+1];
  131. char *nazwa, *adres;
  132. char *kontakt;
  133.  
  134. system("cls");
  135. printf("Podaj nazwe klienta \n");
  136. gets(bufor);
  137. nazwa = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
  138. strcpy(nazwa, bufor);
  139. printf("Podaj adres klienta\n");
  140. gets(bufor);
  141. adres = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
  142. strcpy(adres,bufor);
  143. printf("Podaj numer telefonu klienta\n");
  144. gets(bufor);
  145. kontakt = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
  146. strcpy(kontakt,bufor);
  147.  
  148. nowy=(klient*)malloc(sizeof(klient));
  149. nowy->nast=NULL;
  150. nowy->informacje.id=id;
  151. nowy->informacje.nazwa=nazwa;
  152. nowy->informacje.adres=adres;
  153. nowy->informacje.kontakt=kontakt;
  154. id++;
  155. return nowy;
  156.  
  157. }
  158. void dodaj_na_koniec_tr(tr **head1)
  159. {
  160. tr *pom, *tmp=nowa_tr();
  161. if (*head1 == NULL)
  162. *head1 = tmp;
  163. else
  164. {
  165. pom = *head1;
  166. while (pom->next != NULL)
  167. pom = pom->next;
  168. pom->next = tmp;
  169. }
  170. }
  171.  
  172. //dodawanie na koniec listy
  173. void dodaj_na_koniec(klient **head)
  174. {
  175. klient *pom, *tmp=nowy_element();
  176. if (*head == NULL)
  177. *head = tmp;
  178. else
  179. {
  180. pom = *head;
  181. while (pom->nast != NULL)
  182. pom = pom->nast;
  183. pom->nast = tmp;
  184. }
  185. }
  186.  
  187. //wyswietlanie calej listy
  188. void wyswietl(klient *head, tr *head1)
  189. {
  190. klient *pom;
  191. tr *tmp;
  192. system("cls");
  193. pom=head;
  194. tmp=head1;
  195. if(pom==NULL || tmp==NULL)
  196. printf("nie mozna polaczyc elementow\n");
  197. else
  198. {
  199. while(pom!=NULL)
  200. {
  201. while(tmp!=NULL)
  202. {
  203. if(tmp->trans.id_klienta==pom->informacje.id)
  204. printf("nazwa: %s\n adres: %s\n kontak %s\n nazwa towaru %s\n ilosc %d\n kwota %d\n", pom->informacje.nazwa, pom->informacje.adres,
  205. pom->informacje.kontakt, tmp->trans.nazwa_tr,tmp->trans.ilosc_zamowionego_towaru,tmp->trans.kwota);
  206. tmp=tmp->next;
  207.  
  208. printf("--------------------\n");
  209. }
  210. pom=pom->nast;
  211. tmp=head1;
  212. }
  213. getchar();
  214. }
  215. }
  216. void wyswietlanie_listy(klient *head)
  217. {
  218. klient *tmp;
  219.  
  220. system("cls");
  221. tmp = head;
  222. if (tmp == NULL)
  223. printf("Brak elementow");
  224. else
  225. {
  226. while (tmp != NULL)
  227. {
  228.  
  229. printf("id: %d\n nazwa: %s \nadres: %s \nkontakt: %s\n", tmp->informacje.id, tmp->informacje.nazwa,
  230. tmp->informacje.adres, tmp->informacje.kontakt);
  231. printf("-----------------\n");
  232. tmp = tmp->nast;
  233. }
  234. }
  235. getchar();
  236. }
  237. //funkcja wyswietlajaca liste transakcji
  238. void wyswietlanie_tr(tr *head1)
  239. {
  240. tr *tmp;
  241. system("cls");
  242. tmp = head1;
  243. if (tmp == NULL)
  244. printf("Brak elementow");
  245. else
  246. {
  247. while (tmp != NULL)
  248. {
  249. printf("nazwa: %s \nilosc: %d\n cena: %d\n", tmp->trans.nazwa_tr, tmp->trans.ilosc_zamowionego_towaru,
  250. tmp->trans.kwota);
  251. printf("-------------------------\n");
  252. tmp = tmp->next;
  253. }
  254. }
  255. getchar();
  256. }
  257. //wyszukiwanie klienta
  258. klient * wyszukaj_element(klient* head, char *nazwa)
  259. {
  260. klient *pom;
  261.  
  262. pom = head;
  263. while (pom && strcmp(pom->informacje.nazwa, nazwa)!=0 )
  264. pom = pom->nast;
  265.  
  266. return pom;
  267. }
  268. //wyszukiwanie transakcji
  269. tr *wyszukaj(tr* head1, char *nazwa_tr)
  270. {
  271. tr *pom;
  272. pom=head1;
  273. while(pom && strcmp(pom->trans.nazwa_tr,nazwa_tr)!=0)
  274. pom=pom->next;
  275. return pom;
  276. }
  277. //Usuwanie wybranego elementu
  278. void usuwanieklienta(klient **head)
  279. {
  280. klient *tmp, *pom;
  281. char *nazwa;
  282. char bufor[30 + 1];
  283.  
  284. system("cls");
  285. printf("Podaj nazwe klienta: \n");
  286. gets(bufor);
  287. nazwa = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
  288. strcpy(nazwa, bufor);
  289.  
  290. tmp = wyszukaj_element(*head, nazwa);
  291. if (tmp == NULL)
  292. printf("Nie ma takiej osoby\n");
  293. else
  294. {
  295. if (tmp == *head)
  296. {
  297. *head = (*head)->nast;
  298. if(tmp->informacje.id)
  299. free(tmp->informacje.id);
  300. if (tmp->informacje.nazwa)
  301. free(tmp->informacje.nazwa);
  302. if (tmp->informacje.adres)
  303. free(tmp->informacje.adres);
  304. if(tmp->informacje.kontakt)
  305. free(tmp->informacje.kontakt);
  306. free(tmp);
  307. }
  308. else
  309. {
  310. pom = *head;
  311. while ((strcmp(pom->nast->informacje.nazwa, tmp->informacje.nazwa) != 0))
  312. pom = pom->nast;
  313. pom->nast = tmp->nast;
  314. if(tmp->informacje.id)
  315. free(tmp->informacje.id);
  316. if (tmp->informacje.nazwa)
  317. free(tmp->informacje.nazwa);
  318. if (tmp->informacje.adres)
  319. free(tmp->informacje.adres);
  320. if(tmp->informacje.kontakt)
  321. free(tmp->informacje.kontakt);
  322. free(tmp);
  323.  
  324. }
  325. }
  326. }
  327. void usuwanietr(tr **head1)
  328. {
  329. tr *tmp, *pom;
  330. char *nazwa;
  331. char bufor[30 + 1];
  332.  
  333. system("cls");
  334. printf("Podaj nazwe towaru: \n");
  335. gets(bufor);
  336. nazwa = (char*)malloc(sizeof(char)* (strlen(bufor) + 1));
  337. strcpy(nazwa, bufor);
  338.  
  339. tmp = wyszukaj_element(*head1, nazwa);
  340. if (tmp == NULL)
  341. printf("Nie ma takiej transakcji\n");
  342. else
  343. {
  344. if (tmp == *head1)
  345. {
  346. *head1 = (*head1)->next;
  347. if(tmp->trans.nazwa_tr)
  348. free(tmp->trans.nazwa_tr);
  349. if (tmp->trans.ilosc_zamowionego_towaru)
  350. free(tmp->trans.ilosc_zamowionego_towaru);
  351. if (tmp->trans.kwota)
  352. free(tmp->trans.kwota);
  353. if(tmp->trans.id_klienta)
  354. free(tmp->trans.id_klienta);
  355. free(tmp);
  356. }
  357. else
  358. {
  359. pom = *head1;
  360. while ((strcmp(pom->next->trans.nazwa_tr, tmp->trans.nazwa_tr) != 0))
  361. pom = pom->next;
  362. pom->next = tmp->next;
  363. if(tmp->trans.nazwa_tr)
  364. free(tmp->trans.nazwa_tr);
  365. if (tmp->trans.ilosc_zamowionego_towaru)
  366. free(tmp->trans.ilosc_zamowionego_towaru);
  367. if (tmp->trans.kwota)
  368. free(tmp->trans.kwota);
  369. if(tmp->trans.id_klienta)
  370. free(tmp->trans.id_klienta);
  371. free(tmp);
  372. }
  373. }
  374. }
  375.  
  376.  
  377. //Usuwanie wybranego elementu
  378.  
  379.  
  380.  
  381. //funkcja glowna zawierajaca menu
  382. int main()
  383. { klient *head=NULL;
  384. tr *head1=NULL;
  385. char wybor;
  386. do{
  387. system("cls");
  388. printf("1. dodaj osobe do listy \n");
  389. printf("2. wyswietl liste osob\n");
  390. printf("3. usun wybranego przez siebie klienta\n");
  391. printf("4. dodaj nowa transakcje\n");
  392. printf("5. wyswietl obie listy\n");
  393. printf("6.wyswietl liste transakcji\n");
  394. printf("7. usun wybrana transakcje\n");
  395. printf("00. koniec programu\n");
  396. wybor=getchar();
  397. getchar();
  398.  
  399. if(wybor=='1')
  400. {dodaj_na_koniec(&head);
  401. zapisz_do_pliku(head);}
  402. else if(wybor=='2') wyswietlanie_listy(head);
  403. else if(wybor=='3')
  404. {usuwanieklienta(&head);
  405. zapisz_do_pliku(head);}
  406. else if(wybor=='4')
  407. {
  408. dodaj_na_koniec_tr(&head1);
  409. zapisz_do_pliku_tr(head1);
  410. }
  411. else if(wybor=='5') wyswietl(head, head1);
  412. else if(wybor=='6') wyswietlanie_tr(head1);
  413. else if(wybor=='7')
  414. {
  415. usuwanietr(&head1);
  416. zapisz_do_pliku_tr(head1);
  417. }
  418. } while(wybor!='0');
  419.  
  420. return 0;
  421.  
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement