Introduction
0.1 + 0.2 isn't 0.3 in floating point, but 1/10 + 2/10 is exactly 3/10. This project builds a rational-number type with operator overloading and class invariants (lowest terms, positive denominator), and shows how careful arithmetic — cross-cancelling, LCM-based addition, checked multiplication — avoids overflow.
Problem statement
<a> <op> <b> lines exactly. Operands may be integers, fractions (-7/4), finite decimals (0.125), bracketed mixed numbers ([2 1/3]) or ans. Support + - * /, integer powers (including negative ones), comparisons < > = and sum of a list. Show each result as a fraction, a mixed number and an approximate decimal. Reject zero denominators, division by zero, malformed numbers and results too large for 64-bit integers.Objectives
- Maintain a class invariant in every constructor and operation
- Overload arithmetic, comparison and stream operators
- Parse several textual formats into one type
- Detect integer overflow before it happens
Features
- Always-normalised fractions
- Decimal and mixed-number input
- Exponentiation by squaring, negative exponents
- Overflow and divide-by-zero detection with exceptions
Concepts demonstrated
- Class invariants
- Operator overloading
- Overflow checking
- Parsing
- Exceptions
System architecture
Fraction stores int64_t num_, den_; the constructor and every compound operator end with normalise(), which makes the denominator positive and divides by std::gcd. Addition uses the least common multiple of the denominators; multiplication cross-cancels before multiplying. checked_mul and checked_add throw std::overflow_error instead of wrapping. Comparisons reuse subtraction, and equality is exact because both sides are in lowest terms. main.cpp tokenises the line (keeping [...] together) and dispatches on the operator.
Folder structure
Complete source code
Makefile
What each file does
- src/fraction.h
- The
Fractionclass interface, free operators andpow. - src/fraction.cpp
- Normalisation, checked arithmetic,
parse(integer / fraction / decimal / mixed), comparisons, printing and exponentiation by squaring. - src/main.cpp
- Tokeniser, the
ansregister, thesumcommand and result display. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
1/2 + 1/3 ans * 6 3/4 - 5/6 [2 1/3] * 3/14 0.125 + 1/8 -7/4 / 1/2 ans ^ 3 2/3 ^ -2 6/8 = 3/4 0.3 > 1/3 1/0 + 1 5 / 0 sum 1/2 1/3 1/6 1/4 1/x + 2 1/2 % 3 1/2 + quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - 1/2 + 1/3 = 5/6;
ans * 6= 5; 3/4 − 5/6 = −1/12. [2 1/3] * 3/14= 1/2 and-7/4 / 1/2= −7/2 = −3 1/2.0.3 > 1/3is false;1/0and5 / 0are both rejected.
Common errors
6/8 = 3/4 prints false
Equality by members only works if every fraction is normalised; call normalise() after each operation.
Wrong results with large numbers
Plain int64 multiplication overflows silently; check before multiplying, as checked_mul does.
Possible improvements
- Parse full expressions with precedence
- Support repeating decimals like 0.(3)
- Convert results to continued fractions
Additional challenges
- 1Make the class a template over the integer type.
- 2Add
sqrtthat returns an exact result when possible. - 3Implement
std::hash<Fraction>and store fractions in an unordered_set.
Built and tested it yourself? Mark it to update your dashboard.