Server IP : 172.16.15.8 / Your IP : 18.191.202.48 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 (0755) : /home/kahenry/../zwang/cs212/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// CS212 CS 2 Assignment 3 // Due: Monday February 28, 2011 // // File Name: cs2assign3.cpp // Author: Jennifer Whiley // Instuctor: Dr. Wang // // Compile: g++ cs2assign3.cpp // Run: ./a.out < in2.txt > out3.txt // Goal: The program will read fractions (using class) // and output each student's average to a file called out3.txt; // the program will perform the operations indicated. // ("+", "-", "*", "/") #include <iostream> using namespace std; class Fraction { int numr; int deno; /* void reduce() { int n = numr; int d = deno; while(d != 0) { int f = n % d; n = d; d = f; } if (d != 0) { numr /= n; deno /= n; } if (deno < 0) { numr *= (-1); deno *= (-1); } } */ void reduce() { int n = numr; int d = deno; //find GCD: n while( d != 0 ) { int r = n % d; n = d; d = r; } // process if ( n != 0 ) { numr /= n; deno /= n; } if (deno < 0) { numr *= (-1); deno *= (-1); } } public: Fraction() { numr = 0; deno = 1; } Fraction(int n, int d) { numr = n; deno = d; } void Set(int n, int d) { numr = n; deno = d; // reduce(); } void Print() const { cout << numr << "/" << deno; } Fraction Addition (Fraction x) { Fraction ans; ans.numr = numr * x.deno + deno * x.numr; ans.deno = deno * x.deno; // use the reduce() // ???? ans.reduce(); return ans; } Fraction Subtraction (Fraction x) { Fraction ans; ans.numr = numr * x.deno - deno * x.numr; ans.deno = deno * x.deno; ans.reduce(); return ans; } Fraction Multiplication (Fraction x) { Fraction mult; mult.numr = numr * x.numr; mult.deno = deno * x.deno; mult.reduce(); return mult; } Fraction Division (Fraction x) { Fraction div; div.numr = numr * x.deno; div.deno = deno * x.numr; div.reduce(); return div; } }; int main() { Fraction a, b, c; // c = a + b Fraction d, e, f; // d = a - b; e = a * b int an, ad, bn, bd; cout << "The 1st fraction (nume, deno); "; cin >> an >> ad; cout << "The 2nd fraction (nume, deno); "; cin >> bn >> bd; a.Set(an, ad); b.Set(bn, bd); c = a.Addition(b); e = a.Multiplication(b); a.Print(); // 1/2 cout << " + "; b.Print(); // 3/4 cout << " = "; c.Print(); cout << "\n\n"; a.Print(); // 1/2 cout << " * "; b.Print(); // 3/4 cout << " = "; e.Print(); cout << "\n\nHi.\n\n."; return 0; }