C++ Develop a rational number calculator. It should
accept all examples below and any rational number the user puts in
it. A rational number is of the form a/b, where a and b
are integers with b ≠ 0. Develop and test a
class for processing rational numbers. The class should have a
numerator data member and a
denominator data member. It should read and display all rational
numbers in the format a/b; for
output, just display a if the denominator is 1. The following
examples illustrate the operations that
should be provided.
Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file.
|
Operator |
Example |
Result |
|
Addition |
3/9 + 1/7 |
10/21 |
|
Subtraction |
3/9*1/7 |
4/21 |
|
Multiplication |
3/8 * 1/6 |
1/16 |
|
Division |
3/8 / 1/6 |
9/4 |
|
Invert |
2/4 I |
4/2 |
|
Mixed fraction |
8/3 M |
2 and 2/3 |
|
Reduce |
18/24 R |
3/4 |
|
Less than |
1/6 < 3/8 |
True |
|
Less than or equal to |
1/6 <= 3/8 |
True |
|
Greater Than |
1/6 > 3/8 |
False |
|
Greater than or equal |
1/6 >= 3/8 |
False |
|
Equal to |
3/8 == 9/24 |
True |
/* I have created the code in simplest manner possible for
understanding. No complex methods used.
Hence code is little long. Also could not complete entire function
list due to time restrictions.
However rest of the functions are fairly easy. Main challenge was
to reduce the fraction numbers*/
#include <iostream>
#include <conio.h>
using namespace std;
// function definations
void menuFunction();
void reduce(int, int, int&, int&);
void addition(int, int, int, int, int&, int&);
void subtraction(int, int, int, int, int&, int&);
void multiplication(int, int, int, int, int&, int&);
void division(int, int, int, int, int&, int&);
//main function
int main()
{
menuFunction();
getch();
return 0;
}
//menu function to choose all options for user.
void menuFunction()
{
int choice, num1, num2, denom1, denom2, reducenum = 0,
reducedenom = 0, numResult = 0, denomResult = 0;
cout<<"This program lets you perform operations
on fractions."<<endl;
cout<<"Input data as (a/b) and (c/d), then
select an operation to perform."<<endl<<endl;
cout<<"Operation Number"<<endl;
cout<<"-------------------------------"<<endl;
cout<<"Adding fractions: 1"<<endl;
cout<<"Subtracting fractions:
2"<<endl;
cout<<"Multiplying fractions:
3"<<endl;
cout<<"Dividing fractions: 4"<<endl;
cout<<"Reduce fractions: 5"<<endl;
cout<<"-------------------------------"<<endl;
cout<<"\nEnter fractions: "<<endl;
cin>>num1>>denom1>>num2>>denom2;
cout<<"Enter operation (number):
"<<endl;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
addition(num1, denom1, num2,
denom2, numResult, denomResult);
break;
case 2:
subtraction(num1, denom1, num2,
denom2, numResult, denomResult);
break;
case 3:
multiplication(num1, denom1, num2,
denom2, numResult, denomResult);
break;
case 4:
division(num1, denom1, num2,
denom2, numResult, denomResult);
break;
case 5:
reduce(num1, denom1, reducenum,
reducedenom);
break;
default:
cout<<"\nInvalid
input.\n"<<endl;
}
reduce(numResult, denomResult, reducenum, reducedenom
);
cout<<"Result =
"<<reducenum<<"/"<<reducedenom<<endl;
}
//reduce function to simplify fraction numbers. eg. 18/8 will be
simplified to 9/4 and so on.
void reduce(int a, int b, int& reducenum, int&
reducedenom)
{
int originala = a;
int originalb = b;
while(1)
{
a = a % b;
if(a == 0)
{
reducenum =
originala / b;
reducedenom = originalb / b;
break;
}
b = b % a;
if(b == 0)
{
reducenum =
originala / a;
reducedenom = originalb / a;
break;
}
}
}
//addition of fraction numbers taking inputs as top1/bottom1 +
top2/bottom2
void addition(int top1, int bottom1, int top2, int bottom2,
int& topResult, int& bottomResult )
{
if(bottom1 != 0 && bottom2 != 0)
{
int numerator1 = (bottom1 * bottom2) / bottom1;
int numerator2 = (bottom1 * bottom2) / bottom2;
bottomResult = bottom1 * bottom2;
int newNum1 = numerator1 * top1;
int newNum2 = numerator2 * top2;
topResult = newNum1 + newNum2;
}
else
cout<<"\nInvalid input,
denominator cannot be 0."<<endl;
}
//subtraction of fraction numbers taking inputs as top1/bottom1
- top2/bottom2
void subtraction(int top1, int bottom1, int top2, int bottom2,
int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int numerator1 = (bottom1 *
bottom2) / bottom1;
int numerator2 = (bottom1 *
bottom2) / bottom2;
bottomResult = bottom1 *
bottom2;
int newNum1 = numerator1 *
top1;
int newNum2 = numerator2 *
top2;
topResult = newNum1 -
newNum2;
}
else
cout<<"\nInvalid input,
denominator cannot be 0."<<endl;
}
//multiplication of fraction numbers taking inputs as
top1/bottom1 * top2/bottom2
void multiplication(int top1, int bottom1, int top2, int bottom2,
int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
bottomResult = bottom1 *
bottom2;
topResult = top1 * top2;
}
else
cout<<"\nInvalid input,
denominator cannot be 0."<<endl;
}
//division of fraction numbers taking inputs as top1/bottom1 /
top2/bottom2
void division(int top1, int bottom1, int top2, int bottom2,
int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int newTop = bottom2;
int newBottom = top2;
topResult = top1 * newTop;
bottomResult = bottom1 * newBottom;
}
else
cout<<"\nInvalid
input.\n"<<endl;
}
C++ Develop a rational number calculator. It should accept all examples below and any rational number...