All pastes #609635 Raw Edit

Miscellany

public cpp v1 · immutable
·published 2007-07-09 00:18 UTC
rendered paste body
#include <iostream>using namespace std;class Rational{public: void input(); void output(); void simpleform(); Rational(int n, int d); Rational(int whole_number); Rational(); Rational add(Rational); Rational sub(Rational); Rational mul(Rational); Rational div(Rational); Rational neg(Rational); bool less(Rational); Rational operator + (Rational); Rational operator - (Rational); Rational operator * (Rational); Rational operator / (Rational); bool operator == (Rational); bool operator != (Rational); bool operator > (Rational); bool operator < (Rational); bool operator >= (Rational); bool operator <= (Rational);private: int numer, denom;};int main(){    Rational first, second;    first.input();    second.input();    Rational a,b,c,d;    bool e,f,g,h,i,j;    cout << "The two rational numbers : ";first.output(); cout<<"   "; second.output(); cout<<endl;    first.simpleform(); second.simpleform();    cout << "The two rational numbers in simple form: ";first.output(); cout<<"   "; second.output(); cout<<endl;    a = first + second;    a.output(); cout<<endl;    b = first - second;    b.output(); cout<<endl;    c = first * second;    c.output(); cout<<endl;    d = first / second;    d.output(); cout<<endl;    e = first > second;    f = first < second;    g = first <= second;    h = first >= second;    i = first == second;    j = first != second;    cout << e << f << g << h << i << j;    return 0;}void Rational::input(){    cout << "Please the Numerator" << endl;    cin >> numer;    cout << "Please the Denominator" << endl;    cin >> denom;}void Rational::output(){    cout << numer << "/" << denom;}Rational::Rational(int n,int d){    numer = n;    denom = d;}Rational::Rational(int whole_number){    numer = whole_number;    denom = 0;}Rational::Rational(){    numer = 0;    denom = 0;}Rational Rational::add(Rational c){    Rational temp;    temp.numer = ((numer*c.denom) + (denom*c.numer));    temp.denom = (denom * c.denom);    temp.simpleform();    return temp;}Rational Rational::sub(Rational c){    Rational temp;    temp.numer = numer - c.numer;    temp.denom = denom - c.denom;    temp.simpleform();    return temp;}Rational Rational::mul(Rational c){    Rational temp;    temp.numer = numer * c.numer;    temp.denom = denom * c.denom;    temp.simpleform();    return temp;}Rational Rational::div(Rational c){    Rational temp;    temp.numer = numer / c.numer;    temp.denom = denom / c.denom;    temp.simpleform();    return temp;}Rational Rational::neg(Rational c){    c.numer = -(c.numer);    c.denom = -(c.denom);    return c;}bool Rational::less(Rational c){    bool temp = 0;    if ((numer * c.denom) < (c.numer * denom))    temp = 1;    return temp;}void Rational::simpleform(){    float n=(float)this->numer;    float d=(float)this->denom;    int i=2;    while((i<=n) && (n!=1))      {        while((n/i==(int)n/i) && (d/i ==(int)d/i))         {            n/=i;            d/=i;         }        i++;      }    numer=n;    denom=(n==0)?1:d;}Rational Rational::operator + (Rational c){    Rational temp;    temp.numer = ((numer*c.denom) + (denom*c.numer));    temp.denom = (denom * c.denom);    temp.simpleform();    return temp;}Rational Rational::operator - (Rational c){    Rational temp;    temp.numer = numer - c.numer;    temp.denom = denom - c.denom;    temp.simpleform();    return temp;}Rational Rational::operator * (Rational c){    Rational temp;    temp.numer = numer * c.numer;    temp.denom = denom * c.denom;    temp.simpleform();    return temp;}Rational Rational::operator / (Rational c){    Rational temp;    temp.numer = numer / c.numer;    temp.denom = denom / c.denom;    temp.simpleform();    return temp;}bool Rational::operator == (Rational c){    bool temp = 0;    if ((numer * c.denom) == (c.numer * denom))    temp = 1;    return temp;}bool Rational::operator != (Rational c){    bool temp = 0;    if ((numer * c.denom) != (c.numer * denom))    temp = 1;    return temp;}bool Rational::operator > (Rational c){    bool temp = 0;    if ((numer * c.denom) > (c.numer * denom))    temp = 1;    return temp;}bool Rational::operator < (Rational c){    bool temp = 0;    if ((numer * c.denom) < (c.numer * denom))    temp = 1;    return temp;}bool Rational::operator >= (Rational c){    bool temp = 0;    if ((numer * c.denom) >= (c.numer * denom))    temp = 1;    return temp;}bool Rational::operator <= (Rational c){    bool temp = 0;    if ((numer * c.denom) <= (c.numer * denom))    temp = 1;    return temp;}