Question

In: Computer Science

c++ Programming For this assignment you will be building on your Fraction class. However, the changes...

c++ Programming

For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions.

Please keep all of your code in one file for this week. We will separate things into three files for the next assignment. Your class will go first, then your class member function definitions, then main().

Here are the client program and correct output.

Your class should support the following operations on Fraction objects:

  • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors.

    You should check to make sure that the denominator is not set to 0. The easiest way to do this is to use an assert statement: assert(inDenominator != 0); You can put this statement at the top of your constructor. Note that the variable in the assert() is the incoming parameter, not the data member. In order to use assert(), you must #include <cassert>

    For this assignment, you may assume that all Fractions are positive. We'll fix that next week.

  • Printing a Fraction to a stream with an overloaded << operator. Next week we will get fancy with this, but for now just print the numerator, a forward-slash, and the denominator. No need to change improper Fractions to mixed numbers, and no need to reduce.

  • All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare Fractions to other Fractions as well as Fractions to integers. Either Fractions or integers can appear on either side of the binary comparison operator. You should only use one function for each operator.

  • The four basic arithmetic operations (+, -, *, /) should be supported. Again, they should allow Fractions to be combined with other Fractions, as well as with integers. Either Fractions or integers can appear on either side of the binary operator. Only use one function for each operator.

    Note that no special handling is needed to handle the case of dividing by a Fraction that is equal to 0. If the client attempts to do this, they will get a runtime error, which is the same behavior they would expect if they tried to divide by an int or double that was equal to 0.

  • The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. Fractions can appear on the left-hand side, and Fractions or integers on the right-hand side. Only use one function for each operator.

  • The increment and decrement (++, --) operators should be supported in both prefix and postfix form for Fractions. To increment or decrement a Fraction means to add or subtract (respectively) one (1).

Additional Requirements and Hints:

  • You will not be graded on documentation on this assignment. You'll be working on the documentation next week.
  • The name of your class must be "Fraction". No variations will work.
  • Use exactly two data members.
  • You should not compare two Fractions by dividing the numerator by the denominator. This is not guaranteed to give you the correct result every time, because of the way that double values are stored internally by the computer. I would cross multiply and compare the products.
  • Don't go to a lot of trouble to find the common denominator (when adding or subtracting). Simply multiply the denominators together.
  • The last two bullets bring up an interesting issue: if your denominators are really big, multiplying them together (or cross multiplying) may give you a number that is too big to store in an int variable. This is called overflow. The rule for this assignment is: don't worry about overflow in these two situations.
  • My solution has 20 member functions (including friend functions). All of them are less than 4 lines long. I'm not saying yours has to be like this, but it shouldn't be way off.
  • Do not use as a resource a supplementary text or website if it includes a Fraction class (or rational or ratio or whatever).

Getting Started

Here are some suggestions for those of you who have trouble just figuring out where to start with assignment 1. Remember to use iterative development. That means start with the smallest, simplest subset of the final product that you can, make sure it works, and then start adding things to it one at a time (preferably the simple things first, if possible).

Start with just a default constructor and a stream insertion operator. For now, don't even worry about mixed numbers, just write the stream insertion operator so that it works with proper fractions. Test this out with a client program something like this:

int main(){
        Fraction f1;

        cout << f1;
}

(You should get output of "0/1" because you should have initialized the fraction to 0/1 in your constructor.)

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#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 the lowest terms.
public:
fraction(const int & = 0, const int & = 1);
// The numerator and the denominator
// fraction(5) = 5/1 = 5 :)
fraction(const fraction &);
// copy constructor

void operator=(const fraction &);

// You must know the meaning of +-*/, don't you ?
fraction operator+(const fraction &) const;
fraction operator-(const fraction &) const;
fraction operator*(const fraction &) const;
fraction operator/(const fraction &) const;

void operator+=(const fraction &);
void operator-=(const fraction &);
void operator*=(const fraction &);
void operator/=(const fraction &);

// Comparison operators
bool operator==(const fraction &) const;
bool operator!=(const fraction &) const;
bool operator<(const fraction &) const;
bool operator>(const fraction &) const;
bool operator<=(const fraction &) const;
bool operator>=(const fraction &) const;

friend std::istream & operator>>(std::istream &, fraction &);
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
friend std::ostream & operator<<(std::ostream &, const fraction &);
// Normally you should output "a/b" without any space and LF
// Sometims you may output a single integer (Why? Guess XD)
// If it is not a number (den = 0), output "NaN"
};

#endif

____________________________

// fraction.cpp

#include <iostream>
using namespace std;
#include "Fraction.h"


int fraction::gcd(const int &a, const int &b) const
{
int divi = (a > b ? a : b);
int div = (a < b ? a : b);
int rem = divi % div;
while (rem != 0)
{
divi = div;
div = rem;
rem = divi % div;
}
return div;

}
// If you don't need this method, just ignore it.
void fraction::simp()
{
int n = _numerator < 0 ? -_numerator : _numerator;
int d = _denominator;
int largest = n > d ? n : d;
int gcdi = 0; // greatest common divisor
//This loop will find the GCD of two numbers
for (int loop = largest; loop >= 2; loop--)
if (_numerator % loop == 0 && _denominator % loop == 0) {
gcdi = loop;
break;
}
//Dividing the Fraction class Numerator and Denominator with GCD to reduce to lowest terms
if (gcdi != 0) {
_numerator /= gcdi;
_denominator /= gcdi;
}

}
// To get the lowest terms.

fraction::fraction(const int &num, const int &den)
{
int n,d;
n=num;
d=den;
_numerator = (d < 0 ? -n : n);
_denominator = (d < 0 ? -d : d);
simp();

}
// The numerator and the denominator
// fraction(5) = 5/1 = 5 :)
fraction::fraction(const fraction& f)
{
this->_numerator=f._numerator;
this->_denominator=f._denominator;
}
// copy constructor

void fraction::operator=(const fraction& a)
{
this->_numerator=a._numerator;
this->_denominator=a._denominator;
}

// You must know the meaning of +-*/, don't you ?
fraction fraction::operator+(const fraction& a) const
{
fraction t;
//Setting the numerator to Fraction class
t._numerator = a._numerator * _denominator + a._denominator * _numerator;
//Setting the denominator to Fraction class
t._denominator = a._denominator * _denominator;
t.simp();
return t;

}
fraction fraction::operator-(const fraction& a) const
{
//Getting numerator and Denominator of Current class
int e=this->_numerator;
int f=this->_denominator;
//Getting numerator and Denominator of Parameter Fraction class
int c=a._numerator;
int d=a._denominator;
//Setting the numerator to Fraction class
int subnumer=(e*d - f*c);
//Setting the denominator to Fraction class
int denom=(f*d);
fraction frac(subnumer,denom);
frac.simp();
return frac;

}
fraction fraction::operator*(const fraction& a) const
{
fraction t;
//Setting the numerator to Fraction class
t._numerator = a._numerator * _numerator;
//Setting the denominator to Fraction class
t._denominator = a._denominator * _denominator;
t.simp();
return t;

}
fraction fraction::operator/(const fraction& a) const
{
fraction t;
//Setting the numerator to Fraction class
t._numerator = _numerator * a._denominator;
//Setting the denominator to Fraction class
t._denominator = a._numerator * _denominator;
t.simp();
return t;

}

void fraction::operator+=(const fraction& a)
{
this->_numerator=a._numerator * _denominator + a._denominator * _numerator;
this->_denominator= a._denominator * _denominator;
}
void fraction::operator-=(const fraction& a)
{
int e=this->_numerator;
int f=this->_denominator;
//Getting numerator and Denominator of Parameter Fraction class
int c=a._numerator;
int d=a._denominator;
//Setting the numerator to Fraction class
this->_numerator=(e*d - f*c);
//Setting the denominator to Fraction class
this->_denominator=(f*d);
  
}
void fraction::operator*=(const fraction& a)
{
//Setting the numerator to Fraction class
this->_numerator = a._numerator * _numerator;
//Setting the denominator to Fraction class
this->_denominator = a._denominator * _denominator;
}
void fraction::operator/=(const fraction &a)
{
//Setting the numerator to Fraction class
this->_numerator = _numerator * a._denominator;
//Setting the denominator to Fraction class
this->_denominator = a._numerator * _denominator;
}

// Comparison operators
bool fraction::operator==(const fraction &f) const
{
int a = this->_numerator;
int b = this->_denominator;
int c = f._numerator;
int d = f._denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;

}
bool fraction::operator!=(const fraction &f) const
{
int a = this->_numerator;
int b = this->_denominator;
int c = f._numerator;
int d = f._denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;

}
bool fraction::operator<(const fraction &f) const
{
int a = this->_numerator;
int b = this->_denominator;
int c = f._numerator;
int d = f._denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;

}
bool fraction::operator>(const fraction &f) const
{
int a = this->_numerator;
int b = this->_denominator;
int c = f._numerator;
int d = f._denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;

}
bool fraction::operator<=(const fraction &f) const
{
int a = this->_numerator;
int b = this->_denominator;
int c = f._numerator;
int d = f._denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;

}
bool fraction::operator>=(const fraction &f) const
{
int a = this->_numerator;
int b = this->_denominator;
int c = f._numerator;
int d = f._denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;

  
}

std::istream & operator>>(std::istream &din, fraction &c)
{

char ch;
din >> c._numerator;
din >> ch;
din >> c._denominator;
return din;
}
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
std::ostream & operator<<(std::ostream &dout, const fraction &c)
{
dout << c._numerator << "/" << c._denominator;
return dout;
  
}

_____________________________

// main.cpp

#include "fraction.h"

void print(const bool & f) {
if (f)
std::cout << "True" << std::endl;
else
std::cout << "False" << std::endl;
}

int main() {
fraction f1, f2;
std::cin >> f1 >> f2;
std::cout << f1 + f2 << ' ' << f1 - f2 << ' '
<< f1 * f2 << ' ' << f1 / f2 << std::endl;
f1 += f2;
f1 -= f2;
f1 *= f2;
f1 /= f2;
std::cout << f1 << std::endl;
print(f1 == f2);
print(f1 != f2);
print(f1 < f2);
print(f1 > f2);
print(f1 <= f2);
print(f1 >= f2);
return 0;
}

_________________________

Output:

_____________________Thank You


Related Solutions

Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
For this programming assignment, you will use your previous code that implemented a video game class...
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user. #include <iostream> #include...
C++ In this assignment you will use your Card class to simulate a deck of cards....
C++ In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows: The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers. The destructor will free the...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
This programming assignment will consist of a C++ program. Your program must compile correctly and produce...
This programming assignment will consist of a C++ program. Your program must compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting described in the Required Program Development Best Practices document that has been discussed in class and is posted to the eLearning system. Please see this descriptive file on the eLearning system for more details. The name to use in the main configuration screen text box Name: [ ] in...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT