Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define true 1
  5. #define false 0
  6. typedef int bool;
  7.  
  8. typedef struct s_listaO{
  9. char imie[16];
  10. char nazwisko[16];
  11. int kwotaNaPrezent;
  12. bool czyKupiono;
  13. char opisPrezentu[32];
  14. int cenaPrezentu;
  15. struct s_listaO *next;
  16. }ListaO;
  17.  
  18. typedef struct s_listaP{
  19. int indeks;
  20. int cena;
  21. char opis[50];
  22. struct s_listaP *next;
  23. }ListaP;
  24.  
  25. ListaP *DodajPrezent (ListaP *start)
  26. {
  27. ListaP *pozycjaPrzed = start;
  28. ListaP *nowyPrezent = malloc(sizeof(ListaP));
  29. printf("Podaj opis prezentu: \n");
  30. scanf("%s", nowyPrezent->opis);
  31. printf("Podaj cene prezentu: \n");
  32. scanf("%d", &nowyPrezent->cena);
  33. if (start == 0) {
  34. printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
  35. nowyPrezent->next = NULL;
  36. nowyPrezent->indeks = 1;
  37. start = nowyPrezent;
  38. return start;
  39. }
  40. else
  41. {
  42. int mark = 0;
  43. ListaP *pozycjaPo = start->next;
  44. while (mark == 0){
  45. if ((strcmp(pozycjaPrzed->opis, nowyPrezent->opis) < 0 && pozycjaPo == NULL) || (strcmp(pozycjaPrzed->opis, nowyPrezent->opis) < 0 && strcmp(pozycjaPo->opis, nowyPrezent->opis) > 0)) {
  46. pozycjaPrzed->next = nowyPrezent;
  47. nowyPrezent->next = pozycjaPo;
  48. mark = 1;
  49. nowyPrezent->indeks = pozycjaPrzed->indeks++;
  50. printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
  51. return start;
  52. }
  53. else if (strcmp(pozycjaPrzed->opis, nowyPrezent->opis) > 0) {
  54. nowyPrezent->next = pozycjaPrzed;
  55. start = nowyPrezent;
  56. mark = 1;
  57. nowyPrezent->indeks = pozycjaPrzed->indeks--;
  58. printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
  59. return start;
  60. }
  61. else if (strcmp(pozycjaPrzed->opis, nowyPrezent->opis) == 0) {
  62. pozycjaPrzed->next = nowyPrezent;
  63. nowyPrezent->next = pozycjaPo;
  64. nowyPrezent->indeks = pozycjaPrzed->indeks++;
  65. printf("Dodano do listy prezent: %s o wartosci %d\n", nowyPrezent->opis, nowyPrezent->cena);
  66. mark = 1;
  67. return start;
  68. }
  69. else {
  70. pozycjaPrzed = pozycjaPrzed->next;
  71. pozycjaPo = pozycjaPo->next;
  72. }
  73. }
  74. }
  75. }
  76.  
  77. void WypiszPrezenty(ListaP *start)
  78. {
  79. ListaP *tmp = start;
  80. while (tmp!=0)
  81. {
  82. printf("Prezent nr %d to: %s o wartosci %d zl", tmp->indeks, tmp->opis, tmp->cena);
  83. tmp = tmp->next;
  84. }
  85. }
  86.  
  87. ListaO *Dodaj(ListaO *start, ListaP **start2)
  88. {
  89. ListaO *pozycjaPrzed = start;
  90. ListaO *nowaOsoba = malloc(sizeof(ListaO));
  91. char input[33];
  92. printf("Podaj imie i nazwisko osoby: \n");
  93. fgets(input, 32, stdin);
  94. sscanf(input,"%s %s", nowaOsoba->imie, nowaOsoba->nazwisko);
  95. printf("Podaj maksymalna kwote przeznaczona na prezent:\n");
  96. scanf("%d", &nowaOsoba->kwotaNaPrezent);
  97. printf("Aby wybrac prezent z listy, napisz lista\nAby podac nowy pomysl, napisz nowy\n");
  98. char input2[6];
  99. scanf("%s", input2);
  100. if (strncmp(input2, "lista", 5) == 0)
  101. {
  102. WypiszPrezenty(start2);
  103. }
  104. else if (strncmp(input2, "nowy", 4) == 0)
  105. {
  106. DodajPrezent(&start2);
  107. }
  108. else
  109. {
  110. printf("NIE DZIALA\n");
  111. }
  112. if (start == 0) {
  113. printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
  114. nowaOsoba->next = NULL;
  115. }
  116. else
  117. {
  118. int mark = 0;
  119. ListaO *pozycjaPo = start->next;
  120. while (mark == 0){
  121.  
  122. if ((strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) < 0 && pozycjaPo == NULL) || (strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) < 0 && strcmp(pozycjaPo->nazwisko, nowaOsoba->nazwisko) > 0)) {
  123. pozycjaPrzed->next = nowaOsoba;
  124. nowaOsoba->next = pozycjaPo;
  125. mark = 1;
  126. printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
  127. return start;
  128. }
  129. else if (strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) > 0) {
  130. nowaOsoba->next = pozycjaPrzed;
  131. start = nowaOsoba;
  132. mark = 1;
  133. printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
  134. return start;
  135. }
  136. else if (strcmp(pozycjaPrzed->nazwisko, nowaOsoba->nazwisko) == 0) {
  137. pozycjaPrzed->next = nowaOsoba;
  138. nowaOsoba->next = pozycjaPo;
  139. printf("Dodano osobe: %s %s\n", nowaOsoba->imie, nowaOsoba->nazwisko);
  140. return start;
  141. }
  142. else {
  143. pozycjaPrzed = pozycjaPrzed->next;
  144. pozycjaPo = pozycjaPo->next;
  145. }
  146. }
  147. }
  148. return start;
  149. }
  150. void Wypisz(ListaO *start)
  151. {
  152. ListaO *tmp = start;
  153. int licznik = 0;
  154. while (tmp!=0) {
  155. licznik++;
  156. printf("Osoba nr %d to: %s %s\n", licznik, tmp->imie, tmp->nazwisko);
  157. tmp=tmp->next;
  158. }
  159. }
  160.  
  161. void Zapisz(){
  162. printf("Zapisano..\n");
  163. }
  164.  
  165. void Odczytaj(){
  166. printf("Odczytano..\n");
  167. }
  168.  
  169. void WyczyscO(ListaO *start)
  170. {
  171. ListaO *czysc = start;
  172. ListaO *trzymaj = NULL;
  173. while (trzymaj!=NULL) {
  174. trzymaj = czysc->next;
  175. free(czysc);
  176. czysc = trzymaj;
  177. }
  178. }
  179.  
  180. void WyczyscP(ListaP *start)
  181. {
  182. ListaP *czysc = start;
  183. ListaP *trzymaj = NULL;
  184. while (trzymaj!=NULL) {
  185. trzymaj = czysc->next;
  186. free(czysc);
  187. czysc = trzymaj;
  188. }
  189. }
  190. int main(){
  191. char input[16];
  192. char komenda[16];
  193. ListaO *poczatekListyO = NULL;
  194. ListaP *poczatekListyP = NULL;
  195. while(fgets(input, 15, stdin))
  196. {
  197. sscanf(input, "%s", komenda);
  198. if (strncmp(komenda, "dodajo", 6) == 0)
  199. {
  200. poczatekListyO = Dodaj(poczatekListyO, poczatekListyP);
  201. }
  202. else if (strncmp(komenda, "dodajp", 6) == 0)
  203. {
  204. poczatekListyP = DodajPrezent(poczatekListyP);
  205. }
  206. else if (strncmp(komenda, "wypiszo", 12) == 0)
  207. {
  208. Wypisz(poczatekListyO);
  209. }
  210. else if (strncmp(komenda, "wypiszp", 7) == 0)
  211. {
  212. WypiszPrezenty(poczatekListyP);
  213. }
  214. else if (strncmp(komenda, "zapisz", 6) == 0)
  215. {
  216. Zapisz();
  217. }
  218. else if (strncmp(komenda, "odczytaj", 8) == 0)
  219. {
  220. Odczytaj();
  221. }
  222. else if (strncmp(komenda, "wyjdz", 5) == 0)
  223. break;
  224. }
  225. WyczyscO(poczatekListyO);
  226. WyczyscP(poczatekListyP);
  227. return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement