Cpp
Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part).
Details and Requirements
Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A zero in the denominator of the fraction part constitutes an illegal number and should not be allowed. You should create appropriate member data in your class. All member data must be private.
There should be two constructors. One constructor should take in three parameters, representing the integer part, the numerator, and the denominator (in that order), used to initialize the object. If the mixed number is to be a negative number, the negative should be passed on the first non-zero parameter, but on no others. If the data passed in is invalid (negatives not fitting the rule, or 0 denominator), then simply set the object to represent the value 0. Examples of declarations of objects:
Mixed m1(3, 4, 5); // sets object to 3 4/5 Mixed m2(-4, 1, 2); // sets object to -4 1/2 Mixed m3(0, -3, 5); // sets object to -3/5 (integer part is 0). Mixed m4(-1, -2, 4); // bad parameter combination. Set object to 0.
The other constructor should expect a single int parameter with a default value of 0 (so that it also acts as a default constructor). This constructor allows an integer to be passed in and represented as a Mixed object. This means that there is no fractional part. Example declarations:
Mixed m5(4); // sets object to 4 (i.e. 4 and no fractional part). Mixed m6; // sets object to 0 (default)
Use member initialization list to initialize class fields. Note that this last constructor will act as a “conversion constructor”, allowing automatic type conversions from type int to type Mixed.
The Mixed class should have public member function Print(). This function should output to the console 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.
Examples: 0 , 2 , -5 , 3/4 , -6/7 , -2 4/5 , 7 2/3
Instructions
Driver Program
This code provides minimal set of tests for your Mixed class. Note, this is not a comprehensive set of tests. It is just a some code to get you started, illustrating some sample calls.
#include <iostream> using namespace std; class Mixed { // write your solution here } int main(){ Mixed m1(3, 4, 5); // sets object to 3 4/5 m1.Print(); // prints 3 4/5 Mixed m2(-4, 1, 2); // sets object to -4 1/2 m2.Print(); // prints -4 1/2 Mixed m3(0, -3, 5); // sets object to -3/5 (integer part is 0). m3.Print(); // prints -3/5 Mixed m4(-1, -2, 4); // bad parameter combination. Set object to 0. m4.Print(); // prints 0 Mixed m4a(1, -2, -4); // bad parameter combination. Set object to 0. m4a.Print(); // prints 0 Mixed m5(4); m5.Print(); // prints: 4 Mixed m6; m6.Print(); // prints 0 }
Here numerous variables are used whose description are given in code ... #include <bits/stdc++.h> using namespace std; class Mixed{ bool contain_integer=false; //check whether given data contains integer or not bool is_negative=false; //check whether given data is positive or negative bool contain_fraction=false; //check whether given data contains fraction or not bool done=false; //check whether we have dealt with the data or not int Integer_part;//integer part of data int Numerator; //number of fraction if exists int Denominator; //denominator of fraction if exists public: Mixed(int integer_part,int numerator,int denominator) { if(denominator==0) //if denominator is 0 it is invalid { Integer_part=0; // make the value 0 contain_integer=true; done=true; } if(!done) //is condition aboe doesnot match { if(integer_part!=0) //checking for integet part { Integer_part=integer_part; //assigning the value if(numerator<0||denominator<0) //this conditions checks for invalidity { Integer_part=0; contain_integer=true; done=true; } if(!done) //check if above condition is false { if(integer_part<0) //check whether integer_part is negative { is_negative=true; //set variable to true } if(numerator==0) // if numerator is 0 then only integer part will be //there { Integer_part=integer_part; //setting the integer part contain_integer=true; done=true; } if(!done) //if above conditions does not match { Numerator=numerator; //then simple data contains all three parts Denominator=denominator; contain_fraction=true; contain_integer=true; done=true; } } } else { contain_integer=false; if(numerator!=0) { if(denominator<0) //checks for invalidity { Integer_part=0; contain_integer=true; done=true; } else { if(numerator<0) //contains only fraction --> if it is negative then //set variable to true { is_negative=true; } Numerator=numerator; //assigning the values Denominator=denominator; contain_fraction=true; } } else //if numerator is zerp then value is 0 { Integer_part=0; contain_integer=true; done=true; } } } } Mixed(int integer_part=0) //default argument { contain_integer=true; // contains only integer if(integer_part<0) is_negative=true; Integer_part=integer_part; } void Print() { if(is_negative==true) //check whether data was negative or positive cout<<"-"; if(contain_integer) //check whether data contains integer part or not { cout<<abs(Integer_part)<<" "; } if(contain_fraction) //check whether data contains fraction { cout<<abs(Numerator)<<"/"<<abs(Denominator); } cout<<"\n"; } }; int main() { Mixed m1(3,4,5); m1.Print(); // prints 3 4/5 Mixed m2(-4, 1, 2); // sets object to -4 1/2 m2.Print(); // prints -4 1/2 Mixed m3(0, -3, 5); // sets object to -3/5 (integer part is 0). m3.Print(); // prints -3/5 Mixed m4(-1, -2, 4); // bad parameter combination. Set object to 0. m4.Print(); // prints 0 Mixed m4a(1, -2, -4); // bad parameter combination. Set object to 0. m4a.Print(); // prints 0 Mixed m5(4); m5.Print(); // prints: 4 Mixed m6; m6.Print(); // prints 0 }
Get Answers For Free
Most questions answered within 1 hours.