reqdy question 3

This commit is contained in:
S.long 2020-08-03 18:46:29 +07:00
parent 094998cc79
commit d43edbf475
7 changed files with 329 additions and 6 deletions

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/sbn.cassigment.iml" filepath="$PROJECT_DIR$/.idea/sbn.cassigment.iml" />
</modules>
</component>
</project>

9
.idea/sbn.cassigment.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

61
.idea/workspace.xml Normal file
View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="148ae5c7-f1d7-4a85-b690-f098fa2020ae" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/work1/q3.c" beforeDir="false" afterPath="$PROJECT_DIR$/work1/q3.c" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1fa6Llp1oeaDTJ4C2tIincKHtqA" />
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="148ae5c7-f1d7-4a85-b690-f098fa2020ae" name="Default Changelist" comment="" />
<created>1596443802448</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1596443802448</updated>
<workItem from="1596443803712" duration="377000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>

BIN
a.out Executable file

Binary file not shown.

View File

@ -1,6 +1,7 @@
#include "stdio.h" #include "stdio.h"
#include "stdlib.h" #include "stdlib.h"
#include "string.h" #include "string.h"
#include "time.h"
#define DEPOT_SIZE 10 #define DEPOT_SIZE 10
@ -11,7 +12,11 @@ struct Bus
time_t schedule; time_t schedule;
} Depot[DEPOT_SIZE]; } Depot[DEPOT_SIZE];
typedef enum { true, false } bool; typedef enum
{
true,
false
} bool;
void createBuses(); void createBuses();
void printBuses(); void printBuses();
@ -22,32 +27,260 @@ void emergency();
// utils variable // utils variable
bool has_created = false; bool has_created = false;
bool has_scheduled = false;
bool has_aligned = false;
int top = -1;
// utils function // 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() 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; 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() 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++) for (int i = 0; i < DEPOT_SIZE; i++)
{ {
top++;
Depot[i].BusID = i + 1; Depot[i].BusID = i + 1;
Depot[i].RouteID = 1000 + (i + i); Depot[i].RouteID = 1000 + (i + i);
printf("\n - Starting Create Bus %d", i + 1);
} }
has_created = true; has_created = true;
printf("\nFinish Created Buses");
} }
print_depot(struct Bus *depot) { void printBuses()
int size = sizeof(depot) / sizeof(depot[0]); {
if (has_created == false)
{
printf("\nPlease Create Buses First!");
return;
}
print_depot(Depot);
}
printf("\n Depost size: %d", size); void scheduleBuses()
for (int i = 0; i < size; i++) { {
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("Bus ID: %d\n", depot[i].BusID);
printf("Route ID: %d\n", depot[i].RouteID); 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(&currentTime);
long currentTimeNumber = (long)localtime(&currentTime);
// 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;
}