forked from phoenixx1/Robert-Lafore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.cpp
53 lines (45 loc) · 1.33 KB
/
9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
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.
*/
// author @Nishant
#include<iostream>
using namespace std;
class addF{
int num, den;
public:
void getData();
void addFraction(addF, addF);
void display();
};
void addF::getData(){
char temp;
cin >> num >> temp >> den;
}
void addF::addFraction(addF f1, addF f2){
num = f1.num * f2.den + f1.den * f2.num;
den = f1.den * f2.den;
}
void addF::display(){
cout << "Sum is: " << num << "/" << den << endl;
}
int main(){
char choice;
addF f1, f2, f3;
do{
cout << "Enter first fraction in p/q: ";
f1.getData();
cout << "Enter second fraction in p/q: ";
f2.getData();
f3.addFraction(f1, f2);
f3.display();
cout << "Do you want to continue (y/n): ";
cin >> choice;
}while(choice == 'y' || choice == 'Y');
cout << endl;
return 0;
}