CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Thread: Classes help

Threaded View

  1. #1
    Join Date
    Mar 2011
    Posts
    9

    Classes help

    I wasn't sure what to name the thread, but I have a few questiong. I've written a program to add, subtract, multiply, and divide for class. It all works well except I need to add a few odds and ends. I've added the reduction part of the code but it's not working. I also need to add in a way top automatically input a 1 if the user hits enter w/o entering a number.

    The only thing left is I need to ensure the denom is not a 0 or negative. I was thinking something like

    if(denom >= 0)
    cout<<"please enter a valid denominator\n";
    cin>>denom;
    a.set_den(denom);

    which works but I need to make it recursive or add in a check. I'm trying to keep my code from becoming bloated as well, so can I write that as a single code also w/o needing to have the code twice, once for each input of the denom. I'm listing my code below, thanks in advance.

    //Define class Fraction Num\Denom int only, no float***
    //Program operations +,-,*,/ to (overload)***
    //When a user inputs a blank input build a constructor that enters "1"
    //Research Euclidean Algorithm to find GCD for reduction.
    //Class checks, no 0 or "-" in denominator.
    //Minimal code in "main***

    #include <iostream>
    #include <cassert>
    #include <cmath>
    using namespace std;


    class Fraction

    {
    int num;
    int den;


    public:

    //public function can access private function's and set variables

    void set_num(int numpub)
    {num=numpub;};

    void set_den(int denpub)
    {den=denpub;};

    int get_num()
    {return num;};

    int get_den()
    {return den;};


    int gcd(int set_num, int set_den)
    {
    if(set_den == 0) return set_num;
    else
    {return gcd(set_den, set_num % set_den);};
    }


    Fraction(int a, int b)
    {set_num(a); set_den(b);};
    Fraction(){};

    //Fraction x is the b input and the default w/ no x. is the a input
    Fraction operator +(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_den()+den*x.get_num());
    result.set_den(den*x.get_den());
    return result;
    }

    Fraction operator -(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_den()-den*x.get_num());
    result.set_den(den*x.get_den());
    return result;
    }
    Fraction operator *(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_num());
    result.set_den(den*x.get_den());
    return result;
    }
    Fraction operator /(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_den());
    result.set_den(den*x.get_num());
    return result;
    }



    };



    int main()

    {
    Fraction a;
    Fraction b;
    Fraction c;
    Fraction out;
    int numer;
    int denom;
    char oper;


    cout<<"Please enter your FIRST Numerator and Denominator:\n";
    cin>>numer >> denom;
    a.set_num(numer); a.set_den(denom);


    if(denom >= 0)
    cout<<"please enter a valid denominator\n";
    cin>>denom;
    a.set_den(denom);


    cout<<"Please enter the SECOND Numerator Denominator:\n";
    cin>>numer >> denom;
    b.set_num(numer); b.set_den(denom);

    if(denom >= 0)
    cout<<"please enter a valid denominator\n";
    cin>>denom;
    b.set_den(denom);

    //operation, c is the new output after the operation is complete.
    cout<<"Choose operation of '+,-,*,/'\n";
    cin>>oper;
    switch(oper)
    {
    case '+':c=a+b;
    break;
    case '-':c=a-b;
    break;
    case '*':c=a*b;
    break;
    case '/':c=a/b;
    break;
    default: cout<<"Invalid Operator!\n";
    break;
    }




    cout<<"Success!\n"<<c.get_num()<<"/"<<c.get_den()<<endl;

    return 0;

    };

    /*
    int gcd(int set_num, int set_den)
    {
    if(set_den == 0) return set_num;
    else
    {return gcd(set_den, set_num % set_den);};
    }
    */
    Last edited by Belcross; March 6th, 2011 at 12:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured