In c++ Create an overload of the extraction operator >> for reading mixed number
ID: 3906053 • Letter: I
Question
In c++
Create an overload of the extraction operator >> for reading mixed numbers from an input stream. The input format for a Mixed number object will be:
i.e. the integer part, a space, and the fraction part (in numerator/denominator form), where the integer, numerator, and denominator parts are all of type int. You may assume that this will always be the format that is entered (i.e. your function does not have to handle entry of incorrect types that would violate this format). However, this function should check the values that come in. In the case of an incorrect entry, just set the Mixed object to represent the number 0, as a default. An incorrect entry occurs if a denominator value of 0 is entered, or if an improper placement of a negative sign is used. Valid entry of a negative number should follow this rule -- if the integer part is non-zero, the negative sign is entered on the integer part; if the integer part is 0, the negative sign is entered on the numerator part (and therefore the negative sign should never be in the denominator). Examples:
Create an overload of the insertion operator << for output of Mixed numbers. This should output the mixed number in the same format as above, with the following exceptions: If the object represents a 0, then just display a 0. Otherwise: If the integer part is 0, do not display it. If the fraction part equals 0, do not display it. For negative numbers, the minus sign is always displayed to the left.
Explanation / Answer
#include <iostream>
using namespace std;
// class Mixed definition
class Mixed
{
// Data member to store data
int integer;
int numerator;
int denominator;
public:
// Default constructor definition
Mixedx()
{
integer;
numerator;
denominator;
}// End of default constructor
// Function to return true if number is zero otherwise returns false
bool validate(int no)
{
(no == 0) ? true : false;
}// End of function
// Function to return true if the number is negative otherwise returns false
bool validSign(int no)
{
(no < 0) ? true : false;
}// End of function
// Output redirection operator overloaded
friend istream& operator >> (istream &is, Mixed &f)
{
// To store symbol
char ch;
// Stores the integer
is>>f.integer;
// Stores numerator in is stream
is>>f.numerator;
// To discard fraction symbol
is>>ch;
// Stores denominator in is stream
is>>f.denominator;
// Checks if the integer is zero and negative
if(f.validate(f.integer) && f.validSign(f.integer))
{
f.integer *= -1;
f.numerator *= -1;
}// End of if condition
// Checks if the denominator is zero
if(f.validate(f.denominator))
f.integer = f.numerator = f.denominator = 0;
// Checks if denominator is negative
if(f.validSign(f.denominator))
f.denominator *= -1;
// Returns input stream
return is;
}// End of function
// Input redirection operator overloaded
friend ostream& operator<<(ostream &os, const Mixed &f)
{
// Stores numerator, operator symbol (/) and denominator in os stream
os<<f.integer<<" "<<f.numerator<<" / " <<f.denominator;
// Returns output stream
return os;
}// End of function
};// End of class
// main function definition
int main()
{
// Class Mixed object declared
Mixed mixed;
// Loops 3 times
for(int x = 0; x < 3; x++)
{
// Accepts a fraction
cout<<" Enter a Fractional number in the form of (integer numerator/denominator): ";
cin>>mixed;
// Displays fraction
cout<<" Fractional number : "<<mixed;
}// End of loop
}// End of main function
Sample Output:
Enter a Fractional number in the form of (integer numerator/denominator): 1 2/-3
Fractional number : 1 2 / 3
Enter a Fractional number in the form of (integer numerator/denominator): 5 6/1
Fractional number : 5 6 / 1
Enter a Fractional number in the form of (integer numerator/denominator): 1 3/0
Fractional number : 0 0 / 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.