Like Tree3Likes
  • 1 Post By laserlight
  • 2 Post By manasij7479

Aritmethic operations on Stack (Array implementation)

This is a discussion on Aritmethic operations on Stack (Array implementation) within the C Programming forums, part of the General Programming Boards category; Hey folks, I have number arrays(int a[]) and operator arrays(char b[]) seperately. And also I can combine them both into ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    19

    Post Aritmethic operations on Stack (Array implementation)

    Hey folks,

    I have number arrays(int a[]) and operator arrays(char b[]) seperately. And also I can combine them both into one another integer array(operator characters will be transfered there with ASCII codes). It doesn't matter how the arrays are. I can do it in multi ways.

    The problem is; you know aritmethic operations can be fulfilled by stacks. Just like 7+(9-3)*3 etc. There are operator priorities just as '*' prior to '+'. So how can I get them into stack operation so that it's going to give the correct answer 25.

    It's just a basic stack implementation. Thanks in advance.

    (Firstly I have to turn that operation(7+(9-3)*3) into postfix struct. Just like (7 9 3 - 3 * +)
    Last edited by ilerleartik; 03-10-2012 at 03:14 PM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    You have to order them by prefix or RPN notation.
    Converting normal (infix) input to RPN notation is done by the Shunting Yard Algorithm.
    After that, the evaluation is simple.
    Last edited by manasij7479; 03-10-2012 at 03:31 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    Hmm If I google that shunting yard algorithm, Can I find the code that ready to execute?

    Because I know the algorythm, I just need the codes that is ready to use if exists.

    I cant code that algoryhtm.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    No need for warning, I don't want you to code all the program. If exists, Ive just requested the ready-code. If not exist, somehow I will code it on my own.

    Here is another problem. I've seen some codes, all elementaries kept in Char Array. But you can't keep '300' number in a Char array. Because it's out of '256' unsigned char interval. So I gotta keep it in integer array. Am I wrong?
    Last edited by ilerleartik; 03-10-2012 at 03:53 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote Originally Posted by ilerleartik
    Here is another problem. I've seen some codes, all elementaries kept in Char Array. But you can't keep '300' number in a Char array. Because it's out of '256' unsigned char interval. So I gotta keep it in integer array. Am I wrong?
    You are right.

    One way to do this is to read and parse the input into a sequence of tokens. The tokens are strings, or perhaps struct objects containing a string and a tag (which could be an enum) that determines if it is a number or an operator. Then, you process this sequence of tokens.
    ilerleartik likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by laserlight View Post
    You are right.
    May not be,, btw.
    That particluar stack could have exclusively been for the operators in that implementation.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote Originally Posted by manasij7479
    May not be,, btw.
    That particluar stack could have exclusively been for the operators in that implementation.
    That has no bearing on whether integer values in a range of [0, 300] can be stored in an unsigned char, given an 8-bit byte.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by laserlight View Post
    That has no bearing on whether integer values in a range of [0, 300] can be stored in an unsigned char, given an 8-bit byte.
    If someone is trying to 'erroneously' store numbers in a character stack, changing the stack to an integer stack will ....well.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Heh, but that's a problem with the initial implementation in the first place, isn't it? After all, it means that can only possibly work for say, single digit numbers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by laserlight View Post
    Heh, but that's a problem with the initial implementation in the first place, isn't it? After all, it means that can only possibly work for say, single digit numbers.
    Possibly not.
    I'd mean that only single digit opreators can be used.. no ++ etc.
    The numbers are not stored during the shunting, only afterwards.
    From the wiki:
    Code:
    Input: 3+4
    
    
    1. Add 3 to the output queue (whenever a number is read it is added to the output)
    2. Push + (or its ID) onto the operator stack
    3. Add 4 to the output queue
    4. After reading the expression pop the operators off the stack and add them to the output.
    5. In this case there is only one, "+".
    6. Output 3 4 +
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote Originally Posted by manasij7479
    I'd mean that only single digit opreators can be used.. no ++ etc.
    Indeed, that would be another limitation.

    Quote Originally Posted by manasij7479
    The numbers are not stored during the shunting, only afterwards.
    There has to be an initial sequence of tokens to process, and this sequence includes both numbers and operators. Since ilerleartik observed that "all elementaries kept in Char Array", the array in question is this initial sequence. Hence, my suggestion in post #6 of using strings or struct objects for the tokens.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    Firstly, thanks for your recommendations and arguing on this issue. To talk about what Laserlight said, yes, I've heard about tokens, however I don't exactly know what it is and how it works. So if I keep my elementaries in Char array, I will encounter a problem like unable to keep the numbers bigger than 256 (Unsigned char). But If I keep all elementaries seperately in an integer array, just like a[0]=745 a[1]= '+' a[2]=17 ... etc. this time, a[1] will be an integer value by getting its value from ASCII table. And its going to be 45 or something. So there will be no operators.

    If I assume that 45 value as '+', then if user enters any 45 number for calculating, there will be conflict and a great problem. I think I can solve it in this way: If I keep '+' (45 in ASCII) as '+'*100.....00 (a big number that user can't/doesn't enter), then I can evaluate it as an operator. Just like
    Code:
    if(a[1]==('+')*1000...00) then it is ADDITION operator;{ block}
    ... so on.

    is that Possible?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-01-2011, 12:35 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 02:12 PM
  3. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 10:32 AM
  4. code review request: stack implementation
    By cs32 in forum C Programming
    Replies: 6
    Last Post: 02-24-2008, 02:26 PM
  5. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM

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