From c56cc5dca28ccc8fa560f3aa563cd862630082de Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Tue, 17 Mar 2026 15:36:27 -0600 Subject: [PATCH] all done, just adding tests for redundancy --- CMakeLists.txt | 1 + include/stack.h | 24 +++++---- include/utils.h | 14 ++++++ src/main.cpp | 89 ++++++++++++++++++++++++++++++++- src/utils.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 242 insertions(+), 15 deletions(-) create mode 100644 include/utils.h create mode 100644 src/utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f880cae..c2d6644 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ add_compile_options( add_executable(main_exec src/main.cpp + src/utils.cpp ) target_include_directories(main_exec diff --git a/include/stack.h b/include/stack.h index 93e9de3..e1e3ecc 100644 --- a/include/stack.h +++ b/include/stack.h @@ -39,11 +39,9 @@ public: template requires std::formattable Stack::Stack() { - Stack new_stack; - new_stack.data = new T[DEFAULT_STACK_SIZE]; - new_stack.cap = 0; - new_stack.len = 0; - return new_stack; + this->data = new T[DEFAULT_STACK_SIZE]; + this->cap = DEFAULT_STACK_SIZE; + this->len = 0; } template @@ -109,7 +107,7 @@ std::expected Stack::pop() { this->cap = new_capacity; } - return std::expected(return_val); + return return_val; } template @@ -119,7 +117,7 @@ std::expected Stack::peek() { return std::unexpected(StackErr::empty); } - return std::expected(this->data[this->len - 1]); + return this->data[this->len - 1]; } template @@ -129,15 +127,15 @@ void Stack::print() { std::println("Length: {}.", this->len); std::println("Capacity: {}.", this->cap); - std::println("{:^20}", "Datos"); - std::println("{:^20}", "|"); - std::println("{:^20}", "v"); + std::println("{:^22}", "Datos"); + std::println("{:^22}", "|"); + std::println("{:^22}", "v"); - for (int i = 0; i < this->cap; i++) { + for (uint64_t i = 0; i < this->cap; i++) { if (i < this->len) { - std::println("{:^20}", this->data[i]); + std::println("[{:^20}]", this->data[i]); } else { - std::println("{:^20}", "NULL"); + std::println("[{:^20}]", "EMPTY"); } } } diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..f7af976 --- /dev/null +++ b/include/utils.h @@ -0,0 +1,14 @@ +#ifndef UTILS_H +#define UTILS_H + +#include +#include + +void clear_screen(); +void wait_enter(); +void sleep_seconds(size_t s); +void read_string(const char *message, char **dest); +int read_int(const char *message, int *dest); +int read_double(const char *message, double *dest); + +#endif // ! diff --git a/src/main.cpp b/src/main.cpp index 6952221..a6150f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,92 @@ -#include "stack" +#include "stack.h" +#include "utils.h" +#include #include +void menu(Stack stack); + int main(void) { - std::print("hola"); + Stack stack = {}; + menu(stack); return 0; } + +void menu(Stack stack) { + while (true) { + clear_screen(); + std::println("--Stack Manager--"); + std::println("1) Pop."); + std::println("2) Peek."); + std::println("3) Push."); + std::println("4) Print."); + std::println("5) Exit"); + int choice; + if (!read_int("", &choice)) { + std::print("Invalid input, try again."); + wait_enter(); + clear_screen(); + continue; + } + + clear_screen(); + switch (choice) { + case 1: { + auto pop_val = stack.pop(); + + if (pop_val.has_value()) { + std::println("The value at the top of the stack was {}.", pop_val.value()); + } else if (pop_val.error() == StackErr::empty) { + std::println("The stack is empty."); + } else { + std::println("The system couldn't allocate memory fo the stack."); + } + + wait_enter(); + clear_screen(); + break; + } + case 2: { + auto peek_val = stack.peek(); + + if (peek_val.has_value()) { + std::println("The value at the top of the stack is {}.", peek_val.value()); + } else { + std::println("The stack is empty."); + } + + wait_enter(); + clear_screen(); + break; + } + case 3: { + int val; + if (!read_int("Enter a number to push into the stack.\n", &val)) { + std::println("Invalid input."); + continue; + } + + stack.push(val); + std::println("The value {} was correclty pushed into the stack.", val); + wait_enter(); + break; + } + case 4: { + stack.print(); + wait_enter(); + break; + } + case 5: { + std::println("Goodbye :)"); + wait_enter(); + clear_screen(); + exit(1); + } + default: { + std::println("Invalid option."); + wait_enter(); + break; + } + } + + } +} diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..3c1c82e --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,129 @@ +#include "utils.h" +#include +#include +#include +#include +#include +#include +#include +#include + +void clear_screen() { + printf("\033[2J\033[H"); + fflush(stdout); +} + +void wait_enter() { + int c; + printf("Press Enter to continue..."); + fflush(stdout); + + while ((c = getchar()) != '\n' && c != EOF) { + ; + } +} + +void sleep_seconds(size_t s) { + #ifdef _WIN32 + #include + Sleep(s * 1000); + #else + // #include + // sleep(s); + #endif /* ifdef _WIN32 + */ +} + +void read_string(const char *message, char **dest) { + if (!dest) { + return; + } + + printf("%s", message); + + char buffer[256]; // I mean, should be enough + if (!fgets(buffer, sizeof(buffer), stdin)) + return; + + buffer[strcspn(buffer, "\n")] = '\0'; + + char *tmp = static_cast(malloc(strlen(buffer) + 1)); + if (!tmp) { + return; + } + + strcpy(tmp, buffer); + + free(*dest); + *dest = tmp; +} + +int read_int(const char *message, int *dest) { + char buffer[64]; + char *end; + long value; + + printf("%s", message); + fflush(stdout); + + if (!fgets(buffer, sizeof(buffer), stdin)) { + return 0; + } + + errno = 0; + value = strtol(buffer, &end, 10); + + if (errno != 0) { + return 0; + } + + if (end == buffer) { + return 0; + } + + if (*end != '\n' && *end != '\0') { + return 0; + } + + if (value < INT_MIN || value > INT_MAX) { + return 0; + } + + *dest = (int)value; + return 1; +} + +int read_double(const char *message, double *dest) { + char buffer[64]; + char *end; + double value; + + printf("%s", message); + fflush(stdout); + + if (!fgets(buffer, sizeof(buffer), stdin)) { + return 0; + } + + errno = 0; + value = strtod(buffer, &end); + + if (errno != 0) { + return 0; + } + + if (end == buffer) { + return 0; + } + + if (*end != '\n' && *end != '\0') { + return 0; + } + + if (!isfinite(value)) { + return 0; + } + + *dest = value; + return 1; +}