Program with classes

Mar 11, 2013 at 6:53pm
In my program I am using a class called Fraction and having the user input a numerator and a denominator and the solving the fraction and then the fraction gets reduced to lowest terms and printed. I am having trouble figuring out how to get the fraction to reduce and then print. This would go in the
void Fraction::setFractReduce() function.

Can someone help me? Tips, examples?


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
  
  #include <iostream>
  #include <cmath>
  #include <string>
  #include <cstring>
  #include <cstdlib>
 
  using namespace std;
 
  class Fraction
  {
    public:
 
       void setFractFinder();
       void setFractAnswer();
       void setFractReduce();
 
    private:
 
       double numurator;
       double denomonator;
       double answer;
 
  };
 
  int main (){
 
     Fraction Fract;
 
     Fract.setFractFinder();
     Fract.setFractAnswer();
     Fract.setFractReduce();
 
  }
 
 
  void Fraction::setFractFinder(){
 
     Fraction Fract;
 
     cout << "Enter numerator:\n";
     cin >> numurator;
     cout << "Enter denomonator:\n";
     cin >> denomonator;
 
     if(denomonator == 0){
 
        cout << "You cannot devide by zero!" << endl;
        exit(0);
     }
 
  }
 
  void Fraction::setFractAnswer(){
 
     Fraction Fract;
 
     answer = numurator / denomonator;
 
     cout << "Your answer is: ";
     cout << answer << "\n";
 
  }
 
  void Fraction::setFractReduce()
  {
     Fraction Fact;
 
     cout << "Fraction in lowest terms is: " << endl;
 
 
  }
Last edited on Mar 11, 2013 at 6:53pm
Mar 11, 2013 at 8:30pm
- Personally, i would divide the numerator and denominator by every number up until half of itself, then i would see if there are any common factors.
Mar 11, 2013 at 11:03pm
How would I do that here exactly?
Mar 11, 2013 at 11:06pm
I want to do this with a void function and I am having trouble with it.
Mar 11, 2013 at 11:34pm
closed account (3qX21hU5)
What do you have so far for the function? If we can see what you have we can push you in the right direction.

Also something I noticed doing a quick scan of the code you posted is this.

You declare a object of you class in every member function... Why? you don't need them there delete them. I think you are doing this so you can use the private variables of the class? If so you don't need to do that, every member function has access to the private variable of its class.
Mar 12, 2013 at 12:57am
See that is where I am stuck. I am not sure how to start this function. I have seen some examples with bool functions but it does not go well to use double values with the % sign I guess.

And I understand about the declaring the object of my class in every function, it was habit but I understand that I do not need it there. Thank you.
Mar 12, 2013 at 12:58am
nothing has changed much in my code except for getting rid of the unnecessary declaring of the object in each function.
Mar 12, 2013 at 1:11am
closed account (3qX21hU5)
I suck at fractorials and specially decimal ones but here is a one that uses int's and solves your problem with recursion (Which is probably not the best way to go about it) maybe it can give you some ideas.

1
2
3
4
5
6
7
int Fraction::setFractReduce(int n,int d)
{
  if (d != 0)
    setFractReduce(d , n % d); //recursive call

  return n; //return n when d equals 0
}
Last edited on Mar 12, 2013 at 1:46am
Mar 12, 2013 at 1:35am
- Make a for loop from zero until the number or half of that number, then modulus your number by the number being incremented.

-Any number that makes your result 0 is a factor of your original number.

- You could even keep a counter for how many times this happens, then make an array of that size and loop through again to get the numbers.

- There are a multitude of ways that you could go about solving this problem.
Mar 12, 2013 at 2:50am
I'm not fond of your approach. Here's a minimal example of a different one.

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
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>


class MyFraction
{
public:
    MyFraction( int num, int den ) : _numerator(num), _denominator(den) 
    {
        if ( (_numerator < 0 && _denominator < 0) || _denominator < 0 )
        {
            _numerator = -_numerator ;
            _denominator = -_denominator ;
        }      
    }

    void reduce() ;

    int numerator() const { return _numerator; }
    int denominator() const { return _denominator; }

    operator double() const 
        { return static_cast<double>(_numerator) / _denominator; }

private:
    int _numerator ;
    int _denominator ;

};

MyFraction getFraction()
{
    int numerator, denominator ;

    std::cout << "Enter numerator: " ;
    std::cin >> numerator ;

    std::cout << "Enter denominator: " ;
    std::cin >> denominator ;

    if ( denominator == 0 )
    {
        std::cout << "Invalid denominator.  Terminating program."  << std::endl ;
        throw 1 ;
    }

    return MyFraction(numerator, denominator) ;
}

std::ostream& operator<<(std::ostream& os, const MyFraction & f )
{
    os << f.numerator() ;

    if ( f.denominator() != 1 )
        os << '/' << f.denominator() ;

    return os ;
}

int gcd( int x, int y )
{
    if ( y != 0 )
        gcd(y, x%y) ;

    return x ;
}

int main (){

    MyFraction f = getFraction() ;
    f.reduce() ;

    std::cout << f << " is " << static_cast<double>(f) << '\n' ;
}

void MyFraction::reduce()
{
    int divisor = gcd(std::min(std::abs(_numerator),_denominator), std::max(std::abs(_numerator),_denominator)) ;

    _numerator /= divisor ;
    _denominator /= divisor ;
}
Mar 12, 2013 at 4:59am
So I am a beginner and was wondering
@cire what does std::ostream& operator<<(std::ostream& os, const MyFraction & f ) mean? What is going on here?

@thejman250 Your suggestions were very helpful!! Thank you very much, those options are good.

@Zereo Thank you for that example it does give me a lot of ideas, I appreciate you helping out.
Mar 12, 2013 at 5:00am
closed account (Dy7SLyTq)
the @cire: that is an overloaded operator that prints f
Mar 12, 2013 at 6:35am
oh okay!! That makes sense.

How can I get a do while loop to work for this program? At the end i want it to ask the user if they want to find another fraction answer but I am not sure how? any hints?
Mar 12, 2013 at 6:42am
A do while loop for the code above. Or another loop that will get it to repeat until the user says they are done.
Mar 13, 2013 at 1:33am
I figured out the do while loop. Thank you everyone for your help on this. Things make a lot more sense now. Thank you again!!!
Mar 13, 2013 at 5:06am
bruntmjust wrote:

bruntmjust (115) Mar 12, 2013 at 12:59am
So I am a beginner and was wondering
@cire what does std::ostream& operator<<(std::ostream& os, const MyFraction & f ) mean? What is going on here?

@thejman250 Your suggestions were very helpful!! Thank you very much, those options are good.

@Zereo Thank you for that example it does give me a lot of ideas, I appreciate you helping out.
bruntmjust wrote:
I figured out the do while loop. Thank you everyone for your help on this. Things make a lot more sense now. Thank you again!!!


- No problem.
Topic archived. No new replies allowed.