displaying a mixed fraction in a fraction class

Mar 19, 2009 at 11:25am
Hi

I am having a little difficulty in creating a function called display2() that will display the result of an addition, subtraction, multiply and division fraction as a mixed fraction (for example the result should output say 1 3/4). I have 3 files called fract.h, fract.cc and TestClassFract, as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// file fract.h
// a Fraction Abstract Data Type

#ifndef FRACTION_H
#define FRACTION_H

// The DOMAIN OF VALUES

class Fraction
{
 private:
  int num;
  int denom;

 public:
  // The SET OF OPERATIONS 

  Fraction(); // default constructor

  // initialises fraction with numerator up.
  // denominator down
  // returns 0 if no error
  int create(int up, int down);

  // displays in form up/down
  void display();

  // displays in form up/down
  void display2();
  
  // reduce to lowest terms
  void simplify();

  // adds another to produce third in simplified form
  void add(Fraction, Fraction&);

  // and now the others
  void subtract(Fraction, Fraction&);
  void multiply(Fraction, Fraction&);
  void divide(Fraction, Fraction&);

};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// file Fract.cpp
// a fraction Abstract Data Type

// starter version for tutorials

#include <iostream>
#include <cstdlib> // for abs()
#include "fract.h"

using namespace std;

int GreatestCommonDivisor(int, int);

Fraction::Fraction()
{
  num = 0;
  denom = 0;
}

int Fraction::create(int up, int down)
{
  // temporary solution (no error checking)
  int errorCode = 0;
  num = up;
  denom = down;

  if (num < 0 && denom < 0)
    {
      num = abs(up);
      denom = abs(down);
    }
  if ((num < 0 && denom > 0) || (num > 0 && denom < 0))
    {
      if (num > 0)
	{
	  num *= -1;
	}
      denom = abs(down);
    }
      
  return errorCode;
}

void Fraction::display()
{
  cout << num << '/' << denom;
}

void Fraction::display2()
{
  cout << num << '/' << denom;
}

void Fraction::simplify()
{
  // use Euclid's algorithm to find gcd
  int gcd = GreatestCommonDivisor(num, denom);
  num /= gcd;
  denom /= gcd;
}

void Fraction::add(Fraction f2, Fraction& f3)
{
  int a_denom = denom * f2.denom ;
  int a_num = (num * f2.denom) + (f2.num * denom);
  f3.create(a_num,a_denom) ;
  f3.simplify();
}

void Fraction::subtract(Fraction f2, Fraction& f3)
{
  //  int s_denom = denom * f2.denom ;
  //int s_num = ((num * 1) * f2.denom) - ((f2.num * 1) * denom);
  //f3.create(s_num,s_denom);
  //f3.simplify();

  int s_denom = denom * f2.denom ;
  int s_num = num * f2.denom - f2.num  * denom;
  f3.create(s_num,s_denom);
  f3.simplify();
}

void Fraction::multiply(Fraction f2, Fraction& f3)
{
  int m_denom = denom * f2.denom ;
  int m_num = (num * f2.num) ;
  f3.create(m_num,m_denom) ;
  f3.simplify();
}

void Fraction::divide(Fraction f2, Fraction& f3)
{
  int a_denom = num * f2.denom ;
  int a_num = (f2.num * denom) ;
  f3.create(a_denom,a_num) ;
  f3.simplify();
}

// Euclid's algorithm
int GreatestCommonDivisor(int a, int b)
{
  a = abs(a);
  b = abs(b);
  int temp = a % b;
  while (temp > 0)
    {
      a = b;
      b = temp;
      temp = a % b;
    }
  return b;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include "fract.h"

using namespace std;

int main()
{
   Fraction f1, f2, f3;

   int f12;
   int f13;
   int f21;
   int f22;

   cout << "Enter first numerator: ";
   cin >> f12;
   cout << endl;
   cout << "Enter first denominator: ";
   cin >> f13;
   cout << endl;
   cout << "Enter second numerator: ";
   cin >> f21;
   cout << endl;
   cout << "Enter second denominator: ";
   cin >> f22;
   cout << endl;


   f1.create(f12,f13);
   f2.create(f21,f22);
   f1.add(f2,f3);
   //f1.subtract(f2,f3);
   //f1.multiply(f2,f3);
   //f1.divide(f2,f3);
   f1.display();	
   cout << " + ";
   //cout << " - ";
   //cout << " * ";
   //cout << " / ";
   f2.display();
   cout << " = ";
   f3.display();
   //f3.display2();
   cout << endl;
   
   return ( 0 );
}


Any ideas/hints on how to display a fraction as a mixed one would be great.
Thanks in advance.
Mar 19, 2009 at 12:39pm
5/3 = 1 2/3.

How do you perform this calculation in your head? Write the code the same way.

5/3 == 1 using integer division. To get the remainder, 5 - 1*3 = 2.

On the other hand, if the numerator is strictly less than the denominator then the whole part is 0, which is typically not written, so display2() can simply revert to calling display1() in that case.
Topic archived. No new replies allowed.