Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a rational number calculator. A rational number is a number of the form a/b, where a and b are integers and b != 0.

write a rational number calculator. A rational number is a number of the form a/b, where a and b are integers and b != 0. Also, by convention, a/b is always shown in reduced form, i.e., the greatest common divisor of a and b is 1, and if a == 0 then b == 1. You will implement a Rational class allowing use of rational numbers as an abstract data type via a library. You will also write a client calculator program to drive the rational number class and allow testing the operations on rational expressions. It will read the rational numbers from a file, in the form ( a / b ) ( c / d ) where the parentheses are required, a, b, c and d are integers (possibly signed) and is one of +, -, * or /. Your output will be to another file, with lines of output in the form ( a / b ) ( c / d ) = ( e / f ) Loop over the input file until end-of-file. Take the file names for the input and output files from the command line arguments. In both input and output formats, the spaces between tokens are required. Your class will be called Rational, and must be defined in a header file (a file named rational.h) and implemented in a separate library source file (named rational.cpp), both in the current working directory. Your client code (another .cpp file) will #include the header you wrote using double quotes, not angle brackets: #include "rational.h" instead of like this: #include and will be linked with the class object file. You will create a project containing the 2 source files. Your class must provide public methods to add, subtract, multiply, and divide rational numbers. These must have the general form Rational Rational::add( Rational augend ) This adds this (*this or self) rational number (the addend) to the argument augend (remember, sum = addend + augend) producing another Rational for the sum, and returns the sum Rational object. The other functions; subtract(), multiply(), and divide(), will also take a rational number and treat it as the right hand operand of their particular operation, using this as the left hand side operand. None of these operatorlike methods may change their operands (either the parameter or "self" AT ALL. These operator-like methods may take a reference instead of a value parameter. You may return a reference also, but you have to be careful not to return a reference to a temporary local Rational object (which will be destroyed when the method returns). Your private data elements in the Rational class must be two longs called num and denom for the numerator and denominator. You may not have any other data elements in the class AT ALL. You MAY NOT add other parameters to the add(), multiply(), etc., methods. Your data members must be private. You must also provide at least these other methods: default and normal constructors, observers (i.e., to print a rational number to an output file handle, in the form ( a / b ) with parenthesis required, with the leading sign on a optional unless negative) manipulators (i.e., to read a rational number from the keyboard or a file handle in the form ( a / b ), or to take supplied values and set them into the rational number) other private "helper" functions, for example, to convert the rational number to its "normal" form as described above.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions