#include #include #include #include #include "rational.hpp" using std::cout; using std::endl; using std::setw; using std::istringstream; using std::ostringstream; using std::string; using cs427_527::Rational; int main(int argc, char *argv[]) { Rational pi{22, 7}; Rational zero{}; Rational one{1}; Rational half{std::string("1/2")}; Rational third{"1/3"}; Rational sum = half + third; half.print(cout); cout << " + "; third.print(cout); cout << " = "; sum.print(cout) << endl; sum += one; cout << "adding one: " << sum << endl; // 1 automatically converted to Rational via Rational::Rational(int) // can (and maybe should) turn off by marking that constructor explicit sum += 1; cout << "adding one again: " << sum << endl; // the following requires multiple conversions, so doesn't work //sum += "2/10"; //cout << "after adding 1 and 2/10: " << setw(16) << sum << endl; int x = pi; cout << "pi is approx " << x << endl; // compiler won't choose between converting lhs via Rational::operator int // to int and doing int+int or converting rhs to Rational via // Rational::Rational(int) and doing Rational+Rational // cout << sum + 1 << endl; try { Rational undef{1, 0}; } catch (const std::domain_error& e) { cout << e.what() << endl; } catch (const std::out_of_range& e) { cout << "this won't happen, but you can handle different types of\n" << "excpetions with multiple catch blocks" << endl; } catch (...) { cout << "or just any exception with ... (but then refer to the\n" << "exception to get the message inside, for example" << endl; } }