Factorial operator !

This is a discussion on Factorial operator ! within the C++ Programming forums, part of the General Programming Boards category; I'm trying to overload a factorial operator, and I'm caught on recursion. Here is my code so far: Code: int ...

  1. #1
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870

    Factorial operator !

    I'm trying to overload a factorial operator, and I'm caught on recursion. Here is my code so far:
    Code:
    int operator!(int number);
    ...
    int operator!(int number)
    {
        int temp;
        temp = number*((number-1)!)
        return temp;
    }
    Is there any way I can use ! in the definition? Also, if I define a simple operator, it will not compile, giving the error: 'int operator!(int)' must have an argument of class or enumerated type. I redefined ! to simply multiply the number by itself. (number*number). Can anyone give me some pointers?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    You can't redefine operators for basic types.

    You need to create your own integer-like class to define your own factoral opperator.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    from the C++ standard (13.5.6)
    An operator function shall either be a non-static member function or be a non-member function and have at
    least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Thanks, I've rewrote it but I still need an aswer to my recursive question.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Recursion is a form of a loop. A loop generally has a termination condition. The recursion needs this as well. Perhaps when you've reached zero?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you just need an end case.
    since 1! = 1 just check for that
    Code:
    if (number == 1)
        return 1;
    btw you might want to consider what happens if you input a negative number?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    And you need to check for 0 factorial; 0 factorial equals 1.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Sorry, copying error. Here is my current code:
    Code:
    factorial operator!(factorial thenum)
    {
        factorial temp;
        if(thenum.number <= 0) 
             return 1;
        temp.number = number.number*((number.number-1)!)
        return temp.number;
    }
    This still won't compile, and when I try to use the operator it tells me there should be a ; before it, aka this
    Code:
    3!
    would become this:
    Code:
    3;!
    And of course that won't compile
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The ! operator is a prefix operator. The only postfix operator in C++ is postfix ++ and --.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Your termination condition should read

    Code:
        if(thenum.number <= 0) 
             return 1;
    Should read
    Code:
        if(thenum.number <= 1) 
             return 1;

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the ! operator normally goes in front of a variable. I'm not even sure there is any way to make it a postfix operator.

    And to calculate a factorial, I would prefer a non-recursive loop. Set result = 1, then multiply it with all values from 2 to n to get n!

    Code:
    ....
    int operator!(const MyInt a) //non-recursive
    {
        int result = 1;
        for (int i = 2; i <= a.value; i++)
            result *= i;
        return result;
    }
    
    int main() 
    {
        MyInt test(5);
        std::cout << !test << std::endl; //NB, prefix
        std::cin.get();
    }

  12. #12
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    btw you can calculate the factorial using template meta programming for compile-time constants.

    Code:
    template <unsigned int Input>
    unsigned int Factorial(void)
    {
    	return Input * Factorial<Input - 1>();
    }
    
    template <>
    unsigned int Factorial<1>(void)
    {
    	return 1;
    }
    
    template <>
    unsigned int Factorial<0>(void)
    {
    	return 1;
    }
    no run-time code at all! doesn't work for every situation, but still a useful trick.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    I cringe at the possibility of using ! as factorial operator. Even if just for some specific class. It goes against everything an operator overload should be. Not to mention it's not even possible to mimic the mathematical semantics. So there isn't even that to hold on as a pro argument.

    ChaosEngine solution is the absolute best.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    >> ChaosEngine solution is the absolute best.

    thanks, but unfortunately my solution doesn't work for non-compile time constants.
    Code:
    unsigned int someNum = 0;
    cin >> someNum;
    cout << Factorial<someNum>(); //arrggg! compile error
    so you still need a runtime version
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Thanks for all the replies. I already have a function that works, but I was hoping I could make the notation cleaner. Ah well.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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