Compare commits

...

16 Commits

Author SHA1 Message Date
3b0d1b5ed7 Updated the final file and source and zip 2020-08-03 19:01:26 +07:00
caee955384 Updated cached 2020-08-03 18:49:57 +07:00
ee7a5389c9 reset .idaa 2020-08-03 18:47:00 +07:00
d43edbf475 reqdy question 3 2020-08-03 18:46:29 +07:00
094998cc79 Updated and create q3 2020-08-03 14:58:28 +07:00
139b370552 Updated q3 2020-08-03 14:31:09 +07:00
cbd1003ebb Updated run script 2020-08-03 13:36:40 +07:00
80a8b001de Init function header 2020-08-03 13:36:14 +07:00
a290788bd3 Init q3 2020-08-03 13:22:48 +07:00
e13be3a0b6 Updated q1 for update then show all records again 2020-08-03 13:19:44 +07:00
cd08400d5f Add real q2 2020-08-03 13:17:18 +07:00
c005c34103 complete q2 in student id 2020-08-03 13:14:33 +07:00
c8f250e9d6 init q2 for new sol 2020-08-03 11:59:06 +07:00
579fa56ae8 Updated compile and name 2020-08-03 11:57:27 +07:00
90bdecd365 Updated for q1 and compeleted 2020-08-03 11:54:43 +07:00
b7228aab16 Updated menu and order the function 2020-08-03 11:39:03 +07:00
12 changed files with 842 additions and 37 deletions

5
.gitignore vendored
View File

@@ -1 +1,4 @@
*.bin
*.bin
.idea
*.out

View File

@@ -31,4 +31,10 @@ CHS10
CHS2
2
19
90
90
CMS
345
SDC
2
1
4

View File

@@ -1,3 +1,5 @@
### Guide
#### Compile
```shell
gcc q1.c -o q1
./q1

View File

@@ -38,13 +38,14 @@ typedef struct student_tag STUDENT;
void read_file();
// util functions
void display_menu();
int menu();
void print_welcome();
void print_student(STUDENT *student);
void release(STUDENT *data);
int find_min_in_array(int *array);
STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE]);
STUDENT *find_maximum_avg();
STUDENT *add_last_student(STUDENT *nodes, STUDENT *element);
// core functions
void display_students();
@@ -56,6 +57,7 @@ void quite();
// Linked list functions
void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[MAX_NO_OF_UNITS]);
int count_elements(STUDENT *elements);
int count();
int main(void)
@@ -70,9 +72,7 @@ int main(void)
while (1)
{
display_menu();
printf("Enter your option: ");
scanf("%d", &selected);
selected = menu();
switch (selected)
{
@@ -123,6 +123,7 @@ void display_students()
void print_student(STUDENT *student)
{
int records_count = 0;
printf("\n================ Student Details ================\n");
while (student != NULL)
{
@@ -140,22 +141,31 @@ void print_student(STUDENT *student)
printf("]\n");
printf("Avg: %.2f\n", student->course_info.avg);
student = student->next;
records_count++;
}
// output the records count
printf("\nRecords: %d has been loaded successfully!\n", records_count);
// clean up the memory
// after output the data
// because we don;t need it anymore after output
release(student);
}
void display_menu()
int menu()
{
printf("\n(1) Display students’ details\n");
printf("(2) Search for a student’s mark\n");
printf("(3) Find the details of student with the largest average\n");
printf("(4) Find the details of failed students\n");
printf("(5) Add new student to the record\n");
printf("(6) Quit program\n\n");
printf("(6) Quit program\n");
int choosen;
printf("\nEnter your option: ");
scanf("%d", &choosen);
return choosen;
}
void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[MAX_NO_OF_UNITS])
@@ -200,11 +210,11 @@ void add_student(char student_name[20], char student_id[10], char course_name[20
}
}
int count()
int count_elements(STUDENT *elements)
{
int n = 1;
STUDENT *temp;
temp = head;
temp = elements;
if (head == NULL)
{
return 0;
@@ -219,6 +229,11 @@ int count()
return n;
}
int count()
{
return count_elements(head);
}
void search_student()
{
char search_key[NAME_SIZE];
@@ -229,7 +244,7 @@ void search_student()
STUDENT *found_student = search_student_by_name_or_id(search_key);
if (found_student == NULL)
{
printf("No student found!");
printf("\nNo student found!\n");
return;
}
@@ -248,6 +263,13 @@ STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE])
if (strcmp(temp->student_info.name, search_key) == 0 || strcmp(temp->student_info.id, search_key) == 0)
{
printf("\nSearch found with key: %s\n", search_key);
// set the temp next to null
// and free up the memory
temp->next = NULL;
free(temp->next);
// return the current temp
// that found in current search
return temp;
}
temp = temp->next;
@@ -308,6 +330,35 @@ int find_min_in_array(int *array)
return min;
}
STUDENT *add_last_student(STUDENT *nodes, STUDENT *element)
{
STUDENT *temp = nodes, *node;
node = (struct student_tag *)malloc(sizeof(struct student_tag));
node->student_info = element->student_info;
node->course_info = element->course_info;
node->next = NULL;
if (nodes == NULL)
{
nodes = node;
return nodes;
}
// store current as tempo
// ref for nodes
temp = nodes;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
return nodes;
}
// ISSUE: can't find all failed students
// It's just return one record back
STUDENT *find_failed_mark(int upper_mark)
@@ -315,30 +366,31 @@ STUDENT *find_failed_mark(int upper_mark)
// refresh data from file first
read_file();
printf("Count elements: %d\n", count());
STUDENT *temp = head, *failed_students = NULL;
int count = count_elements(temp);
printf("Temp count elements: %d", count);
int run_count = 0;
while (temp != NULL)
{
run_count++;
int min = find_min_in_array(temp->course_info.marks);
if (min < upper_mark)
{
if (failed_students == NULL)
{
failed_students = temp;
failed_students->next = NULL;
}
else
{
while (failed_students->next != NULL)
{
failed_students = failed_students->next;
}
failed_students->next = temp;
}
printf("\nRun min: %d\n", min);
failed_students = add_last_student(failed_students, temp);
}
temp = temp->next;
}
printf("\nRun count: %d", run_count);
return failed_students;
}
@@ -398,7 +450,7 @@ again:
if (no_of_units > MAX_NO_OF_UNITS && no_of_units <= 0)
{
printf("you cannot input the units bigger than %d or less than 0\n", MAX_NO_OF_UNITS);
printf("\nyou cannot input the units bigger than %d or less than 0\n", MAX_NO_OF_UNITS);
getchar();
goto again;
}
@@ -430,10 +482,13 @@ again:
fclose(file);
printf("\nRecord saved successfully!");
printf("\nRecord saved successfully!\n");
// reload data into linked list again
read_file();
// read_file();
// show back the all results
display_students();
}
void read_file()

79
final/q2.c Normal file
View File

@@ -0,0 +1,79 @@
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STUDENT_ID_SIZE 10
#define CHAR_INT '0'
struct studentID
{
int value;
struct studentID *next;
};
typedef struct studentID STUDENTID;
typedef STUDENTID *STUDENTIDPtr;
int main()
{
const char student_id[] = "1001245345";
// 1. creation nodes
size_t student_id_len = sizeof(student_id) / sizeof(student_id[0]) - 1;
STUDENTIDPtr student_ptr_1 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_2 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_3 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_4 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_5 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
student_ptr_1->next = student_ptr_2;
student_ptr_2->next = student_ptr_3;
student_ptr_3->next = student_ptr_4;
student_ptr_4->next = student_ptr_5;
student_ptr_5->next = NULL;
// 2. fill the node's values
STUDENTIDPtr temp = student_ptr_1;
int last_len = 5;
while (temp != NULL)
{
temp->value = student_id[student_id_len - last_len] - CHAR_INT;
temp = temp->next;
last_len--;
}
// 3. removes dup
STUDENTIDPtr temp1, temp2, duplicate;
temp1 = student_ptr_1;
while (temp1 != NULL && temp1->next != NULL)
{
temp2 = temp1;
while (temp2->next != NULL)
{
if (temp1->value == temp2->next->value)
{
duplicate = temp2->next;
temp2->next = temp2->next->next;
free(duplicate);
}
else
temp2 = temp2->next;
}
temp1 = temp1->next;
}
// output the result
while (student_ptr_1 != NULL)
{
printf("%d ", student_ptr_1->value);
student_ptr_1 = student_ptr_1->next;
}
printf("\n");
return EXIT_SUCCESS;
}

286
final/q3.c Normal file
View File

@@ -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(&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;
}

2
run
View File

@@ -7,4 +7,4 @@
rm -r single.bin
gcc work1/single.c -o single.bin && ./single.bin
gcc work1/q3.c -o single.bin && ./single.bin

BIN
swi-final.zip Normal file

Binary file not shown.

View File

@@ -38,7 +38,7 @@ typedef struct student_tag STUDENT;
void read_file();
// util functions
void display_menu();
int menu();
void print_welcome();
void print_student(STUDENT *student);
void release(STUDENT *data);
@@ -72,9 +72,7 @@ int main(void)
while (1)
{
display_menu();
printf("Enter your option: ");
scanf("%d", &selected);
selected = menu();
switch (selected)
{
@@ -155,14 +153,19 @@ void print_student(STUDENT *student)
release(student);
}
void display_menu()
int menu()
{
printf("\n(1) Display students’ details\n");
printf("(2) Search for a student’s mark\n");
printf("(3) Find the details of student with the largest average\n");
printf("(4) Find the details of failed students\n");
printf("(5) Add new student to the record\n");
printf("(6) Quit program\n\n");
printf("(6) Quit program\n");
int choosen;
printf("\nEnter your option: ");
scanf("%d", &choosen);
return choosen;
}
void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[MAX_NO_OF_UNITS])
@@ -447,7 +450,7 @@ again:
if (no_of_units > MAX_NO_OF_UNITS && no_of_units <= 0)
{
printf("you cannot input the units bigger than %d or less than 0\n", MAX_NO_OF_UNITS);
printf("\nyou cannot input the units bigger than %d or less than 0\n", MAX_NO_OF_UNITS);
getchar();
goto again;
}
@@ -479,10 +482,12 @@ again:
fclose(file);
printf("\nRecord saved successfully!");
printf("\nRecord saved successfully!\n");
// reload data into linked list again
read_file();
// read_file();
display_students();
}
void read_file()

83
work1/q2.c Normal file
View File

@@ -0,0 +1,83 @@
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STUDENT_ID_SIZE 10
#define CHAR_INT '0'
struct studentID
{
int value;
struct studentID *next;
};
typedef struct studentID STUDENTID;
typedef STUDENTID *STUDENTIDPtr;
int main()
{
const char student_id[] = "1001245345";
// printf("Student ID: %s\n", student_id);
// memcpy(five_digits, &student_id[5], 5);
// five_digits[5] = '\0';
// creation nodes
size_t student_id_len = sizeof(student_id) / sizeof(student_id[0]) - 1;
STUDENTIDPtr student_ptr_1 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_2 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_3 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_4 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
STUDENTIDPtr student_ptr_5 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
student_ptr_1->next = student_ptr_2;
student_ptr_2->next = student_ptr_3;
student_ptr_3->next = student_ptr_4;
student_ptr_4->next = student_ptr_5;
student_ptr_5->next = NULL;
// fill the node's values
STUDENTIDPtr temp = student_ptr_1;
int last_len = 5;
while (temp != NULL)
{
temp->value = student_id[student_id_len - last_len] - CHAR_INT;
temp = temp->next;
last_len--;
}
// removes dup
STUDENTIDPtr temp1, temp2, duplicate;
temp1 = student_ptr_1;
while (temp1 != NULL && temp1->next != NULL)
{
temp2 = temp1;
while (temp2->next != NULL)
{
if (temp1->value == temp2->next->value)
{
duplicate = temp2->next;
temp2->next = temp2->next->next;
free(duplicate);
}
else
temp2 = temp2->next;
}
temp1 = temp1->next;
}
while (student_ptr_1 != NULL)
{
printf("%d ", student_ptr_1->value);
student_ptr_1 = student_ptr_1->next;
}
printf("\n");
return EXIT_SUCCESS;
}

286
work1/q3.c Normal file
View File

@@ -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(&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;
}