2022-09-20 14:38:03 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "fraction_toolbox.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// read command line arguments
|
|
|
|
static void readcmdline(fraction & frac, int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (argc!=3)
|
|
|
|
{
|
|
|
|
printf("Usage: n d\n");
|
|
|
|
printf(" n numerator of fraction\n");
|
|
|
|
printf(" d denominator of fraction\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read n
|
|
|
|
frac.num = atoi(argv[1]);
|
|
|
|
|
|
|
|
// read d
|
|
|
|
frac.denom = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test23467(int argc, char* argv[])
|
|
|
|
{
|
2022-09-21 07:59:27 +00:00
|
|
|
fraction frac;
|
|
|
|
readcmdline(frac, argc, argv);
|
|
|
|
|
|
|
|
cout << "#2: squared fraction" << endl;
|
|
|
|
print_fraction(square_fraction(frac));
|
|
|
|
|
|
|
|
cout << "#3: squared fraction in-place" << endl;
|
|
|
|
fraction frac2 = frac;
|
|
|
|
square_fraction_inplace(frac2);
|
|
|
|
print_fraction(frac2);
|
|
|
|
|
|
|
|
cout << "#4: fraction to double" << endl;
|
|
|
|
cout << fraction2double(frac) << endl;
|
|
|
|
|
|
|
|
cout << "#6: gcd of fraction" << endl;
|
|
|
|
cout << gcd(frac) << endl;
|
|
|
|
|
|
|
|
cout << "#7: fraction reduction in-place" << endl;
|
|
|
|
fraction frac3 = frac;
|
|
|
|
reduce_fraction_inplace(frac3);
|
|
|
|
print_fraction(frac3);
|
2022-09-20 14:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test5()
|
|
|
|
{
|
2022-09-21 07:59:27 +00:00
|
|
|
int a, b;
|
|
|
|
cout << "#5: recursive gcd of two integers" << endl;
|
|
|
|
cout << "Input first number: ";
|
|
|
|
cin >> a;
|
|
|
|
cout << "Input second number: ";
|
|
|
|
cin >> b;
|
|
|
|
cout << "GCD is: " << gcd(a, b) << endl;
|
2022-09-20 14:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_array_functions(int n)
|
|
|
|
{
|
2022-09-21 07:59:27 +00:00
|
|
|
fraction* a = (fraction*) malloc(sizeof(fraction) * n);
|
|
|
|
fill_fraction_array(a, n);
|
2022-09-25 16:06:55 +00:00
|
|
|
|
2022-09-21 07:59:27 +00:00
|
|
|
cout << "Sum of array as fraction: " << endl;
|
|
|
|
print_fraction(sum_fraction_array(a, n));
|
|
|
|
|
|
|
|
cout << "Sum of array as double (approx.): " << endl;
|
|
|
|
cout << sum_fraction_array_approx(a, n) << endl;
|
2022-09-21 09:51:41 +00:00
|
|
|
|
2022-09-25 16:06:55 +00:00
|
|
|
// find n for which sum function breaks. Explain what is happening. n=1290
|
|
|
|
// this is the first number where the denominator overflows and thus
|
2022-09-21 09:51:41 +00:00
|
|
|
// erroneoulsly becomes a negative number. The overflow is caused by the
|
|
|
|
// sum_fractions function, specifically in the multiplication between
|
|
|
|
// denominators. The approximation function doesn't overflow since fractions
|
|
|
|
// are first converted into double floating point numbers, thus never
|
|
|
|
// calling the sum_fractions functions and performing dangerous fixed size
|
|
|
|
// integer multiplications. Additionally, due to the floating point nature
|
|
|
|
// of the double type, a sum of doubles will never overflow to something
|
|
|
|
// "weird", albeit precision will be lost due to the limited number of
|
|
|
|
// significant digits and for larger and larger values the sum will
|
|
|
|
// eventually reach the value "+Infinity" due to the fixed exponent size.
|
2022-09-20 14:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_toolbox(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
cout << "\n=============== test23467 =============== " << endl;
|
|
|
|
test23467(argc, argv);
|
|
|
|
|
|
|
|
cout << "\n================= test5 ================= " << endl;
|
|
|
|
test5();
|
|
|
|
|
|
|
|
cout << "\n========== test_array_functions ========= " << endl;
|
2022-09-25 16:06:55 +00:00
|
|
|
int n = 1290;
|
2022-09-20 14:38:03 +00:00
|
|
|
test_array_functions(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-09-21 07:59:27 +00:00
|
|
|
test_toolbox(argc, argv);
|
|
|
|
}
|