Question

(Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your clas
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
class Rational
{
private:
int num;
int denom;
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
void reduce()
{
int d;
d=gcd(num, denom);
num=num/d;
denom=denom/d;
}
string to_string(int num)
{
ostringstream str1;
str1 << num;
return str1.str();
}
string to_string(float num)
{
ostringstream str1;
str1 << num;
return str1.str();
}
public:
void set(int x,int y)
{
num=x;
denom=y;
if(y==0)
denom=1;
reduce();
}
int getNum()
{
return num;
}
int getDenom()
{
return denom;
}
Rational(int x,int y)
{
num=x;
denom=y;
if(y==0)
denom=1;
reduce();
}
Rational()
{
num=1;
denom=2;
}
Rational operator +(Rational r)
{
Rational sum;
int x=num*r.getDenom()+denom*r.getNum();
int y=denom*r.getDenom();
sum.set(x,y);
return sum;
}
Rational operator -(Rational r)
{
Rational sub;
int x=num*r.getDenom()-denom*r.getNum();
int y=denom*r.getDenom();
sub.set(x,y);
return sub;
}
Rational operator *(Rational r)
{
Rational mul;
int x=num*r.getNum();
int y=denom*r.getDenom();
mul.set(x,y);
return mul;
}
Rational operator /(Rational r)
{
Rational div;
int x=num*r.getDenom();
int y=denom*r.getNum();
div.set(x,y);
return div;
}
string fraction()
{
string x=to_string(num);
string y=to_string(denom);
return x+"/"+y;
}
string decimal(int place)
{
float no=(float)num/(float)denom;
string str=to_string(no);
int dotindex=0;
for(int i=0;i<str.size();i++)
{
if(str[i]=='.')
{
dotindex=i;
break;
}
}
if(str.size()-dotindex-1<place)
{
int s=str.size();
for(int i=0;i<place-s+dotindex+1;i++)
str+="0";
}
else if(str.size()-dotindex>place)
{
str=str.substr(0,dotindex+place+1);
}
return str;
}
};
int main()
{
Rational r1(3,6);
Rational r2(4,10);
cout<<"r1="<<r1.fraction()<<"="<<r1.decimal(2)<<endl;
cout<<"r2="<<r2.fraction()<<"="<<r2.decimal(2)<<endl;
Rational r3=r1+r2;
cout<<"r1+r2="<<r3.fraction()<<"="<<r3.decimal(2)<<endl;
Rational r4=r1-r2;
cout<<"r1-r2="<<r4.fraction()<<"="<<r4.decimal(2)<<endl;
Rational r5=r1*r2;
cout<<"r1*r2="<<r5.fraction()<<"="<<r5.decimal(2)<<endl;
Rational r6=r1/r2;
cout<<"r1/r2="<<r6.fraction()<<"="<<r6.decimal(2)<<endl;
return 0;
}

Output:

CAUsers Ashish Shrivastav Desktoplc,C++ Programs\rational.exe r1-1/2=0.50 r 2-2/5=0.40 r1+r2-9/10-0.90 r1-r2- 1/10-0.10 r1r

Add a comment
Know the answer?
Add Answer to:
(Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 2...

  • PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)*...

    PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)***** Also please dont use BigInteger thanks it's really appreciated for all the help PLEASE MAKE IT SO ITS USER INPUT Create a class called Rational for prforming arithmetie with fractions. Write an app to test your class Use integer variables to represent the private instance variables of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

  • Can somebody help me with this assignment. I will highly appreciate. Also, please display the output...

    Can somebody help me with this assignment. I will highly appreciate. Also, please display the output as well. I need to use JAVA to to write the program. This is the sample output provided by my professor. HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    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...

  • Define a class for rational numbers. A rational number is a number that can be represented...

    Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 etc we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class rational Num...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT