Lab 3: Write a program in the C/C++ programming language to input and add two fr
ID: 665399 • Letter: L
Question
Lab 3:
Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions:
a/b + c/d = (a*d + b*c)/(b*d)
Example:
1/2 + 1/4 = ( 1(4) + 2(1) ) / 2(4) = 6/8 = 3/4
Compile your C/C++ program with the following command (assume your program is saved as lab1.C or lab1.cpp)
Re-design this using a C++ class. Use examples from the study notes on data modeling (w/ classes). Convert your data model from a structure to a C++ class. Convert your data model operations from using functions to class member functions. Re-design your main program using objects from your class definition to perform the same functionality as in lab2. Use the example(s) from the notes to assist you in the re-design.
I have already completed the above lab (lab 3). I need help with lab 5, as shown below:
Lab 5 – Fraction addition (with overloaded operators)
Re-write the add method for your Fraction class (from lab3) as an overloaded addition (+) operator. Your main program should perform the same as in lab3. If time permits (but not required), implement additional mathematical operations by overloading other operators in your Fraction class. As in lab4, implement your Fraction class in separate source and header files.
Compile your program using the GNU C++ compiler, g++, as follows:
jdoe@shaula % g++ -c Fraction.C
jdoe@shaula % g++ -c lab5.C
jdoe@shaula % g++ -o lab5 lab5.o Fraction.o
(lab5 is the resultant program executable name)
Urgent help needed!
Explanation / Answer
#include<iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction()
{ numerator = 0;
denominator=1;
}
void getFraction();
void showFraction();
Fraction operator+(Fraction);
};
void Fraction::getFraction()
{
cout<<" Enter the fraction as follows : ";
cout<<"Enter numerator : ";
cin>>numerator;
cout<<"Enter denominator : ";
cin>>denominator;
}
void Fraction::showFraction()
{
cout<<"The fraction is : "<<numerator<<"/"<<denominator<<" ";
}
Fraction Fraction::operator+(Fraction f)
{
Fraction final;
final.numerator = (numerator*f.denominator)+(f.numerator*denominator); //Calculates numerator of result after addition
final.denominator = denominator*f.denominator; //Calculates denominator of result after addition
int flag;
do //This loop simplifies result with the least common denominator
{ int n=2;
flag=0;
if(final.numerator%final.denominator==0)
{
final.numerator=final.numerator/final.denominator;
final.denominator=1;
break;
}
while(n<=final.denominator/2)
{
if(final.denominator%n==0)
if(final.numerator%n==0)
{
final.numerator = final.numerator/2;
final.denominator = final.denominator/2;
flag=1;
}
n++;
}
}while(flag==1);
return final;
}
int main()
{
Fraction f1,f2,f3;
f1.getFraction();
f1.showFraction();
f2.getFraction();
f2.showFraction();
f3 = f1+f2; //Here '+' is the overloaded constructor used to add fractions f1,f2 and stores their result in f3
cout<<" The result after adding the fractions gives ";
f3.showFraction();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.