forked from mk5135795/lab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
56 lines (49 loc) · 1.18 KB
/
main.cpp
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
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
int gcd(int n, int d);
struct fraction
{
int numerator;
int denominator;
bool is_correct()
{
return denominator != 0 ? (abs(numerator) < abs(denominator) ? true : false) : false;
}
void shorten()
{
int divider = gcd(denominator, numerator);
divider != 1 ? (numerator /= divider , denominator /= divider) : divider;
denominator < 0 ? (numerator= -numerator , denominator=abs(denominator)) : denominator;
}
};
int gcd (int numerator, int denominator)
{
int pom;
while(denominator!=0)
{
pom = numerator % denominator;
numerator = denominator;
denominator = pom;
}
return numerator;
};
int main(int argc, char **argv)
{
srand(time(NULL));
int n = atoi(argv[1]);
fraction *fractions = new fraction[n];
for (int i = 0; i < n; i++)
{
do
{
fractions[i].numerator=rand()% 19 + (-9);
fractions[i].denominator=rand()% 19 + (-9);
}while(fractions[i].is_correct() != true);
assert(fractions[i].is_correct() );
fractions[i].shorten();
fractions[i].numerator == 0 ? printf(" 0 \n ") : printf("%d / %d \n", fractions[i].numerator , fractions[i].denominator);
}
delete []fractions;
}