Information Technology - Computer Programming - Source Code - Homebrew - Open Source - Software - Hardware - 8 bit - 16 bit - 32 bit - 64 bit - x86 - x64 - DOS - Windows - Linux - Arduino - Embedded - Development - Retro - Vintage - Math - Science - History - Hobby - Beginners - Professionals - Experiment - Research - Study - Fun - Games

ATTENTION NEW USERS: Due to bot traffic, we are forced to manually approve registrations. We get thousands of bots trying to register, causing us to delete registrations in bulk with little ability to filter what is real or not. If you're having trouble getting approved, then send an email to ptrworkmails@gmail.com explaining that you are a real user. Use the same email you're trying to register with. Thank you.

Expression Evaluator

Share your Express Basic creations here.
Post Reply
admin
Site Admin
Posts: 145
Joined: Wed Feb 22, 2023 6:51 am

Expression Evaluator

Post by admin »

Expression Evaluator

This example was created to test the new string functionality.

Supports */+-() operators with precedence.
Also supports decimals and negation.

Code: Select all

1 REM RPN Expression Evaluator by Gemino Smothers
2 REM Written in Express BASIC
5 CLS: PRINT "Expression Evaluator": PRINT: PRINT "Enter an expression.": PRINT
10 INPUT "Infix: ", infix$: IF LEN(infix$) THEN GOSUB 10000: PRINT "Evaluation: "; @(0)
30 PRINT: INPUT "Continue? (Y/N)", continue$: PRINT: continue$ = UCASE$(continue$)
40 IF continue$ = "N" THEN END
50 IF continue$ = "Y" THEN 10
60 GOTO 30
10000 postfix$ = "": operatorstack$ = "": GOSUB 14000
10010 FOR symbolpos = 1 TO LEN(infix$)
10020 symbol$ = MID$(infix$, symbolpos, 1): IF symbol$ = " " THEN 10120
10030 IF symbol$ = "." OR (ASC(symbol$) >= 48 AND ASC(symbol$) <= 57) THEN postfix$ = postfix$ + symbol$: GOTO 10080
10040 IF symbol$ = "(" THEN operatorstack$ = operatorstack$ + symbol$: GOTO 10080
10050 IF symbol$ = ")" THEN GOSUB 11000: GOTO 10080
10060 IF symbol$ = "*" OR symbol$ = "/" OR symbol$ = "+" OR symbol$ = "-" THEN GOSUB 12000: GOTO 10080
10070 IF symbol$ = "_" THEN postfix$ = postfix$ + "-"
10080 NEXT symbolpos
10090 newstack$ = ""
10100 FOR symbolpos = 1 TO LEN(operatorstack$)
10110 newstack$ = newstack$ + " " + MID$(operatorstack$, symbolpos, 1)
10120 NEXT symbolpos
10130 operatorstack$ = newstack$
10140 postfix$ = postfix$ + operatorstack$
10150 GOSUB 15000: RETURN
11000 IF RIGHT$(operatorstack$, 1) = "(" THEN 11040
11010 postfix$ = postfix$ + " " + RIGHT$(operatorstack$, 1)
11020 operatorstack$ = LEFT$(operatorstack$, LEN(operatorstack$) - 1)
11030 IF RIGHT$(operatorstack$, 1) <> "(" THEN 11010
11040 operatorstack$ = LEFT$(operatorstack$, LEN(operatorstack$) - 1)
11050 RETURN
12000 GOSUB 13000
12010 IF LEN(operatorstack$) = 0 OR RIGHT$(operatorstack$, 1) = "(" OR symbolprecedence > stackprecedence THEN 12060
12020 postfix$ = postfix$ + " " + RIGHT$(operatorstack$, 1)
12030 operatorstack$ = LEFT$(operatorstack$, LEN(operatorstack$) - 1)
12040 GOSUB 13000
12050 IF LEN(operatorstack$) AND RIGHT$(operatorstack$, 1) <> "(" AND symbolprecedence <= stackprecedence THEN 12020
12060 operatorstack$ = operatorstack$ + symbol$
12070 postfix$ = postfix$ + " "
12080 RETURN
13000 stacktop$ = RIGHT$(operatorstack$, 1)
13010 IF stacktop$ = "+" OR stacktop$ = "-" THEN stackprecedence = 1: GOTO 13040
13020 IF stacktop$ = "*" OR stacktop$ = "/" THEN stackprecedence = 2: GOTO 13040
13030 stackprecedence = 0
13040 IF symbol$ = "+" OR symbol$ = "-" THEN symbolprecedence = 1: GOTO 13070
13050 IF symbol$ = "*" OR symbol$ = "/" THEN symbolprecedence = 2: GOTO 13070
13060 symbolprecedence = 0
13070 RETURN
14000 infix$ = "(" + infix$ + ")": IF LEN(infix$) = 3 THEN RETURN
14010 FOR replacesymbol = 1 TO LEN(infix$)
14020 IF MID$(infix$, replacesymbol, 1) <> "-" THEN 14150
14030 symbol$ = MID$(infix$, replacesymbol - 1, 1)
14040 symbolpos = replacesymbol
14050 IF symbol$ <> " " THEN 14090
14060 symbolpos = symbolpos - 1
14070 symbol$ = MID$(infix$, symbolpos - 1, 1)
14080 IF symbol$ = " " THEN 14060
14090 negative = 0
14100 IF symbol$ = "(" OR symbol$ = "*" OR symbol$ = "/" OR symbol$ = "+" OR symbol$ = "-" THEN negative = 1
14110 IF negative = 0 THEN 14150
14120 leftfix$ = LEFT$(infix$, replacesymbol - 1)
14130 infix$ = RIGHT$(infix$, LEN(infix$) - LEN(leftfix$) - 1)
14140 infix$ = leftfix$ + "_1*" + infix$
14150 NEXT replacesymbol
14160 RETURN
15000 @(0) = 0: pointer = 0
15010 postfix$ = LTRIM$(postfix$): IF INSTR(postfix$, " ") = 0 THEN symbol$ = postfix$: postfix$ = "": GOTO 15030
15020 symbol$ = LEFT$(postfix$, INSTR(postfix$, " ") - 1): postfix$ = RIGHT$(postfix$, LEN(postfix$) - LEN(symbol$) - 1)
15030 leftsymbol$ = LEFT$(symbol$, 1)
15040 IF (leftsymbol$ = "." OR (ASC(leftsymbol$) >= 48 AND ASC(leftsymbol$) <= 57)) = 0 THEN 15060
15050 @(pointer) = VAL(symbol$): pointer = pointer + 1: GOTO 15080
15060 IF leftsymbol$ = "*" OR leftsymbol$ = "/" OR leftsymbol$ = "+" THEN GOSUB 17000: GOTO 15080
15070 IF leftsymbol$ = "-" THEN GOSUB 16000
15080 IF LEN(postfix$) THEN 15010
15090 RETURN
16000 IF LEN(symbol$) > 1 THEN 16050
16010 operator$ = symbol$
16020 pointer = pointer - 1: operand2 = @(pointer): @(pointer) = 0
16030 pointer = pointer - 1: operand1 = @(pointer): @(pointer) = 0
16040 GOSUB 18000: GOTO 16060
16050 @(pointer) = VAL(symbol$): pointer = pointer + 1
16060 RETURN
17000 operator$ = symbol$
17010 pointer = pointer - 1: operand2 = @(pointer): @(pointer) = 0
17020 pointer = pointer - 1: operand1 = @(pointer): @(pointer) = 0
17030 GOSUB 18000: RETURN
18000 IF operator$ = "*" THEN @(pointer) = operand1 * operand2
18010 IF operator$ = "/" THEN @(pointer) = operand1 / operand2
18020 IF operator$ = "+" THEN @(pointer) = operand1 + operand2
18030 IF operator$ = "-" THEN @(pointer) = operand1 - operand2
18040 pointer = pointer + 1: RETURN
Post Reply