# Thread: Aritmethic operations on Stack (Array implementation)

1. ## 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 * +)

2. 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.

3. 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. 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?

5. 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.

6. Originally Posted by laserlight
You are right.
May not be,, btw.
That particluar stack could have exclusively been for the operators in that implementation.

7. 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.

8. Originally Posted by laserlight
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.

9. 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.

10. Originally Posted by laserlight
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

Add 3 to the output queue (whenever a number is read it is added to the output)Push + (or its ID) onto the operator stackAdd 4 to the output queueAfter reading the expression pop the operators off the stack and add them to the output.In this case there is only one, "+".Output 3 4 +```

11. Originally Posted by manasij7479
I'd mean that only single digit opreators can be used.. no ++ etc.
Indeed, that would be another limitation.

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.

12. 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?