Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated code style #1

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 99 additions & 98 deletions repetitionFinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,118 +2,119 @@
#include <stdlib.h>
#include <string.h>

int checkTwoFive(int num){//checks if the number is in 2^a * 5^b form
while(num%5==0 && num>0){//divides until can no more be divided
num /= 5;
}
while(num%2==0 && num>0){//divides until can no more be divided
num /= 2;
}
return (num==1 ? 1: 0); //1 if valid, 0 if not
//checks if the number is in 2^a * 5^b form
int checkTwoFive(int num) {
while (num % 5 == 0 && num > 0) { //divides until can no more be divided
num /= 5;
}
while (num % 2 == 0 && num > 0) { //divides until can no more be divided
num /= 2;
}
return (num == 1 ? 1 : 0); //1 if valid, 0 if not
}

int lengthOfRepetition(int n){ //finds the length of the repetition
int value=1 ,length=0;
while(n%2==0 && n>0){
n /= 2;
}
while(n%5==0 && n>0){//makes sure not divisable by 2 or 5
n /= 5;
}

do{
value = value *10;
value = value % n;
length++;
}while (value != 1); //checks after how many steps 'value' returns to what it started as
//finds the length of the repetition
int lengthOfRepetition(int n) {
int value = 1, length = 0;
while (n % 2 == 0 && n > 0) {
n /= 2;
}
while (n % 5 == 0 && n > 0) { //makes sure not divisable by 2 or 5
n /= 5;
}

do {
value = value * 10;
value = value % n;
length++;
} while (value != 1); //checks after how many steps 'value' returns to what it started as
return length;
}

void Cosimplify(unsigned long *fraction){ //simply den and num until they are co-primes
for(int i=2;i<=fraction[0]/2;i++){ //fraction[0] is the numerator. assume that den is always bigger
while(fraction[0]%i==0 && fraction[1]%i==0){
fraction[0] /= i;
fraction[1] /= i;
}
}
void Cosimplify(unsigned long *fraction) { //simply den and num until they are co-primes
for (int i = 2; i <= fraction[0] / 2; i++) { //fraction[0] is the numerator. assume that den is always bigger
while (fraction[0] % i == 0 && fraction[1] % i == 0) {
fraction[0] /= i;
fraction[1] /= i;
}
}
}

int findRepetition(char *number, int lenRep){//takes the decimal point string and length of repetition as input. returns the index the repetition starts
int match;//stores number of same chars
for(int i=0;i<lenRep*3;i++){ //because of the dot character, non-decimal points will never be mistaken
match=0;
for(int j=0;j<lenRep;j++){//check the series for each digit
if(number[i+j]==number[i+j+lenRep]){ //check if repeats
match++;
}
}
if(match==lenRep){//if all the repeating digits were present.
return i; //return the index repetition starts
}
}
return -1; //there is a problem (no repetition?)
//takes the decimal point string and length of repetition as input. returns the index the repetition starts
int findRepetition(char *number, int lenRep) {
int match; //stores number of same chars
for (int i = 0; i < lenRep * 3; i++) { //because of the dot character, non-decimal points will never be mistaken
match = 0;
for (int j = 0; j < lenRep; j++) { //check the series for each digit
if (number[i + j] == number[i + j + lenRep]) { //check if repeats
match++;
}
}
if (match == lenRep) { //if all the repeating digits were present.
return i; //return the index repetition starts
}
}
return -1; //there is a problem (no repetition?)
}

int makeDenBigger(unsigned long *fraction){//if the num is bigger than den
int nonDecimal = fraction[0]/fraction[1];
fraction[0] %= fraction[1];
return nonDecimal;
int makeDenBigger(unsigned long *fraction) { //if the num is bigger than den
int nonDecimal = fraction[0] / fraction[1];
fraction[0] %= fraction[1];
return nonDecimal;
}

void markRepetition(char *number, int index, int length){//formats the string for printing
if(index==-1){ //in some cases where length==1, index may not be found.
index=strlen(number)-1;
}
for(int i=length;i>0;i--){
number[i+index] = number[i+index-1];
}
number[index] = '[';
number[length+index+1] = ']';
number[length+index+2] = '\0'; //dont read beyond this
void markRepetition(char *number, int index, int length) { //formats the string for printing
if (index == -1) { //in some cases where length==1, index may not be found.
index = strlen(number) - 1;
}
for (int i = length; i > 0; i--) {
number[i + index] = number[i + index - 1];
}
number[index] = '[';
number[length + index + 1] = ']';
number[length + index + 2] = '\0'; //dont read beyond this
}

void longDivision(unsigned long *fraction, char* number, int lenRep){//applies long division and puts results in a char array
int remainder = 0,num = fraction[0],den = fraction[1],tracker;
//applies long division and puts results in a char array
void longDivision(unsigned long *fraction, char *number, int lenRep) {
int remainder = 0, num = fraction[0], den = fraction[1], tracker;

for(int i=0;i<lenRep*3;i++){
tracker=0;
while(num<den){
num *= 10;
if(tracker){
number[i]= 0 + '0';
i++;
}
tracker++;
}
remainder = num %den;
number[i] = num/den + '0';
num = remainder;
}
number[lenRep*3] = '\0';
for (int i = 0; i < lenRep * 3; i++) {
tracker = 0;
while (num < den) {
num *= 10;
if (tracker) {
number[i] = 0 + '0';
i++;
}
tracker++;
}
remainder = num % den;
number[i] = num / den + '0';
num = remainder;
}
number[lenRep * 3] = '\0';
}

int main(int argc, char **argv)
{
unsigned long fraction[2]; //to hold num and den
int nonDecimal =0; //hold the non-decimal part
int lenRep; //length of repetition
printf("Please enter the numerator and the denominator respectively seperated by a space\n");
scanf("%lu %lu", &fraction[0],&fraction[1]);
if(fraction[0]>fraction[1]){ //if fraction >1
nonDecimal = makeDenBigger(fraction);
}
Cosimplify(fraction); //Simplify everything
if(checkTwoFive(fraction[1])){ //if true, there is no repetition
printf("%g\n", nonDecimal + (double)fraction[0]/(double)fraction[1]);
}else{ //there is repetition
lenRep = lengthOfRepetition(fraction[1]);
char *number = malloc(lenRep*3*sizeof(char)); //3 times the length of repetition just to be safe
longDivision(fraction,number,lenRep);
markRepetition(number,findRepetition(number,lenRep),lenRep);
printf("%d.%s\n",nonDecimal,number);
free(number);
}

return 0;
int main(int argc, char **argv) {
unsigned long fraction[2]; //to hold num and den
int nonDecimal = 0; //hold the non-decimal part
int lenRep; //length of repetition
printf("Please enter the numerator and the denominator respectively seperated by a space\n");
scanf("%lu %lu", &fraction[0], &fraction[1]);
if (fraction[0] > fraction[1]) { //if fraction >1
nonDecimal = makeDenBigger(fraction);
}
Cosimplify(fraction); //simplify everything
if (checkTwoFive(fraction[1])) { //if true, there is no repetition
printf("%g\n", nonDecimal + (double) fraction[0] / (double) fraction[1]);
} else { //there is repetition
lenRep = lengthOfRepetition(fraction[1]);
char *number = malloc(lenRep * 3 * sizeof(char)); //3 times the length of repetition just to be safe
longDivision(fraction, number, lenRep);
markRepetition(number, findRepetition(number, lenRep), lenRep);
printf("%d.%s\n", nonDecimal, number);
free(number);
}
return 0;
}