WRI Colleges Forum
Hello WRIans...

If you are not a member yet, please register with complete information to be able to post, reply and to create thread or topics in the forum.

Madya na po kita!!

Login with FACEBOOK is now available for facebook users.


[Admin]

Join the forum, it's quick and easy

WRI Colleges Forum
Hello WRIans...

If you are not a member yet, please register with complete information to be able to post, reply and to create thread or topics in the forum.

Madya na po kita!!

Login with FACEBOOK is now available for facebook users.


[Admin]
WRI Colleges Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Please notify or send an SMS to forum admin @09501527151 for activation of your account.
Welcome...... Alumni of Naga, Rinconada, Calabanga, Partido and Sorsogon....
Alumni of the WRI-Goa Branch is planning to have a simple Alumni Reunion on 2018. Please text 09501527151 for attendance.
We are looking for Forum Moderator from other branches; Naga, Rinconada, Calabanga and Sorsogon Alumni. Salamat.
Search
Display results as :
Advanced Search
April 2024
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930    

Calendar

Latest topics
WRI-Goa Pa-Raffle 2010 WinnersSat Dec 02, 2017 6:47 pmarvie21
alumni reunion... Thu Nov 30, 2017 6:33 pmarvie21
Annyeong-haseyoThu Nov 30, 2017 6:28 pmarvie21
hello.... good daySun Nov 19, 2017 6:59 pmChelleAnn
Forum Rules and GuidelinesSun Nov 19, 2017 3:36 pmradax
Uploading of Images and VideosSun Nov 19, 2017 3:34 pmradax
Troubleshooting your telephone line... Sat Jul 29, 2017 6:18 pmradax
What is PROTOCOL?Sat Jul 29, 2017 6:17 pmradax
Pa-Raffle 2010 @ WRI-Goa GroundMon Jul 17, 2017 8:20 pmradax
Forum Rules & GuidelinesMon Jul 17, 2017 8:17 pmradax
Forum Group SynchronizationMon Jul 17, 2017 8:12 pmradax
The Sportsfest 2014Wed Mar 19, 2014 10:18 amradax
kumusta? goanians???? Thu Mar 13, 2014 10:19 pmGuest
WRI Foundation 2014 at SM City NagaMon Mar 10, 2014 11:47 amGuest
hi everyone..Mon Mar 03, 2014 12:37 pmradax
Care and maintenance of Leather furnitureMon Oct 28, 2013 5:14 pmGuest
Consider the filling of the sofaFri Oct 25, 2013 5:12 pmGuest
Conversion Dining Top for Pool TableTue Oct 22, 2013 4:48 pmGuest
Antique Writing DesksMon Oct 21, 2013 5:01 pmGuest
Take care of your wooden outdoor furniture Sat Oct 19, 2013 4:39 pmGuest
Cooking FeastSat Aug 27, 2011 3:18 pmmherlcan
wri-calabanga please read thiszzz....Sun Jul 17, 2011 3:51 pmrickzmanansala
wri-calabangaFri Jul 08, 2011 6:02 pmrickzmanansala
haen lamang ang pictures kan nakaaging instramurals 2010??Tue Jul 05, 2011 12:16 pmrickzmanansala
PROGRAMMING EXERCISE ConcentrationTue Jul 05, 2011 12:15 pmrickzmanansala
Have a nice day WriansTue Jul 05, 2011 12:12 pmrickzmanansala
2012...must see it and must believe it???Tue Jul 05, 2011 12:10 pmrickzmanansala
wri calabanga team building...Mon Jul 04, 2011 11:26 amrickzmanansala
FM TRANSCEIVER ARCHITECTURESun Jul 03, 2011 3:49 pmrickzmanansala
hi po sir danSun Jul 03, 2011 3:48 pmrickzmanansala
asar talaga.. Sun Jul 03, 2011 3:48 pmrickzmanansala
Saang Branch ka ba?Sun Jul 03, 2011 3:34 pmrickzmanansala
Attention!! aLL WRI Graduates..!!!Tue Dec 21, 2010 3:00 pmmeowchi
WRI Intramurals 2010Thu Nov 04, 2010 4:35 pmmherlcan
What is a Protocol?Tue Oct 26, 2010 12:00 pmBlex
What is a Network?Tue Oct 26, 2010 11:59 amGuest
How to crimp a cable RJ45Tue Oct 26, 2010 11:39 amGuest
Troubleshooting Your Phone LineTue Oct 26, 2010 11:24 amBlex
General printer troubleshooting.Tue Oct 26, 2010 11:08 amGuest
kelan po ba ang intrams ng WRI-Goa?Tue Oct 12, 2010 10:10 amGuest
ehem...Sun Sep 19, 2010 4:35 pmGuest
rationalTue Sep 07, 2010 7:07 amarvie21
Pagsungko ni INA (2010)Sat Sep 04, 2010 1:49 pmGuest
WRI-GOA STARTING POINT...Fri Sep 03, 2010 1:37 amarvie21
WRI-Goa Alumni EventsSun Aug 29, 2010 11:53 amBlex
When there was a WRI Kolping in CanamanWed Aug 18, 2010 12:29 pmarvie21
kumusta wrians?Sun Aug 15, 2010 10:23 pmbonnkeivy
Basic PC AssemblySun Aug 08, 2010 6:59 pmGuest
Propaganda TechniquesSun Aug 08, 2010 6:32 pmGuest
Good news for WRIFri Jan 22, 2010 10:08 amradax
Tictactoe in C++Wed Dec 16, 2009 2:29 amviraxz
Multiple Form Splash ScreenFri Dec 11, 2009 8:53 pmINKHRIPTOR
WRI Goa momentsThu Dec 10, 2009 3:15 pmGuest
help me my friends.. :DThu Dec 10, 2009 3:01 pmGuest
To all WRIan Graduates...Thu Dec 10, 2009 2:03 pmGuest
Inline encryption with using bintoint algoTue Dec 08, 2009 5:20 pmjcodee
Shaker Sort ANSI CTue Dec 08, 2009 4:52 pmjcodee
Calling of all WRI Goa GraduatesTue Dec 01, 2009 2:18 amradax
WRI Foundation Anniversary on March 2006Sun Nov 29, 2009 1:22 pmradax
Alumni Date on the making..Sat Nov 28, 2009 12:02 pmBlex
Simple Calculator in VBFri Nov 27, 2009 6:49 pmGuest
Planning for WRI AlumniThu Nov 26, 2009 11:22 pmGuest
WRI SorsogonFri Nov 20, 2009 2:34 amradax
Batch 2005 Grads From Rassel SilosFri Nov 20, 2009 1:43 amradax
WRI Goa (Tuloy po kau sa bahay ni Kuya)Fri Nov 20, 2009 1:40 amradax
WRI Foundation Anniversary on March 2006Fri Nov 20, 2009 1:29 amradax
To all WRIans...Mon Nov 16, 2009 9:12 pmmeowchi
Online Forum MeetingSat Nov 14, 2009 2:31 pmmeowchi
Merry Christmas..Sat Nov 14, 2009 2:28 pmmeowchi
Calling all WRI Graduates!Thu Nov 12, 2009 6:40 pmmeowchi
Updated WRIans Forum SiteFri Nov 06, 2009 4:19 amradax
Pictures of WRIWed Nov 04, 2009 4:08 pmmeowchi
List of names of WRI GraduatesMon Oct 26, 2009 11:11 amradax
Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest

None

Top posters
157 Posts - 43%
42 Posts - 12%
25 Posts - 7%
23 Posts - 6%
23 Posts - 6%
21 Posts - 6%
20 Posts - 6%
20 Posts - 6%
19 Posts - 5%
13 Posts - 4%
Cool Radio Hits



Page Hits
reset 12/3/2017
rational Counter

Go down
arvie21
arvie21
Newbie
Posts : 23
Join date : 2009-12-04

rational Empty rational

Tue Sep 07, 2010 7:07 am
/******************************************************
Name:

Filename: RationalNum.h
******************************************************/
#ifndef RATIONALNUM_H
#define RATIONALNUM_H

class RationalNumber {
public:
RationalNumber( int = 0, int = 1 );
const RationalNumber operator+( const RationalNumber& );
const RationalNumber operator-( const RationalNumber& );
const RationalNumber operator*( const RationalNumber& );
const RationalNumber operator/( RationalNumber& );
bool operator>( const RationalNumber& ) const;
bool operator<( const RationalNumber& ) const;
bool operator>=( const RationalNumber& ) const;
bool operator<=( const RationalNumber& ) const;
bool operator==( const RationalNumber& ) const;
bool operator!=( const RationalNumber& ) const;
void printRational( void ) const;
private:
int numerator;
int denominator;
void reduction( void );
};

#endif



------------------------------------------------------------------------


/******************************************************
Name:

Filename: RationalNum.cpp
******************************************************/
#include
#include
#include "RationalNum.h"
using namespace std;

RationalNumber::RationalNumber( int n, int d )
{
numerator = n;
denominator = d;
reduction();
}

const RationalNumber RationalNumber::operator+(
const RationalNumber &s )
{
RationalNumber sum;

sum.numerator = numerator * s.denominator + denominator *
s.numerator;
sum.denominator = denominator * s.denominator;
sum.reduction();
return sum;
}


const RationalNumber RationalNumber::operator-(
const RationalNumber &s )
{
RationalNumber sub;

sub.numerator = numerator * s.denominator - denominator *
s.numerator;
sub.denominator = denominator * s.denominator;
sub.reduction();
return sub;
}

const RationalNumber RationalNumber::operator*(const RationalNumber &m)
{
RationalNumber multiply;

multiply.numerator = numerator * m.numerator;
multiply.denominator = denominator * m.denominator;
multiply.reduction();
return multiply;
}


const RationalNumber RationalNumber::operator/( RationalNumber &d )
{
RationalNumber divide;

if ( d.numerator !=0 ) {
divide.numerator = numerator * d.denominator;
divide.denominator = denominator * d.numerator;
divide.reduction();
}
else {
cout << "Divide by zero error: terminating program\n";
exit( 1 );
}

return divide;
}

bool RationalNumber::operator>(const RationalNumber &gr ) const
{
if ( static_cast< double >( numerator ) / denominator >
static_cast< double >( gr.numerator ) / gr.denominator )
return true;
else
return false;
}

bool RationalNumber::operator<(const RationalNumber &lr ) const
{
return !(*this > lr);
}

bool RationalNumber::operator>=(
const RationalNumber &rat ) const
{ return *this == rat || *this > rat; }

bool RationalNumber::operator<=(
const RationalNumber &rat ) const
{ return *this == rat || *this < rat;}

bool RationalNumber::operator==(
const RationalNumber &rat ) const
{ if (numerator == rat.numerator &&
denominator == rat.denominator)
return true;
else
return false;
}

bool RationalNumber::operator!=(
const RationalNumber &rat ) const
{
return !(*this == rat);
}

void RationalNumber::printRational( void ) const
{
if ( numerator == 0 ) // Fraction as zero
cout << numerator;
else if ( denominator == 1 ) // Fraction as integer
cout << numerator;
else
cout << numerator << '/' << denominator;
}

void RationalNumber::reduction( void )
{
int smallest, gcd = 1; // GCD

smallest = ( numerator < denominator ) ? numerator
: denominator;

for ( int loop = 2; loop <= smallest; ++loop )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;

numerator /= gcd;
denominator /= gcd;
}



--------------------------------------------------------------------



/******************************************************

** Name:

** Filename: RationalNumDrv.cpp

** Project #: Deitel & Deitel 8.17

** Project Description: Create a class RationalNumber
(fractions) with the following capabilities:
a)Create a constructor that prevents a 0
denominator in a fraction, reduces or
simplifies fractions that are not in reduced
form and avoids negative denominators.
b)Overload the addition, subtraction,
multiplication and division operators for this
class.
c)Overload the relational and quality operators
for this class.

** Output: The result of the first and second fraction
using: Addition
Subtraction
Multiplication
Division
> or <
>= or <=
!= or ==

** Input: First fraction numerator and denominator
Second fraction numerator and denominator

** Algorithm: Create class RationalNumber.cpp with operator all
operator functions.
Create header file RationalNumber.h
Print out results using RationalNumberDrv.cpp
Ask user for first fraction
Ask user for second fraction
Use operator+ produce result
Use operator- produce result
Use operator* produce result
Use operator/ produce result
Use operator> and operator< produce result
Use operator>= and operator<= produce result
Use operator!= and operator== produce result
Print result of all operator functions
End program

******************************************************/
// Include files
#include
#include
#include "RationalNum.h"
using namespace std;

// Global Type Declarations

// Function Prototypes
void instruct (void);
void pause ();

//Global Variables - should not be used without good reason.


int main()
{
// Declaration section
int a,
b,
c,
d;

// Executable section
instruct ();

cout << "Enter first fraction numerator: ";
cin >> a;
cout << " ---";
cout << "\nEnter first fraction denominator: ";
cin >> b;
cout << "\nEnter second fraction numerator: ";
cin >> c;
cout << " ---";
cout << "\nEnter second fraction denominator: ";
cin >> d;
cout << "\n";

RationalNumber x( a, b ), y( c, d ), z;

x.printRational();
cout << " + " ;
y.printRational();
cout << " = ";
z = x + y;
z.printRational();

cout << '\n';
x.printRational();
cout << " - " ;
y.printRational();
cout << " = ";
z = x - y;
z.printRational();

cout << '\n';
x.printRational();
cout << " * " ;
y.printRational();
cout << " = ";
z = x * y;
z.printRational();

cout << '\n';
x.printRational();
cout << " / " ;
y.printRational();
cout << " = ";
z = x / y;
z.printRational();


cout << "\n\n";
x.printRational();
cout << " is:";

cout << "\t";
cout << ( ( x > y ) ? " > " : " <= " );
y.printRational();
cout << " according to the overloaded > operator\n";

cout << "\t";
cout << ( ( x < y ) ? " < " : " >= " );
y.printRational();
cout << " according to the overloaded < operator\n";

cout << "\t";
cout << ( ( x >= y ) ? " >= " : " < " );
y.printRational();
cout << " according to the overloaded >= operator\n";

cout << "\t";
cout << ( ( x <= y ) ? " <= " : " > " );
y.printRational();
cout << " according to the overloaded <= operator\n";

cout << "\t";
cout << ( ( x == y ) ? " == " : " != " );
y.printRational();
cout << " according to the overloaded == operator\n";

cout << "\t";
cout << ( ( x != y ) ? " != " : " == " );
y.printRational();
cout << " according to the overloaded != operator" << endl;
pause();
return 0;
}

void instruct (void)
{
// Declaration section
cout << "Display the use of class RationalNumber to ";
cout << "perform various operator functions.\n";
cout << "___________________________________________";
cout << "___________________________________\n\n";
// Executable section
}

void pause ()
{
// Declaration section

// Executable section
cout << "\nPress any key to continue...";
getch();
cout << "\r";
cout << " ";
cout << "\r";
}


/*
Program Output

Display the use of class RationalNumber to perform various operator functions.
______________________________________________________________________________

Enter first fraction numerator: 2
---
Enter first fraction denominator: 3

Enter second fraction numerator: 1
---
Enter second fraction denominator: 2

2/3 + 1/2 = 7/6
2/3 - 1/2 = 1/6
2/3 * 1/2 = 1/3
2/3 / 1/2 = 4/3

2/3 is: > 1/2 according to the overloaded > operator
>= 1/2 according to the overloaded < operator
>= 1/2 according to the overloaded >= operator
> 1/2 according to the overloaded <= operator
!= 1/2 according to the overloaded == operator
!= 1/2 according to the overloaded != operator

Enter first fraction numerator: 1
---
Enter first fraction denominator: 5

Enter second fraction numerator: 1
---
Enter second fraction denominator: 5

1/5 + 1/5 = 2/5
1/5 - 1/5 = 0
1/5 * 1/5 = 1/25
1/5 / 1/5 = 1

1/5 is: <= 1/5 according to the overloaded > operator
< 1/5 according to the overloaded < operator
>= 1/5 according to the overloaded >= operator
<= 1/5 according to the overloaded <= operator
== 1/5 according to the overloaded == operator
== 1/5 according to the overloaded != operator

Press any key to continue...
*/
Back to top
Permissions in this forum:
You cannot reply to topics in this forum