From ad78da7af7659eb757ef70cd4d25c9221cad3432 Mon Sep 17 00:00:00 2001 From: Natalie Agus Date: Thu, 20 Feb 2020 23:45:32 +0800 Subject: [PATCH] first commit --- .DS_Store | Bin 0 -> 6148 bytes BankersAlgorithmLab/.DS_Store | Bin 0 -> 6148 bytes BankersAlgorithmLab/StarterCode_C/.DS_Store | Bin 0 -> 6148 bytes .../StarterCode_C/C_code/BankerQ1.c | 285 ++++++++++++++++++ .../StarterCode_C/C_code/BankerQ2.c | 284 +++++++++++++++++ .../StarterCode_C/C_code/checkerQ1.c | 70 +++++ .../StarterCode_C/C_code/checkerQ2.c | 70 +++++ BankersAlgorithmLab/StarterCode_C/makefile | 16 + .../StarterCode_C/testcases/answerq1_1.txt | 37 +++ .../StarterCode_C/testcases/answerq2_1.txt | 64 ++++ .../StarterCode_C/testcases/q1_1.txt | 15 + .../StarterCode_C/testcases/q2_1.txt | 17 ++ .../StarterCode_Java/.DS_Store | Bin 0 -> 6148 bytes .../StarterCode_Java/Banker.class | Bin 0 -> 3737 bytes .../StarterCode_Java/Banker.java | 227 ++++++++++++++ .../StarterCode_Java/TestBankQ1.class | Bin 0 -> 295 bytes .../StarterCode_Java/TestBankQ1.java | 12 + .../StarterCode_Java/TestBankQ2.class | Bin 0 -> 295 bytes .../StarterCode_Java/TestBankQ2.java | 8 + .../StarterCode_Java/answer.txt | 6 + .../StarterCode_Java/answerq1_1.txt | 38 +++ .../StarterCode_Java/answerq2_1.txt | 66 ++++ .../StarterCode_Java/checkerQ1.c | 70 +++++ .../StarterCode_Java/checkerQ2.c | 70 +++++ BankersAlgorithmLab/StarterCode_Java/checkq1 | Bin 0 -> 13016 bytes BankersAlgorithmLab/StarterCode_Java/checkq2 | Bin 0 -> 13016 bytes BankersAlgorithmLab/StarterCode_Java/makefile | 17 ++ BankersAlgorithmLab/StarterCode_Java/q1_1.txt | 15 + BankersAlgorithmLab/StarterCode_Java/q2_1.txt | 17 ++ 29 files changed, 1404 insertions(+) create mode 100644 .DS_Store create mode 100644 BankersAlgorithmLab/.DS_Store create mode 100644 BankersAlgorithmLab/StarterCode_C/.DS_Store create mode 100644 BankersAlgorithmLab/StarterCode_C/C_code/BankerQ1.c create mode 100644 BankersAlgorithmLab/StarterCode_C/C_code/BankerQ2.c create mode 100644 BankersAlgorithmLab/StarterCode_C/C_code/checkerQ1.c create mode 100644 BankersAlgorithmLab/StarterCode_C/C_code/checkerQ2.c create mode 100644 BankersAlgorithmLab/StarterCode_C/makefile create mode 100644 BankersAlgorithmLab/StarterCode_C/testcases/answerq1_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_C/testcases/answerq2_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_C/testcases/q1_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_C/testcases/q2_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_Java/.DS_Store create mode 100644 BankersAlgorithmLab/StarterCode_Java/Banker.class create mode 100644 BankersAlgorithmLab/StarterCode_Java/Banker.java create mode 100644 BankersAlgorithmLab/StarterCode_Java/TestBankQ1.class create mode 100644 BankersAlgorithmLab/StarterCode_Java/TestBankQ1.java create mode 100644 BankersAlgorithmLab/StarterCode_Java/TestBankQ2.class create mode 100644 BankersAlgorithmLab/StarterCode_Java/TestBankQ2.java create mode 100644 BankersAlgorithmLab/StarterCode_Java/answer.txt create mode 100644 BankersAlgorithmLab/StarterCode_Java/answerq1_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_Java/answerq2_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_Java/checkerQ1.c create mode 100644 BankersAlgorithmLab/StarterCode_Java/checkerQ2.c create mode 100755 BankersAlgorithmLab/StarterCode_Java/checkq1 create mode 100755 BankersAlgorithmLab/StarterCode_Java/checkq2 create mode 100644 BankersAlgorithmLab/StarterCode_Java/makefile create mode 100644 BankersAlgorithmLab/StarterCode_Java/q1_1.txt create mode 100644 BankersAlgorithmLab/StarterCode_Java/q2_1.txt diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b2dbc2d2aaf29c10c7b0c510a809cab76f7d1c99 GIT binary patch literal 6148 zcmeHK!Ab)$5S`SjX+LjB#ZmjBqTwb-V%h4Mb~0(5L-}$NkufN!agyCNk_k9<6Mim zL6Z(bkBt4;k%fJs2t7Lb9UTtBHOM71zzmEsFkz|{y8m}SfB%mrag7;Z2L2TTqFDFq z9!|;Ltz(m;yVgOyKqaBL+~6<;4Sf}3EM3J*s9MnPkb&r0%nhOkg?|Jz4O}n-Kgz%x D%I;dG literal 0 HcmV?d00001 diff --git a/BankersAlgorithmLab/.DS_Store b/BankersAlgorithmLab/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4d409bb6922843fedef33226b3d8e9eecc44be5a GIT binary patch literal 6148 zcmeHK!Ab)$5S_GD(~7tUL63X!=AlwiJP1px2R#WZdQhpm?xG9pM!H*z*2?~d{*hnc z?>Li0sZ>3x$P7&0B$-JzFH4dE0MYJ8s{jrFNL0c~9g9zd`bj6GVq0p6LXS~E3_a+8 zf@s2<8~!2#w0CphLjeKq-1z>*(1lJo$gjgxh3NGhUZ+W3G@CD?Qmsu*&q&#jjk(6D zf2jt35fuHr9rTW=wjUIip|uZ`O5g1uJWS$2*IDSOtO$}U?&;tpiqYldD9Iu>Sm+c0k)DxS(5761VqA-!#nK@5pa>I+XhMZe zF@y=ncGvoO7E6OB9E43igq>N~3`OYKaeP<1gYXP;$qX<99~r2cszdkx&e!?>#~`jT z1I)m`VnEck{p~g`$=k1% zQQGl_=QKO^vKwF9Cql%Zw&$Nl;kegW>xv}vq9h#3xwU_Eb$kDiKTe*W6{`#{At|dGKi~z8 zQjvG=Fo{KSjeg3pAcGK69Q)uQqW$F>n=2Y-lp3V{*vH?D*qX8YjVp>WG8YW!cBy^0EjhoOF^4@3CgiGIvNXwxPrn|Dxykdy2W5B z9sRbBb2JtTRXQ-;d@%iGraKfSzmEIcG8~wr(2Rxw!$6sVs+u% zFjow)+JSq}!IJdbI#nFKwKnP%Dhb6E3S|lo`Z<;cJ;fWSQgF>C1JTh~C`1p6`6D1{ LFoR*>PZ@XzU_zgZ literal 0 HcmV?d00001 diff --git a/BankersAlgorithmLab/StarterCode_C/C_code/BankerQ1.c b/BankersAlgorithmLab/StarterCode_C/C_code/BankerQ1.c new file mode 100644 index 0000000..70fb7ab --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/C_code/BankerQ1.c @@ -0,0 +1,285 @@ +/** + * CSE lab project 3 -- C version + * +**/ + +#include +#include +#include + +int numberOfCustomers = 0; // the number of customers +int numberOfResources = 0; // the number of resources + +int *available; // the available amount of each resource +int **maximum; // the maximum demand of each customer +int **allocation; // the amount currently allocated +int **need; // the remaining needs of each customer + +// Utility function to allocate an int vector. +int *mallocIntVector(int size) { + int i, *a = malloc(sizeof(int) * size); + for (i = 0; i < size; i++) a[i] = 0; + return a; +} + +// Utility function to free an int vector. +void freeIntVector(int *a) { + free(a); +} + +// Utility function to allocate an int matrix. +int **mallocIntMatrix(int numRows, int numColumns) { + int i, j, **a = malloc(sizeof(int*) * (numRows+1)); + for (i = 0; i < numRows; i++) { + a[i] = malloc(sizeof(int) * numColumns); + for (j = 0; j < numColumns; j++) a[i][j] = 0; + } + a[numRows] = 0; + return a; +} + +// Utility function to free an int matrix. +void freeIntMatrix(int **a) { + int i; + for (i = 0; a[i] != 0; i++) free(a[i]); + free(a); +} + +/** + * Initializes the state of the bank. + * @param resources An array of the available count for each resource. + * @param m The number of resources. + * @param n The number of customers. + */ +void initBank(int *resources, int m, int n) { + // Allocate memory for the vector and matrices + available = mallocIntVector(m); + need = mallocIntMatrix(n, m); + allocation = mallocIntMatrix(n, m); + maximum = mallocIntMatrix(n, m); + + // TODO: initialize the numberOfCustomers and numberOfResources + + // TODO: initialize the available vector + +} + +/** + * Frees the memory used to store the state of the bank. + */ +void freeBank() { + freeIntVector(available); + freeIntMatrix(need); + freeIntMatrix(allocation); + free(maximum); +} + +/** + * Prints the state of the bank. + */ +void printState() { + int i; + int j; + // TODO: print the current state with a tidy format + printf("\nCurrent state:\n"); + + // TODO: print available + printf("Available:\n"); + for (i=0; i maxLineLen) maxLineLen = lineLen; + } + i++; + } while ((c = fgetc(fp)) != EOF); + + rewind(fp); + lineLen++; + char *line = malloc(lineLen), *token; + i = 0; + while (fgets(line, lineLen, fp) != NULL) { + for (j = 0; j < lineLen-1; j++) + if (line[j] == '\n') line[j] = '\0'; + if (i == 0) { + token = strtok(line, ","); + token = strtok(NULL, ","); + n = atoi(token); + } else if (i == 1) { + token = strtok(line, ","); + token = strtok(NULL, ","); + m = atoi(token); + } else if (i == 2) { + token = strtok(line, ","); + token = strtok(NULL, ","); + int *resources = malloc(sizeof(int) * m); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + initBank(resources, m, n); + bankInited = 1; + free(resources); + } else { + int *resources = malloc(sizeof(int) * m); + token = strtok(line, ","); + if (strcmp(token, "c") == 0) { + int customerIndex = atoi(strtok(NULL, ",")); + int *resources = malloc(sizeof(int) * m); + token = strtok(NULL, ","); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + setMaximumDemand(customerIndex, resources); + free(resources); + } else if (strcmp(token, "r") == 0) { + int customerIndex = atoi(strtok(NULL, ",")); + int *resources = malloc(sizeof(int) * m); + token = strtok(NULL, ","); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + requestResources(customerIndex, resources); + free(resources); + } else if (strcmp(token, "f") == 0) { + int customerIndex = atoi(strtok(NULL, ",")); + int *resources = malloc(sizeof(int) * m); + token = strtok(NULL, ","); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + releaseResources(customerIndex, resources); + free(resources); + } else if (strcmp(token, "p") == 0) { + printState(); + } + } + i++; + } + if (bankInited) freeBank(); + free(line); + fclose(fp); +} + +/** + * Main function + * @param args The command line arguments + */ +int main (int argc, const char ** argv) +{ + if (argc > 1) { + runFile(argv[1]); + } + return 0; +} + + + diff --git a/BankersAlgorithmLab/StarterCode_C/C_code/BankerQ2.c b/BankersAlgorithmLab/StarterCode_C/C_code/BankerQ2.c new file mode 100644 index 0000000..9add3e3 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/C_code/BankerQ2.c @@ -0,0 +1,284 @@ +/** + * CSE lab project 3 -- C version + * +**/ + +#include +#include +#include + +int numberOfCustomers = 0; // the number of customers +int numberOfResources = 0; // the number of resources + +int *available; // the available amount of each resource +int **maximum; // the maximum demand of each customer +int **allocation; // the amount currently allocated +int **need; // the remaining needs of each customer + +// Utility function to allocate an int vector. +int *mallocIntVector(int size) { + int i, *a = malloc(sizeof(int) * size); + for (i = 0; i < size; i++) a[i] = 0; + return a; +} + +// Utility function to free an int vector. +void freeIntVector(int *a) { + free(a); +} + +// Utility function to allocate an int matrix. +int **mallocIntMatrix(int numRows, int numColumns) { + int i, j, **a = malloc(sizeof(int*) * (numRows+1)); + for (i = 0; i < numRows; i++) { + a[i] = malloc(sizeof(int) * numColumns); + for (j = 0; j < numColumns; j++) a[i][j] = 0; + } + a[numRows] = 0; + return a; +} + +// Utility function to free an int matrix. +void freeIntMatrix(int **a) { + int i; + for (i = 0; a[i] != 0; i++) free(a[i]); + free(a); +} + +/** + * Initializes the state of the bank. + * @param resources An array of the available count for each resource. + * @param m The number of resources. + * @param n The number of customers. + */ +void initBank(int *resources, int m, int n) { + // Allocate memory for the vector and matrices + available = mallocIntVector(m); + need = mallocIntMatrix(n, m); + allocation = mallocIntMatrix(n, m); + maximum = mallocIntMatrix(n, m); + + // TODO: initialize the numberOfCustomers and numberOfResources + + // TODO: initialize the available vector + +} + +/** + * Frees the memory used to store the state of the bank. + */ +void freeBank() { + freeIntVector(available); + freeIntMatrix(need); + freeIntMatrix(allocation); + free(maximum); +} + +/** + * Prints the state of the bank. + */ +void printState() { + int i; + int j; + // TODO: print the current state with a tidy format + printf("\nCurrent state:\n"); + + // TODO: print available + printf("Available:\n"); + for (i=0; i maxLineLen) maxLineLen = lineLen; + } + i++; + } while ((c = fgetc(fp)) != EOF); + + rewind(fp); + lineLen++; + char *line = malloc(lineLen), *token; + i = 0; + while (fgets(line, lineLen, fp) != NULL) { + for (j = 0; j < lineLen-1; j++) + if (line[j] == '\n') line[j] = '\0'; + if (i == 0) { + token = strtok(line, ","); + token = strtok(NULL, ","); + n = atoi(token); + } else if (i == 1) { + token = strtok(line, ","); + token = strtok(NULL, ","); + m = atoi(token); + } else if (i == 2) { + token = strtok(line, ","); + token = strtok(NULL, ","); + int *resources = malloc(sizeof(int) * m); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + initBank(resources, m, n); + bankInited = 1; + free(resources); + } else { + int *resources = malloc(sizeof(int) * m); + token = strtok(line, ","); + if (strcmp(token, "c") == 0) { + int customerIndex = atoi(strtok(NULL, ",")); + int *resources = malloc(sizeof(int) * m); + token = strtok(NULL, ","); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + setMaximumDemand(customerIndex, resources); + free(resources); + } else if (strcmp(token, "r") == 0) { + int customerIndex = atoi(strtok(NULL, ",")); + int *resources = malloc(sizeof(int) * m); + token = strtok(NULL, ","); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + requestResources(customerIndex, resources); + free(resources); + } else if (strcmp(token, "f") == 0) { + int customerIndex = atoi(strtok(NULL, ",")); + int *resources = malloc(sizeof(int) * m); + token = strtok(NULL, ","); + for (j = 0; j < m; j++) { + resources[j] = atoi(strtok(j == 0 ? token : NULL, " ")); + } + releaseResources(customerIndex, resources); + free(resources); + } else if (strcmp(token, "p") == 0) { + printState(); + } + } + i++; + } + if (bankInited) freeBank(); + free(line); + fclose(fp); +} + +/** + * Main function + * @param args The command line arguments + */ +int main (int argc, const char ** argv) +{ + if (argc > 1) { + runFile(argv[1]); + } + return 0; +} + + + diff --git a/BankersAlgorithmLab/StarterCode_C/C_code/checkerQ1.c b/BankersAlgorithmLab/StarterCode_C/C_code/checkerQ1.c new file mode 100644 index 0000000..4710220 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/C_code/checkerQ1.c @@ -0,0 +1,70 @@ +#include +#include +#include +#define NUMQUESTIONS 1 + +int main() +{ + + int i = 1; + char full_command[64] = {}; + char full_filename_answer[64] = {}; + + char command_1[] = "./q1 ./testcases/q1_"; + char command_2[] = ".txt > answer.txt"; + char filename_answer_p1[] = "./testcases/answerq1_"; + char filename_answer_p2[] = ".txt"; + + int correct_answers = 0; + + while (i <= NUMQUESTIONS) + { + sprintf(full_command, "%s", command_1); + sprintf(full_command + sizeof(command_1) - 1, "%d", i); + sprintf(full_command + sizeof(command_1), "%s", command_2); + + sprintf(full_filename_answer, "%s", filename_answer_p1); + sprintf(full_filename_answer + sizeof(filename_answer_p1) - 1, "%d", i); + sprintf(full_filename_answer + sizeof(filename_answer_p1), "%s", filename_answer_p2); + + int result = system(full_command); + if (result != 0) + { + perror("Command fails. Try again"); + exit(0); + } + + FILE *answer = fopen(full_filename_answer, "r+"); + FILE *student_answer = fopen("answer.txt", "r+"); + + int state = 0; + if (answer != NULL && student_answer != NULL) + { + state = 1; + // compare char by char + int student_answer_char = 0; + int answer_char = 0; + while (state) + { + student_answer_char = getc(student_answer); + answer_char = getc(answer); + if (student_answer_char != answer_char) + { + state = 0; + } + else if (student_answer_char == EOF && answer_char == EOF) + { + break; + } + } + } + + if (state) + { + correct_answers++; + } + i++; + } + + printf("For Q1: You have scored %d/%d \n", correct_answers, NUMQUESTIONS); +} \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_C/C_code/checkerQ2.c b/BankersAlgorithmLab/StarterCode_C/C_code/checkerQ2.c new file mode 100644 index 0000000..efd9eaf --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/C_code/checkerQ2.c @@ -0,0 +1,70 @@ +#include +#include +#include +#define NUMQUESTIONS 1 + +int main() +{ + + int i = 1; + char full_command[64] = {}; + char full_filename_answer[64] = {}; + + char command_1[] = "./q2 ./testcases/q2_"; + char command_2[] = ".txt > answer.txt"; + char filename_answer_p1[] = "./testcases/answerq2_"; + char filename_answer_p2[] = ".txt"; + + int correct_answers = 0; + + while (i <= NUMQUESTIONS) + { + sprintf(full_command, "%s", command_1); + sprintf(full_command + sizeof(command_1) - 1, "%d", i); + sprintf(full_command + sizeof(command_1), "%s", command_2); + + sprintf(full_filename_answer, "%s", filename_answer_p1); + sprintf(full_filename_answer + sizeof(filename_answer_p1) - 1, "%d", i); + sprintf(full_filename_answer + sizeof(filename_answer_p1), "%s", filename_answer_p2); + + int result = system(full_command); + if (result != 0) + { + perror("Command fails. Try again"); + exit(0); + } + + FILE *answer = fopen(full_filename_answer, "r+"); + FILE *student_answer = fopen("answer.txt", "r+"); + + int state = 0; + if (answer != NULL && student_answer != NULL) + { + state = 1; + // compare char by char + int student_answer_char = 0; + int answer_char = 0; + while (state) + { + student_answer_char = getc(student_answer); + answer_char = getc(answer); + if (student_answer_char != answer_char) + { + state = 0; + } + else if (student_answer_char == EOF && answer_char == EOF) + { + break; + } + } + } + + if (state) + { + correct_answers++; + } + i++; + } + + printf("For Q2: You have scored %d/%d \n", correct_answers, NUMQUESTIONS); +} \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_C/makefile b/BankersAlgorithmLab/StarterCode_C/makefile new file mode 100644 index 0000000..3ed00cc --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/makefile @@ -0,0 +1,16 @@ +all: C_code/BankerQ1.c C_code/checkerQ1.c C_code/BankerQ2.c C_code/checkerQ2.c + gcc -o checkq1 C_code/checkerQ1.c + gcc -o q1 C_code/BankerQ1.c + gcc -o checkq2 C_code/checkerQ2.c + gcc -o q2 C_code/BankerQ2.c + +test: ./checkq1 ./checkq2 + ./checkq1 + ./checkq2 + +clean: + rm checkq2 + rm checkq1 + rm q2 + rm q1 + rm answer.txt \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_C/testcases/answerq1_1.txt b/BankersAlgorithmLab/StarterCode_C/testcases/answerq1_1.txt new file mode 100644 index 0000000..c4057f2 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/testcases/answerq1_1.txt @@ -0,0 +1,37 @@ +Customer 0 requesting +0 1 0 +Customer 1 requesting +2 0 0 +Customer 2 requesting +3 0 2 +Customer 3 requesting +2 1 1 +Customer 4 requesting +0 0 2 +Customer 1 releasing +1 0 0 + +Current state: +Available: +4 3 2 +Maximum: +7 5 3 +3 2 2 +9 0 2 +2 2 2 +4 3 3 + +Allocation: +0 1 0 +1 0 0 +3 0 2 +2 1 1 +0 0 2 + +Need: +7 4 3 +1 2 2 +6 0 0 +0 1 1 +4 3 1 + diff --git a/BankersAlgorithmLab/StarterCode_C/testcases/answerq2_1.txt b/BankersAlgorithmLab/StarterCode_C/testcases/answerq2_1.txt new file mode 100644 index 0000000..0244562 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/testcases/answerq2_1.txt @@ -0,0 +1,64 @@ +Customer 0 requesting +0 1 0 +Customer 1 requesting +2 0 0 +Customer 2 requesting +3 0 2 +Customer 3 requesting +2 1 1 +Customer 4 requesting +0 0 2 +Customer 1 requesting +1 0 2 + +Current state: +Available: +2 3 0 +Maximum: +7 5 3 +3 2 2 +9 0 2 +2 2 2 +4 3 3 + +Allocation: +0 1 0 +3 0 2 +3 0 2 +2 1 1 +0 0 2 + +Need: +7 4 3 +0 2 0 +6 0 0 +0 1 1 +4 3 1 + +Customer 0 requesting +0 2 0 + +Current state: +Available: +2 3 0 +Maximum: +7 5 3 +3 2 2 +9 0 2 +2 2 2 +4 3 3 + +Allocation: +0 1 0 +3 0 2 +3 0 2 +2 1 1 +0 0 2 + +Need: +7 4 3 +0 2 0 +6 0 0 +0 1 1 +4 3 1 + diff --git a/BankersAlgorithmLab/StarterCode_C/testcases/q1_1.txt b/BankersAlgorithmLab/StarterCode_C/testcases/q1_1.txt new file mode 100644 index 0000000..cc8baf5 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/testcases/q1_1.txt @@ -0,0 +1,15 @@ +n,5 +m,3 +a,10 5 7 +c,0,7 5 3 +c,1,3 2 2 +c,2,9 0 2 +c,3,2 2 2 +c,4,4 3 3 +r,0,0 1 0 +r,1,2 0 0 +r,2,3 0 2 +r,3,2 1 1 +r,4,0 0 2 +f,1,1 0 0 +p \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_C/testcases/q2_1.txt b/BankersAlgorithmLab/StarterCode_C/testcases/q2_1.txt new file mode 100644 index 0000000..b8c7245 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_C/testcases/q2_1.txt @@ -0,0 +1,17 @@ +n,5 +m,3 +a,10 5 7 +c,0,7 5 3 +c,1,3 2 2 +c,2,9 0 2 +c,3,2 2 2 +c,4,4 3 3 +r,0,0 1 0 +r,1,2 0 0 +r,2,3 0 2 +r,3,2 1 1 +r,4,0 0 2 +r,1,1 0 2 +p +r,0,0 2 0 +p \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_Java/.DS_Store b/BankersAlgorithmLab/StarterCode_Java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0ZYCHEsh7j%ge=*dvpst@ zfkwH+q5+c1^33u-tanF^&GZAZ7?=h8i-C zV_I%m+BIA=8p14Gr6H_iHs%OsPr)eAQc6tHqU3U4v}YsbCafKG6&*obEeLHGFcL{) zd(w;+p#%{P^K^U+*U*Q0!|F2~K@r{aA%eWFSP1j+aSdfU7GPn{os64E)@*PbV|Q8* zbZuXW;Og?OSjA-JK`at5EY}g-Wb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Cf>%*P8-fgxFrQxe5# zxfw6YW}J}@(%UdBJ$Q-HIV*kmsqDgt?7`1uFJ6`d__;iUb8-mhQ qY4cya0OUia@%T|P<*^1HBb~wBw 0) { + runFile(args[0]); + } + } + +} \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_Java/TestBankQ1.class b/BankersAlgorithmLab/StarterCode_Java/TestBankQ1.class new file mode 100644 index 0000000000000000000000000000000000000000..c4e818920b3db0963da0961e34a94a04a4552df2 GIT binary patch literal 295 zcmYLFF>b;@5S%pz!^MdSk?1JsfC7{f1yKYk6#_-JXsFKQgpR-=8NA_aJ^> javac Banker.java TestBankQ1.java TestBankQ2.java +//.class files will be generated. Then use java to run it +//>> java TestBankQ1 + +public class TestBankQ1 { + + public static void main(String[] args) { + Banker.main(args); + } + +} diff --git a/BankersAlgorithmLab/StarterCode_Java/TestBankQ2.class b/BankersAlgorithmLab/StarterCode_Java/TestBankQ2.class new file mode 100644 index 0000000000000000000000000000000000000000..910778e2f941dbc7e22f5940efc93a1b3ad95d6a GIT binary patch literal 295 zcmYL_ziz@X5XQfQK)_8K+DdE;Y`_2#TUEpWQYX}^0I zRDGxb4+wZR((cEk1TWFn5$dhQB;PR2QtQ_5?YWO9f|{9KCj57w`SEsT2;sy!^SMtV zlg@M$^NpHr_vzAnSZTss(v`Jn;*tHzDS*T=$B?@QXX0GyoIpn|_$3@=ENE5SaxG|+ U^FR6vKS$*sSP$ +#include +#include +#define NUMQUESTIONS 1 + +int main() +{ + + int i = 1; + char full_command[64] = {}; + char full_filename_answer[64] = {}; + + char command_1[] = "java TestBankQ1.java q1_"; + char command_2[] = ".txt > answer.txt"; + char filename_answer_p1[] = "answerq1_"; + char filename_answer_p2[] = ".txt"; + + int correct_answers = 0; + + while (i <= NUMQUESTIONS) + { + sprintf(full_command, "%s", command_1); + sprintf(full_command + sizeof(command_1) - 1, "%d", i); + sprintf(full_command + sizeof(command_1), "%s", command_2); + + sprintf(full_filename_answer, "%s", filename_answer_p1); + sprintf(full_filename_answer + sizeof(filename_answer_p1) - 1, "%d", i); + sprintf(full_filename_answer + sizeof(filename_answer_p1), "%s", filename_answer_p2); + + int result = system(full_command); + if (result != 0) + { + perror("Command fails. Try again"); + exit(0); + } + + FILE *answer = fopen(full_filename_answer, "r+"); + FILE *student_answer = fopen("answer.txt", "r+"); + + int state = 0; + if (answer != NULL && student_answer != NULL) + { + state = 1; + // compare char by char + int student_answer_char = 0; + int answer_char = 0; + while (state) + { + student_answer_char = getc(student_answer); + answer_char = getc(answer); + if (student_answer_char != answer_char) + { + state = 0; + } + else if (student_answer_char == EOF && answer_char == EOF) + { + break; + } + } + } + + if (state) + { + correct_answers++; + } + i++; + } + + printf("For Q1: You have scored %d/%d \n", correct_answers, NUMQUESTIONS); +} \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_Java/checkerQ2.c b/BankersAlgorithmLab/StarterCode_Java/checkerQ2.c new file mode 100644 index 0000000..87515e1 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_Java/checkerQ2.c @@ -0,0 +1,70 @@ +#include +#include +#include +#define NUMQUESTIONS 1 + +int main() +{ + + int i = 1; + char full_command[64] = {}; + char full_filename_answer[64] = {}; + + char command_1[] = "java TestBankQ2.java q2_"; + char command_2[] = ".txt > answer.txt"; + char filename_answer_p1[] = "answerq2_"; + char filename_answer_p2[] = ".txt"; + + int correct_answers = 0; + + while (i <= NUMQUESTIONS) + { + sprintf(full_command, "%s", command_1); + sprintf(full_command + sizeof(command_1) - 1, "%d", i); + sprintf(full_command + sizeof(command_1), "%s", command_2); + + sprintf(full_filename_answer, "%s", filename_answer_p1); + sprintf(full_filename_answer + sizeof(filename_answer_p1) - 1, "%d", i); + sprintf(full_filename_answer + sizeof(filename_answer_p1), "%s", filename_answer_p2); + + int result = system(full_command); + if (result != 0) + { + perror("Command fails. Try again"); + exit(0); + } + + FILE *answer = fopen(full_filename_answer, "r+"); + FILE *student_answer = fopen("answer.txt", "r+"); + + int state = 0; + if (answer != NULL && student_answer != NULL) + { + state = 1; + // compare char by char + int student_answer_char = 0; + int answer_char = 0; + while (state) + { + student_answer_char = getc(student_answer); + answer_char = getc(answer); + if (student_answer_char != answer_char) + { + state = 0; + } + else if (student_answer_char == EOF && answer_char == EOF) + { + break; + } + } + } + + if (state) + { + correct_answers++; + } + i++; + } + + printf("For Q2: You have scored %d/%d \n", correct_answers, NUMQUESTIONS); +} \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_Java/checkq1 b/BankersAlgorithmLab/StarterCode_Java/checkq1 new file mode 100755 index 0000000000000000000000000000000000000000..9882bb2881ca7d507b83cb651486712d053a8196 GIT binary patch literal 13016 zcmeHOU2IfE6rS>H|O5!t_NoMAJ zGv}N+^Udt-<>qkypX+z-7oude5W7bSAts@27%PM;EJ#AkK($dFXHES}jjuMYUCq(q zrDQn#PeeZ#q3AfxjT@SWt4Mn;TwF9pD8jFtcARu{XL?96=&yGiHiPNku^|@XF-lbJ z7_qw;I!-Fx(~=q-3i?Yk2 z!?^OODz2glD-aj3EP=TEJv&Zk*h%f`YDsiD-Rb0A{rP+e_$!VJzb!Fdtsq;+iG zwTV=z0qBKvR*%!obk0-@fh#Pko)O|GdhT@%DvQ4cb-9OP9yniIF2m1*#RFQVZwqw- z>inKma(-t=%lyc$&ImBeOjO2k@UPEXbF&vU_bpieNz3b{*Ab4ilY0!|A%IF*aXDqw zQ;;X2`eiQmPd|KK6c?^rgUUHYT?rmXB}1_wo^`uY>1bEwl1jwY(W0#9prij=;EQ}d zsWJI7#V}wPFbo(5Ml$e?w3lbma zc5dZy+dJ?5@J$_&_oL_L?-UBQm%EHE{}~?lb}rWqZD?gr$hqtxId_0_+47lqTY~x; zs$|QjDzrhNkV2b*8r~UO2Gp?6jj6fOYOYG5+jQpTYJl1rvgJ1c+Zw!Fothm`vvc(9 zA9}VDI6L+Nkmo)G8EgCmfHh_U^}CbMVpk&5C$}4VFNVKB)i+nY)9m;KxOWHyC38lPPDqmE} zOMPYT!_M_}qw@opCKZ2GiVy2B6z`eS;{uU9ubf|jn&|EoYMaa`ZgHw+_M?ACOkHZ^J5ojNS|RoHUYw+qq}=aAr4$ zJXgq7YqwztGwqydQ!}^VzTdrp7XE~t>7y##A5)0aKpz4rEm%SNTkH3`*R+)BlRiRw z<220n(J)^MZBHs=gpTt!G3!aAtQ>z)%Avh@4c7}m5Ce~+$@FoRLdcyW0T%+`Kb8jg z5;K4*23!nEX>fgd%F4TyIaT6GL0u9FdD8~6w{bB~@cP-g+8AOy$z9>TY@zKTixb(} z(-^d~i|y>nTlT{9o_huXc!y4*4Y_I&%J!Z!{4sC(K3@jo(D>d$#lFcx6e`}4?Ypww zFWX+(9+2%p*&ded5wt6Ek9>fxeqEuW3GRAha3%(>B9hotN|z`-P3ad(3n`tYw2acX zl-5%ElG0{MpHkvs417fCO-hF$p)V?1!rQ}Eb2OD+5{_?es;-oHTeah>RHk>PtwmNi zp4t&j5(p(yMZ$`dsECNAiLS12JYuzlJ33R9R&#Qf6>blA#6@zRHjv885=pD6`UPua zqQ{Dbw@0m1Ya$tqSQU}^6%mW4%7d7t7ccH{W97T+0`#o>cPa~I{5J9aT!QyeTFWvh z_yeXl3>XFs1BL;^fMLKeU>GnA7zPXjh5^HXVc_0n;A?y*;KzXPbbn6w7j%DF_kZgC zhVJj^z7$`2%*TX#_kNh283qgkh5^HXVZbn87%&VN1`Gp+0mHz(!NBCl#76F9a-GA} z^>FJ}r!}_KY3~UqBcdrOdh3{ME0g4%7UYd1slAu_DQ(e>-1KxCZ7`lH4Y*;t1IH&} zJi4%F#=Attf znTa^eC1E}`^P8D6RF3+8bS-4SkL1{q9Pd= Xc>l;pTu9{9^NwQa?csD38$tOOO%+U^ literal 0 HcmV?d00001 diff --git a/BankersAlgorithmLab/StarterCode_Java/checkq2 b/BankersAlgorithmLab/StarterCode_Java/checkq2 new file mode 100755 index 0000000000000000000000000000000000000000..a300ac181ba4229ca1219b8859f1c08624a34233 GIT binary patch literal 13016 zcmeHOYiv|S6rSZ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()W~B zQ~HL|MoM2&;$aj&r}RFhLy*vyl}(YY5vwtl&Mb^1Hm{mnE%BDQj;~Uk*`Bf9up)`{ zwpfZlD3K}>R;5K%R4ht%bVL$St0mIbp02hUQ#-6kYosk9QZu!IR9>7+S*zy0X01zh zS@Fo$n3Zl$reaa6DmtqwYVlNg5YzPHGHFkl!k3>XFs1BL;^fMLKeU>GnAJiH8ii|+*d81RGc&*}b>?yu_p58dC= z{e9h6;7gDBnDFr453@7FfMLKeU>GnA7zPXjh5^HXVZbn87>6lcT4TKc>RFVs(Yl!4hM0G%EnRE-JOUS|h z6By!j#IaDUxhoSx4t|8;KGnfIiz+5a3>Do>7Gd#)1CSpleQfcZZa%GZA{fU^P2+z(``$^3ZcqB9qn zi8#z9VLmqVo0&3HiTZzZEo8tC=Gc)O^9GgOhXEH#-h=@o>WZmrp)Z8G3jQVVkKaFd W|HubjNaWP>j$-MpkxUF5LHQRj7)>w$ literal 0 HcmV?d00001 diff --git a/BankersAlgorithmLab/StarterCode_Java/makefile b/BankersAlgorithmLab/StarterCode_Java/makefile new file mode 100644 index 0000000..42a7126 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_Java/makefile @@ -0,0 +1,17 @@ +all: Banker.java TestBankQ1.java TestBankQ2.java + javac Banker.java TestBankQ1.java TestBankQ2.java + gcc -o checkq1 checkerQ1.c + gcc -o checkq2 checkerQ2.c + +test: checkq1 checkq2 Banker.class TestBankQ1.class TestBankQ2.class + ./checkq1 + ./checkq2 + + +clean: + rm checkq1 + rm checkq2 + rm Banker.class + rm TestBankQ1.class + rm TestBankQ2.class + rm answer.txt \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_Java/q1_1.txt b/BankersAlgorithmLab/StarterCode_Java/q1_1.txt new file mode 100644 index 0000000..cc8baf5 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_Java/q1_1.txt @@ -0,0 +1,15 @@ +n,5 +m,3 +a,10 5 7 +c,0,7 5 3 +c,1,3 2 2 +c,2,9 0 2 +c,3,2 2 2 +c,4,4 3 3 +r,0,0 1 0 +r,1,2 0 0 +r,2,3 0 2 +r,3,2 1 1 +r,4,0 0 2 +f,1,1 0 0 +p \ No newline at end of file diff --git a/BankersAlgorithmLab/StarterCode_Java/q2_1.txt b/BankersAlgorithmLab/StarterCode_Java/q2_1.txt new file mode 100644 index 0000000..b8c7245 --- /dev/null +++ b/BankersAlgorithmLab/StarterCode_Java/q2_1.txt @@ -0,0 +1,17 @@ +n,5 +m,3 +a,10 5 7 +c,0,7 5 3 +c,1,3 2 2 +c,2,9 0 2 +c,3,2 2 2 +c,4,4 3 3 +r,0,0 1 0 +r,1,2 0 0 +r,2,3 0 2 +r,3,2 1 1 +r,4,0 0 2 +r,1,1 0 2 +p +r,0,0 2 0 +p \ No newline at end of file