Help with methods of a fraction class

This is a discussion on Help with methods of a fraction class within the C++ Programming forums, part of the General Programming Boards category; I'm doing this fraction arithmetic program, and have the following so far: A Frac class with methods that allow for ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Help with methods of a fraction class

    I'm doing this fraction arithmetic program, and have the following so far:

    A Frac class with methods that allow for addition, subtraction, etc.
    all of which crossmultiply for a common denominator, when necessary.

    My problem is - I can't figure out how to code a function for reducing the results. I've got no idea what a systematic formula might look like. Could anyone help?

    Oh, and I'm not supposed to use overloaded operators.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Code:
    int reduce (int p, int q)
    {
         int r;
         return ((r=p%q)==0)?  q : reduce (q,r);
    }
    then to call/reduce it

    Code:
          int gcd
          gcd = reduce (num,denom);
          denom =/gcd;
          num =/ gcd;

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    oh yea
    just make it a member of the class...
    what i did, is have the displace function in the class do all the reducing, then displaying it

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    Much appriciated.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    anytime

    question: is this for a takehome test 3?

    sounds exactly like something im doing for my class over spring break

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    Homework set 3. Probably from the same godawful book.

    Because I'm such a noob, here's a few debug questions for all and sundry:

    Basically...what the heck is wrong with my compiler? It won't recognize default args in the constructor, it's not recognizing the tilde as a destructor, and I'm passing functions with scope res operators, and when I call them in the main, it says it can't access the private parts of my class! My code seems pretty textbook, but Visual C++ is going nuts. Code is attatched.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    with dev c++
    u gottahave it public
    not Public

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    this may be just me not knowing u can do this
    but i didnt know u can pass it an arg and have it equal something

    int a=1, int b=1, int c=1, int d=1, int e=1, int f=1
    i dont think u can do that

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    there are a lot of small mistakes in ur code
    like, having the prototype return type void
    but the actual def return type int

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    i fixed all of ur little errors

    Code:
    //CIS 37
    //Program Set #3
    //
    //Katherine Busch
    //
    //
    
    #include <iostream.h>
    
    class Frac {
    
    			int num1, num2, dem1, dem2, resnum, resdem;
    			float dec;
        public:
    		    Frac(int a=1, int b=1, int c=1, int d=1, int e=1, int f=1);
    			~Frac ();
    			int addFrac(/*int, int, int, int, int, int, float*/);
    			int subFrac(/*int, int, int, int, int, int, float*/);
    			int multFrac(/*int, int, int, int, int, int, float*/);
    			int divFrac(/*int, int, int, int, int, int, float*/);
    			int printFrac();
    			int printDec();
    			int reduce(int p, int q);
    
    };
    
    Frac::Frac(int a, int b, int c, int d, int e, int f)
    {
    	num1=a; num2=b; dem1=c; dem2=d; resnum=e; resdem=f; 
    	
    }
    
    Frac::~Frac()
    {
    	cout << "Destructed!"<<endl;
    }
    
    int Frac::printDec()
    {
    	cout << "Decimal results are: "<< dec << endl;
    }
    
    int Frac::reduce (int p, int q)
    {
         int r;
         return ((r=p%q)==0)?  q : reduce (q,r);
    }
    
    int Frac::printFrac()
    {
    	cout << "Results are: "<< resnum<<"/"<<resdem<<endl;
    }
    
    
    int Frac::addFrac()
    {
    	if(dem1 != dem2)
    		resdem = dem1 * dem2;
    		resnum = (dem2 * num1) + (dem1 * num2);
    	
    }
    
    int Frac::subFrac()
    {
    	if(dem1 != dem2)
    	
    		resdem = dem1 * dem2;
    		resnum = (dem2 * num1) - (dem1 * num2);
    	
    }
    
    int Frac::multFrac()
    {
    	resnum = num1 * num2;
    	resdem = dem1 * dem2;
    }
    
    int Frac::divFrac()
    {
    	resnum = dem1 * num1;
    	resdem = num1 * dem2;
    }
    
    main()
    {
    	Frac fraction (1, 3, 2, 5);
    	fraction.addFrac();
    	fraction.printFrac();
    	fraction.printDec();
    
    	fraction.subFrac();
    	fraction.printFrac();
    	fraction.printDec();
    
    	fraction.multFrac();
    	fraction.printFrac();
    	fraction.printDec();
    
    	fraction.divFrac();
    	fraction.printFrac();
    	fraction.printDec();
    
    	return 0;
    }
    u gotta remember to make sure the function prototypes and the actual def match in return type as well as variables passed to it

    u cant have something like

    void foo ();

    then have

    int foo (int a)
    {
    }

    also, for if statements, u dont need a semicolmn (sp)

    just if (a!=b), not if (a !=b

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    oh yea, for the code
    Frac(int a=1, int b=1, int c=1, int d=1, int e=1, int f=1);
    in the constructor field in the class, im just mearly creating variables for it to use
    i dunno if that works, but for now its compiling without error
    and im sure ur going to want to take out the "=1"s and pass it values u want

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access to methods of another (external) class
    By praul in forum C++ Programming
    Replies: 12
    Last Post: 04-19-2006, 12:42 PM
  2. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  3. problem: cant acess class methods using vectors
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2003, 07:47 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21