From d43edbf47555fb1a279108199806ae150f837711 Mon Sep 17 00:00:00 2001 From: "S.long" Date: Mon, 3 Aug 2020 18:46:29 +0700 Subject: [PATCH] reqdy question 3 --- .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/sbn.cassigment.iml | 9 ++ .idea/vcs.xml | 6 + .idea/workspace.xml | 61 ++++++++++ a.out | Bin 0 -> 18008 bytes work1/q3.c | 245 ++++++++++++++++++++++++++++++++++++++- 7 files changed, 329 insertions(+), 6 deletions(-) create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sbn.cassigment.iml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100755 a.out diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a4c3c7a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sbn.cassigment.iml b/.idea/sbn.cassigment.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/sbn.cassigment.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..9899cd8 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + 1596443802448 + + + + + + + + + \ No newline at end of file diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..f66544b7a36fcddcddc4cc39bfd81444f042a27d GIT binary patch literal 18008 zcmeHPdvF{@dheCAwvc661{;jQczBrDU@h_!+d;6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QrS2@1WJV`f_!YY8;`4+2^mknEj5)uHRV)s=TV` zcht;2Atlxs-C2{5a(VR(e%pl#dd^hsC?w-|(}}JqjVQd%iFOn+)bWXYHrj<3NpuXW z?G}`fNM@pVwUZFCsfBL4erC4Y-GelY)HyV71t>=9-5K<^=ku!0g!2oYgpeo3n6(Q~ zC(FFvlKIv`3WwO}T_6sRsb)v%z*;sPmB-X_1|3Mlh)o!~qS+36Y@=xR*|>41*W!lC zbR=`>D9)SFZ6nXo;V_yS`Z@dkq3RPlvR3vSn)g)PxO?hps+<=Qp?pu;jBN)sb2jbb z92#cixt*Njjn-duI(vfW0`>~TTc3rQSLO(qo7+|t%y#yW*mM_ju^DAAKa5jHh* zw&Wfom&cn;4-?orIT=Gn`20b5XK_Cq8a@d}Kk2AeWX>$EpkLkUT)7}vC{JWiLSlRpQuVB%K48@e~`S(53lS+}x z_qYFR%DzSE@%|T6-Y+A2ikaX331Acx_KziwofP%|vi0}>qdt30*)bha?wI)8i*NYs zdH=zoY^bQ;{u!UWruHeA4t$RN3qE@+^6aGOA9KJI%;hUq(08fs`0>66Q{H!FeSiGu zcP-g-{|ilND*AyVuyJ2?#Or|F_Pme6^jYPOiNE}x`|NqYh3PAwQ~ukqr@ad`?Kt}~ z{C^Pq^<|#v2e6&tvgiGPtqlN?SkLChdB#75fW|JD&-(`ae=Yd`SJ3@OiS4)_>GuQ0 zkL`Iss9pV_q4DKTY|nHGjNSGkth5_#swtKU+c8hyf)K{#YhhcWDf@n#b^dBj&d$*O z1eLGU9Vy!{b`U#5x|H11*Tquat{gV1jq-UwL%6v9JpYoI>n;t|pS`Q?%qKn}m&>|z TyZTh5=*#My`&^%aj}`w7%)xM5 literal 0 HcmV?d00001 diff --git a/work1/q3.c b/work1/q3.c index 3b2baf6..c5adc90 100644 --- a/work1/q3.c +++ b/work1/q3.c @@ -1,6 +1,7 @@ #include "stdio.h" #include "stdlib.h" #include "string.h" +#include "time.h" #define DEPOT_SIZE 10 @@ -11,7 +12,11 @@ struct Bus time_t schedule; } Depot[DEPOT_SIZE]; -typedef enum { true, false } bool; +typedef enum +{ + true, + false +} bool; void createBuses(); void printBuses(); @@ -22,32 +27,260 @@ void emergency(); // utils variable bool has_created = false; +bool has_scheduled = false; +bool has_aligned = false; +int top = -1; // utils function -print_depot(struct Bus *depot); +void clrscr(); +void print_depot(struct Bus *depot); +time_t get_random_time(); + +void confirm_on_finish(); +int quicksort_compare_func(const void *elem1, const void *elem2); +bool isEmpty(); +int menu(); + +void remove_index_of_array(int index); + +// Main Function int main() { + while (1) + { + int selection = menu(); + clrscr(); + printf("You Select : %d", selection); + printf("\n"); + + switch (selection) + { + case 1: + createBuses(); + break; + case 2: + printBuses(); + break; + + case 3: + scheduleBuses(); + break; + case 4: + alignupBuses(); + break; + + case 5: + releaseBuses(); + break; + + case 6: + emergency(); + break; + + default: + printf("You are enter incorrect option number"); + } + + confirm_on_finish(); + }; + return EXIT_SUCCESS; } +int menu() +{ + fflush(stdout); + + clrscr(); + + printf("\nQuestion 3 "); + printf("\n========================================="); + printf("\n"); + printf("\n1. Create Buses"); + printf("\n2. Print Buses"); + printf("\n3. Schedule Buses"); + printf("\n4. Align up Buses"); + printf("\n5. Release Buses"); + printf("\n6. Emergency Buses"); + + int chosen; + printf("\n\nEnter Your Selection : "); + scanf("%d", &chosen); + return chosen; +} + void createBuses() { + + if (has_created == true) + { + printf("\nYou have created already..."); + return; + } + + printf("\n\nStart Create Buses..."); + for (int i = 0; i < DEPOT_SIZE; i++) { + top++; Depot[i].BusID = i + 1; Depot[i].RouteID = 1000 + (i + i); + printf("\n - Starting Create Bus %d", i + 1); } has_created = true; + + printf("\nFinish Created Buses"); } -print_depot(struct Bus *depot) { - int size = sizeof(depot) / sizeof(depot[0]); +void printBuses() +{ + if (has_created == false) + { + printf("\nPlease Create Buses First!"); + return; + } + print_depot(Depot); +} - printf("\n Depost size: %d", size); - for (int i = 0; i < size; i++) { +void scheduleBuses() +{ + if (has_created == false) + { + printf("\nPlease Create Buses First!"); + return; + } + + printf("\nStart Scheduling Buses...\n\n"); + + size_t currentLen = sizeof(Depot) / sizeof(Depot[0]); + + for (int i = 0; i < currentLen; i++) + { + printf("\n - Start Random Schedule %d...", i); + + Depot[i].schedule = get_random_time(); + } + + printf("\n\nEnd Scheduling Buses..."); + + has_scheduled = true; +} + +void print_depot(struct Bus *depot) +{ + + size_t currentLen = sizeof(Depot) / sizeof(*Depot); + + for (int i = 0; i < top; i++) + { printf("Bus ID: %d\n", depot[i].BusID); printf("Route ID: %d\n", depot[i].RouteID); + + char *scheduleForShow = ""; + if (depot[i].schedule != 0) + { + scheduleForShow = ctime(&depot[i].schedule); + } + + printf("Schdule Time : %s\n\n", scheduleForShow); } +} + +void alignupBuses() +{ + if (has_scheduled == false) + { + printf("\n\nYou are not scheduling buses yet ..."); + return; + } + size_t currentLen = sizeof(Depot) / sizeof(Depot[0]); + + qsort(Depot, sizeof(Depot) / sizeof(*Depot), sizeof(*Depot), quicksort_compare_func); + + printf("\n\nFinish Align up Buses Schedule ... "); + has_aligned = true; +} + +void releaseBuses() +{ + if (has_aligned == false) + { + printf("\n\nYou are not align buses schedule yet ..."); + return; + } + + int last_index = sizeof(Depot) / sizeof(*Depot) - 1; + + remove_index_of_array(last_index); + + printf("\n\nRelease Complete...\n\n"); +} + +void emergency() +{ + if (has_aligned == false) + { + printf("\n\nYou are not align buses schedule yet ..."); + return; + } + + remove_index_of_array(0); + + printf("\n\nRelease Complete...\n\n"); +} + +void remove_index_of_array(int remove_index) +{ + + int current_len = sizeof(Depot) / sizeof(*Depot); + memmove(Depot + remove_index, Depot + remove_index + 1, (sizeof(Depot) - remove_index - 1) * sizeof(*Depot)); + top--; +} + +time_t get_random_time() +{ + time_t currentTime; + + time(¤tTime); + + long currentTimeNumber = (long)localtime(¤tTime); + + // Random in next 5 hours + long randomAddOnTime = rand() % (60 * 60 * 5); + + long additionTime = currentTimeNumber + randomAddOnTime; + + return additionTime; +} + +void clrscr() +{ + system("clear"); +} + +void confirm_on_finish() +{ + + printf("\n\nPress Enter to Back to Menu..."); + + getchar(); + getchar(); +} + +bool isFull() +{ + return top == -1; +} + +int quicksort_compare_func(const void *elem1, const void *elem2) +{ + struct Bus element1 = *((struct Bus *)elem1); + struct Bus element2 = *((struct Bus *)elem2); + if (element1.schedule > element2.schedule) + return -1; + if (element1.schedule < element2.schedule) + return 1; + return 0; } \ No newline at end of file