Thread: Aritmethic operations on Stack (Array implementation)

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  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
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 +

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:35 AM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09: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