C++ I need help adding to this program. Here's my assignment:
(1) Add the following functions to your working mixed number class:
Overload the ==, <, and > comparison operators
Overload the + and * arithmetic operators
Overload the insertion (<<) operator: It should display whole part, space, numerator, forward
slash, denominator: 5 4/13
Overload the extraction (>>) operator: It should read integer, integer, character, integer. That is,
it can assume that the numerator, forward slash, and denominator are input any white space
between: 7 2/3
(2) Modify your main driver file so that it will test all of your functions completely. Using an operator
only one time in your driver isn't a sufficient test of that operator.
Here is my 3 Files so far
Main.cpp:
#include
#include "MixedNum.h"
using namespace std;
int main()
{
MixedNum n1(5,6,7), n2(3,15,9), n3(-6,9,8), n4(8,3,1),
n5(10, 6, 1), n6(4,3,1), n7(0,5,2);
n1.display();
n2.display();
n3.display ();
n4.display();
n5.display();
n6.display();
n7.display();
return 0;
}
Implementation File:
#include
#include "MixedNum.h"
#include
using namespace std;
MixedNum::MixedNum(){
wholeNum= 0;
numer= 0;
denom=1;
}
MixedNum::MixedNum(int wNum,int num, int den){
wholeNum=wNum;
numer= num; denom = den;
simplify();
}
void MixedNum::simplify()
{
int gcd = GCD( numer , denom);
if (wholeNum < 0 || numer < 0 || denom <= 0 )
{
cout<< "ERROR : invalid number"<<
endl;
wholeNum= 0;
numer= 0;
denom=1;
}
else
wholeNum = numer/ denom + wholeNum;
numer %= denom;
numer /=
gcd;
denom /=
gcd;
}
void MixedNum::display()const {
cout << "Number: " <<
wholeNum<<" "<< numer << "/"<< denom<
}
int MixedNum::GCD(int numer, int denom){
while (numer!= denom){
if(numer >
denom)
numer = numer - denom;
else
denom= denom- numer;
}
return
numer;
}
Header File:
class MixedNum
{
private:
int
wholeNum;
int numer,
denom;
void
simplify();
public:
MixedNum();
MixedNum(int,int,int);
void display()
const;
GCD(int , int
);
}
;
Program:
MixedNum.h : header file
#ifndef MIXEDNUM_H
#define MIXEDNUM_H
#include<iostream>
using namespace std;
class MixedNum
{
private:
int wholeNum;
int numer;
int denom;
void simplify();
public:
MixedNum();
MixedNum(int,int,int);
void display() const;
int GCD(int , int );
bool operator==(const MixedNum &num);
bool operator<(const MixedNum &num);
bool operator>(const MixedNum &num);
MixedNum operator+(const MixedNum &num);
MixedNum operator*(const MixedNum &num);
friend istream& operator>>(istream &is, MixedNum
&num);
friend ostream& operator<<(ostream &os, const
MixedNum &num);
};
#endif
MixedNum.cpp :
#include<iostream>
#include"MixedNum.h"
using namespace std;
//default constructor
MixedNum::MixedNum()
{
wholeNum = 0;
numer = 0;
denom = 1;
}
//parameterized constructor
MixedNum::MixedNum(int wNum,int num, int den)
{
wholeNum = wNum;
numer = num;
denom = den;
simplify();
}
//function to simplify the mixed number
void MixedNum::simplify()
{
int gcd = GCD( numer , denom);
if(wholeNum < 0 || numer < 0 || denom <= 0 )
{
cout<< "ERROR : invalid number"<< endl;
wholeNum = 0;
numer = 0;
denom =1;
}
else
{
wholeNum = numer/ denom + wholeNum;
numer %= denom;
numer /= gcd;
denom /= gcd;
}
}
//display
void MixedNum::display()const
{
cout << "Number: " << wholeNum<<" "<< numer
<< "/"<< denom<<endl;
}
//function to calculate gcd
int MixedNum::GCD(int numer, int denom)
{
while (numer!= denom)
{
if(numer > denom)
numer = numer - denom;
else
denom = denom - numer;
}
return numer;
}
// == operator overloading function
bool MixedNum::operator==(const MixedNum &num)
{
return wholeNum==num.wholeNum && numer==num.numer
&& denom==num.denom;
}
// << operator overloading function
bool MixedNum::operator<(const MixedNum &num)
{
if(wholeNum<num.wholeNum) return true;
if(wholeNum==num.wholeNum)
{
if(numer*num.denom<denom*num.numer)
return true;
return false;
}
return false;
}
// > operator overloading function
bool MixedNum::operator>(const MixedNum &num)
{
if(wholeNum>num.wholeNum) return true;
if(wholeNum==num.wholeNum)
{
if(numer*num.denom>denom*num.numer)
return true;
return false;
}
return false;
}
// + operator overloading function
MixedNum MixedNum::operator+(const MixedNum &num)
{
MixedNum temp;
temp.wholeNum = wholeNum + num.wholeNum;
temp.numer = numer*num.denom + denom*num.numer;
temp.denom = denom * num.denom;
temp.simplify();
return temp;
}
// * operator overloading function
MixedNum MixedNum::operator*(const MixedNum &num)
{
MixedNum temp;
temp.wholeNum = 0;
temp.numer = (wholeNum * denom + numer) * (num.wholeNum * num.denom
+ num.numer);
temp.denom = denom * num.denom;
temp.simplify();
return temp;
}
// >> operator overloading function
istream& operator>>(istream &is, MixedNum
&num)
{
is>>num.wholeNum>>num.numer>>num.denom;
num.simplify();
return is;
}
// << operator overloading function
ostream& operator<<(ostream &os, const MixedNum
&num)
{
os << "Number: " << num.wholeNum<<" "<<
num.numer << "/"<< num.denom<<endl;
return os;
}
main.cpp
#include <iostream>
#include "MixedNum.h"
using namespace std;
int main()
{
MixedNum n1(1,5,6), n2(4,1,2), n3, n4, n5;
cout<<n1;
cout<<n2;
n3 = n1 + n2;
cout<<"Addition: "<< n3;
n3 = n1 * n2;
cout<<"Multiplication: "<< n3;
if(n1<n2)
cout<<"n1 is smaller than n2 "<<endl;
else if(n1>n2)
cout<<"n1 is larger than n2 "<<endl;
else if(n1==n2)
cout<<"n1 and n2 are equal " <<endl;
cout<<"Enter first mixed number: "<<endl;
cin>>n4;
cout<<"Enter second mixed number: "<<endl;
cin>>n5;
n3 = n4 + n5;
cout<<"Addition: "<< n3;
n3 = n4 * n5;
cout<<"Multiplication: "<< n3;
return 0;
}
Output:

N.B. Whether you face any problem then share with me in the comment section, I'll happy to help you. I expect positive feedback from your end.
C++ I need help adding to this program. Here's my assignment: (1) Add the following functions...
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...
NEED ASAP PLEASE HELP
Task:
---------------------------------------------------------------------------------------------------------------------------------
Tasks to complete:
----------------------------------------------------------------------------------------------------------------------------------------
given code:
-----------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include <iostream>
#include <iomanip>
#include "fractionType.h"
using namespace std;
int main()
{
fractionType num1(5, 6);
fractionType num2;
fractionType num3;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << "Num1 = " << num1 << endl;
cout << "Num2 = " << num2 << endl;
cout << "Enter the fraction in the form a / b: ";
cin >> num2;
cout << endl;
cout <<...
Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...
NEED HELP IN C!! Answer in C programming language.
Question:
Functions and .h file:
Test function:
part 1:
part 2:
For the assignment use the following structs for Binary Trees and Binary Search Trees. struct Binode { int value; struct Binode* left; struct BTnode* right; struct BTnode* parent; }; typedef struct Binode BTnode_t; typedef struct BST { BTnode_t* root; BST_t; Question 2 [10 points] Write a function that gets a binary tree and returns the sum of its elements. //...
I need to do object oriented programming for c++. I had to make make a program where it would add,subtract,multiply,and divide fraction. I got that working, but it wont work for negative can anyone fix it and explain how they did it? Source.cpp code: #include "Fraction.h" #include <iostream> using namespace std; int main() { Fraction f1(-4, 6); Fraction f2(5, -9); Fraction sum = sum.add(f1, f2); sum.print(sum); Fraction diff = diff.subtract(f1, f2); diff.print(diff); ...
13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...
I need assistance with the C++ code below to remove the "this" pointers while maintaining the code's current output and functionality. Also, there should be a private class in the header file, but I'm not sure what I should include there. Any help would be greatly appreciated! ***main.cpp driver file*** #include <iostream> #include "vector.h" using namespace std; int main() { vectorInfo v1(7, 6); vectorInfo v2(5, 4); v1.set(3, 2); cout << "v1 : "; v1.print(); cout << "v2 :...
In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...
Hello, im looking for help on adding overload operators to my code. It needs to consist of + (addition) for the Fraction class * (multiplication), no need not reduce == (equality operator) = (assignment operator) << and >> so in main you can use << and >> as follows: cin>>frac1; cout<< frac1; where frac1 is a an object of the Fraction class the original code is: #include <iostream> using namespace std; class Fractions { private: int numerator; int denominator;...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...