i am starting programming using CL on IBM system i. My task is to implement RPN calculator using CL language. Typically, this calculator uses stack. But I have no idea have to implement it in CL. Can someone give me some advice? Maybe *PTR variables are solution, but can someone give me an example how to use it?
Thanks in advance JS
edit:
CL language is imposed by my tutor.
My program should receives signs from user (digits/number, signs of mathematical operations).
After receiving each sign program should analize what was received and then take some action, according to pseudo-code:
Do when new sign was entered:
if: entered sign is a number
then push it into stack
else if: entered sign is a sign of mathematical operation
then
- pop two elements from stack
- make operation using this elements and entered sign
- push result into stack
My problem is how to implement or substitude this stack. A need to reach element in LIFO order and I do not know how many elements the structure finally will contain.
Understand that CL is ill-suited for this. Following is untested:
DCL &ARRAY *CHAR 50
DCL &PTR *PTR ADDRESS(&ARRAY)
DCL &ELEM *CHAR 10 STG(*BASED) BASPTR(&PTR)
DCL &i *DEC (9 0)
...
/* Loop through the array */
DOFOR VAR(&i) FROM(1) TO(5) BY(1)
CHGVAR %OFFSET(&PTR) (%OFFSET(&PTR) + 10)
/* &ELEM is the current array element */
ENDDO
Post some code; what did you try? If you can't post any code, post pseudo-code and explain what you want the code to do.
EDIT: The above code snippet implements an array. This is quite similar to a stack. LIFO is as easy as keeping track of the latest array index. In the above snippet, the CHGVAR adds 10 bytes to the pointer. It is 10 bytes because each array element / stack element is 10 bytes (DCL &ELEM). This particular stack holds 5 entries - 5 times 10 = 50 (DCL &ARRAY). Adding 10 bytes is PUSH, subtracting 10 bytes is POP. The current &PTR is the most recent stack entry.
Your next step should be to write one subroutine for each of the major operations in your outline. Start with PUSH. If you aren't familiar with the debuggers on IBM i, use DMPCLPGM to see the results of your code in operation. Try it, then if you still have questions, post your code and ask a specific question about that code. Programming is about writing code, so jump in and try! :-)