From f3dc4ce7c56cc2095f0cd24a065c5e453de4e47e Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Tue, 17 Mar 2026 22:05:59 -0600 Subject: [PATCH] All wroking fine, too tired to make tests, everything seems fine --- include/queue.h | 43 +++++++++------- include/utils.h | 14 ++++++ src/main.cpp | 101 ++++++++++++++++++++++++++++++++++++- src/utils.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 269 insertions(+), 18 deletions(-) diff --git a/include/queue.h b/include/queue.h index 6e1cd35..075af6e 100644 --- a/include/queue.h +++ b/include/queue.h @@ -42,7 +42,7 @@ public: template requires std::formattable Queue::Queue() { - this->cap = 0; + this->cap = DEFAULT_QUEUE_SIZE; this->len = 0; this->head = 0; this->tail = 0; @@ -52,7 +52,7 @@ Queue::Queue() { template requires std::formattable Queue::Queue(uint64_t size) { - this->cap = 0; + this->cap = DEFAULT_QUEUE_SIZE; this->len = 0; this->head = 0; this->tail = 0; @@ -60,6 +60,16 @@ Queue::Queue(uint64_t size) { } +template +requires std::formattable +Queue::~Queue() { + delete[] this->data; + this->cap = 0; + this->len = 0; + this->head = 0; + this->tail = 0; +} + template requires std::formattable @@ -69,13 +79,13 @@ QueueErr Queue::enqueue(T value) { T *tmp; try { - T *tmp = new T[new_cap]; + tmp = new T[new_cap]; } catch(const std::bad_alloc& e) { return QueueErr::bad_alloc; } for (uint64_t i = 0; i < this->len; i++) { - uint64_t index = (this->tail) % this->cap; + uint64_t index = (this->tail + i) % this->cap; tmp[i] = this->data[index]; } @@ -113,7 +123,7 @@ std::expected Queue::dequeue() { } for (uint64_t i = 0; i < this->len; i++) { - uint64_t index = (this->tail) % this->cap; + uint64_t index = (this->tail + i) % this->cap; tmp[i] = this->data[index]; } @@ -142,9 +152,6 @@ std::expected Queue::peek() { T out; out = this->data[this->tail]; - this->len--; - this->tail = (this->tail + 1) % this->cap; - return out; } @@ -156,25 +163,27 @@ void Queue::print() { return; } - std::println("Lenght: {}", this->len); + std::println("Length: {}", this->len); std::println("Capacity: {}", this->cap); + for (uint64_t i = 0; i < this->cap; i++) { - if (this->tail == this->head) { - std::println("H/T --->"); - } else if (this->tail == i) { - std::println("Tail--->"); - } else if (this->head == i) { - std::println("Head--->"); + if (i == this->tail && i == this->head) { + std::print("H/T --->"); + } else if (i == this->tail) { + std::print("Tail--->"); + } else if (i == this->head) { + std::print("Head--->"); } else { - std::println("|{:^20}|", ""); + std::print(" "); } uint64_t dist = (i + this->cap - this->tail) % this->cap; + if (dist < this->len) { std::println("|{:^20}|", this->data[i]); } else { std::println("|{:^20}|", ""); - } + } } } #endif // !QUEUE_H diff --git a/include/utils.h b/include/utils.h index e69de29..f7af976 100644 --- a/include/utils.h +++ 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 5f2739b..ee78906 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,106 @@ +#include "queue.h" +#include "utils.h" #include #include +void menu(Queue queue); + int main(void) { - std::println("Hola"); + Queue queue = {}; + + menu(queue); return EXIT_SUCCESS; } + +void menu(Queue queue) { + while (true) { + clear_screen(); + std::println("---Queue Manager---"); + std::println("1) Enqueue."); + std::println("2) Dequeue."); + std::println("3) Peek."); + std::println("4) Diplay."); + std::println("5) Exit."); + int choice; + + if (!read_int("", &choice)) { + std::println("Invalid input."); + wait_enter(); + continue; + } + + clear_screen(); + switch (choice) { + case 1: { + int input; + + if (!read_int("Type the integer to enqueue:\n", &input)) { + std::println("Invalid input."); + wait_enter(); + continue; + } + + if (queue.enqueue(input) == QueueErr::bad_alloc) { + std::println("Program couldn't allocate memory for queue."); + wait_enter(); + continue; + } + + std::println("Integer {} was correclty enqueued.", input); + wait_enter(); + break; + } + case 2: { + auto out = queue.dequeue(); + if (!out.has_value() && out.error() == QueueErr::empty) { + std::println("The queue is empty."); + wait_enter(); + continue; + } else if (!out.has_value() && out.error() == QueueErr::bad_alloc) { + std::println("Program couldn't allocate memory for queue"); + wait_enter(); + continue; + } + + std::println("The element at the tail was {}.", out.value()); + wait_enter(); + break; + } + case 3: { + auto out = queue.peek(); + if (!out.has_value() && out.error() == QueueErr::empty) { + std::println("The queue is empty."); + wait_enter(); + continue; + } else if (!out.has_value() && out.error() == QueueErr::bad_alloc) { + std::println("Program couldn't allocate memory for queue"); + wait_enter(); + continue; + } + + std::println("The element at the tail was {}.", out.value()); + wait_enter(); + break; + + } + case 4: { + queue.print(); + wait_enter(); + break; + } + case 5: { + std::println("Goodbye :)"); + wait_enter(); + clear_screen(); + exit(1); + } + default: { + std::println("Invalid option."); + wait_enter(); + clear_screen(); + break; + } + + } + } +} diff --git a/src/utils.cpp b/src/utils.cpp index e69de29..3c1c82e 100644 --- a/src/utils.cpp +++ 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; +}