all done, just adding tests for redundancy

This commit is contained in:
2026-03-17 15:36:27 -06:00
parent 3dd365012c
commit c56cc5dca2
5 changed files with 242 additions and 15 deletions

View File

@@ -20,6 +20,7 @@ add_compile_options(
add_executable(main_exec
src/main.cpp
src/utils.cpp
)
target_include_directories(main_exec

View File

@@ -39,11 +39,9 @@ public:
template <typename T>
requires std::formattable<T, char>
Stack<T>::Stack() {
Stack<T> 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 <typename T>
@@ -109,7 +107,7 @@ std::expected<T, StackErr> Stack<T>::pop() {
this->cap = new_capacity;
}
return std::expected(return_val);
return return_val;
}
template <typename T>
@@ -119,7 +117,7 @@ std::expected<T, StackErr> Stack<T>::peek() {
return std::unexpected(StackErr::empty);
}
return std::expected(this->data[this->len - 1]);
return this->data[this->len - 1];
}
template <typename T>
@@ -129,15 +127,15 @@ void Stack<T>::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");
}
}
}

14
include/utils.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef UTILS_H
#define UTILS_H
#include <stddef.h>
#include <stdio.h>
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 // !

View File

@@ -1,7 +1,92 @@
#include "stack"
#include "stack.h"
#include "utils.h"
#include <cstdlib>
#include <print>
void menu(Stack<int> stack);
int main(void) {
std::print("hola");
Stack<int> stack = {};
menu(stack);
return 0;
}
void menu(Stack<int> 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;
}
}
}
}

129
src/utils.cpp Normal file
View File

@@ -0,0 +1,129 @@
#include "utils.h"
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
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 <windows.h>
Sleep(s * 1000);
#else
// #include <unistd.h>
// 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<char *>(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;
}