1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
using namespace std;
//class stuff
class fraction // a/bi
{
private:
int n,d,temp,x,y,g; // the variables which "make up" the fraction number a/bi
public:
//constructor declarations
fraction(); //default constructor
fraction(double); //1-parameter constructor
fraction(double,double); //maximal constructor (because user sets both a and b)
//class function declarations
double num(); //gives the value of numerator
double denom(); //gives the value of denominator
double convert(); // converts to decimal - (num*1.00)/denom
double gcd(); // finds gcd
double reduce(); // reduces the fraction
void reciprocal();
void show(); //displays a/bi to monitor
void set(double,double); //changes a/bi to c/di
};
//Constructor definitions
fraction::fraction() // 0/1
{
n=0,d=1;
}
fraction::fraction(double x) // x/1
{
n=x,d=1;
}
fraction::fraction(double x, double y) // x/yi
{
n=x, d=y;
}
//Function definitions
double fraction::num() //a
{
return n;
}
double fraction::denom() //b
{
return d;
}
double fraction::convert() //fraction to decimal
{
return ((n*1.00)/d);
}
double fraction::gcd()
{
while(d!=0)
{
temp=n;
n=d;
d=temp%d;
}
return n;
}
double fraction::reduce()
{
cout << n/(gcd()) << "/" << d/(gcd());
}
void fraction::show() //displays a/bi to monitor
{
cout << n << "/" << d;
};
void fraction::reciprocal()
{
cout << d << "/" << n;
};
void fraction::set(double e, double f) //changes a/bi to c/di
{
n=e, d=f;
};
fraction operator* (fraction z1, fraction z2)
{
double n=z1.num()*z2.num();
double d=z1.denom()*z2.denom();
fraction z3(n,d);
return z3;
}
int main()
{
fraction x; //uses default constructor (no parameters provided)
fraction y(5); //uses the 1 - parameter contructor (1 - parameter provided)
fraction z(2,-3); //uses the maximal constructor (2 - parameters provided)
// What can I do with x, y, and z? real, imag, modulus, conjugate, show, set
cout << "The numerator of x is " << x.num()
<< " and the denominator of x is " << x.denom() << endl;
cout << "The numerator of y is " << y.num()
<< " and the denominator of y is " << y.denom() << endl;
cout << "The numerator of z is " << z.num()
<< " and the denominator of z is " << z.denom() << endl << endl;
cout << "Where x=";
x.set(3,4); //uses set function x
x.show();
cout << ", the decimal value of x is " << x.convert() << endl << endl;
fraction d;
cout << "Enter a fraction: numerator 'Enter' denominator\n";
int a,b;
cin >> a >> b;
z.set(a,b);
cout << a << "/" << b << " converted to a decimal is= " << z.convert() << endl;
z.set(a,b);
cout << a << "/" << b << " has a GCD = " << z.gcd() << "\n\n";
z.set(a,b);
cout << z.reduce() << "\n\n";
system("PAUSE");
return 0;
}Put the code you need help with here.
|