Thread: postfix calculator using stacks-problem

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    24

    postfix calculator using stacks-problem

    i made a postfix calculator using stacks in c.

    i want to know how to right stack code for calculating exponentiation?

    i want to know this part only
    example

    this is the stack commands for adding numbers

    case '+':
    push(pop() + pop());
    break;

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by blogchama View Post
    i made a postfix calculator using stacks in c.

    i want to know how to right stack code for calculating exponentiation?

    i want to know this part only
    example

    this is the stack commands for adding numbers

    case '+':
    push(pop() + pop());
    break;
    For exponentiation you should include math.h and use:

    pow(base,exponent);
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yep what he said , except I am not sure if you can do 2 pops on 1 line like that... something about multiple side-effects or something or the other..

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    24
    Quote Originally Posted by claudiu View Post
    For exponentiation you should include math.h and use:

    pow(base,exponent);
    can't we use stacks to do that?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Using a for loop would be good. Using a stack??

    I suppose it could be done - as could a sled dog race with dachshunds. Once you have the number, and you have the exponent in your variables, what do you want the stack for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Problem using two stacks with following code
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:52 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM