C++ Fraction calculator
Need help with code, cant use "using namespace std"
Objectives
Resources
Assignment requirements
Recursive gcd funtion
int gcd(const int n1, const int n2) {
int x = n1, y = n2;
if (x < 0) x = -x;
if (y > 0) y = -y;
if (y==0) return x;
else return gcd(y, x%y);
}
Menu function text
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
//please find the code below, the user will be asked values for numerator and denominator of both the fractions, then option will be given to add , subtract, multiply or divide the fractions, if keyword other than A,S,M,D is used the program asks for valid operation. 'Q' terminates the program.
//Error is intitally handles by checking if the denominator is zero, if it is the case, then program terminates as fraction is indetrminate.
// Doubts can be clarifed in the comment section.
// run using g++ filename.cpp -std=c++14
-------------------------------------------------------------
// Example program
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
int gcd(int a,int b){
int r;
while(b){
r = a%b;
a = b;
b = r;
}
return a;
}
void simplify(pii &res){
int g = gcd(res.first,res.second);
res.first/=g;
res.second/=g;
}
pii addFractions(int n1,int d1,int n2,int d2){
int lcm = (d1*d2)/gcd(d1,d2);
int f1 = lcm/d1 , f2 = lcm/d2;
pii res;
res.first = f1*n1 + f2*n2;
res.second = lcm;
simplify(res);
return res;
}
pii subtractFractions(int n1,int d1,int n2,int d2){
int lcm = (d1*d2)/gcd(d1,d2);
int f1 = lcm/d1 , f2 = lcm/d2;
pii res;
res.first = f1*n1 - f2*n2;
res.second = lcm;
simplify(res);
return res;
}
pii multiplyFractions(int n1,int d1,int n2,int d2){
pii res = {n1*n2,d1*d2};
simplify(res);
return res;
}
pii divideFractions(int n1,int d1,int n2,int d2){
pii res = {n1*d2,n2*d1};
simplify(res);
return res;
}
int main()
{
int n1,d1,n2,d2;
cout<<"enter numerator of fraction1 ";
cin>>n1;
cout<<"enter denomenator of fraction1 ";
cin>>d1;
cout<<"enter numerator of fraction2 ";
cin>>n2;
cout<<"enter denomenator of fraction1 ";
cin>>d2;
if(d1==0 || d2==0)
{
cout<<"Denominator is zero\n";
return 0;
}
char c='0';
try{
while(c!='Q'){
cout<<"select choice for operation on fraction1
<operation> fraction2 , Q to exit ";
cin>>c;
pii res;
if(c=='A'){
res = addFractions(n1,d1,n2,d2);
}
else if(c=='S'){
res = subtractFractions(n1,d1,n2,d2);
}
else if(c=='M'){
res = multiplyFractions(n1,d1,n2,d2);
}
else if(c=='D'){
res = divideFractions(n1,d1,n2,d2);
}
else if(c=='Q'){
break;
}
else{
cout<<"Enter a valid operation\n";
continue;
}
cout<<"answer = "<<res.first<<" /
"<<res.second<<"\n";
}
}catch(...){
cout<<"Program terminating due to exception";
return 0;
}
return 0;
}
C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...
C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...
I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...
Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...
Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...
Must write in Java - ignore the Junit tests Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-element array (the numerator is at index 0, the denominator is at index 1), and you are to return a resulting, simplified fraction as a new two-element array...
Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead...
CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
Please help I am confused to where start. It C++ program thank you The stoi function converts a string to an integer, but does not check whether or not the value of the string is a good integer. If the stoi function fails, an exception is thrown and the program terminates. Write a program that asks the user to enter two strings, one called sNumerator and the other sDenominator. Using the stoi function, convert the two strings to numbers but...
Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...