GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Feb-2005, 17:00
kjc_13 kjc_13 is offline
New Member
 
Join Date: Feb 2005
Posts: 1
kjc_13 is on a distinguished road

I need help implementing


I need help implementing this code.... if you have any ideas for any of these, email them to me.

CPP / C++ / C Code:
#ifndef rat_h
#define rat_h

#include<iostream> // access to stream insertion, extraction ops.
#include<cstdlib> // access to exit()
#include<cmath> // pow lives here
#include<iomanip> // setiostate lives here

using namespace std;
// To the user:

// This is the declaration for
// class rat{} functions and code. 
// The file rat.cpp has some example code definitions in it



 
int gcd(int,int); // prototype for gcd, needed for setrat
//  Note:  gcd  is a  totally free function



class rat   // a class for performing rational arithmetic
{     public:
       	rat(); // default class constructor, set num =0, den =1
        //   usage:   rat r1;
       	rat( int, int); //class constructor, set num and den
       	// at declaration time
        // usage:   rat r1(7,-77);  // results in  r1.num = -1, r1.den= 11 
       	rat( int ); // class constructor, sets num to given int, and den is 1;
       	// allows implicit cast conversion in arithmetic
       	// operators from int to rat
       	
	// all three of the above could have been given by:
       	//   rat( int a = 0, int b = 1 );
       	//  however,  this would have sent the two trivial
       	//  initializations through setrat,  which is unneccesary
	

	// If you want, you can introduce the 
	// cast operation of rats to doubles:
	//	operator double();
	// This is overloading the (double)  casting operator:
	// usage:   double x = (double)r;  
	// or        x = static_cast<double)(r);
	// or        x = double(r);
	// Introducing this casting operator is not necessarily a good idea:
	// this will cause ambiguity problems  if we try to rely on
        // implicit casts.   Is  r1 + i    to be interpreted as
	//                      rat +  int    or
	//                     double + int  ?????????
	// Perhaps the best idea would be to give explicit prototypes for
	// all mixed mode arithmetic and boolean  operators
	// 

       	void setrat(int,int); // assigning num, den to rat object
       	// the function setrat turns out to be the workhorse of
       	// the entire class
       	int getnum() const; // fetches the numerator
       	int getden() const; // fetches the denominator
       	double getdec() const; // returns the decimal equiv. of a rat

       	// This is the neat way to overload the stream extraction
       	// >> operator, and stream insertion << operator
       	//  We want to be able to input and ouput rats in the
       	//  form:   3/4   

	friend istream & operator>> (istream & istr, rat & x);
       	friend ostream & operator<< (ostream & ostr, const rat & x);

       	// This is  one  correct way to do the arithmetic 
       	// operations:

	//	rat operator + (const rat &) const; // adds: x + v
	//	rat operator - (const rat &) const; // subtracts: x - v
	//	rat operator / (const rat &) const; // divides: x/v 
	//	rat operator * (const rat &) const; // multiplies x*v

        // note: the operators above, as member functions,
       	// are attached to the left member
       	// of the operation: r1+r2 is compiled as r1.operator+(r2)
       	// however, mixed integer-rat arithmetic is not valid
       	// with just these operators.  


       	// We probably should overload the +,-,*, and / operators
       	// as friends:

       	friend rat operator + (const rat & , const rat &);
       	friend rat operator - (const rat & , const rat &);
       	friend rat operator * (const rat & , const rat &);
       	friend rat operator / (const rat & , const rat &);

       	// in this fashion, we could then perform operations
       	// like  3 + r1 . (the first argument as an int. 
       	// the second argument can already be int, as in
       	// r1 + 3, as we have, unwittingly,
       	// provided the compiler with a way (the single
       	// argument constructor rat (int) ) for converting
       	// int to rat. by making the operator friends, instead
       	// of members, mixed mode arithmetic can be performed
       	// in either order: 3 + r1 or r1 +3;

       	// Lastly, we need a negation operator:
        rat operator - (void) const; 
		// the unary negation operator implemented as
		// a member function. 
	//   this allows   expressions like  r = -r1; 

	// In class,  we added the auto-increment operators
       

	rat& operator++(void);       
	 
	// is the prefix  operator:  ++r
	// The effect of ++r  is  to increment r, then return the value
	// This is readily coded up as a function.  Note that this
	// is returning  an  l-value (something that can then be further 
	// assigned to.

	rat operator++(int);
	// which is the postfix  operator:  r++
	// The int  is  a bogus argument, used only by the compiler to tell
	// the two ++ operators apart!
	//
	// The effect of r++ is to return the value of r, then increment
	// Coding this up as a function call is tricky. see the example
	// in rat.cpp   Note that the return value is  a temporary copy
	// ( of r's  previous value,  and so is only  an r-value


       	// the relational operators, as friend functions; 
       	friend bool operator < (const rat & u, const rat & v); 
       	// returns (u < v)  's  truth value
		
       	friend bool operator > (const rat & u, const rat & v); 
       	friend bool operator == (const rat & u, const rat & v);
       	friend bool operator != (const rat & u, const rat & v);
       	friend bool operator >= (const rat & u, const rat & v);
       	friend bool operator <= (const rat & u, const rat & v);
       

       	friend rat pow(const rat&, int);
		// computes integer powers of the rat

      private:
       	int num;
       	int den;
}; // end of class declaration.


#endif  // end header
Last edited by LuciWiz : 15-Feb-2005 at 05:30. Reason: Please insert your C code between [c] & [/c] tags
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.