commit c997a299670a9370930dd0b1b2feb81af3bcae0d Author: Isaac Shoebottom Date: Fri Sep 15 23:55:52 2023 -0300 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b08a72e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# CLion Shit +cmake-build-* diff --git a/Assignment1/.idea/.gitignore b/Assignment1/.idea/.gitignore new file mode 100644 index 0000000..1c2fda5 --- /dev/null +++ b/Assignment1/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Assignment1/.idea/.name b/Assignment1/.idea/.name new file mode 100644 index 0000000..5cd67c1 --- /dev/null +++ b/Assignment1/.idea/.name @@ -0,0 +1 @@ +Assignment1 \ No newline at end of file diff --git a/Assignment1/.idea/Assgn1.iml b/Assignment1/.idea/Assgn1.iml new file mode 100644 index 0000000..6d70257 --- /dev/null +++ b/Assignment1/.idea/Assgn1.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Assignment1/.idea/misc.xml b/Assignment1/.idea/misc.xml new file mode 100644 index 0000000..f1c67df --- /dev/null +++ b/Assignment1/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Assignment1/.idea/modules.xml b/Assignment1/.idea/modules.xml new file mode 100644 index 0000000..ad3f544 --- /dev/null +++ b/Assignment1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment1/.idea/runConfigurations/Assignment1.xml b/Assignment1/.idea/runConfigurations/Assignment1.xml new file mode 100644 index 0000000..725844c --- /dev/null +++ b/Assignment1/.idea/runConfigurations/Assignment1.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Assignment1/.idea/runConfigurations/For_Testing.xml b/Assignment1/.idea/runConfigurations/For_Testing.xml new file mode 100644 index 0000000..20c9e8c --- /dev/null +++ b/Assignment1/.idea/runConfigurations/For_Testing.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Assignment1/.idea/runConfigurations/Test.xml b/Assignment1/.idea/runConfigurations/Test.xml new file mode 100644 index 0000000..4dfb1db --- /dev/null +++ b/Assignment1/.idea/runConfigurations/Test.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/Assignment1/.idea/workspace-ROG-GM501GS.xml b/Assignment1/.idea/workspace-ROG-GM501GS.xml new file mode 100644 index 0000000..e80d98f --- /dev/null +++ b/Assignment1/.idea/workspace-ROG-GM501GS.xml @@ -0,0 +1,100 @@ + + + + + { + "useNewFormat": true +} + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 1 +} + + + + + + + + + + + + + + + + + + + 1694613741352 + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment1/CMakeLists.txt b/Assignment1/CMakeLists.txt new file mode 100644 index 0000000..b5cf30d --- /dev/null +++ b/Assignment1/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.22) +project(Assignment1 C) + +set(CMAKE_C_STANDARD 11) + +include(CTest) + +add_executable( + Assignment1 + linked_list.h + linked_list.c + node.h + node.c + main.c +) \ No newline at end of file diff --git a/Assignment1/documentation/Assignment1.pdf b/Assignment1/documentation/Assignment1.pdf new file mode 100644 index 0000000..e38c3c3 Binary files /dev/null and b/Assignment1/documentation/Assignment1.pdf differ diff --git a/Assignment1/linked_list.c b/Assignment1/linked_list.c new file mode 100644 index 0000000..c31ac01 --- /dev/null +++ b/Assignment1/linked_list.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include "node.h" + +#define MAX_LEN 100 + + +Node *search(Node **head, char *data) { + Node *current = *head; + while (current != NULL) { + if (strcmp(current->data, data) == 0) { + return current; + } + current = current->next_elem; + } + return NULL; +} + +Node *searchForPrevious(Node **head, Node *current) { + Node *previous = *head; + while (previous != NULL) { + if (previous->next_elem == current) { + return previous; + } + previous = previous->next_elem; + } + return NULL; +} + + +void add(Node **head, char *data) { + if (*head == NULL) { + *head = createNode(data); + replaceData(*head, data); + } else { + Node *current = *head; + while (current->next_elem != NULL) { + current = current->next_elem; + } + current->next_elem = createNode(data); + replaceData(current->next_elem, data); + } +} + +bool delete(Node **head, char *data) { + if (*head == NULL) { + return false; + } + + Node *result = search(head, data); + if (result == NULL) { + return false; + } else { + Node *previous = searchForPrevious(head, result); + if (previous == NULL) { + // If the node to be deleted is the head + Node *next = result->next_elem; + destroyNode(result); + *head = next; + } else { + // If the node to be deleted is not the head + previous->next_elem = result->next_elem; + destroyNode(result); + } + return true; + } + +} + + +bool findAndReplace(Node **head, char *data, char *newData) { + Node *current = search(head, data); + if (current != NULL) { + replaceData(current, newData); + return true; + } + return false; +} + +bool stop(Node **head) { + Node *current = *head; + while (current != NULL) { + Node *next = current->next_elem; + destroyNode(current); + current = next; + } + return true; +} + +void printList(Node **head) { + Node *current = *head; + while (current != NULL) { + printf("%s\n", current->data); + current = current->next_elem; + } +} \ No newline at end of file diff --git a/Assignment1/linked_list.h b/Assignment1/linked_list.h new file mode 100644 index 0000000..003398b --- /dev/null +++ b/Assignment1/linked_list.h @@ -0,0 +1,25 @@ +// +// Created by Isaac on 9/15/2023. +// + +#ifndef ASSIGNMENT1_LINKED_LIST_H +#define ASSIGNMENT1_LINKED_LIST_H + +#include +#include "node.h" + +Node *search(Node **head, char *data); + +Node *searchForPrevious(Node **head, Node *current); + +void add(Node **head, char *data); + +bool delete(Node **head, char *data); + +bool findAndReplace(Node **head, char *data, char *newData); + +void printList(Node **head); + +bool stop(Node **head); + +#endif //ASSIGNMENT1_LINKED_LIST_H diff --git a/Assignment1/main.c b/Assignment1/main.c new file mode 100644 index 0000000..3b36ea6 --- /dev/null +++ b/Assignment1/main.c @@ -0,0 +1,41 @@ +// +// Created by Isaac on 9/15/2023. +// +#include +#include +#include +#include "linked_list.h" + + +#define MAX_LEN 100 + +int main() { + Node *head = NULL; + // Double buffer to support up to 100 chars in find and replace command + char buffer[MAX_LEN * 2]; + + bool stopLoop = false; + while (!stopLoop) { + fgets(buffer, MAX_LEN * 2, stdin); + buffer[strcspn(buffer, "\r\n")] = 0; + if (buffer[0] == 'a') { + buffer[MAX_LEN - 1] = '\0'; // Prevent buffer overflow + add(&head, buffer + 2); + } else if (buffer[0] == 'd') { + buffer[MAX_LEN - 1] = '\0'; // Prevent buffer overflow + delete(&head, buffer + 2); + } else if (buffer[0] == 'f') { + char *data = buffer + 2; + char input1[MAX_LEN], input2[MAX_LEN]; + // sscanf assigns a null terminator to the end of the string, so don't need to do it manually + sscanf(data, "%99s %99s", input1, input2); + // Could check if the replacement was successful by using return value of function, but I don't have to + findAndReplace(&head, input1, input2); + } else if (buffer[0] == 'p') { + printList(&head); + } else if (buffer[0] == 's') { + stopLoop = stop(&head); + } + } + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/Assignment1/node.c b/Assignment1/node.c new file mode 100644 index 0000000..b5b71d7 --- /dev/null +++ b/Assignment1/node.c @@ -0,0 +1,48 @@ +// +// Created by Isaac on 9/15/2023. +// + +#include +#include +#include +#include "node.h" + +//Assume that the string passed in is null terminated +Node *createNode(char *data) { + // Allocate memory for the node, and check if it was successful + Node *node = calloc(1, sizeof(Node)); + if (node == NULL) { + printf("Error allocating memory for node\n"); + exit(EXIT_FAILURE); + } + // Allocate memory for the data, and check if it was successful + node->data = calloc(strlen(data), sizeof(char)); + if (node->data == NULL) { + printf("Error allocating memory for node data\n"); + exit(EXIT_FAILURE); + } + // Copy data from the string passed in to the node's data + // Makes sure if the string passed in is changed, the node's data is not changed + // Also makes sure that if the data is on the stack, it is not freed at some other point + strcpy(node->data, data); + node->next_elem = NULL; + return node; +} + +void destroyNode(Node *node) { + // Free the data first, then free the node + free(node->data); + free(node); +} + +void replaceData(Node *node, char *data) { + // Free the data first, then allocate memory for the new data + free(node->data); + node->data = calloc(strlen(data), sizeof(char)); + if (node->data == NULL) { + printf("Error allocating memory for replacing data\n"); + exit(EXIT_FAILURE); + } + // Same rational as for createNode + strcpy(node->data, data); +} diff --git a/Assignment1/node.h b/Assignment1/node.h new file mode 100644 index 0000000..01655f3 --- /dev/null +++ b/Assignment1/node.h @@ -0,0 +1,18 @@ +// +// Created by Isaac on 9/15/2023. +// + +#ifndef ASSIGNMENT1_NODE_H +#define ASSIGNMENT1_NODE_H +typedef struct node { + struct node *next_elem; + char *data; +} Node; + +Node *createNode(char *data); + +void destroyNode(Node *node); + +void replaceData(Node *node, char *data); + +#endif //ASSIGNMENT1_NODE_H diff --git a/Assignment1/tests/add.txt b/Assignment1/tests/add.txt new file mode 100644 index 0000000..f3ceffb --- /dev/null +++ b/Assignment1/tests/add.txt @@ -0,0 +1,4 @@ +a test1 +a test2 +p +s \ No newline at end of file diff --git a/Assignment1/tests/add_Expected.txt b/Assignment1/tests/add_Expected.txt new file mode 100644 index 0000000..bae42c5 --- /dev/null +++ b/Assignment1/tests/add_Expected.txt @@ -0,0 +1,2 @@ +test1 +test2 diff --git a/Assignment1/tests/add_Result.txt b/Assignment1/tests/add_Result.txt new file mode 100644 index 0000000..bae42c5 --- /dev/null +++ b/Assignment1/tests/add_Result.txt @@ -0,0 +1,2 @@ +test1 +test2 diff --git a/Assignment1/tests/delete.txt b/Assignment1/tests/delete.txt new file mode 100644 index 0000000..fac44c1 --- /dev/null +++ b/Assignment1/tests/delete.txt @@ -0,0 +1,5 @@ +a test1 +a test2 +d test1 +p +s \ No newline at end of file diff --git a/Assignment1/tests/deleteEmpty.txt b/Assignment1/tests/deleteEmpty.txt new file mode 100644 index 0000000..7703e43 --- /dev/null +++ b/Assignment1/tests/deleteEmpty.txt @@ -0,0 +1,3 @@ +d hello +p +s \ No newline at end of file diff --git a/Assignment1/tests/deleteEmpty_Expected.txt b/Assignment1/tests/deleteEmpty_Expected.txt new file mode 100644 index 0000000..e69de29 diff --git a/Assignment1/tests/deleteEmpty_Result.txt b/Assignment1/tests/deleteEmpty_Result.txt new file mode 100644 index 0000000..e69de29 diff --git a/Assignment1/tests/delete_Expected.txt b/Assignment1/tests/delete_Expected.txt new file mode 100644 index 0000000..180cf83 --- /dev/null +++ b/Assignment1/tests/delete_Expected.txt @@ -0,0 +1 @@ +test2 diff --git a/Assignment1/tests/delete_Result.txt b/Assignment1/tests/delete_Result.txt new file mode 100644 index 0000000..180cf83 --- /dev/null +++ b/Assignment1/tests/delete_Result.txt @@ -0,0 +1 @@ +test2 diff --git a/Assignment1/tests/exit.txt b/Assignment1/tests/exit.txt new file mode 100644 index 0000000..2f259b7 --- /dev/null +++ b/Assignment1/tests/exit.txt @@ -0,0 +1 @@ +s \ No newline at end of file diff --git a/Assignment1/tests/exit_Expected.txt b/Assignment1/tests/exit_Expected.txt new file mode 100644 index 0000000..e69de29 diff --git a/Assignment1/tests/exit_Result.txt b/Assignment1/tests/exit_Result.txt new file mode 100644 index 0000000..e69de29 diff --git a/Assignment1/tests/findAndReplace.txt b/Assignment1/tests/findAndReplace.txt new file mode 100644 index 0000000..dd4d0e3 --- /dev/null +++ b/Assignment1/tests/findAndReplace.txt @@ -0,0 +1,5 @@ +a test1 +a test2 +f test1 test3 +p +s diff --git a/Assignment1/tests/findAndReplace_Expected.txt b/Assignment1/tests/findAndReplace_Expected.txt new file mode 100644 index 0000000..13a56c2 --- /dev/null +++ b/Assignment1/tests/findAndReplace_Expected.txt @@ -0,0 +1,2 @@ +test3 +test2 diff --git a/Assignment1/tests/findAndReplace_Result.txt b/Assignment1/tests/findAndReplace_Result.txt new file mode 100644 index 0000000..13a56c2 --- /dev/null +++ b/Assignment1/tests/findAndReplace_Result.txt @@ -0,0 +1,2 @@ +test3 +test2 diff --git a/Assignment1/tests/pdf1.txt b/Assignment1/tests/pdf1.txt new file mode 100644 index 0000000..081671c --- /dev/null +++ b/Assignment1/tests/pdf1.txt @@ -0,0 +1,5 @@ +a hello +a hi +a hi +p +s \ No newline at end of file diff --git a/Assignment1/tests/pdf1_Expected.txt b/Assignment1/tests/pdf1_Expected.txt new file mode 100644 index 0000000..9b92d6e --- /dev/null +++ b/Assignment1/tests/pdf1_Expected.txt @@ -0,0 +1,3 @@ +hello +hi +hi diff --git a/Assignment1/tests/pdf1_Result.txt b/Assignment1/tests/pdf1_Result.txt new file mode 100644 index 0000000..9b92d6e --- /dev/null +++ b/Assignment1/tests/pdf1_Result.txt @@ -0,0 +1,3 @@ +hello +hi +hi diff --git a/Assignment1/tests/pdf2.txt b/Assignment1/tests/pdf2.txt new file mode 100644 index 0000000..d1ba821 --- /dev/null +++ b/Assignment1/tests/pdf2.txt @@ -0,0 +1,11 @@ +a hello +a hi +a hey +p +f hey yes +p +d hi +p +f hi maybe +p +s \ No newline at end of file diff --git a/Assignment1/tests/pdf2_Expected.txt b/Assignment1/tests/pdf2_Expected.txt new file mode 100644 index 0000000..a360872 --- /dev/null +++ b/Assignment1/tests/pdf2_Expected.txt @@ -0,0 +1,10 @@ +hello +hi +hey +hello +hi +yes +hello +yes +hello +yes diff --git a/Assignment1/tests/pdf2_Result.txt b/Assignment1/tests/pdf2_Result.txt new file mode 100644 index 0000000..a360872 --- /dev/null +++ b/Assignment1/tests/pdf2_Result.txt @@ -0,0 +1,10 @@ +hello +hi +hey +hello +hi +yes +hello +yes +hello +yes diff --git a/Assignment1/tests/test.sh b/Assignment1/tests/test.sh new file mode 100644 index 0000000..2314a1f --- /dev/null +++ b/Assignment1/tests/test.sh @@ -0,0 +1,36 @@ +#!/usr/bin/bash + +function files() { + if [ ! -f "$1.txt" ]; then + echo "Creating $1.txt" + touch "$1.txt" + fi + if [ ! -f "$1_Expected.txt" ]; then + echo "Creating $1_Expected.txt" + touch "$1_Expected.txt" + fi + if [ ! -f "$1_Result.txt" ]; then + echo "Creating $1_Result.txt" + touch "$1_Result.txt" + fi +} + +function test() { + # Check if the files exist and if not, create them + files "$1" + + ../cmake-build-debug-wsl/Assignment1 < "$1.txt" > "$1_Result.txt" + if diff -u "$1_Result.txt" "$1_Expected.txt"; + then + echo "$1 test passed" + else + echo "$1 test failed" + fi +} +test "findAndReplace" +test "add" +test "delete" +test "pdf1" +test "pdf2" +test "exit" +test "deleteEmpty" \ No newline at end of file diff --git a/Lab1/Lab1.pdf b/Lab1/Lab1.pdf new file mode 100644 index 0000000..ad4bfb5 Binary files /dev/null and b/Lab1/Lab1.pdf differ diff --git a/Lab1/a.out b/Lab1/a.out new file mode 100644 index 0000000..f901c24 Binary files /dev/null and b/Lab1/a.out differ diff --git a/Lab1/compile_script.sh b/Lab1/compile_script.sh new file mode 100644 index 0000000..7132e02 --- /dev/null +++ b/Lab1/compile_script.sh @@ -0,0 +1,4 @@ +#!/bin/bash +gcc -Wall -Werror test.c +./a.out < input.txt > output.txt +diff correct_output.txt output.txt diff --git a/Lab1/correct_output.txt b/Lab1/correct_output.txt new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/Lab1/correct_output.txt @@ -0,0 +1 @@ +Hello diff --git a/Lab1/history_backup.txt b/Lab1/history_backup.txt new file mode 100644 index 0000000..23c21bf --- /dev/null +++ b/Lab1/history_backup.txt @@ -0,0 +1,31 @@ + 108 15:06 pwd + 109 15:06 mkdir "Lab 1" + 110 15:06 cd Lab\ 1/ + 111 15:08 cd .. + 112 15:08 rm Lab\ 1/ + 113 15:09 rmdir Lab\ 1/ + 114 15:09 mkdir lab1 + 115 15:09 mv lab1/ Lab1 + 116 15:09 cd D + 117 15:09 cd Lab1/ + 118 15:10 touch test.c + 123 15:14 nano test.c + 124 15:14 gcc -Wall -Werror test.c + 125 15:14 echo Hello world > input.txt + 126 15:14 echo Hello planet >> input.txt + 127 15:15 cat input.txt + 128 15:15 echo Hello > correct_output.txt + 129 15:15 ./a.out < input.txt + 130 15:17 ./a.out < input.txt > output.txt + 131 15:17 diff correct_output.txt output.txt + 132 15:17 diff correct_output.txt incorrect + 133 15:22 echo Hello world > incorrect_output.txt + 134 15:22 diff correct_output.txt incorrect_output.txt + 135 15:23 history | tail -n 20 > script.sh + 136 15:23 nano script.sh + 137 15:25 nano tmp.txt + 138 15:25 cat tmp.txt script.sh > compile_script.sh + 139 15:25 ls + 141 15:26 chmod +x compile_script.sh + 142 15:26 ./compile_script.sh + 143 15:28 history > history_backup.txt diff --git a/Lab1/incorrect_output.txt b/Lab1/incorrect_output.txt new file mode 100644 index 0000000..802992c --- /dev/null +++ b/Lab1/incorrect_output.txt @@ -0,0 +1 @@ +Hello world diff --git a/Lab1/input.txt b/Lab1/input.txt new file mode 100644 index 0000000..e37a058 --- /dev/null +++ b/Lab1/input.txt @@ -0,0 +1,2 @@ +Hello world +Hello planet diff --git a/Lab1/output.txt b/Lab1/output.txt new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/Lab1/output.txt @@ -0,0 +1 @@ +Hello diff --git a/Lab1/script.sh b/Lab1/script.sh new file mode 100644 index 0000000..3fb7359 --- /dev/null +++ b/Lab1/script.sh @@ -0,0 +1,3 @@ +gcc -Wall -Werror test.c +./a.out < input.txt > output.txt +diff correct_output.txt output.txt diff --git a/Lab1/test.c b/Lab1/test.c new file mode 100644 index 0000000..db560b2 --- /dev/null +++ b/Lab1/test.c @@ -0,0 +1,7 @@ +#include +int main(int argc, char** argv){ +char temp[100]; +scanf("%s", temp); +printf("%s\n", temp); +return 0; +} diff --git a/Lab1/tmp.txt b/Lab1/tmp.txt new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/Lab1/tmp.txt @@ -0,0 +1 @@ +#!/bin/bash diff --git a/Lab1/to_submit.txt b/Lab1/to_submit.txt new file mode 100644 index 0000000..db25ec1 --- /dev/null +++ b/Lab1/to_submit.txt @@ -0,0 +1,35 @@ + 108 15:06 pwd + 109 15:06 mkdir "Lab 1" + 110 15:06 cd Lab\ 1/ + 111 15:08 cd .. + 112 15:08 rm Lab\ 1/ + 113 15:09 rmdir Lab\ 1/ + 114 15:09 mkdir lab1 + 115 15:09 mv lab1/ Lab1 + 116 15:09 cd D + 117 15:09 cd Lab1/ + 118 15:10 touch test.c + 123 15:14 nano test.c + 124 15:14 gcc -Wall -Werror test.c + 125 15:14 echo Hello world > input.txt + 126 15:14 echo Hello planet >> input.txt + 127 15:15 cat input.txt + 128 15:15 echo Hello > correct_output.txt + 129 15:15 ./a.out < input.txt + 130 15:17 ./a.out < input.txt > output.txt + 131 15:17 diff correct_output.txt output.txt + 132 15:17 diff correct_output.txt incorrect + 133 15:22 echo Hello world > incorrect_output.txt + 134 15:22 diff correct_output.txt incorrect_output.txt + 135 15:23 history | tail -n 20 > script.sh + 136 15:23 nano script.sh + 137 15:25 nano tmp.txt + 138 15:25 cat tmp.txt script.sh > compile_script.sh + 139 15:25 ls + 141 15:26 chmod +x compile_script.sh + 142 15:26 ./compile_script.sh + 143 15:28 history > history_backup.txt +#!/bin/bash +gcc -Wall -Werror test.c +./a.out < input.txt > output.txt +diff correct_output.txt output.txt