Create a structure called fraction. It has two members, both type int, called numerator and
denominator. Write a program that input values in two variables of type fraction. Add both
fractions and print the result in fraction format.
#include <iostream>
using namespace std;
struct fraction{
int numerator;
int denominator;
fraction operator+(fraction b){
return fraction{numerator+b.numerator, denominator+b.denominator};
}
};
ostream& operator<<(ostream& os, const fraction& fr)
{
os << "["<<fr.numerator << ',' << fr.denominator << ']';
return os;
}
int main()
{
fraction f1, f2, result;
cout<<"Enter numerator1: ";
cin>>f1.numerator;
cout<<"Enter denominator1: ";
cin>>f1.denominator;
cout<<"Enter numerator2: ";
cin>>f2.numerator;
cout<<"Enter denominator2: ";
cin>>f2.denominator;
result = f1+f2;
cout<< result;
return 0;
}
Comments
Leave a comment