I need to write a program that tells the user to input one fraction(numerator and denominator), along with two other fractions that are given. The program must +,-,/,*,reduce,reciprocate, and find if the given fractions are greater less than or equal to one another. I have no clue of even where to start for the main program. heres what i got
mandm911 0 Newbie Poster
#include<iostream>
#include<cmath>
using namespace std;
class fraction
{
public:
int numer , denom;
fraction();//Default Constructor
fraction(int n, int d);//Alternate Constructor
int GCF();//Finds The GCF
fraction reciprocal();//Displays Reciprocal
void display();//Displays the Fraction
void reduce();//Puts Fraction into simplest form
double convert();//Converts to Decimal
void assign(int n, int d);//Assigns numerators and denoms
int getnumer();//gets the numerator
int getdenom();//gets the denominator
friend ostream& operator << (ostream& outs, const fraction f1);
//Display Operator
fraction operator * (fraction f1); //Multiplication
fraction operator / (fraction f1); //Division
fraction operator + (fraction f1); //Addition
fraction operator - (fraction f1); //Subtraction
bool operator ==(fraction f1);//Equals
bool operator <(fraction f1);//Less Than
bool operator >(fraction f1);//Greater Than
};//EndPublic
ostream& operator << (ostream& outs,const fraction f1)//<< Display Operator
{
outs<<f1.numer<<"/"<<f1.denom;
return outs;
}
fraction::fraction()//Default Constructor
{ }
fraction::fraction(int n, int d) //Alternate Constructor
{
numer=n;
denom=d;
}
int fraction :: getnumer()//gets numerator
{
return numer;
}
int fraction :: getdenom()//gets denom
{
return denom;
}
void fraction::assign(int n, int d)//assigns num and denom to fraction
{ numer = n;
denom = d;
}
void fraction:: display()//Displays
{
cout<<numer<<"/"<<denom;
}
int fraction::GCF()//Finds GCF
{
bool done= false;
int gcf=0;
if (abs(numer)<= abs(denom))
{
gcf=abs(numer);
}//End If
else
{
gcf=abs(denom);
}//End Else
while (gcf > 1 &&! done)
{
if (numer%gcf== 0 && denom%gcf == 0)
{
done= true;
}//End If
else
{
gcf --;
}//End Else
}//End While
return gcf;
}
void fraction::reduce()//Changes To Simplest Form
{
int gcf = GCF();
numer=numer/gcf;
denom=denom/gcf;
}
fraction fraction::operator * (fraction f1)//Multiplication
{
fraction temp;
temp.numer= numer*f1.numer;
temp.denom= denom*f1.denom;
temp.reduce();
return temp;
}
fraction fraction::operator / (fraction f1)//Division
{
fraction temp;
temp.numer= numer*f1.denom;
temp.denom= denom*f1.numer;
temp.reduce();
return temp;
}
fraction fraction::operator + (fraction f1)//Addition
{
fraction temp;
temp.denom=denom*f1.denom;
temp.numer=(numer*f1.denom) + (denom*f1.numer);
temp.reduce();
return temp;
}
fraction fraction::operator - (fraction f1)//Subtraction
{
fraction temp;
temp.denom=denom*f1.denom;
temp.numer=(numer*f1.denom)-(denom*f1.numer);
temp.reduce();
return temp;
}
double fraction::convert()//Converts to Decimal
{
double decimal;
decimal=static_cast<double>(numer)/static_cast<double>(denom);
return decimal;
}
fraction fraction::reciprocal() //Recriprocal
{
fraction temp;
temp.numer=denom;
temp.denom=numer;
return temp;
}
bool fraction::operator ==(fraction f1)//Equals
{
return convert() == f1.convert();
}
bool fraction::operator <(fraction f1)//Less Than
{
return convert() < f1.convert();
}
bool fraction::operator >(fraction f1)//Greater Than
{
return convert() > f1.convert();
}
#include <iostream>
#include <"classfract.cpp">
using namespace std;
int main()
{
int n,d;
cout<<"Enter numerator";
cin>>n;
cout<<"Enter denomator";
cin>>d;
Fraction f;
f1.assign(int n, int d);
cout<<assign;
f1.display();
f1.Convert();
f1.Reduce();
f1.Fraction();
f1.GCF();
f1.gcf;
f1.GetDenom();
f1.
daviddoria 334 Posting Virtuoso Featured Poster
If possible, please post a reasonable compilable length of code using code tags. You will likely get many more responses if people can look at your code directly in the browser versus having to download and open it.
mandm911 0 Newbie Poster
#include<iostream>
#include<cmath>
using namespace std;
class fraction
{
public:
int numer , denom;
fraction();//Default Constructor
fraction(int n, int d);//Alternate Constructor
int GCF();//Finds The GCF
fraction reciprocal();//Displays Reciprocal
void display();//Displays the Fraction
void reduce();//Puts Fraction into simplest form
double convert();//Converts to Decimal
void assign(int n, int d);//Assigns numerators and denoms
int getnumer();//gets the numerator
int getdenom();//gets the denominator
friend ostream& operator << (ostream& outs, const fraction f1);
//Display Operator
fraction operator * (fraction f1); //Multiplication
fraction operator / (fraction f1); //Division
fraction operator + (fraction f1); //Addition
fraction operator - (fraction f1); //Subtraction
bool operator ==(fraction f1);//Equals
bool operator <(fraction f1);//Less Than
bool operator >(fraction f1);//Greater Than
};//EndPublic
ostream& operator << (ostream& outs,const fraction f1)//<< Display Operator
{
outs<<f1.numer<<"/"<<f1.denom;
return outs;
}
fraction::fraction()//Default Constructor
{ }
fraction::fraction(int n, int d) //Alternate Constructor
{
numer=n;
denom=d;
}
int fraction :: getnumer()//gets numerator
{
return numer;
}
int fraction :: getdenom()//gets denom
{
return denom;
}
void fraction::assign(int n, int d)//assigns num and denom to fraction
{ numer = n;
denom = d;
}
void fraction:: display()//Displays
{
cout<<numer<<"/"<<denom;
}
int fraction::GCF()//Finds GCF
{
bool done= false;
int gcf=0;
if (abs(numer)<= abs(denom))
{
gcf=abs(numer);
}//End If
else
{
gcf=abs(denom);
}//End Else
while (gcf > 1 &&! done)
{
if (numer%gcf== 0 && denom%gcf == 0)
{
done= true;
}//End If
else
{
gcf --;
}//End Else
}//End While
return gcf;
}
void fraction::reduce()//Changes To Simplest Form
{
int gcf = GCF();
numer=numer/gcf;
denom=denom/gcf;
}
fraction fraction::operator * (fraction f1)//Multiplication
{
fraction temp;
temp.numer= numer*f1.numer;
temp.denom= denom*f1.denom;
temp.reduce();
return temp;
}
fraction fraction::operator / (fraction f1)//Division
{
fraction temp;
temp.numer= numer*f1.denom;
temp.denom= denom*f1.numer;
temp.reduce();
return temp;
}
fraction fraction::operator + (fraction f1)//Addition
{
fraction temp;
temp.denom=denom*f1.denom;
temp.numer=(numer*f1.denom) + (denom*f1.numer);
temp.reduce();
return temp;
}
fraction fraction::operator - (fraction f1)//Subtraction
{
fraction temp;
temp.denom=denom*f1.denom;
temp.numer=(numer*f1.denom)-(denom*f1.numer);
temp.reduce();
return temp;
}
double fraction::convert()//Converts to Decimal
{
double decimal;
decimal=static_cast<double>(numer)/static_cast<double>(denom);
return decimal;
}
fraction fraction::reciprocal() //Recriprocal
{
fraction temp;
temp.numer=denom;
temp.denom=numer;
return temp;
}
bool fraction::operator ==(fraction f1)//Equals
{
return convert() == f1.convert();
}
bool fraction::operator <(fraction f1)//Less Than
{
return convert() < f1.convert();
}
bool fraction::operator >(fraction f1)//Greater Than
{
return convert() > f1.convert();
}
Edited by mandm911 because: n/a
mandm911 0 Newbie Poster
#include <iostream>
#include <"classfract.cpp">
using namespace std;
int main()
{
int n,d;
cout<<"Enter numerator";
cin>>n;
cout<<"Enter denomator";
cin>>d;
Fraction f;
f1.assign(int n, int d);
cout<<assign;
f1.display();
f1.Convert();
f1.Reduce();
f1.Fraction();
f1.GCF();
f1.gcf;
f1.GetDenom();
f1.
Edited by mandm911 because: n/a
daviddoria 334 Posting Virtuoso Featured Poster
Please use code tags. That is
[ code ]
your code
[ /code ]
(but remove the spaces between the brackets and the word "code")
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.