Question: Assignment 2 : Fraction Calculator with Operator Overloading ( Without User Input ) Create a C + + program that simulates a basic fraction calculator

Assignment 2: 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 1: 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 Fraction(int num, int denom); (Parameterized constructor)
o Fraction(const Fraction& other); (Copy constructor)
o int getNumerator() const; (Getter for the numerator)
o int getDenominator() const; (Getter for the denominator)
o void setNumerator(int num); (Setter for the numerator)
o void setDenominator(int 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 2: 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 10 calculations.
o Randomly generate two Fractions to be used in calculations
(numerators and denominators are random integers between 1 and 15).
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 10 iterations (calculations).
Main Function Steps:
Step 1: 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 srand(time(0)) to ensure different random numbers in each run.
//code example
srand(static_cast(time(0)));
Step 2: Loop for Repeated Calculations
- Implement a loop that repeats the calculation process 10 times.
//code example
for (int i =0; i <10; ++i){
// Inside this loop, generate random fractions
// and perform calculations
}
Step 3: Generate Random Fractions
- Generate four random integers between 1 and 15 using the rand() function.
//code example
int num1= rand()%15+1;
int den1= rand()%15+1;
int num2= rand()%15+1;
int den2= rand()%15+1;
Step 4: Create Fraction Objects
- Create two Fraction objects using the randomly generated numerators and denominators.
//code example
Fraction frac1(num1, den1);
Fraction frac2(num2, den2);
Step 5: Randomly Select an Operation
- Generate a random integer between 0 and 3 to randomly select
an arithmetic operation (+,-,*,/). Use a switch statement
to map the number to an operation.
//code example
int randomOperation = rand()%4;
char operation;
switch (randomOperation){
case 0: operation ='+'; break;
case 1: operation ='-'; break;
case 2: operation ='*'; break;
case 3: operation ='/'; break;
}
Step 6: 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 '+':
(frac1+ frac2).print();
break;
case '-':
(frac1- frac2).print();
break;
case '*':
(frac1* frac2).print();
break;
case '/':
if (frac2.getDenominator()!=0){
(frac1/ frac2).print();
} 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!