ceesort/src/ceesort.c

136 lines
3.2 KiB
C
Raw Permalink Normal View History

2022-05-26 13:34:12 +01:00
/*
* Filename: ceesort.c
* Author(s): Roland (r.weirhowell@gmail.com)
* Description: Main file to handle I/O and calling fucntions
* License: MIT (https://spdx.org/licenses/MIT.html)
*/
2022-04-01 10:34:34 +01:00
#include "bubble.h"
2022-03-18 10:44:20 +00:00
#include "bsrch.h"
2022-05-29 20:01:58 +01:00
#include "lsrch.h"
2022-05-26 13:34:12 +01:00
#include "ccolour/colour.h"
2022-03-18 10:44:20 +00:00
2022-03-26 13:30:43 +00:00
#include <ctype.h>
#include <stdbool.h>
2022-03-18 10:44:20 +00:00
#include <stdio.h>
#include <stdlib.h>
2022-05-26 15:29:57 +01:00
#include <string.h>
2022-03-18 10:44:20 +00:00
2022-05-29 20:01:58 +01:00
static inline void quit_err(const char* msg)
2022-05-26 13:34:12 +01:00
{
ChangeColour(msg, RED_FOREGROUND, DEFAULT_COLOR, true);
exit(EXIT_FAILURE);
}
2022-03-26 13:30:43 +00:00
bool isint(char* s)
{
for (int i = 0; s[i]; i++)
{
if (s[i] == '-' && isdigit(s[i + 1])) continue;
if (!isdigit(s[i])) return false;
}
2022-04-01 10:34:34 +01:00
2022-03-26 13:30:43 +00:00
return true;
}
2022-03-18 10:44:20 +00:00
int main(int argc, char** argv)
{
if (argc < 2)
2022-05-26 13:34:12 +01:00
quit_err("ERROR: Must have at least one argument (number)\n");
2022-03-18 10:44:20 +00:00
int* arr = malloc((argc - 1) * sizeof(int));
for (int i = 1; i < argc; i++)
{
2022-03-26 13:30:43 +00:00
if (!isint(argv[i]))
{
puts("Sorry but arguments must be integers");
return 1;
}
2022-03-18 10:44:20 +00:00
arr[i - 1] = atoi(argv[i]);
}
2022-04-01 10:34:34 +01:00
int n = argc - 1;
2022-03-18 10:44:20 +00:00
2022-05-26 15:29:57 +01:00
/*
* The line:
* buffer[strcspn(buffer, "\n")] = 0;
* Strips trailing newline, so "2\n" becomes "2"
*/
2022-04-01 10:34:34 +01:00
char choice_str[16];
printf(
"1. Binary search\n"
"2. Bubble sort\n"
2022-05-29 21:17:44 +01:00
"3. Liniar search\n"
2022-04-01 10:34:34 +01:00
"Please pick a mode of operation: "
);
2022-05-26 15:29:57 +01:00
fgets(choice_str, 16, stdin);
choice_str[strcspn(choice_str, "\n")] = 0;
2022-04-26 12:42:14 +01:00
// TODO: Add checking that number is in range
2022-04-01 10:34:34 +01:00
while (!isint(choice_str))
2022-03-18 10:44:20 +00:00
{
2022-05-29 20:01:58 +01:00
printf("Sorry but you must enter a number between 1 and 3\n");
2022-04-01 10:34:34 +01:00
printf("Try again: ");
2022-05-26 15:29:57 +01:00
fgets(choice_str, 16, stdin);
choice_str[strcspn(choice_str, "\n")] = 0;
2022-03-18 10:44:20 +00:00
}
2022-04-01 10:34:34 +01:00
int choice = atoi(choice_str);
if (choice == 1)
{
char target_str[16];
printf("Enter number to search for: ");
2022-05-26 15:29:57 +01:00
fgets(target_str, 16, stdin);
target_str[strcspn(target_str, "\n")] = 0;
2022-04-01 10:34:34 +01:00
while (!isint(target_str))
{
printf("Sorry but you must enter a whole number, try again: ");
2022-05-26 15:29:57 +01:00
fgets(target_str, 16, stdin);
2022-04-01 10:34:34 +01:00
}
int target = atoi(target_str);
2022-03-26 13:30:43 +00:00
2022-04-01 10:34:34 +01:00
int result = bsrch(target, arr, n);
if (result == -1)
puts("Value not found");
else
printf("Value found at index: %d\n", result);
}
else if (choice == 2)
{
bubblesort(arr, n);
2022-04-26 12:42:14 +01:00
2022-04-01 10:34:34 +01:00
printf("Here is your sorted list:\n");
for (int i = 0; i < n; i++)
{
printf("%d", arr[i]);
if (i < n - 1) printf(", ");
}
2022-05-18 17:24:19 +01:00
putchar('\n');
2022-04-01 10:34:34 +01:00
}
2022-05-29 20:01:58 +01:00
else if (choice == 3)
{
2022-05-29 21:17:44 +01:00
char target_str[16];
2022-05-29 20:01:58 +01:00
printf("Enter number to search for: ");
2022-05-29 21:17:44 +01:00
fgets(target_str, 16, stdin);
target_str[strcspn(target_str, "\n")] = 0;
2022-05-29 20:01:58 +01:00
while (!isint(target_str))
2022-05-29 21:17:44 +01:00
{
printf("Sorry but you must enter a whole number, try again: ");
fgets(target_str, 16, stdin);
2022-05-29 20:01:58 +01:00
}
2022-05-29 21:17:44 +01:00
int target = atoi(target_str);
int result = lsrch(arr, n, target);
if (result == -1)
puts("Value not found");
else
printf("Value found at index: %d\n", result);
2022-05-29 20:01:58 +01:00
}
2022-03-26 13:30:43 +00:00
free(arr);
2022-03-18 10:44:20 +00:00
return 0;
}