All wroking fine, too tired to make tests, everything seems fine
This commit is contained in:
101
src/main.cpp
101
src/main.cpp
@@ -1,7 +1,106 @@
|
||||
#include "queue.h"
|
||||
#include "utils.h"
|
||||
#include <cstdlib>
|
||||
#include <print>
|
||||
|
||||
void menu(Queue<int> queue);
|
||||
|
||||
int main(void) {
|
||||
std::println("Hola");
|
||||
Queue<int> queue = {};
|
||||
|
||||
menu(queue);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void menu(Queue<int> 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
129
src/utils.cpp
129
src/utils.cpp
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user