Compare commits
72 Commits
7f4670089c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b0d1b5ed7 | |||
| caee955384 | |||
| ee7a5389c9 | |||
| d43edbf475 | |||
| 094998cc79 | |||
| 139b370552 | |||
| cbd1003ebb | |||
| 80a8b001de | |||
| a290788bd3 | |||
| e13be3a0b6 | |||
| cd08400d5f | |||
| c005c34103 | |||
| c8f250e9d6 | |||
| 579fa56ae8 | |||
| 90bdecd365 | |||
| b7228aab16 | |||
| f8749e3427 | |||
| 7bffb64752 | |||
| f929cea52d | |||
| 657807d6a2 | |||
| 52dcaf6fac | |||
| c5416d4704 | |||
| 1007604842 | |||
| ee1ec269b4 | |||
| 9880763344 | |||
| 6a0deedead | |||
| b3ddc90786 | |||
| ef484509f3 | |||
| 55a392584e | |||
| 5d13eb0e38 | |||
| ff0e21d42b | |||
| 909f55f621 | |||
| 632b863ff9 | |||
| aabb594c70 | |||
| 9aa3dc2919 | |||
| 63966b9e34 | |||
| b277d96c36 | |||
| 284a856634 | |||
| 227c5e896d | |||
| 443a30a5d4 | |||
| ba3a1df3a4 | |||
| 171e2e38e6 | |||
| 527b38f3a2 | |||
| 23e2579e56 | |||
| d4887d22ba | |||
| 501babfbf7 | |||
| 2c10067d3e | |||
| 11a394e2ef | |||
| 1f0e556137 | |||
| bc93e0313c | |||
| b1fe773f8a | |||
| 7f888fe3b6 | |||
| bcbcbdcd37 | |||
| 3762d25f4e | |||
| 46cd2556bc | |||
| 9d9eb77688 | |||
| 10fe1747d3 | |||
| b9356d3bc3 | |||
| cdb08a328e | |||
| 7aae05eae6 | |||
| 566ae2c7e8 | |||
| 7bc6a62777 | |||
| f78b5369d6 | |||
| 651042c6b6 | |||
| 47f8fc2341 | |||
| 14f7cb286f | |||
| 366dd9de1b | |||
| 436d5bb6d6 | |||
| 2b3d65c582 | |||
| 7bce631799 | |||
| 5724110cbb | |||
| 1ee2386f7c |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
*.bin
|
||||
|
||||
.idea
|
||||
*.out
|
||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"typeinfo": "c"
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
# SBN C Assigement
|
||||
# SBN C-Assigement
|
||||
|
||||
- [ ] Student Details
|
||||
- [ ] ...
|
||||
- [ ] Linked List
|
||||
- [ ] Book Data Store
|
||||
- [ ] Etc.
|
||||
0
data/books.txt
Normal file
0
data/books.txt
Normal file
40
data/students.test.txt
Normal file
40
data/students.test.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
Jon
|
||||
1101825
|
||||
Computer
|
||||
4
|
||||
88
|
||||
98
|
||||
52
|
||||
95
|
||||
Sambo
|
||||
12345
|
||||
CS90
|
||||
2
|
||||
50
|
||||
40
|
||||
Chea
|
||||
6789
|
||||
CS78
|
||||
2
|
||||
50
|
||||
20
|
||||
SMC1
|
||||
7890
|
||||
CGS
|
||||
4
|
||||
40
|
||||
50
|
||||
60
|
||||
70
|
||||
CHS10
|
||||
7823
|
||||
CHS2
|
||||
2
|
||||
19
|
||||
90
|
||||
CMS
|
||||
345
|
||||
SDC
|
||||
2
|
||||
1
|
||||
4
|
||||
4
data/test.txt
Normal file
4
data/test.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
hey, how are you?
|
||||
i'm fine. thank you!
|
||||
then what you think?
|
||||
nothing!
|
||||
6
final/README.md
Normal file
6
final/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
### Guide
|
||||
#### Compile
|
||||
```shell
|
||||
gcc q1.c -o q1
|
||||
./q1
|
||||
```
|
||||
598
final/q1.c
Normal file
598
final/q1.c
Normal file
@@ -0,0 +1,598 @@
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
#define MAX_NO_OF_UNITS 4
|
||||
#define NAME_SIZE 20
|
||||
|
||||
const char *FILE_STUDENT_DATA_PATH = "./students.txt";
|
||||
|
||||
struct person_tag
|
||||
{
|
||||
char name[20];
|
||||
char id[10];
|
||||
};
|
||||
|
||||
struct course_tag
|
||||
{
|
||||
char course_name[20];
|
||||
int no_of_units;
|
||||
int marks[MAX_NO_OF_UNITS];
|
||||
float avg;
|
||||
};
|
||||
|
||||
struct student_tag
|
||||
{
|
||||
struct person_tag student_info;
|
||||
struct course_tag course_info;
|
||||
struct student_tag *next;
|
||||
} * head;
|
||||
|
||||
// typedef for declared to use as data type
|
||||
typedef struct person_tag PERSON;
|
||||
typedef struct course_tag COURSE;
|
||||
typedef struct student_tag STUDENT;
|
||||
|
||||
// file function
|
||||
void read_file();
|
||||
|
||||
// util functions
|
||||
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();
|
||||
void search_student();
|
||||
void find_maximum();
|
||||
void find_failed();
|
||||
void update_file();
|
||||
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)
|
||||
{
|
||||
// variable use for menu selected
|
||||
int selected;
|
||||
|
||||
print_welcome();
|
||||
|
||||
// init the read data from the file
|
||||
read_file();
|
||||
|
||||
while (1)
|
||||
{
|
||||
selected = menu();
|
||||
|
||||
switch (selected)
|
||||
{
|
||||
case 1:
|
||||
display_students();
|
||||
break;
|
||||
case 2:
|
||||
search_student();
|
||||
break;
|
||||
case 3:
|
||||
find_maximum();
|
||||
break;
|
||||
case 4:
|
||||
find_failed();
|
||||
break;
|
||||
case 5:
|
||||
update_file();
|
||||
break;
|
||||
case 6:
|
||||
quite();
|
||||
break;
|
||||
default:
|
||||
printf("cannot find your option!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_welcome()
|
||||
{
|
||||
printf("Welcome!\n");
|
||||
}
|
||||
|
||||
void display_students()
|
||||
{
|
||||
read_file();
|
||||
|
||||
STUDENT *student = head;
|
||||
|
||||
if (student == NULL)
|
||||
{
|
||||
printf("\nNo student!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
print_student(student);
|
||||
}
|
||||
|
||||
void print_student(STUDENT *student)
|
||||
{
|
||||
int records_count = 0;
|
||||
printf("\n================ Student Details ================\n");
|
||||
while (student != NULL)
|
||||
{
|
||||
printf("\n");
|
||||
printf("Student Name: %s\n", student->student_info.name);
|
||||
printf("Student ID: %s\n", student->student_info.id);
|
||||
printf("Course Name: %s\n", student->course_info.course_name);
|
||||
printf("No of units: %d\n", student->course_info.no_of_units);
|
||||
|
||||
printf("Marks: [ ");
|
||||
for (int i = 0; i < student->course_info.no_of_units; i++)
|
||||
{
|
||||
printf("%d ", student->course_info.marks[i]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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])
|
||||
{
|
||||
STUDENT *temp, *iterator;
|
||||
temp = (struct student_tag *)malloc(sizeof(struct student_tag));
|
||||
PERSON info;
|
||||
memcpy(info.name, student_name, 20);
|
||||
memcpy(info.id, student_id, 10);
|
||||
|
||||
COURSE course;
|
||||
memcpy(course.course_name, course_name, 20);
|
||||
course.no_of_units = no_of_units;
|
||||
// memcpy(course.marks, marks);
|
||||
|
||||
float sum = 0;
|
||||
for (int i = 0; i < no_of_units; i++)
|
||||
{
|
||||
course.marks[i] = marks[i];
|
||||
sum += marks[i];
|
||||
}
|
||||
course.avg = sum / no_of_units;
|
||||
temp->student_info = info;
|
||||
temp->course_info = course;
|
||||
|
||||
// reference in head
|
||||
iterator = head;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
head = temp;
|
||||
head->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (iterator->next != NULL)
|
||||
{
|
||||
iterator = iterator->next;
|
||||
}
|
||||
temp->next = NULL;
|
||||
iterator->next = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int count_elements(STUDENT *elements)
|
||||
{
|
||||
int n = 1;
|
||||
STUDENT *temp;
|
||||
temp = elements;
|
||||
if (head == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
n++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int count()
|
||||
{
|
||||
return count_elements(head);
|
||||
}
|
||||
|
||||
void search_student()
|
||||
{
|
||||
char search_key[NAME_SIZE];
|
||||
|
||||
printf("Enter student name or id to search: ");
|
||||
scanf("%s", &search_key);
|
||||
|
||||
STUDENT *found_student = search_student_by_name_or_id(search_key);
|
||||
if (found_student == NULL)
|
||||
{
|
||||
printf("\nNo student found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
print_student(found_student);
|
||||
}
|
||||
|
||||
STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE])
|
||||
{
|
||||
// refresh data from file first
|
||||
read_file();
|
||||
|
||||
STUDENT *temp = head;
|
||||
|
||||
while (temp != NULL)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
STUDENT *find_maximum_avg()
|
||||
{
|
||||
// refresh data from file first
|
||||
read_file();
|
||||
|
||||
STUDENT *temp = head, *max;
|
||||
max = temp->next;
|
||||
|
||||
while (temp != NULL)
|
||||
{
|
||||
if (max == NULL)
|
||||
{
|
||||
return temp;
|
||||
}
|
||||
|
||||
if (max->course_info.avg < temp->course_info.avg)
|
||||
{
|
||||
max = temp;
|
||||
}
|
||||
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// release the max next record
|
||||
// set max next element to NULL
|
||||
// because we use only one record
|
||||
release(max->next);
|
||||
max->next = NULL;
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
int find_min_in_array(int *array)
|
||||
{
|
||||
if (array == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int min = array[0];
|
||||
size_t size = sizeof(array) / sizeof(array[0]);
|
||||
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
if (array[i] < min)
|
||||
{
|
||||
min = array[i];
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void find_maximum()
|
||||
{
|
||||
// refresh data from file first
|
||||
read_file();
|
||||
|
||||
STUDENT *max_student = find_maximum_avg();
|
||||
if (max_student == NULL)
|
||||
{
|
||||
printf("\nNo maximum student found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nFind maximum avg was found with: %0.2f\n", max_student->course_info.avg);
|
||||
print_student(max_student);
|
||||
}
|
||||
|
||||
void find_failed()
|
||||
{
|
||||
int upper_mark = 50;
|
||||
STUDENT *failed_students = find_failed_mark(upper_mark);
|
||||
if (failed_students == NULL)
|
||||
{
|
||||
printf("\nNo failed student found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nFind the failed students that at least one mark less than %d\n", upper_mark);
|
||||
print_student(failed_students);
|
||||
}
|
||||
|
||||
void update_file()
|
||||
{
|
||||
FILE *file;
|
||||
file = fopen(FILE_STUDENT_DATA_PATH, "a");
|
||||
|
||||
char name[20];
|
||||
char id[10];
|
||||
char course_name[20];
|
||||
int no_of_units;
|
||||
int marks[MAX_NO_OF_UNITS];
|
||||
|
||||
printf("Enter student name: ");
|
||||
scanf("%s", &name);
|
||||
|
||||
printf("Enter student id: ");
|
||||
scanf("%s", &id);
|
||||
|
||||
printf("Enter course name: ");
|
||||
scanf("%s", &course_name);
|
||||
|
||||
again:
|
||||
printf("Enter no of units: ");
|
||||
scanf("%d", &no_of_units);
|
||||
|
||||
if (no_of_units > MAX_NO_OF_UNITS && no_of_units <= 0)
|
||||
{
|
||||
printf("\nyou cannot input the units bigger than %d or less than 0\n", MAX_NO_OF_UNITS);
|
||||
getchar();
|
||||
goto again;
|
||||
}
|
||||
|
||||
for (int i = 0; i < no_of_units; i++)
|
||||
{
|
||||
printf("Enter mark[%d]: ", i + 1);
|
||||
scanf("%d", &marks[i]);
|
||||
}
|
||||
|
||||
if (count() > 0)
|
||||
{
|
||||
fputs("\n", file);
|
||||
}
|
||||
|
||||
fputs(name, file);
|
||||
fputs("\n", file);
|
||||
fputs(id, file);
|
||||
fputs("\n", file);
|
||||
fputs(course_name, file);
|
||||
fputs("\n", file);
|
||||
fprintf(file, "%d", no_of_units);
|
||||
|
||||
for (int i = 0; i < no_of_units; i++)
|
||||
{
|
||||
fputs("\n", file);
|
||||
fprintf(file, "%d", marks[i]);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
printf("\nRecord saved successfully!\n");
|
||||
|
||||
// reload data into linked list again
|
||||
// read_file();
|
||||
|
||||
// show back the all results
|
||||
display_students();
|
||||
}
|
||||
|
||||
void read_file()
|
||||
{
|
||||
// release nodes
|
||||
// we need to clean up the memory
|
||||
if (head != NULL)
|
||||
{
|
||||
STUDENT *temp;
|
||||
while (head != NULL)
|
||||
{
|
||||
temp = head;
|
||||
head = head->next;
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
|
||||
FILE *file;
|
||||
file = fopen(FILE_STUDENT_DATA_PATH, "r");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("cannot read file: %s", FILE_STUDENT_DATA_PATH);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char student_name[20];
|
||||
char student_id[10];
|
||||
char course_name[20];
|
||||
int no_of_units;
|
||||
int marks[4];
|
||||
|
||||
int i;
|
||||
while (!feof(file))
|
||||
{
|
||||
char no[BUFFER_SIZE];
|
||||
|
||||
fgets(student_name, sizeof student_name, file);
|
||||
fgets(student_id, sizeof student_id, file);
|
||||
fgets(course_name, sizeof course_name, file);
|
||||
fgets(no, sizeof no, file);
|
||||
|
||||
i = 0;
|
||||
while (student_name[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
student_name[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
while (student_id[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
student_id[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
while (course_name[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
course_name[i] = '\0';
|
||||
|
||||
no_of_units = atoi(no);
|
||||
for (int j = 0; j < no_of_units; j++)
|
||||
{
|
||||
char mark[BUFFER_SIZE];
|
||||
|
||||
fgets(mark, sizeof mark, file);
|
||||
sscanf(mark, "%d", &marks[j]);
|
||||
}
|
||||
|
||||
// add into linked list
|
||||
add_student(student_name, student_id, course_name, no_of_units, marks);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void quite()
|
||||
{
|
||||
printf("\nGoodbye!");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void release(STUDENT *data)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// free the nodes
|
||||
// because it can be use in memory
|
||||
// we need to clear it first
|
||||
// before we re-initailize the new data
|
||||
STUDENT *temp;
|
||||
while (data != NULL)
|
||||
{
|
||||
temp = data;
|
||||
data = data->next;
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
79
final/q2.c
Normal file
79
final/q2.c
Normal 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
286
final/q3.c
Normal 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(¤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;
|
||||
}
|
||||
37
final/students.txt
Normal file
37
final/students.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
Jon
|
||||
1101825
|
||||
Computer
|
||||
4
|
||||
88
|
||||
98
|
||||
52
|
||||
95
|
||||
Peter
|
||||
112152
|
||||
Electrical
|
||||
3
|
||||
67
|
||||
40
|
||||
59
|
||||
Mary
|
||||
1201925
|
||||
Mechanical
|
||||
4
|
||||
78
|
||||
55
|
||||
79
|
||||
75
|
||||
Sherin
|
||||
1201925
|
||||
Civil
|
||||
4
|
||||
69
|
||||
53
|
||||
34
|
||||
88
|
||||
Jose
|
||||
34567
|
||||
Software
|
||||
2
|
||||
34
|
||||
56
|
||||
12
run
12
run
@@ -1,6 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "compile the program.c file..."
|
||||
gcc work1/main.c -o program
|
||||
echo "run the program..."
|
||||
./program
|
||||
# echo "compile the program.c file..."
|
||||
# gcc work1/main.c -o program
|
||||
# echo "run the program..."
|
||||
# ./program
|
||||
|
||||
rm -r single.bin
|
||||
|
||||
gcc work1/q3.c -o single.bin && ./single.bin
|
||||
3
runBook
Executable file
3
runBook
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
gcc tests/bookNode.c -o bookNode.bin && ./bookNode.bin
|
||||
BIN
swi-final.zip
Normal file
BIN
swi-final.zip
Normal file
Binary file not shown.
238
tests/bookNode.c
Normal file
238
tests/bookNode.c
Normal file
@@ -0,0 +1,238 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct book
|
||||
{
|
||||
char *title;
|
||||
char *author;
|
||||
char *isbn;
|
||||
struct book *next;
|
||||
};
|
||||
|
||||
typedef struct book BOOK;
|
||||
|
||||
FILE *file;
|
||||
BOOK *head;
|
||||
|
||||
void add_book(char *title, char *author, char *isbn);
|
||||
void display();
|
||||
int count();
|
||||
void add_book_data();
|
||||
void save_book_data(struct book *data);
|
||||
void read_file_to_data();
|
||||
void print_data(BOOK *data);
|
||||
|
||||
int main()
|
||||
{
|
||||
head = NULL;
|
||||
|
||||
int countEntries = count();
|
||||
printf("There are %d entries\n", countEntries);
|
||||
|
||||
// add book data
|
||||
add_book_data();
|
||||
|
||||
// display the books
|
||||
display();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void add_book(char *title, char *author, char *isbn)
|
||||
{
|
||||
struct book *temp, *iterator;
|
||||
temp = (struct book *)malloc(sizeof(struct book));
|
||||
temp->title = title;
|
||||
temp->author = author;
|
||||
temp->isbn = isbn;
|
||||
// ref
|
||||
iterator = head;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
head = temp;
|
||||
head->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (iterator->next != NULL)
|
||||
{
|
||||
iterator = iterator->next;
|
||||
}
|
||||
temp->next = NULL;
|
||||
iterator->next = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int count()
|
||||
{
|
||||
int n = 1;
|
||||
BOOK *temp;
|
||||
temp = head;
|
||||
if (head == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
n++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void add_book_data()
|
||||
{
|
||||
add_book("My Third Book", "Zero 1", "000-000-111");
|
||||
add_book("My Fouth Book", "1 Zero", "111-000-000");
|
||||
add_book("My Fifth Book", "2 Zero", "222-000-000");
|
||||
|
||||
int i;
|
||||
file = fopen("./data/books.txt", "a+");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("Error: addressbook.dat could not be opened.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
BOOK *temp;
|
||||
temp = head;
|
||||
|
||||
fputs(temp->title, file);
|
||||
fputs("\n", file);
|
||||
fputs(temp->author, file);
|
||||
fputs("\n", file);
|
||||
fputs(temp->isbn, file);
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
fputs("\n", file);
|
||||
fputs(temp->title, file);
|
||||
fputs("\n", file);
|
||||
fputs(temp->author, file);
|
||||
fputs("\n", file);
|
||||
fputs(temp->isbn, file);
|
||||
}
|
||||
|
||||
// close the file
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void save_book_data(struct book *data)
|
||||
{
|
||||
file = fopen("./data/books.txt", "a+");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("Error: addressbook.dat could not be opened.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
BOOK *temp;
|
||||
temp = data;
|
||||
|
||||
if (temp == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fputs(temp->title, file);
|
||||
fputs(temp->author, file);
|
||||
fputs(temp->isbn, file);
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
|
||||
fputs(temp->title, file);
|
||||
fputs(temp->author, file);
|
||||
fputs(temp->isbn, file);
|
||||
}
|
||||
|
||||
fflush(file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void read_file_to_data()
|
||||
{
|
||||
// clear head in linked list
|
||||
head = NULL;
|
||||
|
||||
FILE *file;
|
||||
|
||||
file = fopen("./data/books.txt", "r");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("cannot read file!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char title[20];
|
||||
char author[20];
|
||||
char isbn[20];
|
||||
int i;
|
||||
|
||||
while (!feof(file))
|
||||
{
|
||||
fgets(title, 20, file);
|
||||
fgets(author, 20, file);
|
||||
fgets(isbn, 20, file);
|
||||
|
||||
i = 0;
|
||||
|
||||
while (title[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
title[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
|
||||
while (author[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
author[i] = '\0';
|
||||
|
||||
add_book(title, author, isbn);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void display()
|
||||
{
|
||||
read_file_to_data();
|
||||
print_data(head);
|
||||
}
|
||||
|
||||
void print_data(BOOK *book)
|
||||
{
|
||||
BOOK *temp;
|
||||
temp = book;
|
||||
if (temp == NULL)
|
||||
{
|
||||
printf("book is empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Book title: %s\n", temp->title);
|
||||
printf("Book author: %s\n", temp->author);
|
||||
printf("Book isbn: %s", temp->isbn);
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
printf("\n\n============ %s ============\n", temp->title);
|
||||
printf("Book title: %s\n", temp->title);
|
||||
printf("Book author: %s\n", temp->author);
|
||||
printf("Book isbn: %s", temp->isbn);
|
||||
}
|
||||
}
|
||||
225
tests/linkedlist.c
Normal file
225
tests/linkedlist.c
Normal file
@@ -0,0 +1,225 @@
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
struct node *next;
|
||||
};
|
||||
|
||||
struct node *head = NULL;
|
||||
void add_first(int);
|
||||
void add_last(int);
|
||||
void traverse();
|
||||
void remove_first();
|
||||
void remove_last();
|
||||
int count = 0;
|
||||
void display_menu();
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Welcome!\n");
|
||||
|
||||
int selected, data;
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("\n");
|
||||
|
||||
// show menu
|
||||
display_menu();
|
||||
|
||||
printf("Enter your option: ");
|
||||
scanf("%d", &selected);
|
||||
|
||||
if (selected == 1)
|
||||
{
|
||||
printf("Enter you element to add first: ");
|
||||
scanf("%d", &data);
|
||||
add_first(data);
|
||||
}
|
||||
else if (selected == 2)
|
||||
{
|
||||
printf("Enter you element to add last: ");
|
||||
scanf("%d", &data);
|
||||
add_last(data);
|
||||
}
|
||||
else if (selected == 3)
|
||||
{
|
||||
traverse();
|
||||
}
|
||||
else if (selected == 4)
|
||||
{
|
||||
remove_first;
|
||||
}
|
||||
else if (selected == 5)
|
||||
{
|
||||
remove_last();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\nGoodbye!");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void display_menu()
|
||||
{
|
||||
printf("1. Add first element\n");
|
||||
printf("2. Add last element\n");
|
||||
printf("3. Traverse element\n");
|
||||
printf("4. Remove first element\n");
|
||||
printf("5. Remove last element\n");
|
||||
printf("Any to quite program\n");
|
||||
}
|
||||
|
||||
void add_first(int element)
|
||||
{
|
||||
// init node
|
||||
struct node *node;
|
||||
|
||||
// allocate the node for linkedlist
|
||||
node = (struct node *)malloc(sizeof(struct node));
|
||||
|
||||
// add the data into node
|
||||
node->data = element;
|
||||
|
||||
// count the nodes, that added into data
|
||||
count++;
|
||||
|
||||
// check the head, if null put the data into it
|
||||
if (head == NULL)
|
||||
{
|
||||
// replace the node into head
|
||||
head = node;
|
||||
|
||||
// set the next of head into null
|
||||
head->next = NULL;
|
||||
|
||||
printf("Element %d has been added into the first!", element);
|
||||
|
||||
// return or end this process
|
||||
return;
|
||||
}
|
||||
|
||||
// put the first element of the head
|
||||
// because we need the old head into the next of new head
|
||||
node->next = head;
|
||||
|
||||
// replace the head node from current node
|
||||
head = node;
|
||||
|
||||
printf("Element %d has been added into the first!", element);
|
||||
}
|
||||
|
||||
void add_last(int element)
|
||||
{
|
||||
struct node *node, *temp;
|
||||
|
||||
node = (struct node *)malloc(sizeof(struct node));
|
||||
node->data = element;
|
||||
count++;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
head = node;
|
||||
head->next = NULL;
|
||||
|
||||
printf("Element %d has been add into first/last!\n", element);
|
||||
return;
|
||||
}
|
||||
|
||||
// store current as tempo
|
||||
temp = head;
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
temp->next = node;
|
||||
node->next = NULL;
|
||||
head = temp;
|
||||
}
|
||||
|
||||
void traverse()
|
||||
{
|
||||
struct node *node;
|
||||
|
||||
node = head;
|
||||
|
||||
if (node == NULL)
|
||||
{
|
||||
printf("Linked list is empty!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Total elements count: %d in linked list.\n", count);
|
||||
|
||||
while (node->next != NULL)
|
||||
{
|
||||
printf("%d\n", node->data);
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
printf("%d\n", node->data);
|
||||
}
|
||||
|
||||
void remove_first()
|
||||
{
|
||||
struct node *node;
|
||||
int data;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
printf("No elements found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
data = head->data;
|
||||
node = head->next;
|
||||
free(head);
|
||||
head = node;
|
||||
count--;
|
||||
|
||||
printf("Element %d has been removed from first!\n", data);
|
||||
}
|
||||
|
||||
void remove_last()
|
||||
{
|
||||
struct node *node, *temp;
|
||||
int data;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
printf("No elements found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
count--;
|
||||
|
||||
if (head->next == NULL)
|
||||
{
|
||||
data = head->data;
|
||||
free(head);
|
||||
head = NULL;
|
||||
printf("Element %d has been removed from last!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
node = head;
|
||||
|
||||
while (node->next != NULL)
|
||||
{
|
||||
temp = node;
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
data = node->data;
|
||||
temp->next = NULL;
|
||||
free(node);
|
||||
|
||||
printf("Element %d has been removed from last!\n");
|
||||
}
|
||||
47
tests/readFileFromText.c
Normal file
47
tests/readFileFromText.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct list
|
||||
{
|
||||
char *text;
|
||||
struct list *next;
|
||||
};
|
||||
|
||||
typedef struct list LIST;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
FILE *file;
|
||||
char line[128];
|
||||
LIST *current, *head;
|
||||
|
||||
head = current = NULL;
|
||||
file = fopen("./data/test.txt", "r");
|
||||
|
||||
while (fgets(line, sizeof(line), file))
|
||||
{
|
||||
LIST *node = malloc(sizeof(LIST));
|
||||
node->text = strdup(line);
|
||||
node->next = NULL;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
current = head = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
current = current->next = node;
|
||||
}
|
||||
}
|
||||
|
||||
// close the file
|
||||
fclose(file);
|
||||
|
||||
for (current = head; current; current = current->next)
|
||||
{
|
||||
printf("%s", current->text);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
gcc work1.c -o work1
|
||||
./work1
|
||||
rm -r work1.bin
|
||||
gcc work1.c -o work1.bin && ./work1.bin
|
||||
7
tests/runLinkedlist
Executable file
7
tests/runLinkedlist
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
rm -r linkedlist
|
||||
|
||||
gcc linkedlist.c -o linkedlist
|
||||
|
||||
./linkedlist
|
||||
25
tests/test1.c
Normal file
25
tests/test1.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
struct person {
|
||||
char name[20];
|
||||
int age;
|
||||
};
|
||||
|
||||
struct student {
|
||||
int id;
|
||||
struct person info;
|
||||
struct student *next;
|
||||
};
|
||||
|
||||
void add_first();
|
||||
void add_last();
|
||||
void remove_first();
|
||||
void remove_last();
|
||||
void show();
|
||||
void quite();
|
||||
|
||||
int main() {
|
||||
|
||||
printf("Goodbye!");
|
||||
}
|
||||
17
tests/util.c
Normal file
17
tests/util.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "stdio.h"
|
||||
|
||||
void print(char *text)
|
||||
{
|
||||
printf(text);
|
||||
}
|
||||
|
||||
void println(char *text)
|
||||
{
|
||||
newline();
|
||||
print(text);
|
||||
}
|
||||
|
||||
void newline()
|
||||
{
|
||||
printf("\n");
|
||||
}
|
||||
BIN
tests/work1
BIN
tests/work1
Binary file not shown.
@@ -1,5 +1,7 @@
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "stdlib.h"
|
||||
#include "util.c"
|
||||
|
||||
struct person_tag
|
||||
{
|
||||
@@ -24,7 +26,7 @@ struct student_tag
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int selected;
|
||||
struct person_tag person, person2 = {"Chea", "4567892"}, *personPtr;
|
||||
|
||||
// strcpy style
|
||||
@@ -37,5 +39,97 @@ int main()
|
||||
printf("Hello, %s \n", person2.name);
|
||||
printf("Hello Ptr, %s \n", personPtr->name);
|
||||
|
||||
// using linkedlist
|
||||
struct student_tag *first = NULL;
|
||||
struct student_tag *second = NULL;
|
||||
struct student_tag *third = NULL;
|
||||
|
||||
first = (struct student_tage *)malloc(sizeof(struct student_tag *));
|
||||
|
||||
struct course_tag c1 = {"Computer", 4, {1, 2, 3, 4}};
|
||||
struct person_tag p1 = {"Sambo", "121214"};
|
||||
first->course_info = c1;
|
||||
first->student_info = p1;
|
||||
first->next = second;
|
||||
|
||||
printf("Student name: %s \n", first->student_info.name);
|
||||
printf("Student id: %s \n", first->student_info.id);
|
||||
printf("Course name: %s \n", first->course_info.course_name);
|
||||
printf("Course of units: %d", first->course_info.no_of_units);
|
||||
|
||||
read_file("./../data/welcome.txt");
|
||||
|
||||
display_menu();
|
||||
|
||||
newline();
|
||||
|
||||
// scanf("\nenter the option: ", selected);
|
||||
|
||||
// menu(selected);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void read_file(char *filename)
|
||||
{
|
||||
// read the file here...
|
||||
FILE *file;
|
||||
char c;
|
||||
|
||||
if ((file = fopen(filename, "r")) == NULL)
|
||||
{
|
||||
printf("no file were found...!");
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((c = getc(file)) != EOF)
|
||||
{
|
||||
printf("%c", c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void menu(int menu)
|
||||
{
|
||||
switch (menu)
|
||||
{
|
||||
case 1:
|
||||
printf("1. Student details...");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("No menu found here...!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void display_menu()
|
||||
{
|
||||
printf("\n");
|
||||
read_file("./../note/section1.txt");
|
||||
}
|
||||
|
||||
void display_students()
|
||||
{
|
||||
}
|
||||
|
||||
void search_student()
|
||||
{
|
||||
}
|
||||
|
||||
void find_maximum()
|
||||
{
|
||||
}
|
||||
|
||||
void find_failed()
|
||||
{
|
||||
}
|
||||
|
||||
void update_file()
|
||||
{
|
||||
}
|
||||
|
||||
void exit_program()
|
||||
{
|
||||
println("Goodbye...");
|
||||
}
|
||||
@@ -2,26 +2,32 @@
|
||||
#include "stdio.h"
|
||||
#include "./student_tag.h"
|
||||
|
||||
void display() {
|
||||
void display()
|
||||
{
|
||||
printf("display...");
|
||||
}
|
||||
|
||||
void search(int mark) {
|
||||
void search(int mark)
|
||||
{
|
||||
printf("search mark...");
|
||||
}
|
||||
|
||||
void find_largest_average() {
|
||||
void find_largest_average()
|
||||
{
|
||||
printf("find largest average...");
|
||||
}
|
||||
|
||||
void find_failed_students() {
|
||||
void find_failed_students()
|
||||
{
|
||||
printf("find failed students...");
|
||||
}
|
||||
|
||||
void add_new(struct student_tag student) {
|
||||
void add_new(struct student_tag student)
|
||||
{
|
||||
printf("add new...");
|
||||
}
|
||||
|
||||
void quite() {
|
||||
void quite()
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
@@ -32,5 +32,4 @@ int main()
|
||||
|
||||
// show menu
|
||||
menu();
|
||||
|
||||
}
|
||||
@@ -15,8 +15,6 @@ void menu()
|
||||
printf("\n(6) Quit program\n\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void choose_menu(int menu)
|
||||
{
|
||||
|
||||
@@ -35,8 +33,8 @@ void choose_menu(int menu)
|
||||
find_failed_students();
|
||||
break;
|
||||
case MENU_5:
|
||||
|
||||
struct person_tag info = {"1","Sambo"};
|
||||
|
||||
struct person_tag info = {"1", "Sambo"};
|
||||
struct student_tag std = {.student_info = info};
|
||||
add_new(std);
|
||||
break;
|
||||
|
||||
597
work1/q1.c
Normal file
597
work1/q1.c
Normal file
@@ -0,0 +1,597 @@
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
#define MAX_NO_OF_UNITS 4
|
||||
#define NAME_SIZE 20
|
||||
|
||||
const char *FILE_STUDENT_DATA_PATH = "./data/students.test.txt";
|
||||
|
||||
struct person_tag
|
||||
{
|
||||
char name[20];
|
||||
char id[10];
|
||||
};
|
||||
|
||||
struct course_tag
|
||||
{
|
||||
char course_name[20];
|
||||
int no_of_units;
|
||||
int marks[MAX_NO_OF_UNITS];
|
||||
float avg;
|
||||
};
|
||||
|
||||
struct student_tag
|
||||
{
|
||||
struct person_tag student_info;
|
||||
struct course_tag course_info;
|
||||
struct student_tag *next;
|
||||
} * head;
|
||||
|
||||
// typedef for declared to use as data type
|
||||
typedef struct person_tag PERSON;
|
||||
typedef struct course_tag COURSE;
|
||||
typedef struct student_tag STUDENT;
|
||||
|
||||
// file function
|
||||
void read_file();
|
||||
|
||||
// util functions
|
||||
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();
|
||||
void search_student();
|
||||
void find_maximum();
|
||||
void find_failed();
|
||||
void update_file();
|
||||
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)
|
||||
{
|
||||
// variable use for menu selected
|
||||
int selected;
|
||||
|
||||
print_welcome();
|
||||
|
||||
// init the read data from the file
|
||||
read_file();
|
||||
|
||||
while (1)
|
||||
{
|
||||
selected = menu();
|
||||
|
||||
switch (selected)
|
||||
{
|
||||
case 1:
|
||||
display_students();
|
||||
break;
|
||||
case 2:
|
||||
search_student();
|
||||
break;
|
||||
case 3:
|
||||
find_maximum();
|
||||
break;
|
||||
case 4:
|
||||
find_failed();
|
||||
break;
|
||||
case 5:
|
||||
update_file();
|
||||
break;
|
||||
case 6:
|
||||
quite();
|
||||
break;
|
||||
default:
|
||||
printf("cannot find your option!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_welcome()
|
||||
{
|
||||
printf("Welcome!\n");
|
||||
}
|
||||
|
||||
void display_students()
|
||||
{
|
||||
read_file();
|
||||
|
||||
STUDENT *student = head;
|
||||
|
||||
if (student == NULL)
|
||||
{
|
||||
printf("\nNo student!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
print_student(student);
|
||||
}
|
||||
|
||||
void print_student(STUDENT *student)
|
||||
{
|
||||
int records_count = 0;
|
||||
printf("\n================ Student Details ================\n");
|
||||
while (student != NULL)
|
||||
{
|
||||
printf("\n");
|
||||
printf("Student Name: %s\n", student->student_info.name);
|
||||
printf("Student ID: %s\n", student->student_info.id);
|
||||
printf("Course Name: %s\n", student->course_info.course_name);
|
||||
printf("No of units: %d\n", student->course_info.no_of_units);
|
||||
|
||||
printf("Marks: [ ");
|
||||
for (int i = 0; i < student->course_info.no_of_units; i++)
|
||||
{
|
||||
printf("%d ", student->course_info.marks[i]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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])
|
||||
{
|
||||
STUDENT *temp, *iterator;
|
||||
temp = (struct student_tag *)malloc(sizeof(struct student_tag));
|
||||
PERSON info;
|
||||
memcpy(info.name, student_name, 20);
|
||||
memcpy(info.id, student_id, 10);
|
||||
|
||||
COURSE course;
|
||||
memcpy(course.course_name, course_name, 20);
|
||||
course.no_of_units = no_of_units;
|
||||
// memcpy(course.marks, marks);
|
||||
|
||||
float sum = 0;
|
||||
for (int i = 0; i < no_of_units; i++)
|
||||
{
|
||||
course.marks[i] = marks[i];
|
||||
sum += marks[i];
|
||||
}
|
||||
course.avg = sum / no_of_units;
|
||||
temp->student_info = info;
|
||||
temp->course_info = course;
|
||||
|
||||
// reference in head
|
||||
iterator = head;
|
||||
|
||||
if (head == NULL)
|
||||
{
|
||||
head = temp;
|
||||
head->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (iterator->next != NULL)
|
||||
{
|
||||
iterator = iterator->next;
|
||||
}
|
||||
temp->next = NULL;
|
||||
iterator->next = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int count_elements(STUDENT *elements)
|
||||
{
|
||||
int n = 1;
|
||||
STUDENT *temp;
|
||||
temp = elements;
|
||||
if (head == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
n++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int count()
|
||||
{
|
||||
return count_elements(head);
|
||||
}
|
||||
|
||||
void search_student()
|
||||
{
|
||||
char search_key[NAME_SIZE];
|
||||
|
||||
printf("Enter student name or id to search: ");
|
||||
scanf("%s", &search_key);
|
||||
|
||||
STUDENT *found_student = search_student_by_name_or_id(search_key);
|
||||
if (found_student == NULL)
|
||||
{
|
||||
printf("\nNo student found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
print_student(found_student);
|
||||
}
|
||||
|
||||
STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE])
|
||||
{
|
||||
// refresh data from file first
|
||||
read_file();
|
||||
|
||||
STUDENT *temp = head;
|
||||
|
||||
while (temp != NULL)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
STUDENT *find_maximum_avg()
|
||||
{
|
||||
// refresh data from file first
|
||||
read_file();
|
||||
|
||||
STUDENT *temp = head, *max;
|
||||
max = temp->next;
|
||||
|
||||
while (temp != NULL)
|
||||
{
|
||||
if (max == NULL)
|
||||
{
|
||||
return temp;
|
||||
}
|
||||
|
||||
if (max->course_info.avg < temp->course_info.avg)
|
||||
{
|
||||
max = temp;
|
||||
}
|
||||
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// release the max next record
|
||||
// set max next element to NULL
|
||||
// because we use only one record
|
||||
release(max->next);
|
||||
max->next = NULL;
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
int find_min_in_array(int *array)
|
||||
{
|
||||
if (array == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int min = array[0];
|
||||
size_t size = sizeof(array) / sizeof(array[0]);
|
||||
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
if (array[i] < min)
|
||||
{
|
||||
min = array[i];
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void find_maximum()
|
||||
{
|
||||
// refresh data from file first
|
||||
read_file();
|
||||
|
||||
STUDENT *max_student = find_maximum_avg();
|
||||
if (max_student == NULL)
|
||||
{
|
||||
printf("\nNo maximum student found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nFind maximum avg was found with: %0.2f\n", max_student->course_info.avg);
|
||||
print_student(max_student);
|
||||
}
|
||||
|
||||
void find_failed()
|
||||
{
|
||||
int upper_mark = 50;
|
||||
STUDENT *failed_students = find_failed_mark(upper_mark);
|
||||
if (failed_students == NULL)
|
||||
{
|
||||
printf("\nNo failed student found!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nFind the failed students that at least one mark less than %d\n", upper_mark);
|
||||
print_student(failed_students);
|
||||
}
|
||||
|
||||
void update_file()
|
||||
{
|
||||
FILE *file;
|
||||
file = fopen(FILE_STUDENT_DATA_PATH, "a");
|
||||
|
||||
char name[20];
|
||||
char id[10];
|
||||
char course_name[20];
|
||||
int no_of_units;
|
||||
int marks[MAX_NO_OF_UNITS];
|
||||
|
||||
printf("Enter student name: ");
|
||||
scanf("%s", &name);
|
||||
|
||||
printf("Enter student id: ");
|
||||
scanf("%s", &id);
|
||||
|
||||
printf("Enter course name: ");
|
||||
scanf("%s", &course_name);
|
||||
|
||||
again:
|
||||
printf("Enter no of units: ");
|
||||
scanf("%d", &no_of_units);
|
||||
|
||||
if (no_of_units > MAX_NO_OF_UNITS && no_of_units <= 0)
|
||||
{
|
||||
printf("\nyou cannot input the units bigger than %d or less than 0\n", MAX_NO_OF_UNITS);
|
||||
getchar();
|
||||
goto again;
|
||||
}
|
||||
|
||||
for (int i = 0; i < no_of_units; i++)
|
||||
{
|
||||
printf("Enter mark[%d]: ", i + 1);
|
||||
scanf("%d", &marks[i]);
|
||||
}
|
||||
|
||||
if (count() > 0)
|
||||
{
|
||||
fputs("\n", file);
|
||||
}
|
||||
|
||||
fputs(name, file);
|
||||
fputs("\n", file);
|
||||
fputs(id, file);
|
||||
fputs("\n", file);
|
||||
fputs(course_name, file);
|
||||
fputs("\n", file);
|
||||
fprintf(file, "%d", no_of_units);
|
||||
|
||||
for (int i = 0; i < no_of_units; i++)
|
||||
{
|
||||
fputs("\n", file);
|
||||
fprintf(file, "%d", marks[i]);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
printf("\nRecord saved successfully!\n");
|
||||
|
||||
// reload data into linked list again
|
||||
// read_file();
|
||||
|
||||
display_students();
|
||||
}
|
||||
|
||||
void read_file()
|
||||
{
|
||||
// release nodes
|
||||
// we need to clean up the memory
|
||||
if (head != NULL)
|
||||
{
|
||||
STUDENT *temp;
|
||||
while (head != NULL)
|
||||
{
|
||||
temp = head;
|
||||
head = head->next;
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
|
||||
FILE *file;
|
||||
file = fopen(FILE_STUDENT_DATA_PATH, "r");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("cannot read file: %s", FILE_STUDENT_DATA_PATH);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char student_name[20];
|
||||
char student_id[10];
|
||||
char course_name[20];
|
||||
int no_of_units;
|
||||
int marks[4];
|
||||
|
||||
int i;
|
||||
while (!feof(file))
|
||||
{
|
||||
char no[BUFFER_SIZE];
|
||||
|
||||
fgets(student_name, sizeof student_name, file);
|
||||
fgets(student_id, sizeof student_id, file);
|
||||
fgets(course_name, sizeof course_name, file);
|
||||
fgets(no, sizeof no, file);
|
||||
|
||||
i = 0;
|
||||
while (student_name[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
student_name[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
while (student_id[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
student_id[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
while (course_name[i] != '\n')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
course_name[i] = '\0';
|
||||
|
||||
no_of_units = atoi(no);
|
||||
for (int j = 0; j < no_of_units; j++)
|
||||
{
|
||||
char mark[BUFFER_SIZE];
|
||||
|
||||
fgets(mark, sizeof mark, file);
|
||||
sscanf(mark, "%d", &marks[j]);
|
||||
}
|
||||
|
||||
// add into linked list
|
||||
add_student(student_name, student_id, course_name, no_of_units, marks);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void quite()
|
||||
{
|
||||
printf("\nGoodbye!");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void release(STUDENT *data)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// free the nodes
|
||||
// because it can be use in memory
|
||||
// we need to clear it first
|
||||
// before we re-initailize the new data
|
||||
STUDENT *temp;
|
||||
while (data != NULL)
|
||||
{
|
||||
temp = data;
|
||||
data = data->next;
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
83
work1/q2.c
Normal file
83
work1/q2.c
Normal 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
286
work1/q3.c
Normal 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(¤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;
|
||||
}
|
||||
Reference in New Issue
Block a user