Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. struct Data {
  7. char number[30];
  8. char name[30];
  9. char surname[30];
  10. char group[15];
  11. };
  12.  
  13. struct Element {
  14. struct Element* next;
  15. struct Element* last;
  16. struct Data data;
  17. };
  18.  
  19. void File_error(void) {
  20. //exit(1);
  21. printf(" Blad pliku, sprobuj ponownie \n");
  22. }
  23.  
  24. struct Element* New_element(struct Element* last_element) {
  25. struct Element* new = malloc(sizeof(struct Element));
  26.  
  27. new->last = NULL;
  28. new->next = NULL;
  29. strcpy(new->data.name, "brak");
  30. strcpy(new->data.surname, "brak");
  31. strcpy(new->data.number, "brak");
  32. strcpy(new->data.group, "brak");
  33. return new;
  34. }
  35.  
  36. struct Element* Free_element(struct Element* active) {
  37. struct Element* temp = malloc(sizeof(struct Element));
  38. temp = active->last;
  39. free(active);
  40. return temp;
  41. }
  42.  
  43. void Find(struct Element* active) {
  44. char find[20], name[30], surname[30];
  45. char cha;
  46. char* tab;
  47. int i = 0;
  48.  
  49. struct Element* temp = malloc(sizeof(struct Element));
  50. temp->last = NULL;
  51. temp->next = NULL;
  52.  
  53. struct Element* temp_active;
  54.  
  55. while (1) {
  56.  
  57. printf(" Lista znalezionych kontaktow: \n");
  58. if (i > 0) {
  59. while (temp->last != NULL) {
  60. printf(" %s %s %s %s \n", temp->data.name, temp->data.surname, temp->data.number, temp->data.group);
  61. temp = Free_element(temp);
  62. }
  63. }
  64.  
  65. printf(" Wpisz dane ktore chcesz wyszukac: \n ");
  66. cha = _getch(stdin);
  67. if ( cha == 13) break;
  68.  
  69. if ( cha == 8) {
  70. find[i] = '\o';
  71. if(i > 0) i--;
  72. }
  73. else {
  74. temp_active = active;
  75. find[i] = cha;
  76.  
  77. for (int j = 0; j <= i; j++) if (find[j] >= 97) find[j] = find[j] - 32; // zamiana malych liter na duze
  78.  
  79. while (temp_active->last != NULL) {
  80. strcpy(name, temp_active->data.name);
  81. //strcpy(surname, temp_active->data.surname);
  82. if (strstr(&name, &find) != NULL) {
  83. printf(" siema \n");
  84. temp = New_element(temp);
  85. temp_active->next = temp;
  86. temp->data = temp_active->data;
  87. temp->last = temp_active;
  88. }
  89. temp_active = temp_active->last;
  90. }
  91.  
  92. for (int k = 0; k <= i; k++) find[k] = find[k] + 32; // zamiana na male litery
  93.  
  94.  
  95. i++;
  96. }
  97. for (int l = 0; l < i; l++) {
  98. printf("%c", find[l]);
  99. }
  100. printf("\n");
  101. }
  102.  
  103. while (temp->last != NULL) {
  104. temp = Free_element(temp);
  105. }
  106. free(temp);
  107. }
  108.  
  109. struct Element* Swap(struct Element* Active) {
  110. if (Active->last != NULL) Active->last->next = Active->next;
  111. Active->next->last = Active->last;
  112.  
  113. Active->last = Active->next;
  114. Active->next = Active->last->next;
  115.  
  116. Active->last->next = Active;
  117.  
  118. if (Active->next != NULL) {
  119. Active->next->last = Active;
  120. return Active->next;
  121. }
  122. else return Active;
  123. }
  124.  
  125. struct Element* Sort(struct Element* Active, int choose) {
  126.  
  127. while (Active->last != NULL) {
  128. int result = 10;
  129.  
  130. switch (choose) {
  131. case 1: {
  132. result = strcoll(Active->last->data.name, Active->data.name);
  133. break;
  134. }
  135. case 2: {
  136. result = strcoll(Active->last->data.surname, Active->data.surname);
  137. break;
  138. }
  139. case 3: {
  140. result = strcoll(Active->last->data.group, Active->data.group);
  141. break;
  142. }
  143. default: {
  144. printf(" blad strcoll \n");
  145. break;
  146. }
  147. }
  148.  
  149.  
  150. switch ( result ) {
  151. case 1: {
  152. Active = Active->last;
  153. break;
  154. }
  155. case -1: {
  156. Active = Swap(Active->last);
  157. while (Active->next != NULL) {
  158. Active = Active->next;
  159. }
  160. break;
  161. }
  162. case 0: {
  163. Active = Active->last;
  164. break;
  165. }
  166. default: {
  167. printf(" blad strcoll \n");
  168. break;
  169. }
  170. }
  171. }
  172.  
  173. while (Active->next != NULL) {
  174. Active = Active->next;
  175. }
  176. return Active;
  177. }
  178.  
  179. void Print_elements(struct Element* active) {
  180. do {
  181. printf(" %s %s %s %s \n", active->data.name, active->data.surname, active->data.number, active->data.group);
  182. active = active->last;
  183. } while (active->last != NULL);
  184. printf(" %s %s %s %s \n", active->data.name, active->data.surname, active->data.number, active->data.group);
  185. }
  186.  
  187. struct Element* Add_element(struct Element* active, int sort) {
  188. struct Element* temp;
  189. temp = active;
  190. active = New_element(active);
  191. temp->next = active;
  192. active->last = temp;
  193.  
  194. printf(" Podaj dane kontaktu \n Imie: \n ");
  195. scanf(" %s", active->data.name);
  196. printf(" Nazwisko: \n ");
  197. scanf(" %s", active->data.surname);
  198. printf(" Numer: \n ");
  199. scanf(" %s", active->data.number);
  200. printf(" Grupa: \n ");
  201. scanf(" %s", active->data.group);
  202.  
  203.  
  204. if (sort != 0) active = Sort(active, sort);
  205. return active;
  206. }
  207.  
  208. struct Element* Menu(struct Element* active) {
  209. int menu = 1, sort_menu, sort = 0;
  210.  
  211. while (menu != 0) {
  212. printf(" Menu \n 1. Wyswietl liste kontaktow \n 2. Sotruj \n 3. Dodaj numer \n 4. Usun kontakt \n 5. Szukaj kontaktu \n 0. Wyjdz \n ");
  213. if (!scanf(" %d", &menu))
  214. {
  215. while ('\n' != getchar());
  216. }
  217. else
  218. {
  219. switch (menu) {
  220. case 1: {
  221. Print_elements(active);
  222. break;
  223. }
  224.  
  225. case 2: {
  226. sort_menu = 1;
  227. while (sort_menu != 0) {
  228. printf(" Wybierz sposob sortowania: \n 1. Sortowanie wedlug imienia \n 2. Sortowanie wedlug nazwiska \n 3. Sortowanie wedlug grupy \n ");
  229. if (!scanf(" %d", &sort)) while ('\n' != getchar());
  230. else {
  231. if (sort > 0 && sort < 4) {
  232. active = Sort(active, sort);
  233. sort_menu = 0;
  234. }
  235. else printf("Nie ma takiej mozliwosci, sprobuj ponownie \n ");
  236. }
  237. }
  238. break;
  239. }
  240. case 3: {
  241. active = Add_element(active, sort);
  242. break;
  243. }
  244. case 4: {
  245.  
  246. break;
  247. }
  248. case 5: {
  249. Find(active);
  250. break;
  251. }
  252. case 0: {
  253. break;
  254. }
  255. default: {
  256. printf("Blad switch \n");
  257. break;
  258. }
  259.  
  260. }
  261. }
  262. }
  263. return active;
  264. }
  265.  
  266. int main() {
  267. struct Element* active = malloc(sizeof(struct Element));
  268. active->last = NULL;
  269. active->next = NULL;
  270. int i = 0;
  271.  
  272. FILE* file = fopen("lista kontaktow.csv", "r");
  273. if (!file) File_error();
  274. else {
  275. while (fscanf(file, " %[^;] ; %[^;] ; %[^;] ; %[^\n] ", active->data.name, active->data.surname, active->data.number, active->data.group) != EOF) {
  276. struct Element* temp;
  277. temp = active;
  278. active = New_element(active);
  279. temp->next = active;
  280. active->last = temp;
  281. }
  282. fclose(file);
  283. }
  284.  
  285. active->last->next = NULL;
  286. active = Free_element(active);
  287. active = Menu(active);
  288.  
  289. while (active->last != NULL) {
  290. active = Free_element(active);
  291. }
  292. free(active);
  293.  
  294. return 0;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement