i need to write a program to calculate fractions, here is my header file:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
using namespace std;
class Fraction {
private:
int *numer;
int *denom;
int gcd(int, int);
public:
void reduce();
int getNum();
int getDen();
Fraction();
Fraction(int numerator);
Fraction(int num, int den);
Fraction(string s); // take string parameter of the form of "numerator/defnominator
Fraction(Fraction &other); // copy constructor
Fraction & operator=(Fraction & rhs);
~Fraction();
// overloading arithematic operation
Fraction & operator+ (Fraction & rhs);
Fraction & operator- (Fraction & rhs);
Fraction & operator* (Fraction & rhs);
Fraction & operator/ (Fraction & rhs);
bool operator > (Fraction & rhs);
bool operator >= (Fraction & rhs);
bool operator == (Fraction & rhs);
bool operator < (Fraction & rhs);
bool operator <= (Fraction & rhs);
bool operator!=(Fraction & rhs);
Fraction & operator++();
Fraction & operator++(int);
Fraction & operator--();
Fraction & operator--(int);
Fraction & operator+=(Fraction & rhs);
Fraction & operator-=(Fraction & rhs);
Fraction & operator*=(Fraction & rhs);
Fraction & operator/=(Fraction & rhs);
// Exponentiation
Fraction & operator^(int n);
bool isZero();
bool isProper(); // a fracton is proper if abs(numerator) < (denominator)
bool isNegative();
bool isPositive();
operator string();
operator double();
string toString();
string toProperString(); // properString format of 15/4 is 3 3/4
friend ostream & operator << (ostream & out, Fraction & rhs);
friend istream & operator >> (istream & in, Fraction &rhs);
};
#endif
for the Fraction.cpp file:
I have the function that converts the faction format to string, here's what i have, but not sure if it is correct. Here is what i got now:
#include <iostream>
#include<cmath>
#include <cassert>
#include <string>
#include<iomanip>
#include <stdlib.h>
#include "Fraction.h"
#include <sstream>
using namespace std;
Fraction::Fraction()
{
numer = new int(0);
denom = new int(1);
//cout<<"construtor"<<endl;
}
int Fraction::getNum()
{
return *numer;
}
int Fraction::getDen()
{
return *denom;
}
int Fraction::gcd(int n, int d) //moving sign to numerator
{
//assert((n > 0 && d > 0)); //ensuring numerator and demominator have no common divisors
//while (n != d) // return the greatest common divisor
//{
// if (n < d)
// d = d - n;
// else
// n = n - d;
//}
//return n;
if(n<0)
n=-n;
if(d<0)
d=-d;
if(n<d)
return gcd(d,n);
if(d==0)
return n;
return gcd(d,n%d);
}
Fraction::Fraction(int numerator)
{
numer = new int(numerator);
denom= new int (1);
//printf("Fraction::Fraction(int numerator) \n");
}
Fraction::Fraction(int num, int den)
{
assert (den != 0);
numer = new int(num);
denom = new int(den);
reduce();
//cout<<"reduce function"<<endl;
}
void Fraction::reduce()
{
int sign = 1;
if (*numer < 0)
{
sign = -1;
*numer = -*numer;
}
if (*numer< 0)
{
sign = -1;
*denom = -*denom;
}
assert(*denom != 0);
int d = 1;
if (*numer>0)
{
d= gcd(*numer, *denom);
*numer = sign*(*numer / d);
*denom = *denom / d;
}
}
Fraction::Fraction(string s)
{
string delimiter = "/";
string n = s.substr(0, s.find(delimiter));
string d=s.substr(s.find(delimiter)+1,s.length());
numer = new int (atoi(n.c_str()));
denom = new int(atoi(d.c_str())); //every first time using pointer in constructor
cout << n << d << endl;
//constructor
}
Fraction::Fraction(Fraction &other)
{
numer = new int( other.getNum());
denom = new int(other.getDen());
//cout<<"copy construtor"<<endl;
}
Fraction::~Fraction()
{
delete numer;
numer = 0; //if (numer) if(denom)
delete denom;
denom = 0;
}
Fraction & Fraction::operator=(Fraction & rhs)
{
if (this != &rhs)
{ //do i need delete pointer here?
*numer = rhs.getNum();
*denom = rhs.getDen();
}
else
return *this;
}
Fraction & Fraction::operator+ (Fraction & rhs)
{
Fraction *result = new Fraction(this->getNum()*rhs.getDen() + this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen());
result->reduce();
return *result;
}
Fraction & Fraction::operator- (Fraction & rhs)
{
Fraction *result=new Fraction((this->getNum()*rhs.getDen() - this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen()));
result->reduce();
return *result;
}
Fraction & Fraction::operator*(Fraction & rhs)
{
Fraction *result=new Fraction((this->getNum()*rhs.getNum(), this->getDen()*rhs.getDen()));
result->reduce();
return *result;
}
Fraction & Fraction::operator/(Fraction & rhs)
{
Fraction *result=new Fraction((this->getNum()*rhs.getDen(), this->getDen()*rhs.getNum()));
result->reduce();
return *result;
}
bool Fraction::operator > (Fraction & rhs)
{
if (((float)getNum() / getDen())>((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator >= (Fraction & rhs)
{
if (((float)getNum() / getDen()) >= ((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator == (Fraction & rhs)
{
if (getNum()*rhs.getDen() == getDen()*rhs.getNum())
return true;
else
return false;
}
bool Fraction::operator < (Fraction & rhs)
{
if (((float)getNum() / getDen())<((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator <= (Fraction & rhs)
{
if (((float)getNum() / getDen()) <= ((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator!=(Fraction & rhs)
{
if (getNum()*rhs.getDen() == getDen()*rhs.getNum())
return false;
else
return true;
}
Fraction & Fraction::operator++() //pre fix
{
*numer += *denom;
reduce();
return *this;
}
Fraction & Fraction::operator++(int inInt) //post fix
{
Fraction *temp = new Fraction (*this);
*numer += *denom;
reduce();
return *temp;
}
Fraction & Fraction::operator--()
{
*numer -= *denom;
reduce();
return *this;
}
Fraction & Fraction::operator--(int)
{
Fraction *temp = new Fraction(*this);
*numer -= *denom;
reduce();
return *temp;
}
Fraction & Fraction::operator+=(Fraction & rhs)
{
*numer = (*numer)*rhs.getDen() + (*denom)*rhs.getNum();
*denom = (*denom)*rhs.getDen();
reduce();
return *this;
}
Fraction & Fraction::operator-=(Fraction & rhs)
{
*numer = (*numer)*rhs.getDen() - (*denom)*rhs.getNum();
*denom = (*denom)*rhs.getDen();
reduce();
return *this;
}
Fraction & Fraction::operator*=(Fraction & rhs)
{
*numer=(*numer)*rhs.getNum();
*denom=(*denom)*rhs.getDen();
reduce();
return *this;
}
Fraction & Fraction::operator/=(Fraction & rhs)
{
*numer=(*numer)*rhs.getDen();
*denom=(*denom)*rhs.getNum();
reduce();
return *this;
}
Fraction & Fraction::operator^(int n)
{
*numer = (double)pow((float)*numer, n);
*denom = (double)pow((float)*denom, n);
reduce();
return *this;
}
bool Fraction::isZero()
{
return (*numer) == 0;
}
bool Fraction::isProper()
{
if (abs(*numer)<abs(*denom))
return true;
else
return false;
}
bool Fraction::isNegative()
{
if (*numer<0)
return true;
else
return false;
}
bool Fraction::isPositive()
{
if (*numer>0)
return true;
else
return false;
}
Fraction::operator string()
{
return this->toString();
}
Fraction::operator double()
{
return ((double)(*numer) / (*denom));
}
string Fraction::toString()
{
char num[100], deom[100];
char *s = new char[50];
itoa(*numer, num, 10);
itoa(*denom, deom, 10);
char * delimiter = new char[2];
delimiter[0] = '\/';
delimiter[1] = '\0'; //stops copying delimiter
strcpy(s, num);
strcat(s, delimiter);
// strcat(s,'\0');
strcat(s, deom);
// strcat(s,'\0');
return s;
}
string Fraction::toProperString()
{
int a = *(this->numer) / *(this->denom);
int num = *(this->numer) % *(this->denom);
ostringstream ostr;
ostr <<a << " " << num << "/" << *(this->denom);
return ostr.str();
}
ostream & operator << (ostream & out, Fraction & rhs)
{
if(rhs.getDen()!=1)
{
out << rhs.getNum() << "/" << rhs.getDen();
}
else
cout<<rhs.getNum();
return out;
}
istream & operator >> (istream & in, Fraction &rhs)
{
int n, d;
in >> n;
char c;
in >> c;
if (c == '/')
in >> d;
else
{
in.putback(c);
d = 1;
}
rhs = Fraction(n, d);
return in;
}
And here's the main program
#include "Fraction.h"
void main()
{
Fraction a1;
Fraction a(1,2);
Fraction b(4,5);
Fraction c(6,8);
Fraction d(b);
Fraction g(-4,8);
Fraction g1(4,-10);
Fraction z(7,5);
cout << a1 << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << g << endl;
cout << g1 << endl;
string s = "3/4";
string s1 = "2/-3";
Fraction b1(s);
Fraction b2(s1);
cout << b1 << endl;
cout << b2 << endl;
a1 = b + c; cout << a1 << endl;
a1 = b-c ; cout << a1 << endl;
a1 = b*c ; cout << a1 << endl;
a1 = b / c; cout << a1 << endl;
b += a; cout << b << endl;
b -= a; cout << b << endl;
b /= a; cout << b << endl;
b++ ; cout << b << endl;
++b ; cout << b << endl;
b-- ; cout << b << endl;
--b ; cout << b << endl;
b /= a; cout << b << endl;
b *a ; cout << b << endl;
b^2 ; cout << b << endl;
cout << a.toString() << endl;
cout << z.toProperString() << endl;
Fraction f1(-4,5);
Fraction f2(0,1);
cout << "is f1 negative?" << f1.isNegative() << endl;
cout << "is f2 zero?" << f2.isZero() << endl;
cout << "is f1 a proper fraction?" << f1.isProper() << endl;
cout << "is f1 a positive fraction? " << f1.isPositive() << endl;
a = Fraction(9, 8);
b = Fraction(7, 8);
if (a < b)
cout << "a is smaller than b" << endl;
else
cout << " a is larger than b" << endl;
if(a==b)
cout << "a is equal to b" << endl;
else
cout << "a does not equal to b" << endl;
}
Since no one was willing to help me, so I post all the codes here for everyone. I took me a while to finish this project with the help of my TA.