forked from taniadovzhenko/CppPracticum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratio.c
More file actions
95 lines (62 loc) · 1.56 KB
/
ratio.c
File metadata and controls
95 lines (62 loc) · 1.56 KB
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
#include <stdio.h>
#include <stdbool.h>
// Rational 11.3 (4.0.3)
typedef struct {
int numerator; // чисельник
unsigned int denominator; // знаменник
} Rational;
int input(Rational* x){
scanf(" %d",&x->numerator);
scanf(" %u",&x->denominator);
return 0;
}
void vyvod(const Rational x){
printf("\nR= %d / %u",x.numerator,x.denominator);
}
Rational add(const Rational a, const Rational b){
Rational c;
c.numerator = (int)(a.numerator * b.denominator) + (int)(b.numerator * a.denominator);
c.denominator = a.denominator * b.denominator;
return c;
}
Rational mul(const Rational a, const Rational b){
Rational c;
c.numerator = a.numerator * b.numerator ;
c.denominator = a.denominator * b.denominator;
return c;
}
bool cmp(const Rational a, const Rational b){
return a.numerator*b.denominator > b.numerator*a.denominator;
}
unsigned gcd(unsigned a, unsigned b){
if(a==0) return b;
if(b==0) return a;
if(a>b) return gcd(b,a%b);
else return gcd(a,b%a);
}
Rational reduce(const Rational x){
unsigned d=gcd(x.numerator, x.denominator);
Rational z;
z.numerator = (int)x.numerator / d;
z.denominator = x.denominator /d;
return z;
}
void reduce1 (Rational* x){
unsigned d=gcd(x->numerator, x->denominator);
Rational z;
x->numerator /= d;
x->denominator /= d;
}
int main(){
Rational x,y,z;
input(&x);
vyvod(x);
input(&y);
vyvod(y);
z = add(x,y);
reduce1(&z);
vyvod(z);
Rational z1= mul(x,y);
vyvod(reduce(z1));
printf("\nz>z1==%d",cmp(z,z1));
}