/ Published in: C
Make database of subscribers that you can add, remove and sort.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <ctype.h> /* Initiliazing structures */ typedef struct { char name[30]; char id[10]; char address[30]; int adults, children; double fee; } subscriber; /* Functions prototype */ void menu(); subscriber * add_subscribers(subscriber * club, int * size); /* Add subscribers to array */ subscriber * del_subscriber(subscriber * out_subscriber , int * size, char * id); /* Remove subscriber from array */ void print_subscribers (subscriber * club, int size); /* Print subscribers from array */ void id_sorting(subscriber * club, int size); /* Sort subscribers by id */ void fee_sorting(subscriber * club, int size); /* Sort subscribers by fee */ void main() { int option, size = 0, i, j, removed = 0; char id[10]; /* Database of subscribers */ subscriber * real_db = NULL; while(1) { /* Call for menu func */ menu(); /* Set option var */ /* Run programm by option */ switch(option) { case 1: /* Call function, that takes pointer "club" & address of variable "size" */ real_db = add_subscribers(real_db, &size); break; case 2: if (size) { for (i=0; i < size; i++) { { real_db = del_subscriber(real_db, &size, id); removed = 1; } } if (!removed) { } } else { } break; case 3: if (size) { print_subscribers (real_db, size); } else { } break; case 4: if (size) { id_sorting(real_db, size); } else { } break; case 5: if (size) { fee_sorting(real_db, size); } else { } break; case 6: break; } } } /* Bubble sort array of structs by id */ void id_sorting(subscriber * club, int size) { int i, j; subscriber temp_member; for (i = 0; i < size; i++) { for(j = i; j < size; j++) { { temp_member = club[i]; club[i] = club[j]; club[j] = temp_member; } } } } /* Bubble sort array of structs by fee */ void fee_sorting(subscriber * club, int size) { int i, j; subscriber temp_member; for (i = 0; i < size; i++) { for(j = i; j < size; j++) { if (club[i].fee > club[j].fee) { temp_member = club[i]; club[i] = club[j]; club[j] = temp_member; } } } } /* Print subscribers */ void print_subscribers (subscriber * club, int size) { int i; for (i = 0; i < size; i++) { } } /* Delete item from subscribers */ subscriber * del_subscriber(subscriber * out_subscriber , int * size, char * id) { int i, j, z; /* Temp database */ subscriber * temp_db; /* Create temp array of structs the same size like real db array is */ if (temp_db == NULL) { } /* Copy array of subscriber to temp array, but without subscriber with id = blablabla */ z = 0; for (j = 0; j < *size; j++) { { temp_db[z] = out_subscriber[j]; z++; } } /* Delete original array of subscribers */ /* Create new array of subscriber with new size-1 */ if (out_subscriber == NULL) { } /* Copying old subscribers to original array */ for (j = 0; j < (*size)-1; j++) { out_subscriber[j] = temp_db[j]; } /* Delete temp array */ /* -1 to size of our subscribers */ *size = (*size)-1; /* We must return new adress of our array beause we deleted him and create again, and adress changed */ return out_subscriber; } /* Function that insert new subscribers to the club array */ subscriber * add_subscribers(subscriber * club, int * size) { int i, new_size = 0; /* Set pointer to array, that have type "subscriber" */ subscriber * temp_db; /* If we have an subscribers in database, create "temp" array and copy them to it */ if (* size > 0) { if (temp_db == NULL) { } for (i = 0; i < *size; i++) { temp_db[i] = club[i]; } /* Delete old array of subscribers */ } /* Create new club array with size of "num of old subscribers + num of new subscribers" */ if (club == NULL) { } /* If we have old subscribers in temp, so copying them back to club array */ if (* size > 0) { for (i = 00; i < *size; i++) { club[i] = temp_db[i]; } } /* Add to array new subscribers */ for (i = *size; i < (*size+new_size); i++) { /* Calculate and set fee by children & adults */ club[i].fee = club[i].children*900+club[i].adults*2000; } /* Set "size" our new subscribers number */ * size += new_size; return club; } /* Function that prints menu */ void menu() { "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"); }