How to resolve the algorithm Arithmetic evaluation step by step in the ZX Spectrum Basic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic evaluation step by step in the ZX Spectrum Basic programming language

Table of Contents

Problem Statement

For those who don't remember, mathematical precedence is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic evaluation step by step in the ZX Spectrum Basic programming language

Source code in the zx programming language

10 PRINT "Use integer numbers and signs"'"+ - * / ( )"''
20 LET s$="": REM last symbol
30 LET pc=0: REM parenthesis counter
40 LET i$="1+2*(3+(4*5+6*7*8)-9)/10"
50 PRINT "Input = ";i$
60 FOR n=1 TO LEN i$
70 LET c$=i$(n)
80 IF c$>="0" AND c$<="9" THEN GO SUB 170: GO TO 130
90 IF c$="+" OR c$="-" THEN GO SUB 200: GO TO 130
100 IF c$="*" OR c$="/" THEN GO SUB 200: GO TO 130
110 IF c$="(" OR c$=")" THEN GO SUB 230: GO TO 130
120 GO TO 300
130 NEXT n
140 IF pc>0 THEN PRINT FLASH 1;"Parentheses not paired.": BEEP 1,-25: STOP 
150 PRINT "Result = ";VAL i$
160 STOP 
170 IF s$=")" THEN GO TO 300
180 LET s$=c$
190 RETURN 
200 IF (NOT (s$>="0" AND s$<="9")) AND s$<>")" THEN GO TO 300
210 LET s$=c$
220 RETURN 
230 IF c$="(" AND ((s$>="0" AND s$<="9") OR s$=")") THEN GO TO 300
240 IF c$=")" AND ((NOT (s$>="0" AND s$<="9")) OR s$="(") THEN GO TO 300
250 LET s$=c$
260 IF c$="(" THEN LET pc=pc+1: RETURN 
270 LET pc=pc-1
280 IF pc<0 THEN GO TO 300
290 RETURN 
300 PRINT FLASH 1;"Invalid symbol ";c$;" detected in pos ";n: BEEP 1,-25
310 STOP

  

You may also check:How to resolve the algorithm Haversine formula step by step in the Stata programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Clojure programming language
You may also check:How to resolve the algorithm Jordan-Pólya numbers step by step in the C programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm String concatenation step by step in the C# programming language