From ddfec2885f20fc8cbbdea22a577bf5ded3f291dc Mon Sep 17 00:00:00 2001 From: quadroli <58091631+quadroli@users.noreply.github.com> Date: Mon, 3 May 2021 17:21:24 +0300 Subject: [PATCH] added files --- Makefile | 2 + colors.h | 29 ++++++++++++ sequencer.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 Makefile create mode 100644 colors.h create mode 100644 sequencer.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..95c920f --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +sequencer: sequencer.c + gcc sequencer.c -O3 -o sequencer -ansi diff --git a/colors.h b/colors.h new file mode 100644 index 0000000..ccad472 --- /dev/null +++ b/colors.h @@ -0,0 +1,29 @@ +#ifndef COLORS +#define COLORS + +#define RED "\x1B[31m" +#define BLUE "\x1B[34m" +#define GREEN "\x1B[32m" +#define YELLOW "\x1B[33m" +#define MAGENTA "\x1B[35m" +#define DEFAULT "\x1B[39m" + +#ifdef __WIN32__ + #include + #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING + #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 + #endif + DWORD dwMode; + DWORD + enableColorsOnWindows() + { + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut == INVALID_HANDLE_VALUE) return GetLastError(); + dwMode = 0; + if (!GetConsoleMode(hOut, &dwMode))return GetLastError(); + dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (!SetConsoleMode(hOut, dwMode))return GetLastError(); + return 0; + } +#endif /*end windows if*/ +#endif /*end colors def*/ diff --git a/sequencer.c b/sequencer.c new file mode 100644 index 0000000..9aa726f --- /dev/null +++ b/sequencer.c @@ -0,0 +1,127 @@ +#include +#include + +#define MAXINPUT 3 + +#include "colors.h" +#define COLORS + +typedef enum {ARITHMETIC,GEOMETRIC,PRIME,UNKNOWN} Sequence; + +unsigned int i,position,count; +float raised,number,term,numbers[10]; + +void +getInput() +{ + printf("Kindly input " RED "first three " DEFAULT "numbers of sequence " YELLOW "below " DEFAULT "using <" GREEN "spacebar" DEFAULT "> to separate\n"); + for (i = 0; i < MAXINPUT; i++){ + if(!scanf("%f",&number)){ + printf(RED"ERROR: " DEFAULT "Unable to get input\n"); + exit(EXIT_FAILURE); + } + numbers[i] = number; + } +} + +unsigned int +checkPrime +(unsigned int number) +{ + count = 1; + for(i=2; i<=number/2; i++){ + if(number%i == 0){ + count=0; + break; + } + } + if(number == 1) count = 0; + return count; +} + +Sequence +determineSequence() +{ + numbers[MAXINPUT] = numbers[MAXINPUT - 1] - numbers[MAXINPUT - 2]; + numbers[MAXINPUT + 1] = numbers[MAXINPUT - 2] - numbers[MAXINPUT - 3]; + if (numbers[MAXINPUT] == numbers[MAXINPUT + 1])return ARITHMETIC; + numbers[MAXINPUT] = numbers[MAXINPUT - 1]/numbers[MAXINPUT - 2]; + numbers[MAXINPUT + 1] = numbers[MAXINPUT - 2]/numbers[MAXINPUT - 3]; + if (numbers[MAXINPUT] == numbers[MAXINPUT + 1])return GEOMETRIC; + if (checkPrime((unsigned int)numbers[0]))return PRIME; + else return UNKNOWN; +} + +void +prompt() +{ + printf(MAGENTA "Position " DEFAULT "of term: " BLUE); + if(!scanf("%u",&position)){ + printf(RED"ERROR: "DEFAULT " Unable to get input\n"); + exit(EXIT_FAILURE); + } + printf(DEFAULT); +} +float +raise +(float base, unsigned int exponent) +{ + raised = 1; + for (i = 0; i < exponent; i++) raised *= base; + return raised; +} + +/*=======================WIP======================= +unsigned int +primeAtN(unsigned int number,unsigned int position) +{ + i = 0; + while(i != position){ + if(checkPrime(number)){ + number++; + i++; + } + else number++; + } + return number; +} +==================================================*/ + +int +sequencer() +{ + switch (determineSequence()){ + case ARITHMETIC: + prompt(); + term = numbers[0] + (position - 1) * numbers[MAXINPUT]; + break; + + case GEOMETRIC: + prompt(); + term = numbers[0] * raise(numbers[MAXINPUT],position - 1); + break; + + case PRIME: + prompt(); + printf(RED"WIP:"DEFAULT"prime number finder not in its prime yet ;)\n"); + return 0; + + case UNKNOWN: + printf(YELLOW "Sorry," DEFAULT " could " RED "not" DEFAULT " determine type of" BLUE " sequence\n" DEFAULT); + return 0; + } + + printf(BLUE "Term " DEFAULT "is: " GREEN "%g\n" DEFAULT, term); + return 0; +} + +int +main() +{ + #ifdef __WIN32__ + enableColorsOnWindows(); + #endif + getInput(); + sequencer(); + return 0; +}