Loading lang_cpp_ex_0102...
struct Fraction { int numer, denom; };
struct Interval { double low, high; };
Fraction add(Fraction f, int i);
Fraction add(Fraction f, int i) { return Fraction{f.numer+i*f.denom, f.denom}; // (a/b)+c = (a+c*b)/b }
void test_add() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; // ...test add() with a Fraction and an integer... // ...test add() with an Interval and a real... // ...test + with a Fraction and an integer... // ...test + with an Interval and a real... }
struct Fraction { int numer, denom; };
struct Interval { double low, high; };
Fraction multiply(Fraction f, int i);
Fraction multiply(Fraction f, int i) { return Fraction{f.numer*i, f.denom}; // (a/b)*c = (a*c)/b }
void test_multiply() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; // ...test multiply() with a Fraction and an integer... // ...test multiply() with an Interval and a real... // ...test * with a Fraction and an integer... // ...test * with an Interval and a real... }
struct Interval { double low, high; };
struct Fraction { int numer, denom; };
Interval divide(Interval i, double d);
Interval divide(Interval i, double d) { return Interval{i.low, i.low+(i.high-i.low)/d}; // reduce range }
void test_divide() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; // ...test divide() with an Interval and a real... // ...test divide() with a Fraction and an integer... // ...test / with an Interval and a real... // ...test / with a Fraction and an integer... }
//----------------------------------------------------------------------------
#ifndef BASICS_OVERLOAD_HPP #define BASICS_OVERLOAD_HPP
namespace basics {
struct Interval { double low, high; };
struct Fraction { int numer, denom; };
struct Duration { int min, sec; };
Fraction add(Fraction f, int i);
Interval add(Interval i, double d);
Fraction operator+(Fraction lhs, int rhs);
Interval operator+(Interval lhs, double rhs);
Fraction multiply(Fraction f, int i);
Interval multiply(Interval i, double d);
Fraction operator*(Fraction lhs, int rhs);
Interval operator*(Interval lhs, double rhs);
Interval divide(Interval i, double d);
Fraction divide(Fraction f, int i);
Interval operator/(Interval lhs, double rhs);
Fraction operator/(Fraction lhs, int rhs);
} // namespace basics
#endif // BASICS_OVERLOAD_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "overload.hpp"
namespace basics {
Fraction add(Fraction f, int i) { return Fraction{f.numer+i*f.denom, f.denom}; // (a/b)+c = (a+c*b)/b }
Interval add(Interval i, double d) { return Interval{i.low+d, i.high+d}; }
Fraction operator+(Fraction f, int i) { return add(f, i); }
Interval operator+(Interval lhs, double rhs) { return add(lhs, rhs); }
Fraction multiply(Fraction f, int i) { return Fraction{f.numer*i, f.denom}; // (a/b)*c = (a*c)/b }
Interval multiply(Interval i, double d) { return Interval{i.low*d, i.high*d}; }
Fraction operator*(Fraction lhs, int rhs) { return multiply(lhs, rhs); }
Interval operator*(Interval lhs, double rhs) { return multiply(lhs, rhs); }
Interval divide(Interval i, double d) { return Interval{i.low, i.low+(i.high-i.low)/d}; // reduce range }
Fraction divide(Fraction f, int i) { return Fraction{f.numer, f.denom*i}; // (a/b)/c = a/(b*c) }
Interval operator/(Interval lhs, double rhs) { return divide(lhs, rhs); }
Fraction operator/(Fraction lhs, int rhs) { return divide(lhs, rhs); }
} // namespace basics
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "overload.hpp"
#include <iostream>
void test_add() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const basics::Fraction f1={2, 5}; std::cout << "f1: " << f1.numer << '/' << f1.denom << '\n'; const basics::Interval i1={2.0, 5.0}; std::cout << "i1: " << i1.low << ';' << i1.high << '\n'; const auto f2=add(f1, 10); std::cout << "f2: " << f2.numer << '/' << f2.denom << '\n'; const auto i2=add(i1, 10.0); std::cout << "i2: " << i2.low << ';' << i2.high << '\n'; const auto f3=f2+3; std::cout << "f3: " << f3.numer << '/' << f3.denom << '\n'; const auto i3=i2+3.0; std::cout << "i3: " << i3.low << ';' << i3.high << '\n'; }
void test_multiply() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const basics::Fraction f1={2, 5}; std::cout << "f1: " << f1.numer << '/' << f1.denom << '\n'; const basics::Interval i1={2.0, 5.0}; std::cout << "i1: " << i1.low << ';' << i1.high << '\n'; const auto f2=multiply(f1, 10); std::cout << "f2: " << f2.numer << '/' << f2.denom << '\n'; const auto i2=multiply(i1, 10.0); std::cout << "i2: " << i2.low << ':' << i2.high << '\n'; const auto f3=f2*3; std::cout << "f3: " << f3.numer << '/' << f3.denom << '\n'; const auto i3=i2*3.0; std::cout << "i3: " << i3.low << ';' << i3.high << '\n'; }
void test_divide() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const basics::Interval i1={2.0, 5.0}; std::cout << "i1: " << i1.low << ';' << i1.high << '\n'; const basics::Fraction f1={2, 5}; std::cout << "f1: " << f1.numer << '/' << f1.denom << '\n'; const auto i2=divide(i1, 10.0); std::cout << "i2: " << i2.low << ':' << i2.high << '\n'; const auto f2=divide(f1, 10); std::cout << "f2: " << f2.numer << '/' << f2.denom << '\n'; const auto i3=i2/3.0; std::cout << "i3: " << i3.low << ';' << i3.high << '\n'; const auto f3=f2/3; std::cout << "f3: " << f3.numer << '/' << f3.denom << '\n'; }
int main() { test_add(); test_multiply(); test_divide(); return 0; }
//----------------------------------------------------------------------------