6 Replies - 3031 Views - Last Post: 02 February 2009 - 04:49 AM Rate Topic: -----

#1 poirot  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 30-October 08

[help] Fraction class C++

Posted 01 February 2009 - 08:37 PM

hi i have a problem here regarding to class function in C++, we are required to write a code and modular design about fraction class. we have to enter two fraction (x1/y1) and (x2/y2) and then choose the operators (op)

here is my fraction.h, nothing wrong and can compile
 1 class Fraction {
  2 
  3	 private:
  4	 int num;
  5	 int denom;
  6 
  7	 public:
  8	 Fraction (void);
  9	// Fraction(int,int);
 10	 Fraction Add(Fraction );
 11	 Fraction Subs(Fraction);
 12	 Fraction divide(Fraction);
 13	 Fraction multy(Fraction);
 14	 void set (int , int);
 15	 int GreatestCommonDivisor(int , int);
 16	 void Simplify();
 17	 void print();
 18 
 19	 ~Fraction();
 20  };



and my fraction.cpp, this one give me compilation errors
 1 #include <iostream>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include "fraction.h"
  5 
  6 using namespace std;
  7 
  8 Fraction::Fraction (void)
  9 {
 10	 num=0;
 11	 denom=1;
 12 }
 13 
 14 void Fraction::print()
 15 {
 16	 cout << num << " " << denom << endl;
 17 }
 18 
 19 void Fraction::set(int n, int d)
 20 {
 21	 num=n;
 22	 denom=d;
 23 }
 24 
 25 Fraction Fraction::Add (Fraction f)
 26 {
 27	 Fraction result;
 28	 result.num=num*f.denom + denom*f.num;
 29	 result.denom=denom*f.denom;
 30	 result.Simplify();
 31	 return result;
 32 }
 33 
 34 Fraction Fraction::Subs(Fraction f)
 35 {
 36	 Fraction result;
 37	 result.num=num*f.denom+ denom*f.num;
 38	 result.denom=denom*f.denom;
 39	 result.Simplify();
 40	 return result;
 41 }
 42 
 43 Fraction Fraction::divide(Fraction f)
 44  {
 45	 Fraction result;
 46	 result.num=num*f.denom;
 47	 result.denom=denom*f.num;
 48	 result.Simplify();
 49	 return result;
 50 }
 51 
 52 Fraction Fraction::multy(Fraction f)
 53 {
 54	 Fraction result;
 55	 result.num=num*f.num;
 56	 result.denom=denom*f.denom;
 57	 result.Simplify();
 58	 return result;
 59 }
 60 
 61 int Fraction::GreatestCommonDivisor(int a, int b)
 62 {
 63	 int temp;
 64	 while(b) {
 65		 temp=b;
 b=a%b;
 67		 a=temp;
 68	 }
 69	 return a;
 70 }
 71 
 72 void Fraction::Simplify()
 73 {
 74	 int gcd, absNum=abs(num);
 75	 if(num != 0 && absNum !=1 && denom !=1) {
 76		 gcd = GreatestCommonDivisor (absNum, denom);
 77		 if (gcd > 1)
 78		 {
 79			 num =num/gcd;
 80			 denom=denom/gcd;
 81		 }
 82	 }
 83 }
 84 
 85 Fraction::~Fraction(void)
 86 {
 87 }



and my main.cpp, i am not sure how to implement and call the operators, did i do a right thing here? and also this one can't compile
 1 #include <iostream>
  2 #include <cstdlib>
  3 #include "fraction.h"
  4 
  5 using namespace std;
  6 
  7 int main()
  8 {
  9	 int x1, y1, x2, y2, op;
 10 
 11	 Fraction part1;
 12	 Fraction part2;
 13 
 14	 cin >> x1 >> y1 >> x2 >> y2 >> op >> endl;
 15 
 16	 part1.set(x1,y1);
 17	 part2.set(x2,y2);
 18 
 19	 switch (op)
 20	 {
 21		 case 0 : Fraction result(part1,part2);
 22				  result.Add;
23				  result.print();
 24 
 25		 case 1 : Fraction result(part1, part2);
 26				  result.Subs;
 27				  result.print();
 28 
 29		 case 2 : Fraction result(part1,part2);
 30				  result.multy;
 31				  result.print();
 32 
 33		 case 3 : Fraction result(part1, part2);
 34				  result.divide;
 35				  result.print();
 36 
 37		 case 4 : cout << "End program" << endl;
 38	 }
 39 
 40 }



thanks in advanced

This post has been edited by poirot: 01 February 2009 - 08:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [help] Fraction class C++

#2 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: [help] Fraction class C++

Posted 01 February 2009 - 09:13 PM

One issue is in this line
cin >> x1 >> y1 >> x2 >> y2 >> op >> endl;

The endl should not be there, i dont know about headers and stuff but you seem to have an issue with creating result.
Also if you un-comment Fraction(int,int); in the header and add
Fraction::Fraction (int a, int b)
  {
	  num=a;
	  denom=b;
  }
to fraction.ccp
You can create values in part 1 and part 2 without using set.
just create them using
Fraction part1(x1,y1)
.

This post has been edited by rs4: 01 February 2009 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: [help] Fraction class C++

Posted 01 February 2009 - 09:25 PM

Ok, lets start from the top....

1) Fraction.h is fine.
2) Fraction.cpp has one slight problem in that the Subs method is the exact same logic as the Add method. But even with this, it is still syntax correct.
3) main.cpp is where the bulk of your problems are.
3A ) where you collect the values, you don't have >> endl; on the end. You would use << endl on printing, you are not storing values into endl.
3B ) Your switch statement, after each case you must have a "break" statement as shown in the example below.
3C ) You are not setting up your function calls correctly. No constructor takes two parameters, so you can't pass both.

Example...

#include <iostream>
#include <cstdlib>
#include "fraction.h"

using namespace std;

int main()
{
  int x1, y1, x2, y2, op;

  Fraction part1;
  Fraction part2;

  // Notice no >> endl
  cin >> x1 >> y1 >> x2 >> y2 >> op;

  part1.set(x1,y1);
  part2.set(x2,y2);

  // Setup a result
  Fraction result;

  switch (op)
  {
     case 0: 
          // Add part2 to part1, store in result
          result = part1.Add(part2);
          result.print();
          break;
     case 1:
          // Subtract part2 from part1, store in result
          // Be sure to fix logic in Subs method of Fraction.cpp
          result = part1.Subs(part2);
          result.print();
          break;
  }

}



Look through the code above with in code comments. Mixed with your other two class statements (which are syntax correct but has a few logic problems) it will work fine. I left the other cases for you to do and work with. Remember to use "break" otherwise execution attempts to go from case 0 right into case 1 and then into case 2 without stopping.

Note: Next time you post code here, don't include line numbers. They make it hard for us to help you.

Enjoy!

"At DIC we be fraction fixing code ninjas... some say I am 1/3 here mentally, I say I am 2/3 eccentric. Why do they have to see the glass half empty?" :snap:

This post has been edited by Martyr2: 01 February 2009 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 poirot  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 30-October 08

Re: [help] Fraction class C++

Posted 01 February 2009 - 09:36 PM

hi,, thank you for your helps..
i'll try to work on it..
for Subs function, yes there's a typo there..
Was This Post Helpful? 0
  • +
  • -

#5 poirot  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 30-October 08

Re: [help] Fraction class C++

Posted 01 February 2009 - 09:44 PM

hi, another questions..
how to handle division by zero..
in my gcd function??
Was This Post Helpful? 0
  • +
  • -

#6 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: [help] Fraction class C++

Posted 01 February 2009 - 09:55 PM

View Postpoirot, on 1 Feb, 2009 - 08:44 PM, said:

hi, another questions..
how to handle division by zero..
in my gcd function??

Place your function within a if statment like this:
if(result.num && result.denom){
cout<<"cannot divide by zero"
}
else{
// normal code
}

if either num or denom equal 0 if is run if not else is run

This post has been edited by rs4: 01 February 2009 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 poirot  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 30-October 08

Re: [help] Fraction class C++

Posted 02 February 2009 - 04:49 AM

hi..i have tried like this,
 Fraction Fraction::Add (Fraction f)
 {
	  Fraction result;
	  if( result.denom==0)
	 {
		  cout << "Division by zero";
	  }
	  else
	  {
	  result.num=num*f.denom + denom*f.num;
	  result.denom=denom*f.denom;
	  result.Simplify();
	  return result;
	  }
  }


input :
1 0 2 3 0 //op=0 use add function

the output should be
"Division by zero"

but, i got

1 0

anyone knows where the mistakes are?

This post has been edited by poirot: 02 February 2009 - 04:50 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1