Posted by MindBreaker at 9:04 AM
Read our previous post
Object
Transform the fraction structure from Exercise 8 in
Chapter 4 into a fraction class. Member data is the fraction’s numerator and
denominator. Member functions should accept input from the user in the form 3/5, and
output the fraction’s value in the same format. Another member function should add two
fraction values. Write a main() program that allows the user to repeatedly input two
fractions and then displays their sum. After each operation, ask whether the user wants to
continue.
Source
Code:
#include<iostream>
using namespace std;
class fraction
{
private:
int a,b,c,d,e,f;
char dummychar;
public:
void getdata()
{
cout<<"Enter first fraction =";
cin>>a;cin>>b;
cout<<"Enter second fraction =";
cin>>c;cin>>d;
}
void showdata()
{
if(b==d)
{
e=a+c;
f=b;
}
else if(b!=d)
{
e=a*d+c*b;
f=b*d;
}
cout<<"The sum of fraction is = "<<e<<"/"<<f<<"\n";}
};
int main()
{ char choice;
fraction f;
do{
f.getdata();
f.showdata();
cout<<"do you want to continue (y,n)";
cin>>choice; }
while(choice=='y');
}
Output
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.