[Dev-C++] PLEASE HELP: a prgm not working
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: chromatonus 7. <chr...@ho...> - 2001-10-19 23:52:15
|
hi, this program is supposed to add, subtract, multiply and divide two
fractions and simplify the answers, using class objects. But the prgm
displays 0/0 for the sums, differences, etc. How can I fix this program?
THank you for your time :)
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//=============================== CLASS HEADER
===============================
class rational
{
public:
// constructors
rational();
rational( int n, int d );
// accessors
int get_numerator();
int get_denominator();
// modifiers
void change_numerator(int n);
void change_denominator(int d);
// enter from keyboard
void enter();
// precondition: two numbers are sent in for each rational
number
// postcondition: they are stored as two rational numbers
// display
void display();
void reduce(int pn, int pd, int sn, int sd, int tn, int td, int
fn, int fd);
// precondition: send in rational @n/@d (@ meaning an operation)
// postcondition: calculate d = gcd(@n,@d)
// reset @n = @n/d and @d = @d/d
private:
int my_numerator,my_denominator;
};
//============================FREE FUNCTIONS
==================================
// assignment operator
void assign( rational a , rational &b );
// precondition: rational number a is sent in
// postcondition: b = a
// bn = an and bd = ad
// comparison operator
bool are_equal( rational a, rational b);
// precondition: rational numbers a and b are sent in
// postcondition: returns true if a = b else returns false
// a = b iff an*bd = ad*bn
// add rational numbers
rational add( rational a, rational b );
// precondition: send in rational numbers a,b where
// b != 0
// postcondition: calculate c = a + b by:
// a = an/ad b = bn/bd
// cn = an*bd + ad*bn
// cd = ad*bd
// reduce to lowest terms
// subtract
rational subt(rational, rational b);
// multiply rationals
rational mult( rational a, rational b);
// precondition: b != 0
// postcondition: calculate c = a*b by:
// cn = an * bn
// cd = ad * bd
// reduce to lowest terms
// divide rationals
rational div(rational a, rational b);
// precondition: b !=0
// postcondition: calculate c = a/b by:
// cn = an * bd
// cd = ad * bn
// reduce to lowest terms
//===============================IMPLEMENTATION================================
rational::rational()
{
my_numerator = 0;
my_denominator = 0;
}
void rational::enter()
{
cout<< "A rational (numerator first)= " << "\n" << "\n";
cin>> my_numerator;
cin>> my_denominator;
cout<< "\n";
}
void rational::change_numerator(int n)
{
my_numerator = n;
}
void rational::change_denominator(int d)
{
my_denominator = d;
}
int rational::get_numerator()
{
return (my_numerator);
}
int rational::get_denominator()
{
return (my_denominator);
}
rational::rational(int n, int d)
{
my_numerator = n;
my_denominator = d;
}
//===============================CLIENT
PROGRAM================================
int main()
{
rational a,
b,
p,
s,
t,
f;
a.enter();
b.enter();
cout<< "\n";
a.display();
b.display();
cout<< "\n" << "\n";
cout<< "Sum= " << "\n";
p.display();
cout<< "Difference= " << "\n";
s.display();
cout<< "Product= " << "\n";
t.display();
cout<< "Quotient= " << "\n";
f.display();
}
bool are_equal(rational a, rational b)
{
int an = a.get_numerator(),
ad = a.get_denominator(),
bn = b.get_numerator(),
bd = b.get_denominator();
if (an * bd == ad * bn)
return( true );
else
return( false );
}
rational add(rational a, rational b)
{
int an = a.get_numerator(),
ad = a.get_denominator(),
bn = b.get_numerator(),
bd = b.get_denominator(),
pn = an * bd + bn * ad,
pd = ad * bd;
rational p(pn, pd);
return p;
}
rational subt(rational a, rational b)
{
int an = a.get_numerator(),
ad = a.get_denominator(),
bn = b.get_numerator(),
bd = b.get_denominator(),
sn = an * bd - bn * ad,
sd = ad * bd;
rational s(sn, sd);
return s;
}
rational mult(rational a, rational b)
{
int an = a.get_numerator(),
ad = a.get_denominator(),
bn = b.get_numerator(),
bd = b.get_denominator(),
tn = an * bn,
td = ad * bd;
rational t(tn, td);
return t;
}
rational div(rational a, rational b)
{
int an = a.get_numerator(),
ad = a.get_denominator(),
bn = b.get_denominator(),
bd = b.get_numerator(),
fn = an * bn,
fd = ad * bd;
rational f(fn, fd);
return f;
}
void rational::reduce(int pn, int pd, int sn, int sd, int tn, int td, int
fn, int fd)
{
int j, d;
if( pd >= pn)
j = pn;
else
j = pd;
for( d = j; d>0; d--)
{
if( pn%d ==0 && pd%d ==0)
{
pn = pn/d;
pd = pd/d;
}
}
if( sd >= sn)
j = sn;
else
j = sd;
for( d = j; d>0; d--)
{
if( sn%d ==0 && sd%d ==0)
{
sn = sn/d;
sd = sd/d;
}
}
if( td >= tn)
j = tn;
else
j = td;
for( d = j; d>0; d--)
{
if( tn%d ==0 && td%d ==0)
{
tn = tn/d;
td = td/d;
}
}
if( fd >= fn)
j = fn;
else
j = fd;
for( d = j; d>0; d--)
{
if( fn%d ==0 && fd%d ==0)
{
fn = fn/d;
fd = fd/d;
}
}
}
void rational::display()
{
cout<< my_numerator << "/" << my_denominator << "\n";
system("PAUSE");
}
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|