## C Program to Build Long Division Calculator With Long Remainder on Command Line

/*Assumptions:
1. the degree of the numenator is higher than or equal to degree of the denaminator
2. this program work for the degrees up to 5.
---------------------------------------------------------------------------------
Consider the function as an array the i-th index is the power of X in the funcion.
for example:  f(x) = 3X^5 - 4X^2 + 15
We represent it : 15 0 -4 0  0 3
the index 0 contains 15 which is the coeff of X^0
the index 2 contains -4 which is the coeff of X^2
the index 5 contains 3 which is the coeff of X^5
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

char Sign(int a) {
if (a < 0)
return '-';
else if (a > 0)
return '+';
}

int Degree(int* function, int size) {
int degree = 0;

for (int i = size - 1; i >= 0; i--) {

//If the coeff not equal 0 for the largest degree return the degree (i is the degree).

if (function[i] != 0) {
degree = i;
break;
}

}
return degree;
}

void PrintFunction(int* input, int inputSize) {

// print the given function.
for (int i = inputSize - 1; i >= 0; i--) {

if (input[i] != 0) {/*Constant*/

// positive constant we will print the Sign.
if (i == 0 && input[i] > 0)
printf("%c%d", Sign(input[i]), input[i]);

// negative constant we won't print the Sign.
else if (i == 0 && input[i] < 0)
printf("%d", input[i]);

else if (i != 0) {/*Xs*/

// coff and power = 1
if (i == 1 && input[i] == 1)
printf("%cX" , Sign(input[i]));

// power = 1 , coeff positive
else if (i == 1 && input[i] > 0 )
printf("%c%dX", Sign(input[i]) ,input[i]);

// power = 1 , coeff negative less than -1
else if (i == 1 && input[i] < -1)
printf("%dX", input[i]);

// power = 1 , coeff negative = -1
else if (i == 1 && input[i] == -1)
printf("%cX", Sign(input[i]));

// power not equal 1,  but coeff = 1
else if (i != 1 && input[i] == 1)
printf("%cX^%d",Sign(input[i]), i);

// power not equal 1 , coeff positive
else if (i != 1 && input[i] > 0)
printf("%c%dX^%d",Sign(input[i]) ,input[i], i);

// power not equal 1 , coeff nigative
else if (i != 1 && input[i] < 0)
printf("%dX^%d", input[i], i);
}
}

else if (input[i] == 0)
continue;
}
return;
}

void PrintResult(int* result, int resultSize, int* numenator, int numenatorSize, int* denaminator, int denaminatorSize) {

PrintFunction(result, resultSize);
printf(" + [(");
PrintFunction(numenator, numenatorSize);
printf(") / (");
PrintFunction(denaminator, denaminatorSize);
printf(")]");
}

void TakeInputAndCheck(int* numenator, int numenatorSize, int* denaminator, int denamenatorSize) {

printf("\nNUMENATOR INSERTION\n--------------------\nAssume the format ( aX^5 + bX^4 + cX^3 + dX^2 + eX + f ) \nEnter a , b , c , d , e and f separated by space :  ");
int _1 =  scanf("%d %d %d %d %d %d", &numenator[5], &numenator[4], &numenator[3], &numenator[2], &numenator[1], &numenator[0]);

printf("\nDENAMINATOR INSERTION\n----------------------\nAssume the format ( aX^5 + bX^4 + cX^3 + dX^2 + eX + f ) \nEnter a , b , c , d , e and f separated by space : ");
int _2 = scanf("%d %d %d %d %d %d", &denaminator[5], &denaminator[4], &denaminator[3], &denaminator[2], &denaminator[1], &denaminator[0]);

printf("\n\n============================================================================\n\n");
printf("The Functions you entered is : ");
printf("(");
PrintFunction(numenator, numenatorSize);
printf(") / (");
PrintFunction(denaminator, denamenatorSize);
printf(")");
}

void main() {

printf("For this program to work properly you need to satisfy the following ASSUMPTIONS:\n1. The degree of the numenator is higher than or equal to the degree of the denaminator\n2. The maximum degree of numenator or denaminator is 5.\n");

int Numenator[10] = { 0 }; int NumenatorSize = sizeof(Numenator) / sizeof(int);
int Denaminator[10] = { 0 }; int DenaminatorSize = sizeof(Denaminator) / sizeof(int);
int Result[10] = { 0 }; int ResultSize = sizeof(Result) / sizeof(int);

TakeInputAndCheck (Numenator, NumenatorSize, Denaminator, DenaminatorSize);

int NumenatorDegree = Degree(Numenator, NumenatorSize);
int DenaminatorDegree = Degree(Denaminator, DenaminatorSize);

if (NumenatorDegree >= DenaminatorDegree) {

//Evaluate the functions.
while (NumenatorDegree >= DenaminatorDegree) {

//Initialize the temporary arrays every loop to clear them.
int TempNumenator[10] = { 0 };
int TempResult[10] = { 0 };

//Get the position(degree) of the current Result element.
int position = NumenatorDegree - DenaminatorDegree;

//Add the Result of devision of numenator by denaminator element.
TempResult[position] = Numenator[NumenatorDegree] / Denaminator[DenaminatorDegree];
int TempResultdegree = Degree(TempResult, ResultSize);

//Multiply Result by Denaminator
for (int i = DenaminatorDegree; i >= 0; i--) {
TempNumenator[TempResultdegree + i] = TempResult[TempResultdegree] * Denaminator[i];
}

//Subtract TempNumenator from Numenator
for (int i = 0; i < NumenatorSize; i++) {

Numenator[i] -= TempNumenator[i];
}

//Refresh the numenator degree to enter the new loop or break.
NumenatorDegree = Degree(Numenator, NumenatorSize);

//Copy the TempResult into Result befor it Initialized againg in the new loop.
for (int i = 0; i < ResultSize; i++) {
Result[i] += TempResult[i];
}

}

printf("\n\n============================================================================\n\n");
printf("The Result is: ");
PrintResult(Result, ResultSize, Numenator, NumenatorSize, Denaminator, DenaminatorSize);
printf("\n\n============================================================================\n\n");

}

else if (NumenatorDegree < DenaminatorDegree) {
printf("\n\nYou entered non divisible Function [Numenator degree less than Denaminator degree].\n\n");

}

}