Parsing/RPN calculator algorithm: Difference between revisions

→‎{{header|ANSI Standard BASIC}}: Simplifications: 1. In get_number, buffer for conversion is not necessary. 2. In is_digit, used the parameter ch$ instead of a global variable. Added result in Decimal BASIC.
(Dialects of BASIC moved to the BASIC section.)
(→‎{{header|ANSI Standard BASIC}}: Simplifications: 1. In get_number, buffer for conversion is not necessary. 2. In is_digit, used the parameter ch$ instead of a global variable. Added result in Decimal BASIC.)
Line 681:
=={{header|BASIC}}==
==={{header|ANSI Standard BASIC}}===
{{works with|Decimal BASIC}}
<syntaxhighlight lang="ansi standard basic">1000 DECLARE EXTERNAL SUB rpn
1010 PUBLIC NUMERIC R(64) ! stack
1020 PUBLIC STRING expn$ ! for keyboard input
Line 766 ⟶ 767:
1830 EXTERNAL FUNCTION get_number
1840 DECLARE EXTERNAL FUNCTION is_digit
1850 LET ji1 = 1 i ! start of number stringsubstring
1860 DECLAREDO STRING number$ ! bufferget forinteger conversionpart
1990 1870 LET ji = ji + 1
1870 DO ! get integer part
1880 LETIF number$is_digit(j:j) = expn$(i:i) ) = false THEN
1890 LET IF expn$(i:i) = i"." THEN + 1
1900 LET ji = ji + 1
1910 IF DO WHILE is_digit( expn$(i:i) ) = falsetrue ! get fractional THENpart
1920 IF expn$(i:i) = "." then LET i = i + 1
1930 LOOP
1930 LET number$(j:j) = expn$(i:i) ! include decimal point
1940 END LET i = i + 1IF
1950 EXIT LET j = j + 1DO
1960 END IF
1960 DO WHILE is_digit( expn$(i:i) ) = true ! get fractional part
20401970 LOOP
1970 LET number$(j:j) = expn$(i:i)
1980 LET iget_number = VAL( expn$(i1:i +- 1) )
20601990 END FUNCTION
1990 LET j = j + 1
2000 LOOP!
2010 ! check for digit END IFcharacter
20902020 EXTERNAL FUNCTION is_digit( ch$ )
2020 EXIT DO
2030 IF "0" <= ENDch$ IFAND ch$ <= "9" THEN
2040 LET is_digit = true
2040 LOOP
21202050 ELSE
2050 LET get_number = VAL( number$ )
2060 LET is_digit = false
2060 END FUNCTION
2070 !END IF
2080 END FUNCTION
2080 ! check for digit character
2090 !
2090 EXTERNAL FUNCTION is_digit( ch$ )
21702100 EXTERNAL SUB print_stack
2100 IF "0" <= expn$(i:i) AND expn$(i:i) <= "9" THEN
2110 PRINT expn$(i:i);" LET is_digit = true";
21902120 FOR ptr=n TO 2 STEP -1
2120 ELSE
2130 LETPRINT is_digitUSING = false"-----%.####":R(ptr);
2140 ENDNEXT IFptr
2150 END FUNCTIONPRINT
22302160 END SUB</syntaxhighlight>
2160 !
{{out}}
2170 EXTERNAL SUB print_stack
<pre>
2180 PRINT expn$(i:i);" ";
enter an RPN expression:
2190 FOR ptr=n TO 2 STEP -1
? 3 4 2 * 1 5 - 2 3 ^ ^ / +
2200 PRINT USING "-----%.####":R(ptr);
expn: 3 4 2 * 1 5 - 2 3 ^ ^ / +
2210 NEXT ptr
3.0000
2220 PRINT
4.0000 3.0000
2230 END SUB</syntaxhighlight>
2.0000 4.0000 3.0000
* 8.0000 3.0000
1.0000 8.0000 3.0000
5.0000 1.0000 8.0000 3.0000
- -4.0000 8.0000 3.0000
2.0000 -4.0000 8.0000 3.0000
3.0000 2.0000 -4.0000 8.0000 3.0000
^ 8.0000 -4.0000 8.0000 3.0000
^ 65536.0000 8.0000 3.0000
/ 0.0001 3.0000
+ 3.0001
result: 3.0001220703125
enter an RPN expression:
?
 
</pre>
 
==={{header|BBC BASIC}}===
512

edits