Public feed #892074

Something

public text v1 · immutable
Raw Edit
·published 2008-02-05 03:02 UTC
rendered paste body

#include <iostream>
#include <stdlib.h>	// needed here for the 'abs()' function
using namespace std;

// Must declare the class and its members *before* any reference is made to them in main()
//
class Fraction {
	private :
		int num, den; // numerator & denominator values
	public:
		void set(int n, int d) { num = n; den = d; normalize(); } // inline function
		int get_num() { return num; }		// inline function
		int get_den() { return den; } 		// inline function
		Fraction add(Fraction other);		// this function accepts and returns a class object
		Fraction subtract(Fraction other);
		Fraction mult(Fraction other);		
		Fraction divide(Fraction other);
	private:
		void normalize();				// put fraction into standard form
		int gcf(int a, int b);				// greatest common factor
		int lcm(int a, int b);				// lowest common multiple
	}; 

int main() {
    int n1, d1;		// to hold the first numerator & denominator values input by the user
    int n2, d2; 	// to hold the second set of num/den
    char sign[4];	// a small string to hold the algebraic sign -- must be 4 char long (9,9,9,\0)
    
    Fraction fract1, fract2, fract3;	// Instantiate a new object (class member)
    
    while(1) {
		// User input section
		//    
	    cout << "Enter the first numerator (or 999 to quit): ";
	    cin >> n1;
	    if (n1 == 999)
	    	break;
	    	
	    cout << "Enter the first denominator: ";
	    cin >> d1;
	    cout << "Enter the second numerator: ";
	    cin >> n2;
	    cout << "Enter the second denominator: ";
	    cin >> d2;
	    
	    cout << "Now enter the algebraic operation you would like to perform (+, -, *, /): ";
	    cin >> sign;
	    
	    // must switch on individual character within the string -- not on whole string!
	    switch (sign[0]) { 
			case '+':
				fract1.set(n1, d1); 		// sets the values for fract1 as (n1, d1)
		    	fract2.set(n2, d2);			// sets the values for fract2 as (n2, d2)
		    	fract3 = fract1.add(fract2);
	    		cout << "Here is the sum of the two fractions you entered: ";
				cout << fract3.get_num() << "/" << fract3.get_den() << endl;
				break;
			
			case '-':
				fract1.set(n1, d1); 		// sets the values for fract1 as (n1, d1)
		    	fract2.set(n2, d2);			// sets the values for fract2 as (n2, d2)
		    	fract3 = fract1.subtract(fract2);
	    		cout << "Here is the difference of the two fractions you entered: ";
				cout << fract3.get_num() << "/" << fract3.get_den() << endl;
				break;
				
			case '*':
				fract1.set(n1, d1); 		// sets the values for fract1 as (n1, d1)
	    		fract2.set(n2, d2);			// sets the values for fract2 as (n2, d2)
		    	fract3 = fract1.mult(fract2);
		    	cout << "Here is the product of the two fractions you entered: ";
				cout << fract3.get_num() << "/" << fract3.get_den() << endl;
				break;
		
			case '/':
				fract1.set(n1, d1); 		// sets the values for fract1 as (n1, d1)
		    	fract2.set(n2, d2);			// sets the values for fract2 as (n2, d2)
		 	   	fract3 = fract1.divide(fract2);
	    		cout << "Here is the quotient of the two fractions you entered: ";
				cout << fract3.get_num() << "/" << fract3.get_den() << endl;
				break;
			
			default:
				cout << "You have entered an invalid operation. Please try again!" << endl;
				break;
		}	
	}
	
	cin.get();	// pause 
    return 0;
}

// Normalize function: put fraction into a standard form, unique for each
//  mathematically different value
//
void Fraction::normalize() { // must use the Fraction:: terminology, to indicate the proper namespace
	// handle cases involving zero
	if (den == 0 || num == 0) { // this would result in a 0/1, instead of a 1/0
		num = 0;
		den = 1;
	}
	
	// put negative sign in numerator only (ie; swap signs)
	if (den < 0) {
		num *= -1;
		den *= -1;
	}
	
	// Factor out GCF from numerator and denominator. Once inside a class function 
	//  ('Fraction::normalize()'), we don't need the Fraction:: any longer 
	int n = gcf(num, den); 	
	num /= n;			
	den /= n;
}

// Greatest Common Factor
//
int Fraction::gcf(int a, int b) {
	if (a % b == 0)
		return abs(b);
	else 
		return gcf(b, a%b);
	}
	
// Lowest Common Multiple
//
int Fraction::lcm(int a, int b) {
	return (a / gcf(a, b)) * b; // the scope is assumed here, and we do not need to say Fraction::gcf()
}

// Here is the new member functions for add and multiply. They accepts a class object and also both 
//  return class objects.
//
Fraction Fraction::add(Fraction other) {
	Fraction fract; // since this function will return a object of type Fraction, we must first create one
	
	// this is my version of the add() and it differs from the text's version & is much simpler
	fract.set(((num * other.den) + (other.num * den)), (den * other.den)); // recall: a/b + c/d = ((a*d) + (b*c) / (b*d))
	return fract;
}

Fraction Fraction::subtract(Fraction other) {
	Fraction fract; // since this function will return a object of type Fraction, we must first create one
	
	// this is my version of the add() and it differs from the text's version & is much simpler
	fract.set(((num * other.den) - (other.num * den)), (den * other.den)); // recall: a/b + c/d = ((a*d) + (b*c) / (b*d))
	return fract;
}

Fraction Fraction::mult(Fraction other) {
	Fraction fract;
	fract.set((num * other.num), (den * other.den));
	return fract;
}

Fraction Fraction::divide(Fraction other) {
	Fraction fract;
	fract.set((num * other.den), (den * other.num));
	return fract;
}