/ Published in: C++
Work 5 "Structures" C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#define _CRT_SECURE_NO_WARNINGS //-------------------------------// #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <ctype.h> //----------------Structure array--------------// typedef struct stam{ char name[30]; char ID[10]; char address[30]; int adults, children; // erkev mishpaha double fee; // dmey manuyot }subscriber; //--------------structure array---------------// //-----------All Function declearations---------------// subscriber* add_subscribers(subscriber * club, int * size); subscriber *del_subscriber(subscriber *club, int *size, char *id); void print_subscribers (subscriber * club, int size); void id_sorting(subscriber * club, int size); void fee_sorting(subscriber * club, int size); void menu(); //---------------All Function declearations------------// //-------------------------------*****VOID MAIN - MAIN FUNCTION*****-------------------------------// void main() { //---Declearation of Variables--// int option, size = 0, count = 1; char id[10]; subscriber *subs = NULL; //-----------------------------// while(1) { printf("\tWelcome To Pool Club!\n\n"); menu(); // MENU FOR USER scanf("%d", &option); switch(option) { case(1): // Option 1 - Add Subscriber { subs = add_subscribers(subs, &size); break; } case(2): // Option 2 - Delete Subscriber { fflush(stdin); //for Solving the conflict of SCANF and GETS in one function printf("Enter ID for deleting\n"); gets(id); subs = del_subscriber(subs, &size, id); break; } case(3): // Option 3 - Print Subscribers { print_subscribers (subs, size); break; } case(4): // Option 4 - Sort By ID { id_sorting(subs, size); break; } case(5): // Option 5 - Sort By Fee { fee_sorting(subs, size); break; } case(6): // Option 6 - Exit from Program { exit(1); } default: printf("Invaild option has been entered, Try again!\n"); //if the option is not in the range (1 - 6) break; } } } //------------------------------------------------------*****END VOID MAIN - MAIN FUNCTION*****----------------------------------------------// //-------------------!!!!!!!!!!!!!!!!!First Function - Add Subscribers!!!!!!!!!!!!!!!!!------------------------------------------------------// subscriber* add_subscribers(subscriber * club, int * size) { //----------Declearations of function 2-------// int i, newsize = 0, scount = 1, j = 0; subscriber *newsubs; //-------------------------------------------// printf("Enter number of subscribers for adding:\n"); do { scanf("%d", &newsize); //Entering buffer size for enterig new subscribers to. newsubs = (subscriber*)malloc(sizeof(subscriber)*(*size+newsize)); //Creating dynamic buffer for subscribers. }while ((newsubs == NULL)&&printf("Memory allocation failed! - Not enough memory in system, enter smaller number!:\n\n")); //Cheacking if dynamic mem aloc is succeed. //---------------Loop for filling (dynamic array) information for subscribers-----------------------------// for (i = 0; i < newsize; i++) { printf("Please Enter the folowing information for the subscriber [%d]:\n\n", scount); fflush(stdin); printf("Enter Name of subscriber:\n"); gets(newsubs[i].name); printf("Enter Address of subscriber:\n"); gets(newsubs[i].address); printf("Enter ID of subscriber:\n"); gets(newsubs[i].ID); printf("Enter number of adults of subscriber:\n"); scanf("%d", &newsubs[i].adults); printf("Enter number of children of subscriber:\n"); scanf("%d", &newsubs[i].children); newsubs[i].fee = ((newsubs[i].adults*2000) + (newsubs[i].children*900)); printf("The fee of subscriber is: %.1lf\n\n", newsubs[i].fee); scount++; } //--------------------------------------------------------------------------------------------------------// //---------------------------Loop for copying the old buffer to new allocated buffer---------------------// for (i = newsize; i < *size+newsize; i++) { newsubs[i] = club[j]; j++; } //-------------------------------------------------------------------------------------------------------// //------------------------------------------Free the old buffer and return the new one-------------------// free(club); *size = *size+newsize; return newsubs; //-------------------------------------------------------------------------------------------------------// } //-----------------------------------!!!!!End First Function!!!!!!-----------------------------------// //----------------------------!!!!!!FUNCTION TWO - DELETING SUBSCRIBERS!!!!!!!-------------------------------// subscriber *del_subscriber(subscriber *club, int *size, char *id) { //-----------------Function 2 Declearations------------// int i, j = 0, index = 0; subscriber *newsubs; //-----------------------------------------------------// //---------------Loop for searching the id fr deleting----------------------// for (i = 0; i < *size; i++) { if (strcmp(club[i].ID, id) == 0) { index = i; break; //if found - do not continue (break) } } //----------------------------------------------------------------------------// //----------------------Case that index (of ID) not found in the loop below----------// if (i == *size) { printf ("ID: [%d] is not found!!!\n\n", id); return club; } //-----------------------------------------------------------------------------------// //-----------------------------If index (ID) found - do the following------------------------// else { newsubs = (subscriber*)malloc((*size-1)*sizeof(subscriber)); //Dynamically allocated memory for new buffer //--------------------------------------Checking if allocation is succeed---------------------------// if (newsubs == NULL) { printf ("Deleting subscriber is failed! - The memory allocation is failed!\n\n"); return club; } //-------------------------------------------------------------------------------------------------// //----------------------------------Coping the old buffer to new one withount the index (of ID)-----// for (i = 0; i < *size; i++) { if (i != index) newsubs[j++] = club[i]; } //--------------------------------------------------------------------------------------------------// printf ("The ID: [%d] removed successcfuly\n\n", id); free(club); //Free old buffer *size = *size-1; //----------------------------in case that buffer left empty--------------------------------------// if (*size == 0) { free(newsubs); newsubs = NULL; } //------------------------------------------------------------------------------------------------// return newsubs; //Return the new buffer } //----------------------------------------------------------------------------------// } //------------------------------------------!!!!!!!!END FUNCTION TWO!!!!!!!!-------------------------------------------------// //--------------------------------------------FUNCTION THREE - PRINT ALL SSUBSCRIBERS--------------------------------------------// void print_subscribers (subscriber * club, int size) { int i; // Declearations of func 3 //---------------------------------------------IF buffer is empty---------------------// if (club == NULL) printf("There are no subscribers!\n\n"); //-----------------------------------------------------------------------------------// //------------------IF NOT - Loop for printing all subscribers that exist in buffer-----------------// else { printf("The list of subscribers:\n\n"); for (i = 0; i < size; i++) { printf("%s\t\t", club[i].name); printf("%s\t\t", club[i].address); printf("%s\t\t", club[i].ID); printf("%.1lf\n", club[i].fee); } } printf("\n"); //-------------------------------------------------------------------------------------------------// } //-------------------------------!!!!!END FUNCTION THREE!!!!!-----------------------------// //-------------------------------!!!!!FUNCTION FOUR - SORTING SUBSCRIBERS BY ID!!!!!-----------------------// void id_sorting(subscriber * club, int size) { int i, j, result = 0; //Declearations fo func 4 if (club != NULL) //IF buffer is not empty { subscriber temp_arr; //Temporary structure for coping //---------------------------Loop for comparing index and sorting--------------------// for (i = 0 ; i < size ; i++) { for (j = 0 ; j < size-i-1 ; j++) { result = strcmp (club[j].ID, club[j+1].ID); if (result > 0) { temp_arr = club[j]; club[j]=club[j+1]; club[j+1]=temp_arr; } } } //----------------------------------------------------------------------------------// printf("The array sorting by ID is succeeded!\n\n"); } else //IF Buffer Is Empty printf("The array is empty! Nothing to sort!\n\n"); } //------------------------------------------!!!!!END FUNCTION FOUR!!!!!---------------------------------------// //------------------------------------------!!!!FUNCTION FIVE - SORTING SUBSCRIBERS BY FEE!!!!!------------// void fee_sorting(subscriber * club, int size) { //----------------Declearations of function 5-----------// int i, j; subscriber temp_arr; //-----------------------------------------------------// if (club != NULL) //IF Buffer Is NOT Empty { //--------------------------Loop - for comparing each index and sort--------------------------------// for (i = 0 ; i < size ; i++) { for (j = 0 ; j < size-i-1 ; j++) { if(club[j].fee > club[j+1].fee) { temp_arr = club[j]; club[j] = club[j+1]; club[j+1] = temp_arr; } } } //--------------------------------------------------------------------------------------------------// printf("The array sorting by fee is succeeded!\n\n"); } else //IF Buffer IS Empty printf("The array is empty! Nothing to sort!\n\n"); } //------------------------------------------!!!!!END FUNCTION FIVE!!!!!--------------------------------------// //---------!!!!!MENU FUNCTION - PRINTING THE OPTION MENU!!!!!----------------------// void menu() { printf("Choose one of the following options:\n" "1 - Add subscribers\n" "2 - Delete subscriber\n" "3 - Print all subscribers \n" "4 - Sort subscribers by ID\n" "5 - Sort subscribers by fee\n" "6 - Exit\n"); } //----------------------------!!!!!END MENU!!!!!-----------------------------------// //***************************************************END PROGRAM************************************************//