Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

oo Verizon 9:56 AM 86% ysu.blackboard.com Programming and Problem Solving CSIS 2

ID: 3865426 • Letter: O

Question

oo Verizon 9:56 AM 86% ysu.blackboard.com Programming and Problem Solving CSIS 2610 Surnmer Semester 2017-CRN 33460 Project 3 Fraction Class Due date: Thursday August 3. 2017 Goal Develop and implement a program hat creates and uses objects that store fractions Details A data stucture s a way of organizing data that tacilnates some way of manipuiating the data For example, a queuea contoner that tocanates first-ntrst-out sooge of elements. lnthis proiect, we wil ate a Fraction class that enables a program to create and perform basic arthmetic on fractions. A raction object contains an integer numerator and denominator with two propertios The numerator and denominator have no common foctor other than 1 . The numerator is always nonnegative A Fraction o ect must support the folong actions: -This con intiale to a default value (oV1).provide just a numerator Cn/1) or provide bot a numerator and denominator Set a fraction to a specifc value -This has the same options as initiale . Add two fractions ·Subtoet two fractions ·M. tply two hctions Divide two froctons ·nput and output fractions The arithmelic functions should have two fomns: c.subtract(a,b) computesc-a-b c.subtract(a) . computes c=c-a Programming and Problem Soving Project 3-Fraction Class Summer 2017-CRN 33450

Explanation / Answer

#include<iostream>

using namespace std;
  


class Fraction {

private:
   int num;
   int den;
   int gcd(int a, int b) {
       int tmp;
       while (a!=b) {
           if (a > b)
               a -= b;
           else
               b -= a;
       }
       return a;
   }
   void reduce() {
       int gcd = gcd(num, den);
       num /= gcd;
       den /= gcd;
   }
public:
   Fraction(int n, int d) {
       num = n;
       den = d;
   }
  
   Fraction() {
       num = 0;
       den = 1;
   }

   void set (int n, int d) {
       num = n;
       den = d;
   }

   void subtract(Fraction a) {
       num = num*a.den - a.num*den;
       den = den * a.den;
       reduce();
   }
  
   void subtract(Fraction a, Fraction b) {
       num = a.num*b.den - b.num*a.den;
       den = a.den * b.den;
       reduce();
   }

   void multiply(Fraction a) {
       num = num*a.num;
       den = den * a.den;
       reduce();
   }
  
   void multiply(Fraction a, Fraction b) {
       num = a.num*b.num;
       den = a.den * b.den;
       reduce();
   }

   void divide(Fraction a) {
       num = num*a.den;
       den = den * a.num;
       reduce();
   }
  
   void divide(Fraction a, Fraction b) {
       num = a.num*b.den;
       den = a.den * b.num;
       reduce();
   }
  
   frienf ostream& operator<<(ostream &in, const Function &b) {
       out << b.num <<"/" << b.den;
   }


   frienf istream& operator>>(istream &in, const Function &b) {
       cout << "Enter Numerator: ";
       in >> b.num <<
       cout << "Enter denominator: ";
       in >> b.den;
   }
};

Seems complete. Let me know if i missed anything :)