SlideShare a Scribd company logo
//The following is the (incomplete) header file for the class Fraction.
//You must complete the class header and implement all of its functions.
typedef unsigned int uint ;
class Fraction {
uint _numerator ;
uint _denominator ;
bool _isNegative ;
public :
//The sign of the first parameter determines the sign of the
//fraction
Fraction ( int pNum , uint pDen ) ;
//The denominator is assumed to be 1.
Fraction ( int pNum ) ;
//getters
uint numerator ( ) const ;
uint denominator ( ) const ;
//Returns the decimal result of performing the fraction's division
float value ( ) const ;
//Add prototypes here for:
//using the + operator to add two fractions.
//using the - operator to subtract two fractions.
//using < and > operators to compare fractions.
}
//Add the prototype here for the insertion operator.
Solution
#include
using namespace std;
class fraction
{
private:
int* num;
int* deno;
public:
fraction();
fraction(int,int);
fraction(const fraction&);
void printfrac();
fraction operator+(fraction);
fraction operator-(fraction);
fraction operator*(fraction);
fraction operator/(fraction);
void operator=(fraction);
};
void fraction::printfrac()
{
cout<<*(fraction::num)<<"/"<<*(fraction::deno);
}
inline fraction::fraction()
{
this->num=new int(0);
this->deno=new int(0);
}
inline fraction::fraction(int num,int deno)
{
this->num=new int(num);
this->deno=new int(deno);
}
inline fraction::fraction(const fraction&f)
{
fraction::num=new int;
fraction::deno=new int;
*(this->num)=*(f.num);
*(this->deno)=*(f.deno);
}
void swap(int &x,int &y)
{
int tmp=x;
x=y;
y=tmp;
}
int lcm(int l,int s)
{
if(lnum)=*(f.num);
*(this->deno)=*(f.deno);
}
fraction fraction::operator+(fraction f1)
{
int slcm=lcm(*(f1.deno),*(this->deno));
int num1=*(f1.num)*(slcm/ *(f1.deno));
int num2=*(this->num)*(slcm/ *(this->deno));
int num=num1+num2;
int t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
return f;
}
fraction fraction::operator-(fraction f2)
{
int slcm=lcm(*(this->deno),*(f2.deno));
int num2=*(f2.num)*(slcm / *(f2.deno));
int num1=*(this->num)*(slcm / *(this->deno));
int num=num1-num2;
int t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
return f;
}
fraction fraction::operator*(fraction f1)
{
int num1=*(f1.num)*(*(this->num));
int num2=*(f1.deno)*(*(this->deno));
int t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
return f;
}
fraction fraction::operator/(fraction f2)
{
int num1=*(this->num)*(*(f2.deno));
int num2=*(this->deno)*(*(f2.num));
int t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
return f;
}
int main()
{
int n1,n2,d1,d2,c;
char ch;
while(true)
{
cout<<" Enter numerator and denominator of 1st fraction:";
cin>>n1>>d1;
cout<<" Enter numerator and denominator of 2nd fraction:";
cin>>n2>>d2;
fraction f1(n1,d1);
fraction f2(n2,d2);
cout<<"Fraction 1: ";
f1.printfrac();
cout<<" Fraction 2: ";
f2.printfrac();
cout<<" ";
do
{
cout<<" Menu "<<"---------------- "<<"1.Addition 2.Multiplication 3.Subtraction
4.Division 5.Copy using copy constructor 6.Copy using assgnment operator ";
cout<<" Enter which operation you want to perform:";
cin>>c;
switch(c)
{
case 1:
{
fraction res=f1+f2;
cout<<" Result of addition:";
res.printfrac();
break;
}
case 2:
{
fraction res=f1*f2;
cout<<" Result of multiplication:";
res.printfrac();
break;
}
case 3:
{
fraction res=f1-f2;
cout<<" Result of subtraction:";
res.printfrac();
break;
}
case 4:
{
fraction res=f1/f2;
cout<<" Result of division:";
res.printfrac();
break;
}
case 5:
{
cout<<" New copy of 1st fraction:";
fraction res1=f1;
res1.printfrac();
cout<<" New copy of 2nd fraction:";
fraction res2=f2;
res2.printfrac();
break;
}
case 6:
{
cout<<" New copy of 1st fraction:";
fraction res1;
res1=f1;
res1.printfrac();
cout<<" New copy of 2nd fraction:";
fraction res2;
res2=f2;
res2.printfrac();
break;
}
default:
cout<<" Invalid choice!!!";
}
cout<<" Do you want to exit from program(Y/N):";
cin>>ch;
if(ch=='Y'||ch=='y')
return 0;
cout<<"Do you want to continue operation with these two fractions or want to continue with
two new fractions(Y/N):";
cin>>ch;
}while(ch=='Y'||ch=='y');
}
return 0;
}

More Related Content

PDF
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
PDF
write the To Dos to get the exact outputNOte A valid Fraction .pdf
PDF
Write a C++ program which create a class for working with mixed frac.pdf
PDF
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
PDF
(Rational Class) - use the original files to create the new progra.pdf
PDF
C++ manual Report Full
PDF
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
DOC
Pads lab manual final
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
Write a C++ program which create a class for working with mixed frac.pdf
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
(Rational Class) - use the original files to create the new progra.pdf
C++ manual Report Full
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Pads lab manual final

Similar to The following is the (incomplete) header file for the class Fracti.pdf (20)

PDF
JAVA Write a class called F.pdf
PDF
Help in JAVAThis program should input numerator and denominator f.pdf
DOCX
Write a class called Fraction with the two instance variables denomin.docx
PDF
fraction_math.c for Project 5 Program Design fraction.pdf
PDF
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
PDF
C++ Programming - 11th Study
PDF
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
DOCX
Friend function by sathya.docx
PDF
Building off of the Fraction class you wrote at the beginning of the s.pdf
PPT
DOCX
Oop project
PDF
I wrote the following change it to having a header, main and cpp fi.pdf
PDF
C++ TUTORIAL 4
PDF
AnswerNote Provided code shows several bugs, hence I implemented.pdf
PDF
#include iostream #include deque #include stdio.h   scan.pdf
PPTX
New presentation oop
PDF
When we test your Fraction.java we will use the given FractionTester.pdf
DOCX
cosc 281 hw3
PPTX
โปรแกรมย่อยและฟังชันก์มาตรฐาน
JAVA Write a class called F.pdf
Help in JAVAThis program should input numerator and denominator f.pdf
Write a class called Fraction with the two instance variables denomin.docx
fraction_math.c for Project 5 Program Design fraction.pdf
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
C++ Programming - 11th Study
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Friend function by sathya.docx
Building off of the Fraction class you wrote at the beginning of the s.pdf
Oop project
I wrote the following change it to having a header, main and cpp fi.pdf
C++ TUTORIAL 4
AnswerNote Provided code shows several bugs, hence I implemented.pdf
#include iostream #include deque #include stdio.h   scan.pdf
New presentation oop
When we test your Fraction.java we will use the given FractionTester.pdf
cosc 281 hw3
โปรแกรมย่อยและฟังชันก์มาตรฐาน

More from 4babies2010 (20)

PDF
Find the domain of the function. (Enter your answer using interval no.pdf
PDF
factors that are significant in the development of disease includ.pdf
PDF
economic institutions 8. According to Thomas Piketty, what is the si.pdf
PDF
Discuss bio films , formation location etc Discuss bio films , .pdf
PDF
Discuss the importance of documentation in the medical record.So.pdf
PDF
Determine the molecular geometry of CO32â».Solution .pdf
PDF
Define each class of incident.types of incidents classifcationsr.pdf
PDF
Complete a force field analysis for an organization with which you a.pdf
PDF
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
PDF
Why was the iodide ion successful for the SN2 reactions What if you.pdf
PDF
What were the triumphs and limitations of democracy in Periclean Ath.pdf
PDF
What process causes plant ion-uptake to acidify soil What soil pH r.pdf
PDF
what is the name and stereochemistry of the compoundSolution.pdf
PDF
What are some of the architectural decisions that you need to make w.pdf
PDF
What are lichens What are foliose lichensSolutionAnswers .pdf
PDF
These are questions to be answered in a paper about the Chernobyl in.pdf
PDF
The adjusted trial balance for Tybalt Construction as of December 31.pdf
PDF
Summarize the case Surgery Iturralde v. Hilo Medical Center USA , i.pdf
PDF
Suppose we draw 2 cards out of a deck of 52. Let A = “the first card.pdf
PDF
std deviationSolutionStandard deviation is the measure of disp.pdf
Find the domain of the function. (Enter your answer using interval no.pdf
factors that are significant in the development of disease includ.pdf
economic institutions 8. According to Thomas Piketty, what is the si.pdf
Discuss bio films , formation location etc Discuss bio films , .pdf
Discuss the importance of documentation in the medical record.So.pdf
Determine the molecular geometry of CO32â».Solution .pdf
Define each class of incident.types of incidents classifcationsr.pdf
Complete a force field analysis for an organization with which you a.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
Why was the iodide ion successful for the SN2 reactions What if you.pdf
What were the triumphs and limitations of democracy in Periclean Ath.pdf
What process causes plant ion-uptake to acidify soil What soil pH r.pdf
what is the name and stereochemistry of the compoundSolution.pdf
What are some of the architectural decisions that you need to make w.pdf
What are lichens What are foliose lichensSolutionAnswers .pdf
These are questions to be answered in a paper about the Chernobyl in.pdf
The adjusted trial balance for Tybalt Construction as of December 31.pdf
Summarize the case Surgery Iturralde v. Hilo Medical Center USA , i.pdf
Suppose we draw 2 cards out of a deck of 52. Let A = “the first card.pdf
std deviationSolutionStandard deviation is the measure of disp.pdf

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Computing-Curriculum for Schools in Ghana
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Computing-Curriculum for Schools in Ghana
O7-L3 Supply Chain Operations - ICLT Program
102 student loan defaulters named and shamed – Is someone you know on the list?
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Insiders guide to clinical Medicine.pdf
Classroom Observation Tools for Teachers
Renaissance Architecture: A Journey from Faith to Humanism
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial diseases, their pathogenesis and prophylaxis
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

The following is the (incomplete) header file for the class Fracti.pdf

  • 1. //The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigned int uint ; class Fraction { uint _numerator ; uint _denominator ; bool _isNegative ; public : //The sign of the first parameter determines the sign of the //fraction Fraction ( int pNum , uint pDen ) ; //The denominator is assumed to be 1. Fraction ( int pNum ) ; //getters uint numerator ( ) const ; uint denominator ( ) const ; //Returns the decimal result of performing the fraction's division float value ( ) const ; //Add prototypes here for: //using the + operator to add two fractions. //using the - operator to subtract two fractions. //using < and > operators to compare fractions. }
  • 2. //Add the prototype here for the insertion operator. Solution #include using namespace std; class fraction { private: int* num; int* deno; public: fraction(); fraction(int,int); fraction(const fraction&); void printfrac(); fraction operator+(fraction); fraction operator-(fraction); fraction operator*(fraction); fraction operator/(fraction); void operator=(fraction); }; void fraction::printfrac() { cout<<*(fraction::num)<<"/"<<*(fraction::deno); } inline fraction::fraction() { this->num=new int(0); this->deno=new int(0); } inline fraction::fraction(int num,int deno) { this->num=new int(num); this->deno=new int(deno); }
  • 3. inline fraction::fraction(const fraction&f) { fraction::num=new int; fraction::deno=new int; *(this->num)=*(f.num); *(this->deno)=*(f.deno); } void swap(int &x,int &y) { int tmp=x; x=y; y=tmp; } int lcm(int l,int s) { if(lnum)=*(f.num); *(this->deno)=*(f.deno); } fraction fraction::operator+(fraction f1) { int slcm=lcm(*(f1.deno),*(this->deno)); int num1=*(f1.num)*(slcm/ *(f1.deno)); int num2=*(this->num)*(slcm/ *(this->deno)); int num=num1+num2; int t=num; num=num/gcd(num,slcm); slcm=slcm/gcd(t,slcm); fraction f(num,slcm); return f; } fraction fraction::operator-(fraction f2) { int slcm=lcm(*(this->deno),*(f2.deno)); int num2=*(f2.num)*(slcm / *(f2.deno)); int num1=*(this->num)*(slcm / *(this->deno)); int num=num1-num2;
  • 4. int t=num; num=num/gcd(num,slcm); slcm=slcm/gcd(t,slcm); fraction f(num,slcm); return f; } fraction fraction::operator*(fraction f1) { int num1=*(f1.num)*(*(this->num)); int num2=*(f1.deno)*(*(this->deno)); int t=num1; num1=num1/gcd(num1,num2); num2=num2/gcd(t,num2); fraction f(num1,num2); return f; } fraction fraction::operator/(fraction f2) { int num1=*(this->num)*(*(f2.deno)); int num2=*(this->deno)*(*(f2.num)); int t=num1; num1=num1/gcd(num1,num2); num2=num2/gcd(t,num2); fraction f(num1,num2); return f; } int main() { int n1,n2,d1,d2,c; char ch; while(true) { cout<<" Enter numerator and denominator of 1st fraction:"; cin>>n1>>d1; cout<<" Enter numerator and denominator of 2nd fraction:"; cin>>n2>>d2;
  • 5. fraction f1(n1,d1); fraction f2(n2,d2); cout<<"Fraction 1: "; f1.printfrac(); cout<<" Fraction 2: "; f2.printfrac(); cout<<" "; do { cout<<" Menu "<<"---------------- "<<"1.Addition 2.Multiplication 3.Subtraction 4.Division 5.Copy using copy constructor 6.Copy using assgnment operator "; cout<<" Enter which operation you want to perform:"; cin>>c; switch(c) { case 1: { fraction res=f1+f2; cout<<" Result of addition:"; res.printfrac(); break; } case 2: { fraction res=f1*f2; cout<<" Result of multiplication:"; res.printfrac(); break; } case 3: { fraction res=f1-f2; cout<<" Result of subtraction:"; res.printfrac(); break; }
  • 6. case 4: { fraction res=f1/f2; cout<<" Result of division:"; res.printfrac(); break; } case 5: { cout<<" New copy of 1st fraction:"; fraction res1=f1; res1.printfrac(); cout<<" New copy of 2nd fraction:"; fraction res2=f2; res2.printfrac(); break; } case 6: { cout<<" New copy of 1st fraction:"; fraction res1; res1=f1; res1.printfrac(); cout<<" New copy of 2nd fraction:"; fraction res2; res2=f2; res2.printfrac(); break; } default: cout<<" Invalid choice!!!"; } cout<<" Do you want to exit from program(Y/N):"; cin>>ch; if(ch=='Y'||ch=='y') return 0;
  • 7. cout<<"Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):"; cin>>ch; }while(ch=='Y'||ch=='y'); } return 0; }