Posted By

AlexanderRavikovich on 01/23/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

oakdragon


Work 5 in C - Subscribers database by array of structs


 / Published in: C
 

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

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

Report this snippet  

You need to login to post a comment.