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);
|
|
|
|
|
|
|
|
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-20 14:38:03 +00:00
|
|
|
|
|
|
|
//TODO: find n for which sum function breaks. Explain what is happening.
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
int n = 5;
|
|
|
|
test_array_functions(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-09-21 07:59:27 +00:00
|
|
|
test_toolbox(argc, argv);
|
|
|
|
}
|