From 3b0d1b5ed77b2858c0dfe1a3b93efdb0d56e777f Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Mon, 3 Aug 2020 19:01:26 +0700 Subject: [PATCH] Updated the final file and source and zip --- {real => final}/README.md | 2 + {real => final}/q1.c | 0 {real => final}/q2.c | 0 final/q3.c | 286 +++++++++++++++++++++++++++++++++++ {real => final}/students.txt | 0 swi-final.zip | Bin 0 -> 6463 bytes 6 files changed, 288 insertions(+) rename {real => final}/README.md (52%) rename {real => final}/q1.c (100%) rename {real => final}/q2.c (100%) create mode 100644 final/q3.c rename {real => final}/students.txt (100%) create mode 100644 swi-final.zip diff --git a/real/README.md b/final/README.md similarity index 52% rename from real/README.md rename to final/README.md index 027e92f..c0f72c5 100644 --- a/real/README.md +++ b/final/README.md @@ -1,3 +1,5 @@ +### Guide +#### Compile ```shell gcc q1.c -o q1 ./q1 diff --git a/real/q1.c b/final/q1.c similarity index 100% rename from real/q1.c rename to final/q1.c diff --git a/real/q2.c b/final/q2.c similarity index 100% rename from real/q2.c rename to final/q2.c diff --git a/final/q3.c b/final/q3.c new file mode 100644 index 0000000..c5adc90 --- /dev/null +++ b/final/q3.c @@ -0,0 +1,286 @@ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +#include "time.h" + +#define DEPOT_SIZE 10 + +struct Bus +{ + int BusID; + int RouteID; + time_t schedule; +} Depot[DEPOT_SIZE]; + +typedef enum +{ + true, + false +} bool; + +void createBuses(); +void printBuses(); +void scheduleBuses(); +void alignupBuses(); +void releaseBuses(); +void emergency(); + +// utils variable +bool has_created = false; +bool has_scheduled = false; +bool has_aligned = false; +int top = -1; + +// utils function +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"); +} + +void printBuses() +{ + if (has_created == false) + { + printf("\nPlease Create Buses First!"); + return; + } + print_depot(Depot); +} + +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 diff --git a/real/students.txt b/final/students.txt similarity index 100% rename from real/students.txt rename to final/students.txt diff --git a/swi-final.zip b/swi-final.zip new file mode 100644 index 0000000000000000000000000000000000000000..d2d38063cf4f05b66e26aa6bb86d63743c2fae19 GIT binary patch literal 6463 zcmb7o1yodP_deY{boVew=Lix*x3qwi)X?41ph!!12uOzvAtBuiAt4}0BM1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