Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Fraction {
- public:
- int num, denom; //numerator, denominator
- Fraction(int n, int d) {
- num = n;
- denom = d;
- }
- void simp() { // simplifier
- int a = num, b = denom;
- while (b != 0) {
- int c = a % b;
- a = b;
- b = c;
- }
- num = num / a;
- denom = denom / a;
- }
- void print() {
- cout << num << " / " << denom << endl;
- }
- };
- void add(Fraction f1, Fraction f2) { // addition
- int nod = 0;
- Fraction f3(0, 0);
- f3.num = f1.num*f2.denom + f1.denom*f2.num;
- f3.denom = f1.denom * f2.denom;
- f3.simp();
- f3.print();
- }
- void subtr(Fraction f1, Fraction f2) { // subtraction
- int nod = 0;
- Fraction f3(0, 0);
- f3.num = f1.num*f2.denom - f1.denom*f2.num;
- f3.denom = f1.denom * f2.denom;
- f3.simp();
- f3.print();
- }
- void mult(Fraction f1, Fraction f2) { // multiplication
- Fraction f3(0, 0);
- f3.num = f1.num*f2.num;
- f3.denom = f1.denom*f2.denom;
- f3.simp();
- f3.print();
- }
- void div(Fraction f1, Fraction f2) { // dividing
- if (f1.denom == 0 || f2.denom == 0) cout << "Impossible" << endl;
- else {
- Fraction f3(0, 0);
- f3.num = f1.num * f2.denom;
- f3.denom = f1.denom * f2.num;
- f3.simp();
- f3.print();
- }
- }
- int main()
- {
- Fraction f1(4, 7), f2(1, 6);
- add(f1, f2);
- subtr(f1, f2);
- mult(f1, f2);
- div(f1, f2);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement