In: Computer Science
Lab 1
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
Lab 2
Re-design lab1 using data modeling design methods using the struct mechanism as discussed in the lecture notes entitled “Data Modeling (w/ structures)”. Your program should function exactly like lab1 with the only difference being the object-oriented manner in which it is written. Use the design example from the notes to assist you in the re-design.
I finished the above 2 labs but I need help with the following lab.
Re-design lab2 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.
Please help with this thank you
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<<"\nEnter the fraction as follows : \n";
cout<<"Enter numerator : ";
cin>>numerator;
cout<<"Enter denominator : ";
cin>>denominator;
}
void Fraction::showFraction()
{
cout<<"The fraction is :
"<<numerator<<"/"<<denominator<<"\n";
}
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<<"\nThe result after adding the fractions gives
\n";
f3.showFraction();
return 0;
}
#please consider my effort and give me a like...thank u....