Work 5 in C - Subscribers database by array of structs


/ Published in: C
Save to your folder(s)

Make database of subscribers that you can add, remove and sort.


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. /* Initiliazing structures */
  8. typedef struct
  9. {
  10. char name[30];
  11. char id[10];
  12. char address[30];
  13. int adults, children;
  14. double fee;
  15. } subscriber;
  16.  
  17. /* Functions prototype */
  18. void menu();
  19. subscriber * add_subscribers(subscriber * club, int * size); /* Add subscribers to array */
  20. subscriber * del_subscriber(subscriber * out_subscriber , int * size, char * id); /* Remove subscriber from array */
  21. void print_subscribers (subscriber * club, int size); /* Print subscribers from array */
  22. void id_sorting(subscriber * club, int size); /* Sort subscribers by id */
  23. void fee_sorting(subscriber * club, int size); /* Sort subscribers by fee */
  24.  
  25. void main()
  26. {
  27. int option, size = 0, i, j, removed = 0;
  28. char id[10];
  29.  
  30. /* Database of subscribers */
  31. subscriber * real_db = NULL;
  32.  
  33. while(1)
  34. {
  35. /* Call for menu func */
  36. menu();
  37.  
  38. /* Set option var */
  39. scanf("%d", &option);
  40.  
  41. /* Run programm by option */
  42. switch(option)
  43. {
  44. case 1:
  45. /* Call function, that takes pointer "club" & address of variable "size" */
  46. real_db = add_subscribers(real_db, &size);
  47. break;
  48.  
  49. case 2:
  50. if (size)
  51. {
  52. printf("Enter ID subscriber for delete:\n");
  53. gets(id);
  54.  
  55. for (i=0; i < size; i++)
  56. {
  57. if (strcmp(real_db[i].id, id) == 0)
  58. {
  59. real_db = del_subscriber(real_db, &size, id);
  60. removed = 1;
  61. printf("The subscriber is DELETED!\n");
  62. }
  63. }
  64. if (!removed)
  65. {
  66. printf("The subscriber not exist in the array!\n");
  67. }
  68. }
  69. else
  70. {
  71. printf("No subscribers!\n\n");
  72. }
  73. break;
  74.  
  75. case 3:
  76. if (size)
  77. {
  78. print_subscribers (real_db, size);
  79. }
  80. else
  81. {
  82. printf("No subscribers!\n\n");
  83. }
  84. break;
  85.  
  86. case 4:
  87. if (size)
  88. {
  89. id_sorting(real_db, size);
  90. }
  91. else
  92. {
  93. printf("No subscribers!\n\n");
  94. }
  95.  
  96. break;
  97.  
  98. case 5:
  99. if (size)
  100. {
  101. fee_sorting(real_db, size);
  102. }
  103. else
  104. {
  105. printf("No subscribers!\n\n");
  106. }
  107. break;
  108.  
  109. case 6:
  110. exit(1);
  111. break;
  112. }
  113. }
  114. }
  115.  
  116. /* Bubble sort array of structs by id */
  117. void id_sorting(subscriber * club, int size)
  118. {
  119. int i, j;
  120. subscriber temp_member;
  121.  
  122. for (i = 0; i < size; i++)
  123. {
  124. for(j = i; j < size; j++)
  125. {
  126. if (strcmp(club[i].id, club[j].id) > 0)
  127. {
  128. temp_member = club[i];
  129. club[i] = club[j];
  130. club[j] = temp_member;
  131. }
  132. }
  133. }
  134. }
  135.  
  136. /* Bubble sort array of structs by fee */
  137. void fee_sorting(subscriber * club, int size)
  138. {
  139. int i, j;
  140. subscriber temp_member;
  141.  
  142. for (i = 0; i < size; i++)
  143. {
  144. for(j = i; j < size; j++)
  145. {
  146. if (club[i].fee > club[j].fee)
  147. {
  148. temp_member = club[i];
  149. club[i] = club[j];
  150. club[j] = temp_member;
  151. }
  152. }
  153. }
  154. }
  155.  
  156. /* Print subscribers */
  157. void print_subscribers (subscriber * club, int size)
  158. {
  159. int i;
  160.  
  161. for (i = 0; i < size; i++)
  162. {
  163. printf("%-20s\t", club[i].name);
  164. printf("%-20s\t", club[i].address);
  165. printf("%-10s\t", club[i].id);
  166. printf("%.0f", club[i].fee);
  167. printf("\n");
  168. }
  169. }
  170.  
  171. /* Delete item from subscribers */
  172. subscriber * del_subscriber(subscriber * out_subscriber , int * size, char * id)
  173. {
  174. int i, j, z;
  175.  
  176. /* Temp database */
  177. subscriber * temp_db;
  178.  
  179. /* Create temp array of structs the same size like real db array is */
  180. temp_db = (subscriber *) malloc(sizeof(subscriber)*((*size)-1));
  181.  
  182. if (temp_db == NULL)
  183. {
  184. printf("No memory! Program is exiting...");
  185. exit(1);
  186. }
  187.  
  188. /* Copy array of subscriber to temp array, but without subscriber with id = blablabla */
  189. z = 0;
  190. for (j = 0; j < *size; j++)
  191. {
  192. if (strcmp(out_subscriber[j].id, id) != 0)
  193. {
  194. temp_db[z] = out_subscriber[j];
  195. z++;
  196. }
  197. }
  198. /* Delete original array of subscribers */
  199. free(out_subscriber);
  200. /* Create new array of subscriber with new size-1 */
  201. out_subscriber = (subscriber *) malloc(sizeof(subscriber)*((*size)-1));
  202.  
  203. if (out_subscriber == NULL)
  204. {
  205. printf("No memory! Program is exiting...");
  206. exit(1);
  207. }
  208.  
  209. /* Copying old subscribers to original array */
  210. for (j = 0; j < (*size)-1; j++)
  211. {
  212. out_subscriber[j] = temp_db[j];
  213. }
  214.  
  215. /* Delete temp array */
  216. free(temp_db);
  217.  
  218. /* -1 to size of our subscribers */
  219. *size = (*size)-1;
  220.  
  221. /* We must return new adress of our array beause we deleted him and create again, and adress changed */
  222. return out_subscriber;
  223. }
  224.  
  225. /* Function that insert new subscribers to the club array */
  226. subscriber * add_subscribers(subscriber * club, int * size)
  227. {
  228. int i, new_size = 0;
  229. /* Set pointer to array, that have type "subscriber" */
  230. subscriber * temp_db;
  231.  
  232. printf("Enter subscribers number:\n");
  233.  
  234. scanf("%d", &new_size);
  235.  
  236. /* If we have an subscribers in database, create "temp" array and copy them to it */
  237. if (* size > 0)
  238. {
  239. temp_db = (subscriber *) malloc(sizeof(subscriber)*(*size));
  240.  
  241. if (temp_db == NULL)
  242. {
  243. printf("No memory! Program is exiting...");
  244. exit(1);
  245. }
  246.  
  247. for (i = 0; i < *size; i++)
  248. {
  249. temp_db[i] = club[i];
  250. }
  251. /* Delete old array of subscribers */
  252. free(club);
  253. }
  254.  
  255. /* Create new club array with size of "num of old subscribers + num of new subscribers" */
  256. club = (subscriber *) malloc(sizeof(subscriber)* (*size+new_size));
  257.  
  258. if (club == NULL)
  259. {
  260. printf("No memory! Program is exiting...");
  261. exit(1);
  262. }
  263.  
  264. /* If we have old subscribers in temp, so copying them back to club array */
  265. if (* size > 0)
  266. {
  267. for (i = 00; i < *size; i++)
  268. {
  269. club[i] = temp_db[i];
  270. }
  271. free(temp_db);
  272. }
  273.  
  274. /* Add to array new subscribers */
  275. for (i = *size; i < (*size+new_size); i++)
  276. {
  277. printf("Enter name of subscriber:\n");
  278. gets(club[i].name);
  279.  
  280. printf("Enter address of subscriber:\n");
  281. gets(club[i].address);
  282.  
  283. printf("Enter ID of subscriber:\n");
  284. gets(club[i].id);
  285.  
  286. printf("Enter number of adults of subscriber:\n");
  287. scanf("%d", &club[i].adults);
  288.  
  289. printf("Enter number of children of subscriber:\n");
  290. scanf("%d", &club[i].children);
  291.  
  292. /* Calculate and set fee by children & adults */
  293. club[i].fee = club[i].children*900+club[i].adults*2000;
  294.  
  295. printf("The fee of subscriber is %.0f\n\n", club[i].fee);
  296. }
  297.  
  298. /* Set "size" our new subscribers number */
  299. * size += new_size;
  300.  
  301. return club;
  302. }
  303.  
  304. /* Function that prints menu */
  305. void menu()
  306. {
  307. printf("Choose function:\n"
  308. "1-Add subscribers\n"
  309. "2-Delete subscriber\n"
  310. "3-Print all subscribers\n"
  311. "4-Sort subscribers by ID\n"
  312. "5-Sort subscribers by fee\n"
  313. "6-Exit\n");
  314. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.