Question

In mathematics, a rational number is any number that can be expressed as the quotient or...

In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, p and q, with the denominator q not equal to zero. Since q may be equal to 1, every integer is a rational number. Define a class that can represent for a rational number. Use the class in a C++ program that can perform all of the following operations with any two valid rational numbers entered at the keyboard (must be entered with the format p/q; 3 points off for not doing this requirement): a. Adding two rational numbers. The result should be stored in reduced form. b. Subtracting two rational numbers. The result should be stored in reduced form. c. Multiplying two rational numbers. The result should be stored in reduced form. d. Dividing two rational numbers. The result should be stored in reduced form. e. Printing rational numbers in the form n/d, where n is the numerator and d is the denominator. f. Printing Rational numbers in floating-point format. Allow your program runs continuously until user wants to quit it. Lastly, your name must be displayed (only) before the termination of the program. Validate your input values; Reject all “junk” values with error message. For floating-point values, limit 2 digits after the dot. Note: There is no credit for not using class, or if your designed class does not represent for a rational number, where its attributes must contain only two integers, one for numerator and another for denominator.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h> #include<iostream> #include<conio.h> #include<math.h> using namespace std; class rational { float numerator; float denom; public: void getdata() { cout<<"\n enter the numerator "; cin>>numerator; cout<<"\n enter the denominator "; cin>>denom; } void operator+(rational); void operator-(rational); void operator *(rational); void operator /(rational); }; void rational ::operator+(rational c1) { rational temp; float ans; temp.numerator=(numerator*c1.denom)+(c1.numerator*denom); temp.denom=denom*c1.denom; cout<<"\nrational no. after addition"; cout<<"\n numerator="<<temp.numerator<<"\n denominator ="<<temp.denom; cout<<"\n Reduced"<<round(temp.numerator/temp.denom); ans=temp.numerator/temp.denom; cout.precision(3); cout<<"\n Floating number"<<ans; } void rational ::operator -(rational c1) { rational temp; float ans1; temp.numerator=(numerator*c1.denom)-(c1.numerator*denom); temp.denom=denom*c1.denom; cout<<"\n rational no. after subtraction"; cout<<"\n numerator="<<temp.numerator<<"\n denominator ="<<temp.denom; cout<<"\n Reduced"<<round(temp.numerator/temp.denom); ans1=temp.numerator/temp.denom; cout.precision(3); cout<<"\n Floating number"<<ans1; } void rational ::operator *(rational c1) { rational temp; float ans2; temp.numerator=numerator*c1.numerator; temp.denom=denom*c1.denom; cout<<"\n rational no. after multiplication"; cout <<"\n numerator="<<temp.numerator<<"\n denominator ="<< temp.denom; cout<<"\n Reduced"<<round(temp.numerator/temp.denom); ans2=temp.numerator/temp.denom; cout.precision(3); cout<<"\n Floating number"<<ans2; } void rational :: operator /(rational c1) { rational temp; float ans3; temp.numerator= numerator*c1.denom; temp.denom=c1.numerator*denom; cout<<"\n rational no. after dividation"; cout <<"\n numerator="<<temp.numerator<<"\n denominator ="<<temp.denom; cout<<"\n Reduced"<<round(temp.numerator/temp.denom); ans3=temp.numerator/temp.denom; cout.precision(3); cout<<"\n Floating number"<<ans3; } int main() { rational c1, c2; int n; do { cout<<"\n 1. Enter data for rational no. "; cout<<"\n 2. Addition of rational no. "; cout<<"\n 3. Subtraction of rational no. "; cout<<"\n 4. Multiplication of rational no."; cout<<"\n 5. Division of rational no. "; cout<<"\n 6. Quit"; cout<<"\n Enter your choice"; cin>>n; switch(n) { case 1: cout<<endl<<"\n enter the data for first rational no."; c1.getdata(); cout<<endl<<"\n enter the data for second rational no. "; c2.getdata (); break; case 2: c1+c2; getch(); break; case 3: c1-c2; getch(); //clrscr(); case 4: c1*c2; getch(); //clrscr(); break; case 5: c1/c2; getch(); //clrscr(); break; case 6: cout<<"Your Name"; break; } } while (n!=6); return 0; getch(); }

C:\UserstJsca\ Desktop rational.exe σ .cpp-[Executing]-Dev-C++ 5.5.3 1. Enter data for rational no 2. Addition of rational no

Add a comment
Know the answer?
Add Answer to:
In mathematics, a rational number is any number that can be expressed as the quotient or...
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...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (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 h and would be stored in the object as 1 in the numerator...

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

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

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

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

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

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

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of 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 could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

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