Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Principles II Rational Numbers Objective: To learn about the definition and use of ADT in C++. This programming assignment is based on the book

image text in transcribed

image text in transcribed

Programming Principles II Rational Numbers Objective: To learn about the definition and use of ADT in C++. This programming assignment is based on the book exercise 6.7. 1) You are to design a class called Rational with public member functions read, write, add, subtract, multiply, divide. The prototype for the read and write methods should be: bool read(); void write() const; The second const means that the method write can't modify the class data members. The method read returns true if the read operation was successful. It returns false if end of file was encountered before the read was completed. 2) The prototype for all arithmetic functions should be of the form Rational add(const Rational & rhs) const The const in the parameter list means the function add can't modify rhs object and the second const means that method add can't modify the class data members. For example: Rational R1, R2; If (R1.read() && R2.read()) { Rational R3 = R1.add (R2); R3.write(); } 3) Create a header file called rational.h which contains the class definitions. Include a definition for a default constructor, a copy constructor, and a constructor that takes two integers to be used as numerator and denominator. 4) Create the file rational.cpp which uses #include to include rational.h and which contains the implementations of the constructors and the other methods. 5) The default constructor should create the Rational 0/1. The constructor that takes two explicit integers should check for the denominator being zero. If it is, your implementation should ignore both parameters and create the Rational 0/1" instead. Do not output error messages from constructors or any other Rational methods. 6) Create another C++ source file called main.cpp, which uses #include to include rational.h and which reads directive of the form: Rational operator Rraction "operator" is one of the characters +, -*, **", 7. Your main code should invoke the Rational read method when reading the two Rationales, and it should read the operator character in the standard way. Based on the operator, you are to invoke the appropriate method and produce an output line on the screen of the form: Rational operator Rraction = Rational_in_lowest_terms For example when the user enter the following: 1/3 + 1/6 The output should be: 1/3 + 1/6 =1/2 Not the output shouldn't be 1/3 + 1/6 = 2/6 (wrong) So you will need to declare and implement a method simplify that converts a rational to its simplest form. 7) When generating the output, be sure to use the Rational write routine for all three Rationales. Your program is to continue read directives until end of file (EOF) is sensed while trying to read one of the inputs. 8) Write another main function, main_file.cpp that reads the rational numbers and the operations from a text file instead of the screen. 9) What to turn in: a. The source files rational.cpp and main.cpp b. The header file rational.h C. A test file contains some Rational numbers. d. The executable file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions