Server IP : 172.16.15.8 / Your IP : 3.133.140.88 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 ] |
---|
// 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 (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; } void Print() const { cout << numr << "/" << deno; } Fraction operator+(Fraction x) { Fraction ans; ans.numr = (numr * x.deno) + (deno * x.numr); ans.deno = deno * x.deno; ans.Set(ans.numr,ans.deno); ans.reduce(); return ans; } Fraction operator- (Fraction x) { Fraction sub; sub.numr = (numr * x.deno) - (deno * x.numr); sub.deno = deno * x.deno; sub.Set(sub.numr,sub.deno); sub.reduce(); return sub; } Fraction operator* (Fraction x) { Fraction mult; mult.numr = numr * x.numr; mult.deno = deno * x.deno; mult.Set(mult.numr,mult.deno); mult.reduce(); return mult; } Fraction operator/(Fraction x) { Fraction div; div.numr = numr * x.deno; div.deno = deno * x.numr; div.Set(div.numr,div.deno); div.reduce(); return div; } }; int main() { Fraction a; Fraction b; Fraction c; int an, ad; int bn, bd; char dummy; char opr; while(cin) { cin >> an >> dummy >> ad >> opr >> bn >> dummy >> bd; a.Set(an, ad); b.Set(bn, bd); if (opr == '+') { c = a + b; // a.Print(); // cout << " " << opr << " "; // b.Print(); // cout << " = "; // c.Print(); // cout << "\n"; } if (opr == '-') { c = a - b; // a.Print(); // cout << " " << opr << " "; // b.Print(); // cout << " = "; // c.Print(); // cout << "\n"; } if (opr == '*') { c = a * b; // a.Print(); // cout << " " << opr << " "; // b.Print(); // cout << " = "; // c.Print(); // cout << "\n"; } if (opr == '/' && "\n") { c = a / b; // a.Print(); // cout << " " << opr << " "; // b.Print(); // cout << " = "; // c.Print(); // cout << "\n"; } a.Print(); cout << " " << opr << " "; b.Print(); cout << " = "; c.Print(); cout << "\n"; } return 0; }