forked from mk5135795/lab2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (69 loc) · 1.33 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (69 loc) · 1.33 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
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
int gcd (int a, int b);
struct fraction
{
int nominator, denominator;
bool is_correct()
{
return (abs(nominator) < abs(denominator) && denominator != 0) ? true : false;
}
void shorten()
{
int gcd_buff = gcd(nominator, denominator);
nominator /= gcd_buff;
denominator /= gcd_buff;
//kwestia kosmetyczna, lepiej wygląda '-' w liczniku
if(denominator < 0)
{
nominator *= -1;
denominator *= -1;
}
}
};
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Nieprawidłowa liczba argumentów.\nUżycie: ./lab2 ilość_ułamków");
return 1;
}
int n = atoi(argv[1]);
if(!(n >0))
{
printf("Ilość ułamków musi być > 0");
return 2;
}
fraction *fractions;
fractions = new fraction[n];
srand(time(NULL));
for (int i = 0; i < n; i++)
{
do
{
fractions[i].nominator = -9 + rand()%19;
fractions[i].denominator = -9 + rand()%19 ;
}while(!(fractions[i].is_correct()));
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;
}
//Algorytm Euklidesa wykorzystujący funkcję modulo
int gcd(int a, int b)
{
int c;
while(b != 0)
{
c = a%b;
a = b;
b = c;
}
return a;
}