Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Ozioma Okonicha BS18-02*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <wchar.h>
- #include <stdbool.h>
- #include <ctype.h>
- #define MAX 100
- /*a struct which will serve as an array of strings to store the courses a TA or professor is trained in*/
- typedef struct train {
- char name[MAX];
- } course_trained;
- /*a struct to hold details for each course*/
- typedef struct class {
- char name[MAX];
- int num_labs;
- int num_students;
- bool not_runnable;
- } Class;
- /*a struct to hold details for each student*/
- typedef struct student {
- char first_name[MAX];
- char last_name[MAX];
- char student_ID[MAX];
- int num_courses;
- course_trained requested[MAX];
- } Student;
- /*a struct to hold details for each professor*/
- typedef struct professor {
- char first_name[MAX];
- char last_name[MAX];
- int num_courses;
- course_trained trained[MAX];
- bool assigned;
- int num_assigned;
- } Professor;
- /*a struct to hold details for each TA*/
- typedef struct ta {
- char first_name[MAX];
- char last_name[MAX];
- int num_courses;
- course_trained trained[MAX];
- bool assigned;
- int num_assigned;
- } TA;
- /*arrays to hold each of the entry types as I read my input*/
- Class Courses[MAX];
- Student Students[MAX];
- Professor Professors[MAX];
- TA TAs[MAX];
- /*important global variables to keep track of how many of each entry type I read*/
- int course_count;
- int stud_count;
- int prof_count;
- int ta_count;
- /*I create matrices of students against course, professors against courses and ta's against courses*/
- int stud_table[MAX][MAX];
- int prof_table[MAX][MAX];
- int ta_table[MAX][MAX];
- /*this is a global array which will serve as a buffer when I'm reading inputs and verifying*/
- char array[MAX];
- /*variable to calculate badness points*/
- int badness_point;
- /*function to verify that the numbers read from input are actually numbers*/
- int isNum(char ch[]) {
- if (strlen(ch) == 0) /*if the character array received is empty*/
- {
- return 0;
- }
- if (strlen(ch) == 1) /*if the character array received contains one char, check if it is a digit*/
- {
- if ((isdigit(ch[0]))) {
- return 1;
- } else { return 0; }
- }
- for (int i = 0; i <
- ch[i]; ++i) { /*iterate the character array, each time checking that each char is a digit*/
- if (!isdigit(ch[i])) {
- return 0;
- }
- }
- return 1;
- }
- /*function to read n input files and create n+1 output files*/
- void GetFiles() {
- int num_files = 50;
- char input_files[100 + 1];
- char output_files[100 + 1];
- FILE *i_files;
- FILE *o_files;
- for (int N = 1; N <= num_files; ++N) {
- sprintf(input_files, "input%d.txt", N);
- i_files = freopen(input_files, "r", stdin);
- if (!i_files) {
- if (N == 1)//no input files
- {
- sprintf(output_files, "OziomaOkonichaEmail.txt");
- freopen("OziomaOkonichaEmail.txt", "w", stdout);
- printf("o.okonicha@innopolis.university");
- } else {
- exit(0);
- }
- }
- sprintf(output_files, "OziomaOkonichaOutput%d.txt", N);
- o_files = freopen(output_files, "w", stdout);
- }
- }
- /*function to read the courses and fill in the appropriate details*/
- void GetCourses() {
- for (int j = 0; j < MAX; ++j) {
- scanf("%[^ \n]", array);
- if ((strcmp(array, "P") == 0)) {
- scanf("%[^\n]", array);
- //printf("%s", array);
- scanf("%[\n]", array);
- //printf("%s", array);
- break;
- }
- strcpy(Courses[j].name, array);
- //printf("%s", Courses[i].name);
- scanf("%[ ]", array);
- //printf("%s", array);
- scanf("%[^ ]", array);
- if (!isNum(array)) {
- printf("Invalid input.");
- fclose(stdout);
- } else if (array[0] == '0' || array[0] == ' ') {
- printf("Invalid input");
- fclose(stdout);
- }
- Courses[j].num_labs = atoi(array);
- //printf("%d", Courses[i].num_labs);
- scanf("%[ ]", array);
- //printf("%s", array);
- scanf("%[^\n]", array);
- if (!isNum(array)) {
- printf("Invalid input.");
- fclose(stdout);
- } else if (array[0] == '0' || array[0] == ' ') {
- printf("Invalid input");
- fclose(stdout);
- }
- Courses[j].num_students = atoi(array);
- //printf("%d", Courses[i].num_students);
- scanf("%[\n]", array);
- //printf("%s", array);
- course_count++;
- }
- }
- /*function to read the professors and fill in their details*/
- void GetProfessors() {
- for (int j = 0; j < MAX; ++j) {
- scanf("%[^ \n]", array);
- if ((strcmp(array, "T") == 0)) {
- scanf("%[^\n]", array);
- //printf("%s", array);
- scanf("%[\n]", array);
- //printf("%s", array);
- break;
- }
- strcpy(Professors[j].first_name, array);
- //printf("%s", Professors[j].first_name);
- scanf("%[ ]", array);
- //printf("%s", array);
- scanf("%[^ ]", array);
- strcpy(Professors[j].last_name, array);
- //printf("%s", Professors[j].last_name);
- scanf("%[ ]", array);
- //printf("%s", array);
- scanf("%[^\n]", array);
- int k = 0;
- char space[] = " ";
- char *div = strtok(array, space);
- while (div != NULL) {
- int found = -1;
- for (int i = 0; i < course_count; ++i) {
- if ((strcmp(div, Courses[i].name) == 0)){
- found = 0;
- }
- if (found == -1){
- printf("Invalid input.");
- fclose(stdout);
- }
- }
- strcpy(Professors[j].trained[k].name, div);
- //printf("%s", Professors[j].trained[k].name);
- //printf("%s", space);
- Professors[j].num_courses++;
- k++;
- div = strtok(NULL, space);
- }
- scanf("%[\n]", array);
- //printf("%s", array);
- prof_count++;
- }
- }
- /*function to read ta details and fill them in the right array*/
- void GetTAs() {
- for (int j = 0; j < MAX; ++j) {
- scanf("%[^ \n]", array);
- if ((strcmp(array, "S") == 0)) {
- scanf("%[^\n]", array);
- //printf("%s", array);
- scanf("%[\n]", array);
- //printf("%s", array);
- break;
- }
- strcpy(TAs[j].first_name, array);
- //printf("%s", TAs[j].first_name);
- scanf("%[ ]", array);
- //printf("%s", array);
- scanf("%[^ ]", array);
- strcpy(TAs[j].last_name, array);
- //printf("%s", TAs[j].last_name);
- scanf("%[ ]", array);
- //printf("%s", array);
- scanf("%[^\n]", array);
- int k = 0;
- char space[] = " ";
- char *div = strtok(array, space);
- while (div != NULL) {
- strcpy(TAs[j].trained[k].name, div);
- //printf("%s", TAs[j].trained[k].name);
- //printf("%s", space);
- TAs[j].num_courses++;
- k++;
- div = strtok(NULL, space);
- }
- scanf("%[\n]", array);
- //printf("%s", array);
- ta_count++;
- }
- }
- /*function to read the students and fill in*/
- void GetStudents() {
- for (int j = 0; j < MAX; ++j) {
- if (scanf("%s ", array) == EOF) {
- break;
- }
- strcpy(Students[j].first_name, array);
- //printf("%s ", Students[j].first_name);
- // scanf("%[ ]", array);
- // printf("%s", array);
- scanf("%s ", array);
- strcpy(Students[j].last_name, array);
- //printf("%s ", Students[j].last_name);
- // scanf("%[ ]", array);
- // printf("%s", array);
- scanf("%s ", array);
- if (strlen(array) == 5) {
- strcpy(Students[j].student_ID, array);
- //printf("%s ", Students[j].student_ID);
- } else{
- printf("Invalid input.");
- fclose(stdout);
- }
- // scanf("%[ ]", array);
- // printf("%s", array);
- scanf("%[^\n]", array);
- int k = 0;
- char space[] = " ";
- char *div = strtok(array, space);
- while (div != NULL) {
- strcpy(Students[j].requested[k].name, div);
- //printf("%s ", Students[j].requested[k].name);
- // printf("%s", space);
- Students[j].num_courses++;
- k++;
- div = strtok(NULL, space);
- }
- scanf("\n");
- //printf("\n");
- // printf("%s", array);
- stud_count++;
- }
- }
- /*this function fills in the array of professors against courses*/
- void assignProfessors(Class classes[], Professor profs[]) {
- for (int i = 0; i < prof_count; ++i) {
- for (int j = 0; j < profs[i].num_courses; ++j) {
- for (int k = 0; k < course_count; ++k) {
- if (strcmp(profs[i].trained[j].name, classes[k].name) == 0) {
- prof_table[i][k] = 1;
- // printf("%s\n", classes[k].name);
- // printf("%s %s\n", profs[i].first_name, profs[i].last_name);
- }
- }
- }
- }
- //printf("Professor/Course matrix is: \n");
- for (int l = 0; l < prof_count + 1; ++l) {
- for (int m = 0; m < course_count + 1; ++m) {
- //printf("%d ", prof_table[l][m]);
- }
- //printf("\n");
- }
- }
- /*function that fills in the Tas against courses array*/
- void assignTAs(Class classes[], TA tas[]) {
- for (int i = 0; i < ta_count; ++i) {
- for (int j = 0; j < tas[i].num_courses; ++j) {
- for (int k = 0; k < course_count; ++k) {
- if (strcmp(tas[i].trained[j].name, classes[k].name) == 0) {
- ta_table[i][k] = 1;
- //printf("%s\n", classes[k].name);
- //printf("%s %s\n", tas[i].first_name, tas[i].last_name);
- }
- }
- }
- }
- //printf("TA/Course matrix is: \n");
- for (int l = 0; l < ta_count + 1; ++l) {
- for (int m = 0; m < course_count + 1; ++m) {
- //printf("%d ", ta_table[l][m]);
- }
- //printf("\n");
- }
- }
- /*function to fill in student against courses array*/
- void assignStudents(Class classes[], Student studs[]) {
- for (int i = 0; i < stud_count; ++i) {
- for (int j = 0; j < studs[i].num_courses; ++j) {
- for (int k = 0; k < course_count; ++k) {
- if (strcmp(studs[i].requested[j].name, classes[k].name) == 0) {
- stud_table[i][k] = 1;
- //printf("%s\n", classes[k].name);
- //printf("%s %s\n", studs[i].first_name, studs[i].last_name);
- }
- }
- }
- }
- //printf("Student/Course matrix is: \n");
- for (int l = 0; l < stud_count + 1; ++l) {
- for (int m = 0; m < course_count + 1; ++m) {
- //printf("%d ", stud_table[l][m]);
- }
- //printf("\n");
- }
- }
- /*to find the index of the course with the minimum number of TA's assigned*/
- int min_ta() {
- int min = ta_table[ta_count][0];
- for (int m = 0; m < course_count; ++m) {
- if (ta_table[ta_count][m] < min) {
- return m;
- }
- }
- }
- /*this returns the index of the course which has minimum number of professors assigned to it*/
- int min_prof() {
- int min = prof_table[prof_count][0];
- for (int m = 0; m < course_count; ++m) {
- if (prof_table[prof_count][m] < min) {
- return m;
- }
- }
- }
- /*This function contains my main algorithm to manipulate the global arrays above*/
- void schedule() {
- /*SUM THE ROWS AND COLUMNS OF THE TA MATRIX*/
- for (int i = 0; i < ta_count; ++i) {
- int r_sum = 0;
- for (int j = 0; j < course_count; ++j) {
- r_sum += ta_table[i][j];
- }
- ta_table[i][course_count] = r_sum;
- }
- for (int k = 0; k < course_count; ++k) {
- int c_sum = 0;
- for (int i = 0; i < ta_count; ++i) {
- c_sum += ta_table[i][k];
- }
- ta_table[ta_count][k] = c_sum;
- }
- // printf("TA/Course matrix is: \n");
- // for (int l = 0; l < ta_count + 1; ++l) {
- // for (int m = 0; m < course_count + 1; ++m) {
- // printf("%d ", ta_table[l][m]);
- // }
- // printf("\n");
- // }
- for (int i = 0; i < course_count; ++i) {
- if (ta_table[ta_count][i] == 0) {
- Courses[i].not_runnable = true;
- }
- }
- for (int m = 0; m < ta_count; ++m) {
- for (int i = 0; i < course_count; ++i) {
- if (Courses[i].not_runnable == true) {
- continue;
- }
- if (ta_table[ta_count][i] == 1 && Courses[i].num_labs <= 4 && ta_table[m][i] == 1) {
- ta_table[m][i] = Courses[i].num_labs;
- } else if (ta_table[ta_count][i] == 1 && Courses[i].num_labs > 4 && ta_table[m][i] == 1) {
- ta_table[m][i] = 4;
- }
- }
- }
- // printf("TA/Course matrix is: \n");
- // for (int l = 0; l < ta_count + 1; ++l) {
- // for (int m = 0; m < course_count + 1; ++m) {
- // printf("%d ", ta_table[l][m]);
- // }
- // printf("\n");
- // }
- /*SUM THE ROWS AND COLUMNS OF THE PROF MATRIX*/
- for (int i = 0; i < prof_count; ++i) {
- int r_sum = 0;
- for (int j = 0; j < course_count; ++j) {
- r_sum += prof_table[i][j];
- }
- prof_table[i][course_count] = r_sum;
- }
- for (int k = 0; k < course_count; ++k) {
- int c_sum = 0;
- for (int i = 0; i < prof_count; ++i) {
- c_sum += prof_table[i][k];
- }
- prof_table[prof_count][k] = c_sum;
- }
- // printf("Prof/Course matrix is: \n");
- // for (int l = 0; l < prof_count + 1; ++l) {
- // for (int m = 0; m < course_count + 1; ++m) {
- // printf("%d ", prof_table[l][m]);
- // }
- // printf("\n");
- // }
- for (int i = 0; i < prof_count; ++i) {
- for (int j = 0; j < course_count; ++j) {
- if (Courses[j].not_runnable == true || Professors[j].num_assigned == 2) {
- continue;
- }
- if (prof_table[prof_count][j] > 0 && Professors[j].num_assigned < 2) {
- Professors[j].num_assigned++;
- } else if (prof_table[prof_count][j] == 0 && Professors[j].num_assigned == 0) {
- prof_table[i][j] = 1;
- Professors[j].num_assigned++;
- } else {
- return;
- }
- }
- }
- // printf("Prof/Course matrix is: \n");
- // for (int l = 0; l < prof_count + 1; ++l) {
- // for (int m = 0; m < course_count + 1; ++m) {
- // printf("%d ", prof_table[l][m]);
- // }
- // printf("\n");
- // }
- /*SUM THE ROWS AND COLUMNS OF THE STUD MATRIX*/
- for (int i = 0; i < stud_count; ++i) {
- int r_sum = 0;
- for (int j = 0; j < course_count; ++j) {
- r_sum += stud_table[i][j];
- }
- stud_table[i][course_count] = r_sum;
- }
- for (int k = 0; k < course_count; ++k) {
- int c_sum = 0;
- for (int i = 0; i < stud_count; ++i) {
- c_sum += stud_table[i][k];
- }
- stud_table[stud_count][k] = c_sum;
- }
- // printf("Stud/Course matrix is: \n");
- // for (int l = 0; l < stud_count + 1; ++l) {
- // for (int m = 0; m < course_count + 1; ++m) {
- // printf("%d ", stud_table[l][m]);
- // }
- // printf("\n");
- // }
- for (int l = 0; l < course_count; ++l) {
- if (Courses[l].not_runnable == true) {
- badness_point += 20;
- }
- }
- }
- void printName(char *name, char *name1, int count) {
- printf("%s %s\n", name, name1);
- count += 1;
- int i = 0;
- if (count < Courses[i].num_labs) {
- printName(name, name1, count);
- }
- i++;
- }
- /*function to print out the schedule in the requested format*/
- void print_schedule() {
- for (int i = 0; i < course_count; ++i) {
- printf("%s\n", Courses[i].name);
- for (int l = 0; l < prof_count; ++l) {
- for (int j = 0; j < Professors[l].num_courses; ++j) {
- if (strcmp(Professors[l].trained[j].name, Courses[i].name) == 0) {
- printf("%s %s\n", Professors[l].first_name, Professors[l].last_name);
- }
- }
- }
- for (int l = 0; l < ta_count; ++l) {
- for (int j = 0; j < TAs[l].num_courses; ++j) {
- if (strcmp(TAs[l].trained[j].name, Courses[i].name) == 0) {
- printName(TAs[l].first_name, TAs[l].last_name, 0);
- }
- }
- }
- for (int l = 0; l < stud_count; ++l) {
- for (int j = 0; j < Students[l].num_courses; ++j) {
- if (strcmp(Students[l].requested[j].name, Courses[i].name) == 0) {
- printf("%s %s %s\n", Students[l].first_name, Students[l].last_name, Students[l].student_ID);
- }
- }
- }
- printf("\n");
- }
- printf("Total score is %d.", badness_point);
- }
- /*my main function from where I call all other functions*/
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("OziomaOkonichaTesting.txt", "w", stdout);
- // GetFiles();
- GetCourses();
- GetProfessors();
- GetTAs();
- GetStudents();
- assignProfessors(Courses, Professors);
- assignTAs(Courses, TAs);
- assignStudents(Courses, Students);
- schedule();
- print_schedule();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement