Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
quadroli authored May 3, 2021
1 parent bf3f774 commit ddfec28
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sequencer: sequencer.c
gcc sequencer.c -O3 -o sequencer -ansi
29 changes: 29 additions & 0 deletions colors.h
Original file line number Diff line number Diff line change
@@ -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 <windows.h>
#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*/
127 changes: 127 additions & 0 deletions sequencer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <stdio.h>
#include <stdlib.h>

#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;
}

0 comments on commit ddfec28

Please sign in to comment.