heya folks. i'm working on an chunk of code here and i don't really understand what i am doing wrong in trying to increment and decrement a fraction. if you could provide some insight i would appreciate it. thanks.
#include <iostream.h>
#include "fraction.h"
void main( )
{
Positive_Fraction a,b,c;
int s,t;
cout << "Enter two ints:";
cin >> s >> t;
b.Assign(s,t);
++b;
b.Print( );
cout << endl;
cout << "Enter two ints:";
cin >> s >> t;
b.Assign(s,t);
--b;
b.Print( );
cout << endl;
}
class Positive_Fraction
{
public:
Positive_Fraction();
Positive_Fraction(int,int);
void Print(); // added 9-19
int Get(); // added 9-19
int Assign(int s, int t);
Positive_Fraction operator++();
Positive_Fraction operator--();
bool operator==(Positive_Fraction f2);
friend ostream& operator<<(ostream& c, Positive_Fraction f1);
friend istream& operator>>(istream& c, Positive_Fraction& f1);
private:
int top;
int bottom;
void Reduce();
};
#include <iostream.h>
#include "fraction.h"
Positive_Fraction :: Positive_Fraction()
{
top = 0;
bottom = 1;
}
Positive_Fraction :: Positive_Fraction(int a, int b)
{
Assign(a,b);
}
void Positive_Fraction :: Print()
{
if (top == 0)
cout << 0 << endl;
else
{
if (top / bottom != 0)
{
cout << top / bottom;
if (top % bottom != 0)
cout << " and ";
}
if (top%bottom != 0)
cout << top % bottom << '/' << bottom;
}
}
int Positive_Fraction :: Get()
{
char ch;
int s,t;
cin >> s >> ch >> t;
if (ch != '/')
{
cout << "Positive_Fraction not assigned!\n";
return 1;
}
return Assign(s,t);
}
int Positive_Fraction :: Assign(int s, int t)
{
if (t == 0)
{
cout << "Positive_Fraction not assigned!\n";
return 1;
}
else
{
top = s;
bottom = t;
Reduce();
return 0;
}
}
ostream& operator<<(ostream& co, Positive_Fraction f1)
{
if (f1.top == 0) co << 0;
if (f1.top >= f1.bottom)
{
co << f1.top / f1.bottom;
if (f1.top % f1.bottom != 0)
co << " and ";
}
if (f1.top % f1.bottom != 0)
co << f1.top % f1.bottom << "/" << f1.bottom;
return co;
}
istream& operator>>(istream& c, Positive_Fraction& f1)
{
char ch;
int s,t;
c >> s >> ch >> t;
if (ch != '/') return c;
f1.Assign(s,t);
return c;
}
void Positive_Fraction :: Reduce()
{
if (top == 0)
bottom = 1;
else
{
int min = (top < bottom ? top : bottom);
int gcd;
for (gcd = min; gcd > 1; --gcd)
{
if (top % gcd == 0 && bottom % gcd == 0)
break;
}
top = top/gcd;
bottom = bottom/gcd;
}
}
// what's below are a few different angles of trying to write this code. i know the decrement function makes little sense with how i was trying to call it...this is all just part of trial and error.
Positive_Fraction Positive_Fraction :: operator++()
{
Positive_Fraction temp;
top += bottom;
return temp;
}
/*
Positive_Fraction operator++(top, bottom)
{
temp = top += bottom;
}
*/
Positive_Fraction Positive_Fraction :: operator--()
{
Positive_Fraction temp;
temp.top = bottom - top;
temp.bottom = bottom;
return temp;
}
hope what i was doing makes sense. this is part of a larger program that works fine except for this portion. thanks in advance for any ideas.