Finished the fraction program/class structure. Completed Homework 2.
This commit is contained in:
parent
e1f5fe2639
commit
331b001c7c
3 changed files with 131 additions and 1 deletions
BIN
assignmentTwo/fraction
Executable file
BIN
assignmentTwo/fraction
Executable file
Binary file not shown.
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
Program Name: Fraction
|
||||
Process: ???
|
||||
Function Input/Parameters: ???
|
||||
Function Output/Parameters: ???
|
||||
Function Output/Returned: ???
|
||||
Device Input/Device: ???
|
||||
Device Output/Monitor: ???
|
||||
Dependencies: ???
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
#include <time.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// class definitions
|
||||
class Fraction
|
||||
{
|
||||
private:
|
||||
int numerator;
|
||||
int denominator;
|
||||
public:
|
||||
Fraction()
|
||||
{
|
||||
// default constructor, initializes to 0/1
|
||||
numerator = 0;
|
||||
denominator = 1;
|
||||
}
|
||||
Fraction(int numeratorInput, int denominatorInput)
|
||||
{
|
||||
numerator = numeratorInput;
|
||||
denominator = denominatorInput;
|
||||
}
|
||||
~Fraction()
|
||||
{
|
||||
// default destructor
|
||||
}
|
||||
// accessor functions
|
||||
// mutator functions
|
||||
// operators
|
||||
friend bool operator==(const Fraction &fractionOne, const Fraction &fractionTwo)
|
||||
{
|
||||
// return true if the numerator and the denominator are equal
|
||||
// return ((fractionOne.numerator == fractionTwo.numerator) && (fractionOne.denominator == fractionTwo.denominator)); // old version before hints
|
||||
return ((fractionOne.numerator * fractionTwo.denominator) == (fractionTwo.numerator * fractionOne.denominator));
|
||||
}
|
||||
friend Fraction operator+(const Fraction &fractionOne, const Fraction &fractionTwo)
|
||||
{
|
||||
return Fraction(((fractionOne.numerator * fractionTwo.denominator) + (fractionTwo.numerator * fractionOne.denominator)),
|
||||
(fractionOne.denominator * fractionTwo.denominator));
|
||||
}
|
||||
friend Fraction operator-(const Fraction &negateFraction)
|
||||
{
|
||||
return Fraction(-negateFraction.numerator, negateFraction.denominator);
|
||||
}
|
||||
friend Fraction operator-(const Fraction &fractionOne, const Fraction &fractionTwo)
|
||||
{
|
||||
return Fraction(((fractionOne.numerator * fractionTwo.denominator) - (fractionTwo.numerator * fractionOne.denominator)),
|
||||
(fractionOne.denominator * fractionTwo.denominator));
|
||||
}
|
||||
friend Fraction operator*(const Fraction &fractionOne, const Fraction &fractionTwo)
|
||||
{
|
||||
return Fraction((fractionOne.numerator * fractionTwo.numerator), (fractionOne.denominator * fractionTwo.denominator));
|
||||
}
|
||||
friend Fraction operator/(const Fraction &fractionOne, const Fraction &fractionTwo)
|
||||
{
|
||||
return Fraction((fractionOne.numerator * fractionTwo.denominator), (fractionOne.denominator * fractionTwo.numerator));
|
||||
}
|
||||
friend ostream &operator<<(ostream &out, const Fraction &fractionOut)
|
||||
{
|
||||
return out << fractionOut.numerator << "/" << fractionOut.denominator;
|
||||
}
|
||||
friend istream &operator>>(istream &in, Fraction &fractionIn)
|
||||
{
|
||||
// get a temporary string to read into.
|
||||
string inStr;
|
||||
// read in the string
|
||||
in >> inStr;
|
||||
// find position of the '/' character in the string (had to use the size_t to prevent errors/warnings.)
|
||||
size_t slashPos = inStr.find("/");
|
||||
// check to see if the slash was found, if it was, load the data.
|
||||
if(slashPos != string::npos)
|
||||
{
|
||||
// set the data in.
|
||||
fractionIn.numerator = atoi(inStr.substr(0, slashPos).c_str());
|
||||
fractionIn.denominator = atoi(inStr.substr(slashPos + 1).c_str());
|
||||
}
|
||||
return in;
|
||||
}
|
||||
};
|
||||
|
||||
// function definitions
|
||||
|
||||
// main function
|
||||
int main()
|
||||
{
|
||||
Fraction fiveFifths(5, 5);
|
||||
Fraction threeFifths(3, 5);
|
||||
Fraction sevenTenths(7, 10);
|
||||
Fraction fourTenths(4, 10);
|
||||
Fraction userFraction;
|
||||
// let user know the fractions.
|
||||
cout << "The fractions are: " << endl;
|
||||
cout << "\t" << fiveFifths << endl;
|
||||
cout << "\t" << threeFifths << endl;
|
||||
cout << "\t" << sevenTenths << endl;
|
||||
cout << "\t" << fourTenths << endl;
|
||||
// ask the user to input a fraction.
|
||||
cout << "Enter in a fraction in numerator/denominator format: [n/d]" << endl;
|
||||
cin >> userFraction;
|
||||
cin.ignore();
|
||||
// print back out the fraction
|
||||
cout << "Your fraction is: " << endl;
|
||||
cout << "\t" << userFraction << endl;
|
||||
// add seven tenths and four tenths
|
||||
cout << sevenTenths << " + " << fourTenths << " is " << sevenTenths + fourTenths << endl;
|
||||
// subtract five fifths from three fifths
|
||||
cout << fiveFifths << " - " << threeFifths << " is " << fiveFifths - threeFifths << endl;
|
||||
// multiply three fifths and four tenths
|
||||
cout << threeFifths << " * " << fourTenths << " is " << threeFifths * fourTenths << endl;
|
||||
// divide four tenths and three fifths.
|
||||
cout << fourTenths << " / " << threeFifths << " is " << fourTenths / threeFifths << endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -282,6 +282,5 @@ int main()
|
|||
cout << endl;
|
||||
cout << "Number of bundle objects: " << UserInventory::getBundleCount() << endl;
|
||||
}
|
||||
// code here.
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue