-
Notifications
You must be signed in to change notification settings - Fork 0
/
deciCalc.c
88 lines (67 loc) · 1.37 KB
/
deciCalc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef char *String;
typedef float Deci;
void getInput(String a);
int getDecimalPlace(int decimalLength)
{
printf("%d", decimalLength);
return pow(10, decimalLength);
}
int getDecimalLength(String a)
{
int x;
int len = strlen(a);
printf("\n");
for (x = len - 1; x >= 0 && a[x] == '0'; x--)
{
}
return x;
}
int findGCF(int a, int b)
{
int r;
do
{
r = a % b;
a = b;
b = r;
} while (r > 0);
return a;
}
void convertToFraction(Deci a, int decimalLength)
{
int fraction[2], GCF;
unsigned int place = getDecimalPlace(decimalLength);
fraction[0] = a * place;
fraction[1] = 1 * place;
GCF = findGCF(fraction[0], fraction[1]);
fraction[0] /= GCF;
fraction[1] /= GCF;
printf("\n%.*f -> %d/%d", decimalLength, a, fraction[0], fraction[1]);
}
int main()
{
char num[100] = "1.5186262650";
String decimal, str;
int ret;
ret = strtol(num, &decimal, 10);
Deci realVal = strtod(num, &str);
int decimalLength = getDecimalLength(decimal);
convertToFraction(realVal, decimalLength);
// scanf("%s", num);
// getInput(num);
return 0;
}
void getInput(String a)
{
String number, decimal;
int negative = 0;
number = strtok(a, ".");
decimal = strtok(NULL, ".");
if (number[0] == '-')
negative = 1;
printf("%s %s", number, decimal);
}