-
Notifications
You must be signed in to change notification settings - Fork 0
/
pRacionais1.c
105 lines (81 loc) · 1.87 KB
/
pRacionais1.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Arquivo fonte do programa inicial que opera sobre numeros racionais,
com as definicoes de tipo de dados e subrotinas.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct {int num, den;} TRac;
TRac SomaRac(TRac, TRac);
TRac SubtraiRac(TRac, TRac);
TRac MultRac(TRac, TRac);
TRac DivRac(TRac, TRac);
TRac SimplRac(TRac);
int mdc(int, int);
int main()
{ TRac X, Y, R, b;
printf("Informe X: ");
scanf("%d/%d", &X.num, &X.den);
printf("Informe Y: ");
scanf("%d/%d", &Y.num, &Y.den);
R = SomaRac(X, Y);
printf("\n%d/%d + %d/%d = %d/%d\n", X.num, X.den,
Y.num, Y.den,
R.num, R.den);
R = SubtraiRac(X, Y);
printf("\n%d/%d - %d/%d = %d/%d\n", X.num, X.den,
Y.num, Y.den,
R.num, R.den);
R = MultRac(X, Y);
printf("\n%d/%d * %d/%d = %d/%d\n", X.num, X.den,
Y.num, Y.den,
R.num, R.den);
R = DivRac(X, Y);
printf("\n%d/%d / %d/%d = %d/%d\n", X.num, X.den,
Y.num, Y.den,
R.num, R.den);
b = R;
R = SimplRac(b);
printf("\n%d/%d = %d/%d\n", b.num, b.den,
R.num, R.den);
return 0;
}
TRac SomaRac(TRac n1, TRac n2)
{ TRac res;
res.num = n1.num * n2.den + n2.num * n1.den;
res.den = n1.den * n2.den;
return res;
}
TRac SubtraiRac(TRac n1, TRac n2)
{ TRac res;
res.num = n1.num * n2.den - n2.num * n1.den;
res.den = n1.den * n2.den;
return res;
}
TRac MultRac(TRac n1, TRac n2)
{ TRac res;
res.num = n1.num * n2.num;
res.den = n1.den * n2.den;
return res;
}
TRac DivRac(TRac n1, TRac n2)
{ TRac res;
res.num = n1.num * n2.den;
res.den = n2.num * n1.den;
return res;
}
TRac SimplRac(TRac n1)
{ TRac res;
res.num = n1.num / mdc(n1.num, n1.den);
res.den = n1.den / mdc(n1.num, n1.den);
return res;
}
int mdc(int m, int n)
{ if (m < 0) m = -m;
if (n < 0) n = -n;
if (m % n == 0)
return n;
else
return mdc(n, m % n);
}