Operator Overload Arithmetic Question

If anyone could help me. This program is to compute the harmonic number in fraction form of the number entered by the user. I have set up the operator overload for the + to add two fractions together. In my main i have the newf + newf1. I am trying to figure out how I can put that result stored in something so i can add the next lowest fraction. For example if i put in 3 for the input i want to add newt which equals 1/3 then newf which equals 1/2. This result is 5/6. So i need to be able to store 5/6 and then add the next fraction in the harmonic number sequence which is 1/1.

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
#include <iostream>
using namespace std;

class fraction
{
public:
  //constructor                                                                                                                  
  fraction (long initial_x =0, long initial_y =1);
  //modification memember functions                                                                                              
   void set_numerator(long x);
   void set_denominator(long x);
  void reduce ();
  //constant member function                                                                                                     
  long get_numerator() const {return numerator;}
  long get_denominator() const {return denominator;}
  //friend function                                                                                                              
  friend std:: istream& operator >> (std::istream& ins, fraction& target);
private:
  long numerator,denominator;
};

fraction operator + (const fraction&f1, const fraction&f2);
fraction operator - (const fraction&f1, const fraction&f2);
fraction operator * (const fraction&f1, const fraction&f2);
fraction operator / (const fraction&f1, const fraction&f2);
bool operator == (const fraction&f1, const fraction&f2);
bool operator != (const fraction&f1, const fraction&f2);
std::ostream& operator << (std::ostream& outs, const fraction& source);


fraction::fraction (long initial_x, long initial_y)
{
  initial_x=0;
  initial_y=1;
}

void fraction :: set_numerator (long x)
{
  numerator=x;
  x=0;;


}
void fraction :: set_denominator (long x)
{
  denominator=x;
  x=0;

  // cout<<numerator<<"/"<<denominator;                                                                                          
}
  fraction  operator + (const fraction&f1, const fraction&f2)
{
  fraction r;
  long a,b;
  long num;
  long numtemp1=1;
  long numtemp2=1;
  long denomtemp=0;
    numtemp1=(f1.get_numerator() * f2.get_denominator());
    numtemp2=(f2.get_numerator() * f1.get_denominator());
    denomtemp=(f2.get_denominator() * f1.get_denominator());
    num=numtemp1+numtemp2;

    cout<<numtemp1<<endl;

    cout<<numtemp2<<endl;
    r.set_numerator(num);
    r.set_denominator(denomtemp);

    cout<<r.get_numerator()<<"/"<<r.get_denominator()<<endl;

    return r;


}

int main()
{
  long data=0;
  fraction newf;
  fraction newf1;
  fraction newf2;
cout<<"Enter any number"<<endl;
 cin>>data;
 newf.set_denominator(data);
 newf.set_numerator(1);
 while (data >=1)
   {
     data--;
 newf1.set_denominator(data);
 newf1.set_numerator(1);

   (newf+newf1);

   }


 return 0;
}
Last edited on
It looks like you are close. I think you just need to call r.reduce() in your operator+ function.
Yes i do still have to call reduce, but at the current point i still need to add the last value of 1/1 so i need to store the current value of 5/6. So the current value of
(newf+newf1);
is 5/6. But the harmonic number of 3 should be 11/6 or (5*1)+(6*1) for the numerator; and then 6*1 for the denominator.
I cant figure out how to store the result of (newf+newf1); so i can add the next value.
Harmonic number of 3. 1/1 +1/2 +1/3=11/6
Lines 33, 34, 40, 47, 54, and 82 do nothing sensible.
Lines 87 and 89 lead to data=0, which is not the greatest of denominators.
The result of evaluating line 93 is not stored.

You will get big values. You should use the greatest common divisor to simplify your fractions.

Standard library provides a template op!= that works if you have op== so you don't need to define both (or the implementation of (a!=b) should be a simple !(a==b)
You probably want op< too.

Have you thought about the assignment (+=, -=, *=, /=) versions of operators?
I have GCD function that will be implemented under reduce.
The result of line 93 is exactly what i want to store but i am drawing a massive blank on how to do so.
I want to put something like fraction newf2=(newf+newf1)
But that doesn't seem to work.
Once i get that result stored i can then complete the calculation and then implement the GCD to reduce the fraction.
**GCD is greatest common divisor.
Last edited on
newf = newf + newf1;
you know sometimes when someone gives you the simplest of answers that i should already know i feel like a massive idiot. it works just like it should. Thank You kesiverto.
Topic archived. No new replies allowed.