--- title: "RPN Calculator" type: "article" slug: "rpn-calculator" url: "http://localhost/article/rpn-calculator/" markdown_url: "http://localhost/article/rpn-calculator.md" published_at: "2025-11-26T19:40:31+00:00" modified_at: "2026-06-21T23:24:15+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "Reverse Polish Notation (RPN) is mathmatical convention for handling expressions. RPN is most commonly found in Hewlett-Packard calculators. RPN, like Forth and Postscript, is stack oriented. Operators only handle data stored on the stack. The infix (or normal) expression 3 + 5 would be expressed as 3 5 +. Note the operator goes at the…" category: - name: "QL Hacker's Journal" slug: "ql-hackers-journal" taxonomy: "category" url: "http://localhost/category/periodicals/ql-hackers-journal/" post_tag: - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" - name: "Superbasic (programming language)" slug: "superbasic-programming-language" taxonomy: "post_tag" url: "http://localhost/tag/superbasic-programming-language/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" model: - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" indiv: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" publication_r: id: 33686 title: "QL Hacker’s Journal" type: "periodical" url: "http://localhost/periodical/ql-hackers-journal/" authors_r: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" issue: "6" issues_articles: - id: 61542 title: "QL Hacker’s Journal 6" type: "issue" url: "http://localhost/issue/ql-hackers-journal-6/" pubdate: "November 1991" archive_link: true --- # RPN Calculator Reverse Polish Notation (RPN) is mathmatical convention for handling expressions. RPN is most commonly found in Hewlett-Packard calculators. RPN, like Forth and Postscript, is stack oriented. Operators only handle data stored on the stack. The infix (or normal) expression 3 + 5 would be expressed as 3 5 +. Note the operator goes at the end of the expression. What really happens is that the 3 is pushed on the stack, then the 5 is pushed, and finally, when the + is encountered, it takes (pops) two numbers off the stack, adds them and puts the results back on the stack. For the expression (1 + 2) * 3, you would enter 1 2 + 3 *. You don’t need ()’s to control what operation takes place first. RPN executes each operator as it comes across it. Below is a Structured SuperBasic program that implements a simple RPN calculator. The left part of the screen is used for entry and results. The right part of the screen is used to show the current stack. With RPN, you can put a lot of numbers on the stack, then enter the operators for them. ### Reverse Polish Notation Calculator ``` T = 0 : F = 0 CLS #1 : CLS #2 num = 0 sx = 0 DIM stack(30) stackp = 1 num_entered = F REPeat loop in$=INKEY$(-1) IF (in$ INSTR "0123456789") THEN num = (10*num) + in$ PRINT #2,in$; num_entered = T END IF IF in$ = "+" THEN IF num_entered = T THEN push(num) num_entered = F END IF plus : num = 0 END IF IF in$ = "-" THEN IF num_entered = T THEN push(num) num_entered = F END IF minus : num = 0 END IF IF in$ = "*" THEN IF num_entered = T THEN push(num) num_entered = F END IF times : num = 0 END IF IF in$ = "/" THEN IF num_entered = T THEN push(num) num_entered = F END IF divide : num = 0 END IF IF in$ = "^" THEN IF num_entered = T THEN push(num) num_entered = F END IF power : num = 0 END IF IF in$ = CHR$(10) THEN push(num) : num = 0 : PRINT #2 END REPeat loop DEFine PROCedure push(x) sx = sx + 1 AT sx,0 : PRINT x stackp = stackp + 1 stack(stackp) = x END DEFine push DEFine FuNction pop LOCal temp IF stackp = 0 THEN PRINT #2,"Stack Error" STOP ELSE temp = stack(stackp) AT sx,0 : PRINT " " sx = sx - 1 IF sx = -1 THEN sx = 0 stackp = stackp - 1 RETURN temp END IF END DEFine pop DEFine PROCedure plus LOCal temp1, temp2 PRINT #2 : PRINT #2," +" temp1 = pop temp2 = pop temp1 = temp1 + temp2 push(temp1) PRINT #2," =" PRINT #2,temp1 END DEFine plus DEFine PROCedure minus LOCal temp1, temp2 PRINT #2 : PRINT #2," -" temp1 = pop temp2 = pop temp1 = temp1 - temp2 push(temp1) PRINT #2," =" PRINT #2,temp1 END DEFine minus DEFine PROCedure times LOCal temp1, temp2 PRINT #2 : PRINT #2," *" temp1 = pop temp2 = pop temp1 = temp1 * temp2 push(temp1) PRINT #2," =" PRINT #2,temp1 END DEFine times DEFine PROCedure divide LOCal temp1, temp2 PRINT #2 : PRINT #2," /" temp1 = pop temp2 = pop IF temp2 = 0 THEN PRINT #2,"Divide by Zero Error" ELSE temp1 = temp1 / temp2 push(temp1) PRINT #2," =" PRINT #2,temp1 END IF END DEFine divide DEFine PROCedure power LOCal temp1, temp2 PRINT #2 : PRINT #2," ^" temp1 = pop temp2 = pop IF temp2 = 0 THEN PRINT #2,"Negative Power Error" ELSE temp1 = temp1 ^ temp2 push(temp1) PRINT #2," =" PRINT #2,temp1 END IF END DEFine power ```