In the following code, it gets hung up at
cout << "Number1 * Number2 = " << number1 * number2 << endl;
giving an error of "no math for operator<<" what am i doing wrong?
Thank you
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
class Rational //class for rational numbers (1/2, 5/9,
ect..)
{
public:
Rational(int numerator, int
denominator);
Rational(int numberator);
Rational(); //default
friend istream& operator
>>(istream& ins, Rational& number);
friend ostream& operator
<<(ostream& outs, Rational& number);
friend Rational operator
+(Rational& number1, Rational& number2);
friend Rational operator
-(Rational& number1, Rational& number2);
friend Rational operator
*(Rational& number1, Rational& number2);
friend Rational operator
/(Rational& number1, Rational& number2);
bool friend operator
==(Rational& number1, Rational& number2);
bool friend operator
<(Rational& number1, Rational& number);
private:
int num; //numerator
int den; //denominator
};
int main( )
{
Rational number1, number2(10), number3(1,2);
//Displays the values
cout << "Number1 = " << number1 <<
endl;
cout << "Number2 = " << number2 <<
endl;
cout << "Number3 = " << number3 <<
endl;
//Tests the operators
cout << "Number1 * Number2 = " << number1
* number2 << endl;
cout << "Number1 / Number3 = " << number1
/ number3 << endl;
cout << "Number2 + Number3 = " << number2
+ number3 << endl;
cout << "Number3 - Number1 = " << number3
- number1 << endl;
if (number1 == number2)
cout << "Number1 is equal to
Number2" << endl;
if (number1 < number2)
cout << "Number1 is less than
Number2" << endl;
*/
return 0;
}
Rational::Rational(int numerator, int denominator)
{
num = numerator;
den = denominator;
}
Rational::Rational(int numerator)
{
num = numerator;
den = 1;
}
Rational::Rational() : num(0), den(1)
{ }
istream& operator >>(istream& ins, Rational&
number)
{
char first, second, slash;
int n, d;
bool negative_num = false, negative_den = false;
ins >> first;
if (first == '-')
{
negative_num =
true;
ins >>
first;
}
ins >> n >> slash >> second;
if (second == '-')
{
negative_den =
true;
ins >>
second;
}
ins >> d;
if (slash != '/')
{
cout <<
"Invalid input" << endl;
exit(1);
}
if (negative_num)
n = -n;
if (negative_den)
d = -d;
number.num = n;
number.den = d;
return ins;
}
ostream& operator <<(ostream& outs, Rational&
number)
{
outs << number.num << '/' <<
number.den;
return outs;
}
Rational operator +(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.den + number2.num *
number1.den );
temp.den = ( number1.den * number2.den );
return (temp);
}
Rational operator -(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.den - number2.num *
number1.den );
temp.den = ( number1.den * number2.den );
return (temp);
}
Rational operator *(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.num );
temp.den = ( number1.den * number2.den );
return(temp);
}
Rational operator /(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.den );
temp.den = ( number2.num * number1.den );
return (temp);
}
bool operator ==(Rational& number1, Rational&
number2)
{
return ( number1 == number2 );
}
bool operator <(Rational& number1, Rational&
number2)
{
return ( number1 < number2 );
}
Sample Output:
Raw Paste Code:
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
class Rational //class for rational numbers (1/2, 5/9,
ect..)
{
public:
Rational(int numerator,
int denominator);
Rational(int
numberator);
Rational();
//default
friend istream&
operator >>(istream& ins, Rational& number);
friend ostream&
operator <<(ostream& outs, Rational& number);
friend Rational operator
+(Rational& number1, Rational& number2);
friend Rational operator
-(Rational& number1, Rational& number2);
friend Rational operator
*(Rational& number1, Rational& number2);
friend Rational operator
/(Rational& number1, Rational& number2);
bool friend operator
==(Rational& number1, Rational& number2);
bool friend operator
<(Rational& number1, Rational& number);
private:
int num;
//numerator
int den;
//denominator
};
int main( )
{
Rational number1, number2(10),
number3(1,2);
//Displays the values
cout << "Number1 = " << number1
<< endl;
cout << "Number2 = " << number2
<< endl;
cout << "Number3 = " << number3
<< endl;
//Tests the operators
cout << "Number1 * Number2 = " <<
number1 * number2 << endl;
cout << "Number1 / Number3 = " <<
number1 / number3 << endl;
cout << "Number2 + Number3 = " <<
number2 + number3 << endl;
cout << "Number3 - Number1 = " <<
number3 - number1 << endl;
if (number1 == number2)
cout <<
"Number1 is equal to Number2" << endl;
else
cout << "Number1 is not equal
to Number2" << endl;
if (number1 < number2)
cout << "Number1
is less than Number2" << endl;
else
cout << "Number2 is less than
Number1" << endl;
system("pause");
return 0;
}
Rational::Rational(int numerator, int denominator)
{
num = numerator;
den = denominator;
}
Rational::Rational(int numerator)
{
num = numerator;
den = 1;
}
Rational::Rational() : num(0), den(1)
{ }
istream& operator >>(istream& ins, Rational&
number)
{
char first, second, slash;
int n, d;
bool negative_num = false, negative_den =
false;
ins >> first;
if (first == '-')
{
negative_num = true;
ins >> first;
}
ins >> n >> slash >>
second;
if (second == '-')
{
negative_den = true;
ins >> second;
}
ins >> d;
if (slash != '/')
{
cout << "Invalid input" << endl;
exit(1);
}
if (negative_num)
n = -n;
if (negative_den)
d = -d;
number.num = n;
number.den = d;
return ins;
}
ostream& operator <<(ostream& outs, Rational&
number)
{
outs << number.num << '/' <<
number.den;
return outs;
}
Rational operator +(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.den +
number2.num * number1.den );
temp.den = ( number1.den * number2.den );
return (temp);
}
Rational operator -(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.den -
number2.num * number1.den );
temp.den = ( number1.den * number2.den );
return (temp);
}
Rational operator *(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.num );
temp.den = ( number1.den * number2.den );
return(temp);
}
Rational operator /(Rational& number1, Rational&
number2)
{
Rational temp;
temp.num = ( number1.num * number2.den );
temp.den = ( number2.num * number1.den );
return (temp);
}
bool operator ==(Rational& numb1, Rational& numb2)
{
if((numb1.num/numb1.den)==(numb2.num/numb2.den))
return true;
else
return false;
}
bool operator <(Rational& number1, Rational&
number2)
{
if((number1.num/number1.den)<(number2.num/number2.den))
return true;
else
return false;
}
In the following code, it gets hung up at cout << "Number1 * Number2 =...
Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...
Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...
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 <<...
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...
C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...
You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...
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...
The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...