Havodat hagasha 5


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

Work 5 "Structures" C++


Copy this code and paste it in your HTML
  1. #define _CRT_SECURE_NO_WARNINGS
  2. //-------------------------------//
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. //----------------Structure array--------------//
  10. typedef struct stam{
  11. char name[30];
  12. char ID[10];
  13. char address[30];
  14. int adults, children; // erkev mishpaha
  15. double fee; // dmey manuyot
  16. }subscriber;
  17. //--------------structure array---------------//
  18.  
  19.  
  20. //-----------All Function declearations---------------//
  21. subscriber* add_subscribers(subscriber * club, int * size);
  22. subscriber *del_subscriber(subscriber *club, int *size, char *id);
  23. void print_subscribers (subscriber * club, int size);
  24. void id_sorting(subscriber * club, int size);
  25. void fee_sorting(subscriber * club, int size);
  26. void menu();
  27. //---------------All Function declearations------------//
  28.  
  29.  
  30.  
  31. //-------------------------------*****VOID MAIN - MAIN FUNCTION*****-------------------------------//
  32. void main()
  33. {
  34. //---Declearation of Variables--//
  35. int option, size = 0, count = 1;
  36. char id[10];
  37. subscriber *subs = NULL;
  38. //-----------------------------//
  39.  
  40. while(1)
  41. {
  42. printf("\tWelcome To Pool Club!\n\n");
  43. menu(); // MENU FOR USER
  44.  
  45. scanf("%d", &option);
  46. switch(option)
  47. {
  48. case(1): // Option 1 - Add Subscriber
  49. {
  50. subs = add_subscribers(subs, &size);
  51. break;
  52. }
  53. case(2): // Option 2 - Delete Subscriber
  54. {
  55. fflush(stdin); //for Solving the conflict of SCANF and GETS in one function
  56. printf("Enter ID for deleting\n");
  57. gets(id);
  58. subs = del_subscriber(subs, &size, id);
  59. break;
  60. }
  61. case(3): // Option 3 - Print Subscribers
  62. {
  63. print_subscribers (subs, size);
  64.  
  65. break;
  66. }
  67. case(4): // Option 4 - Sort By ID
  68. {
  69. id_sorting(subs, size);
  70. break;
  71. }
  72. case(5): // Option 5 - Sort By Fee
  73. {
  74. fee_sorting(subs, size);
  75. break;
  76. }
  77. case(6): // Option 6 - Exit from Program
  78. {
  79. exit(1);
  80. }
  81. default:
  82. printf("Invaild option has been entered, Try again!\n"); //if the option is not in the range (1 - 6)
  83. break;
  84. }
  85.  
  86. }
  87. }
  88. //------------------------------------------------------*****END VOID MAIN - MAIN FUNCTION*****----------------------------------------------//
  89.  
  90.  
  91.  
  92. //-------------------!!!!!!!!!!!!!!!!!First Function - Add Subscribers!!!!!!!!!!!!!!!!!------------------------------------------------------//
  93. subscriber* add_subscribers(subscriber * club, int * size)
  94. {
  95. //----------Declearations of function 2-------//
  96. int i, newsize = 0, scount = 1, j = 0;
  97. subscriber *newsubs;
  98. //-------------------------------------------//
  99.  
  100. printf("Enter number of subscribers for adding:\n");
  101. do
  102. {
  103. scanf("%d", &newsize); //Entering buffer size for enterig new subscribers to.
  104.  
  105. newsubs = (subscriber*)malloc(sizeof(subscriber)*(*size+newsize)); //Creating dynamic buffer for subscribers.
  106.  
  107. }while ((newsubs == NULL)&&printf("Memory allocation failed! - Not enough memory in system, enter smaller number!:\n\n")); //Cheacking if dynamic mem aloc is succeed.
  108.  
  109. //---------------Loop for filling (dynamic array) information for subscribers-----------------------------//
  110. for (i = 0; i < newsize; i++)
  111. {
  112. printf("Please Enter the folowing information for the subscriber [%d]:\n\n", scount);
  113. fflush(stdin);
  114. printf("Enter Name of subscriber:\n");
  115. gets(newsubs[i].name);
  116. printf("Enter Address of subscriber:\n");
  117. gets(newsubs[i].address);
  118. printf("Enter ID of subscriber:\n");
  119. gets(newsubs[i].ID);
  120. printf("Enter number of adults of subscriber:\n");
  121. scanf("%d", &newsubs[i].adults);
  122. printf("Enter number of children of subscriber:\n");
  123. scanf("%d", &newsubs[i].children);
  124.  
  125. newsubs[i].fee = ((newsubs[i].adults*2000) + (newsubs[i].children*900));
  126. printf("The fee of subscriber is: %.1lf\n\n", newsubs[i].fee);
  127. scount++;
  128. }
  129. //--------------------------------------------------------------------------------------------------------//
  130.  
  131. //---------------------------Loop for copying the old buffer to new allocated buffer---------------------//
  132. for (i = newsize; i < *size+newsize; i++)
  133. {
  134. newsubs[i] = club[j];
  135. j++;
  136. }
  137. //-------------------------------------------------------------------------------------------------------//
  138.  
  139. //------------------------------------------Free the old buffer and return the new one-------------------//
  140. free(club);
  141. *size = *size+newsize;
  142.  
  143. return newsubs;
  144. //-------------------------------------------------------------------------------------------------------//
  145. }
  146. //-----------------------------------!!!!!End First Function!!!!!!-----------------------------------//
  147.  
  148.  
  149.  
  150.  
  151. //----------------------------!!!!!!FUNCTION TWO - DELETING SUBSCRIBERS!!!!!!!-------------------------------//
  152. subscriber *del_subscriber(subscriber *club, int *size, char *id)
  153. {
  154. //-----------------Function 2 Declearations------------//
  155. int i, j = 0, index = 0;
  156. subscriber *newsubs;
  157. //-----------------------------------------------------//
  158.  
  159. //---------------Loop for searching the id fr deleting----------------------//
  160. for (i = 0; i < *size; i++)
  161. {
  162. if (strcmp(club[i].ID, id) == 0)
  163. {
  164. index = i;
  165. break; //if found - do not continue (break)
  166. }
  167. }
  168. //----------------------------------------------------------------------------//
  169.  
  170. //----------------------Case that index (of ID) not found in the loop below----------//
  171. if (i == *size)
  172. {
  173. printf ("ID: [%d] is not found!!!\n\n", id);
  174. return club;
  175. }
  176. //-----------------------------------------------------------------------------------//
  177.  
  178. //-----------------------------If index (ID) found - do the following------------------------//
  179. else
  180. {
  181. newsubs = (subscriber*)malloc((*size-1)*sizeof(subscriber)); //Dynamically allocated memory for new buffer
  182.  
  183. //--------------------------------------Checking if allocation is succeed---------------------------//
  184. if (newsubs == NULL)
  185. {
  186. printf ("Deleting subscriber is failed! - The memory allocation is failed!\n\n");
  187. return club;
  188. }
  189. //-------------------------------------------------------------------------------------------------//
  190.  
  191. //----------------------------------Coping the old buffer to new one withount the index (of ID)-----//
  192. for (i = 0; i < *size; i++)
  193. {
  194. if (i != index)
  195. newsubs[j++] = club[i];
  196. }
  197. //--------------------------------------------------------------------------------------------------//
  198.  
  199. printf ("The ID: [%d] removed successcfuly\n\n", id);
  200.  
  201. free(club); //Free old buffer
  202. *size = *size-1;
  203.  
  204. //----------------------------in case that buffer left empty--------------------------------------//
  205. if (*size == 0)
  206. {
  207. free(newsubs);
  208. newsubs = NULL;
  209. }
  210. //------------------------------------------------------------------------------------------------//
  211.  
  212. return newsubs; //Return the new buffer
  213. }
  214. //----------------------------------------------------------------------------------//
  215. }
  216. //------------------------------------------!!!!!!!!END FUNCTION TWO!!!!!!!!-------------------------------------------------//
  217.  
  218.  
  219.  
  220.  
  221.  
  222. //--------------------------------------------FUNCTION THREE - PRINT ALL SSUBSCRIBERS--------------------------------------------//
  223. void print_subscribers (subscriber * club, int size)
  224. {
  225. int i; // Declearations of func 3
  226.  
  227. //---------------------------------------------IF buffer is empty---------------------//
  228. if (club == NULL)
  229. printf("There are no subscribers!\n\n");
  230. //-----------------------------------------------------------------------------------//
  231.  
  232. //------------------IF NOT - Loop for printing all subscribers that exist in buffer-----------------//
  233. else
  234. {
  235. printf("The list of subscribers:\n\n");
  236.  
  237. for (i = 0; i < size; i++)
  238. {
  239. printf("%s\t\t", club[i].name);
  240. printf("%s\t\t", club[i].address);
  241. printf("%s\t\t", club[i].ID);
  242. printf("%.1lf\n", club[i].fee);
  243. }
  244. }
  245. printf("\n");
  246. //-------------------------------------------------------------------------------------------------//
  247. }
  248. //-------------------------------!!!!!END FUNCTION THREE!!!!!-----------------------------//
  249.  
  250.  
  251.  
  252.  
  253.  
  254. //-------------------------------!!!!!FUNCTION FOUR - SORTING SUBSCRIBERS BY ID!!!!!-----------------------//
  255. void id_sorting(subscriber * club, int size)
  256. {
  257. int i, j, result = 0; //Declearations fo func 4
  258.  
  259. if (club != NULL) //IF buffer is not empty
  260. {
  261. subscriber temp_arr; //Temporary structure for coping
  262.  
  263. //---------------------------Loop for comparing index and sorting--------------------//
  264. for (i = 0 ; i < size ; i++)
  265. {
  266. for (j = 0 ; j < size-i-1 ; j++)
  267. {
  268. result = strcmp (club[j].ID, club[j+1].ID);
  269. if (result > 0)
  270. {
  271. temp_arr = club[j];
  272. club[j]=club[j+1];
  273. club[j+1]=temp_arr;
  274. }
  275. }
  276. }
  277. //----------------------------------------------------------------------------------//
  278.  
  279. printf("The array sorting by ID is succeeded!\n\n");
  280. }
  281. else //IF Buffer Is Empty
  282. printf("The array is empty! Nothing to sort!\n\n");
  283. }
  284. //------------------------------------------!!!!!END FUNCTION FOUR!!!!!---------------------------------------//
  285.  
  286.  
  287.  
  288.  
  289.  
  290. //------------------------------------------!!!!FUNCTION FIVE - SORTING SUBSCRIBERS BY FEE!!!!!------------//
  291. void fee_sorting(subscriber * club, int size)
  292. {
  293. //----------------Declearations of function 5-----------//
  294. int i, j;
  295. subscriber temp_arr;
  296. //-----------------------------------------------------//
  297.  
  298. if (club != NULL) //IF Buffer Is NOT Empty
  299. {
  300. //--------------------------Loop - for comparing each index and sort--------------------------------//
  301. for (i = 0 ; i < size ; i++)
  302. {
  303. for (j = 0 ; j < size-i-1 ; j++)
  304. {
  305. if(club[j].fee > club[j+1].fee)
  306. {
  307. temp_arr = club[j];
  308. club[j] = club[j+1];
  309. club[j+1] = temp_arr;
  310. }
  311. }
  312. }
  313. //--------------------------------------------------------------------------------------------------//
  314. printf("The array sorting by fee is succeeded!\n\n");
  315. }
  316. else //IF Buffer IS Empty
  317. printf("The array is empty! Nothing to sort!\n\n");
  318. }
  319.  
  320. //------------------------------------------!!!!!END FUNCTION FIVE!!!!!--------------------------------------//
  321.  
  322.  
  323.  
  324.  
  325.  
  326. //---------!!!!!MENU FUNCTION - PRINTING THE OPTION MENU!!!!!----------------------//
  327. void menu()
  328. {
  329. printf("Choose one of the following options:\n" "1 - Add subscribers\n"
  330. "2 - Delete subscriber\n"
  331. "3 - Print all subscribers \n"
  332. "4 - Sort subscribers by ID\n"
  333. "5 - Sort subscribers by fee\n"
  334. "6 - Exit\n");
  335. }
  336. //----------------------------!!!!!END MENU!!!!!-----------------------------------//
  337.  
  338.  
  339.  
  340. //***************************************************END PROGRAM************************************************//

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.