Question: Assignment 2 : Fraction Calculator with Operator Overloading ( Without User Input ) Create a C + + program that simulates a basic fraction calculator
Assignment : Fraction Calculator with Operator Overloading Without User Input
Create a C program that simulates a basic fraction calculator by overloading
operators for arithmetic operations involving fractions. This assignment will
involve creating a Fraction class that supports these operations through operator
overloading.
Part : Fraction Class Implementation
Create a class named Fraction that encapsulates a fraction numerator and denominator
and overloads the and operators to perform arithmetic operations.
Fraction.h Header File:
Member Variables private access:
o int numerator;
o int denominator;
Member Functions public access:
o Fraction; Default constructor
o Fractionint num, int denom; Parameterized constructor
o Fractionconst Fraction& other; Copy constructor
o int getNumerator const; Getter for the numerator
o int getDenominator const; Getter for the denominator
o void setNumeratorint num; Setter for the numerator
o void setDenominatorint denom; Setter for the denominator
o Overload the arithmetic operators and to perform operations
with Fraction objects.
o void print const; Method to display the fraction
o void simplify; Method to reduce the fraction to its simplest form
Fraction.cpp Implementation File:
Implement the member functions, the overloaded operators, and the simplify method
declared in Fraction.h
Part : Main Function
Write the main function in a separate cpp file that uses the Fraction class to simulate
a calculator without requiring user input. Name the file main.cpp
Main Function Requirements:
o Implement a loop that performs calculations.
o Randomly generate two Fractions to be used in calculations
numerators and denominators are random integers between and
o Randomly select an operation
o Use a switch statement to handle the selected operations.
o Display the result, ensuring the result is simplified before printing.
o Repeat the process for iterations calculations
Main Function Steps:
Step : Initialize Random Number Generator
Include before the main function:
#include For rand and srand
#include For time
Initialize the random number generator at the start of your main function
using srandtime to ensure different random numbers in each run.
code example
srandstaticcasttime;
Step : Loop for Repeated Calculations
Implement a loop that repeats the calculation process times.
code example
for int i ; i ; i
Inside this loop, generate random fractions
and perform calculations
Step : Generate Random Fractions
Generate four random integers between and using the rand function.
code example
int num rand;
int den rand;
int num rand;
int den rand;
Step : Create Fraction Objects
Create two Fraction objects using the randomly generated numerators and denominators.
code example
Fraction fracnum den;
Fraction fracnum den;
Step : Randomly Select an Operation
Generate a random integer between and to randomly select
an arithmetic operation Use a switch statement
to map the number to an operation.
code example
int randomOperation rand;
char operation;
switch randomOperation
case : operation ; break;
case : operation ; break;
case : operation ; break;
case : operation ; break;
Step : Perform the Operation
Use another switch statement to perform the selected operation
using the overloaded operators from the Fraction class. For division,
make sure to handle division by zero by checking if the second
fraction's denominator is not zero.
code example
switch operation
case :
frac fracprint;
break;
case :
frac fracprint;
break;
case :
frac fracprint;
break;
case :
if fracgetDenominator
frac fracprint;
else
std::cout "Division by zero is not allowed." std::endl;
break;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
