Question

I need to do object oriented programming for c++. I had to make make a program...

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);
   Fraction prod = prod.multiply(f1, f2);
   prod.print(prod);
   Fraction quot = quot.divide(f1, f2);
   quot.print(quot);

   return 0;
}

Header Class code:

#include <iostream>
using namespace std;

#pragma once
#ifndef FRACTION_H
#define FRACTION_H

class Fraction
{
private:
   int num;
   int den;
public:
   Fraction();
   Fraction(int, int);
   void setN(int);
   int getN();
   void setD(int);
   int getD();
   Fraction add(Fraction, Fraction);
   Fraction subtract(Fraction, Fraction);
   Fraction multiply(Fraction, Fraction);
   Fraction divide(Fraction, Fraction);
   Fraction simplify(Fraction);
   void print(Fraction);
};

Fraction::Fraction()
{
   setN(1);
   setD(1);
}

Fraction::Fraction(int n, int d)
{
   setN(n);
   setD(d);
}

void Fraction::setN(int n)
{
   num = (n > 0 ? n : 1);
}

void Fraction::setD(int d)
{
   den = (d > 0 ? d : 1);
}

int Fraction::getN()
{
   return num;
}

int Fraction::getD()
{
   return den;
}

Fraction Fraction::add(Fraction f1, Fraction f2)
{
   Fraction temp;

   temp.num = f1.num * f2.den + f2.num * f1.den;
   temp.den = f1.den * f2.den;

   return temp;
}

Fraction Fraction::subtract(Fraction f1, Fraction f2)
{
   Fraction temp;

   temp.num = f1.num * f2.den - f2.num * f1.den;
   temp.den = f1.den * f2.den;

   return temp;
}

Fraction Fraction::multiply(Fraction f1, Fraction f2)
{
   Fraction temp;

   temp.num = f1.num * f2.num;
   temp.den = f1.den * f2.den;

   return temp;
}

Fraction Fraction::divide(Fraction f1, Fraction f2)
{
   Fraction temp;

   temp.num = f1.num * f2.den;
   temp.den = f1.den * f2.num;

   return temp;
}

Fraction Fraction::simplify(Fraction f1)
{
   Fraction temp;
   for (int x = f1.den; x >= 1; x--)
   {
       if (f1.num % x == 0 && f1.den % x == 0)
       {
           temp.num = f1.num / x;
           temp.den = f1.den / x;
           break;
       }
   }
   return temp;
}

void Fraction::print(Fraction f)
{
   Fraction simp = simplify(f);
   cout << simp.num << "/" << simp.den << endl;
}

#endif


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

The wrong output is due to the setters method and also due to simplyfy function.

I modified those functions.

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

// Fraction.h

#include <iostream>
using namespace std;

#ifndef FRACTION_H
#define FRACTION_H

class Fraction
{
private:
int num;
int den;
public:
Fraction();
Fraction(int, int);
void setN(int);
int getN();
void setD(int);
int getD();
Fraction add(Fraction, Fraction);
Fraction subtract(Fraction, Fraction);
Fraction multiply(Fraction, Fraction);
Fraction divide(Fraction, Fraction);
Fraction simplify(Fraction);
void print(Fraction);
};
Fraction::Fraction()
{
setN(1);
setD(1);
}

Fraction::Fraction(int n, int d)
{
setN(n);
setD(d);
}

void Fraction::setN(int n)
{
num =n;
}

void Fraction::setD(int d)
{
den =d;
}

int Fraction::getN()
{
return num;
}

int Fraction::getD()
{
return den;
}

Fraction Fraction::add(Fraction f1, Fraction f2)
{
Fraction temp;


temp.num = f1.num * f2.den + f2.num * f1.den;
temp.den = f1.den * f2.den;

return temp;
}

Fraction Fraction::subtract(Fraction f1, Fraction f2)
{
Fraction temp;

temp.num = f1.num * f2.den - f2.num * f1.den;
temp.den = f1.den * f2.den;

return temp;
}

Fraction Fraction::multiply(Fraction f1, Fraction f2)
{
Fraction temp;

temp.num = f1.num * f2.num;
temp.den = f1.den * f2.den;

return temp;
}

Fraction Fraction::divide(Fraction f1, Fraction f2)
{
Fraction temp;

temp.num = f1.num * f2.den;
temp.den = f1.den * f2.num;

return temp;
}

Fraction Fraction::simplify(Fraction f1)
{
int n1, n2;
if(f1.num<0)   
n1 = -f1.num;
else
n1=f1.num;
  
if(f1.den<0)
n2 = -f1.den;
else
n2=f1.den;
  

int gcd=1;
  
for(int i = 1; i <= n1 && i <= n2; i++)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
  
Fraction f;
  

  
f.num = f1.num / gcd;
f.den = f1.den / gcd;

if(f.num<0 && f.den<0)
{
f.num*=-1;
f.den*=-1;
}
else if(f.den<0)
{
f.num*=-1;
f.den*=-1;
}
  
  
return f;

}

void Fraction::print(Fraction f)
{
Fraction simp = simplify(f);
cout << simp.num << "/" << simp.den ;
}

#endif

=================================

// main.h

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

int main()
{
  
Fraction f1(-4, 6);
Fraction f2(5, -9);

Fraction sum = sum.add(f1, f2);
f1.print(f1);
cout<<" + ";
f2.print(f2);
cout<<" = ";
sum.print(sum);
cout<<endl;
Fraction diff = diff.subtract(f1, f2);
f1.print(f1);
cout<<" - ";
f2.print(f2);
cout<<" = ";
diff.print(diff);
cout<<endl;


Fraction prod = prod.multiply(f1, f2);
f1.print(f1);
cout<<" + ";
f2.print(f2);
cout<<" = ";
prod.print(prod);
cout<<endl;

Fraction quot = quot.divide(f1, f2);
f1.print(f1);
cout<<" + ";
f2.print(f2);
cout<<" = ";
quot.print(quot);
cout<<endl;


return 0;
}

=============================

Output:


=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
I need to do object oriented programming for c++. I had to make make a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    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...

  • Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int);...

    Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int); int main() { f1(10); return 0; } void f1(int n) { f2(n + 5); } void f2(int n) { f3(n - 2); } void f3(int n) { cout << n << endl; // LINE 1 } Just before the program statement marked with the comment "LINE 1" is executed, how many stack frames will be on the program call stack?

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    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...

    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...

  • Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...

    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...

  • Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have...

    Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have set up a program in Visual Studio that will use operation between fractions. I have written a basic code example that does all the resources that you need to follow. Your job is to complete the exercises presented below in quest2.cpp. For this assignment you will learn and demonstrate how to: Work with functions use Past-by-Value and Past-by-Reference. use structs. use cin to get...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    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++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT