Server IP : 172.16.15.8 / Your IP : 18.225.72.161 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/jcwhiley/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// 1/2 + 3/4 = ? // Let the computer do this #include <iostream> using namespace std; class Fraction { int numr; int deno; // reduce the fractions - p. 115 // void reduce(); public: // 0/1 is the Fraction() { numr = 0; deno = 1; } Fraction(int n, int d) { numr = n; deno = d; } // function members void Set(int n, int d) { numr = n; deno = d; } void Print() const { cout << numr << "/" << deno; } Fraction Addition(Fraction x) { Fraction ans; // temporary ans.numr = numr * x.deno + deno * x.numr; ans.deno = deno * x.deno; // use reduce // ??? return ans; } Fraction Subtraction(Fraction x) { Fraction sub; sub.numr = numr * x.deno - deno * x.numr; sub.deno = deno * x.deno; return sub; } Fraction Multiplication(Fraction x) { Fraction mult; mult.numr = numr * x.numr; mult.deno = deno * x.deno; return mult; } }; // Question 1: How do you out both addends/multipliers/etc.? // Question 2: How do you do input from keyboard? // Question 3: How to reduce to lowest common denominator? int main() { Fraction a, b, c, d; // c = a + b Fraction e, f; // d = a - b; e = a * b // ANSWER TO Q2 int an, ad, bn, bd; cout << "The 1st fraction (numr, deno);" ; cin >> bn >> bd; a.Set(an,ad); b.Set(bn,bd); a.Set(1,2); b.Set(3,4); c = a.Addition(b); c.Print(); cout << "\n\n"; d = a.Subtraction(b); d.Print(); cout << "\n\n"; e = a.Multiplication(b); e.Print(); cout << "\n\n"; // ANSWER TO Q1 // Addition a.Print(); cout << " + "; b.Print(); cout << " = "; c.Print(); cout << "\n\n"; // Multiplication a.Print(); cout << " * "; b.Print(); cout << " = "; d.Print(); cout << "\n\n"; cout << "Hey!\n\n."; return 0; }