-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
74 lines (49 loc) · 1.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <cstdlib>
int gcd (int a, int b) ;
struct fraction
{
int nominator;
int denominator ;
bool is_correct()
{
return (denominator!= 0) && ( abs(nominator) < abs(denominator) ) ;
}
void shorten()
{
int d = gcd (nominator , denominator) ;
nominator = nominator / d ;
denominator = denominator / d ;
}
};
int main(int argc, char **argv)
{
int n;
n = atoi (argv[1]);
srand(time(NULL)); //uruchomienie czasu
fraction *fractions = new fraction [n] ;
for(int i =0 ; i < n ; i++)
{
do{
fractions[i] = { rand() % 19 -9 , rand() % 19 -9 } ;
}while(fractions[i].is_correct() == 0 ) ;
fractions[i].shorten();
}
for (int i = 0; i < n; assert(fractions[i++].is_correct()))
printf("[%i] %2i / %2i\n",
i,
fractions[i].nominator,
fractions[i].denominator);
delete[] fractions ;
}
int gcd (int a, int b)
{
do
{
if(a>b) a=a-b;
else b=b-a;
} while(a != b);
return a ;
}