E.g.:
UserINput$ = "Y = 2*x+3"
'-->
'...
Output = 2*x+3
BASIC: QB/Rapid-Q BASIC
--
Christian
Rznvy jbexvat
INPUT "Enter an equation: ", text$
PRINT
index$ = "y=x+-*/0123456789^().abcdefghijklmnopqrstuvwz[]{}#!% $"
PRINT "Index Locations 1 - 54: "; index$
PRINT
PRINT "Character's", "Character's"
PRINT "Equation", "Index"
PRINT "Location", "Location"
text$ = LCASE$(text$)
ka = LEN(text$)
FOR i = 1 TO ka
bt$ = MID$(text$, i, 1)
GOSUB Tibc
NEXT i
PRINT
IF agg > 1 THEN PRINT "Error: There are two 'equals' symbols."
END
Tibc:
FOR t = 1 TO 54
pv$ = MID$(index$, t, 1)
IF pv$ = bt$ THEN
PRINT i, t
IF t = 2 THEN agg = agg + 1
EXIT FOR
END IF
IF pv$ <> bt$ AND t = 54 THEN PRINT i, "N/A"
NEXT t
RETURN
You touched a very intesting problem of the computer science: generating of
program code dynamically in *run time* or a better subject title:
Self-modifying code.
The old GWBASIC.EXE allowed you to use the CHAIN MERGE statement to include
an external file. When you create this file in run time, the you get what
you are looking for. The following example is a small mathematical function
plotter with run time generated function.
100 ' Simple mathematical plotter
110 ' with dynamically generated function
120 :
130 LINE INPUT "Enter function y=f(x): ", FK$
140 INPUT "xmin";XI!
150 INPUT "xmax";XA!
160 INPUT "ymin";YI!
170 INPUT "ymax";YA!
180 TP$ = ENVIRON$("TEMP")
190 IF TP$<>"" THEN 220
200 PRINT "Invalid TEMP variable. Modify AUTOEXEC.BAT accordingly."
210 SYSTEM
220 IF RIGHT$(TP$, 1) <> "\" THEN TP$ = TP$ + "\"
230 ' Create a *unique* filename
240 ' See
http://dreael.catty.ch/Deutsch/BASIC-Knowhow-Ecke/EinbettungBSY.html
250 ' for this issue
260 TP$ = TP$ + "~"
270 FOR I%=1 TO 3: TP$ = TP$ + CHR$(65 + CINT(INT(26! * RND))): NEXT I%
280 TP$ = TP$ + MID$(TIME$, 4, 2) + RIGHT$(TIME$, 2)
290 OPEN TP$+".bas" FOR OUTPUT AS 1
300 PRINT#1,"350 DEF FN F!(X!)="; FK$
310 CLOSE 1
320 CHAIN MERGE TP$,330,ALL
330 KILL TP$+".bas"
340 ' This next line will be produced dynamically!
350 DEF FN F!(X!)=X!^2!
360 SCREEN 9
370 WINDOW(XI!,YI!)-(XA!,YA!)
380 LINE (PMAP(0,2),0!)-(PMAP(639,2),0!),5
390 LINE (0!,PMAP(0,3))-(0!,PMAP(349,3)),5
400 X!=PMAP(-1,2):PSET(X!,FN F!(X!)),14
410 FOR X%=0 TO 640:X!=PMAP(X%,2)
420 LINE -(X!,FN F!(X!)),14
430 NEXT X%
440 D$=INPUT$(1)
450 SCREEN 0
The most important lines for you are 290 to 350 where a small program
fragment will be tritten into your TEMP directory, MERGEd and then executed.
QuickBASIC does not longer support MERGE. There you only have CHAIN which is
virtually like RUN "filename", i.e. the *whole* program in RAM will replaced
with the new file's program except that you can keep some variables with
COMMON.
Andreas
>The old GWBASIC.EXE allowed you to use the CHAIN MERGE statement to include
>an external file. When you create this file in run time, the you get what
Just what I was looking for; and then I can't use it. Damn.
>QuickBASIC does not longer support MERGE. There you only have CHAIN which is
>virtually like RUN "filename", i.e. the *whole* program in RAM will replaced
>with the new file's program except that you can keep some variables with
>COMMON.
Well, it wasn't QuickBASIC I needed if for. It was Rapid-Q BASIC but RQB
doesn't even have CHAIN.
--
Christian
Rznvy jbexvat
read up on chain/merge
CHRistian (nos...@direkte.org) wrote:
: How do I get a string to become a function the program can use?
: E.g.:
: BASIC: QB/Rapid-Q BASIC
: --
: Christian
: Rznvy jbexvat
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, ex seders moderator sed u soon
it is not zat we do not see the s o l u t i o n
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
chain/merge works for interpreted codes only. In MSDOS the command was,
'SPAWN' to run a new task. Problem is that you don't have a new task you only
have a text string - so...
Ask yourself, "How is this problem any different than a simple calculator
program? I type, "2 * 7 + 3 = " and the calculator returns 17. If the
calculator is a stack based processor this is trivial. If it is a memory based
processor it is exactly the same problem. I type 2 it stores it in a variable
N1. I type * it stores it in a variable V1. I type 7 it stores it in a
variable N2. I type + it stores it in a variable V2, I type 3 it stores it in a
variable N3 and finally I type = and it creates a numeric stack - push N1, Push
N2, execute V1, push N3 execute V2, pop stack into display.
Your program should work exactly the same way except it parses in the other
order. You will need to become very familiar with the Switch/Case type
construct which is much more efficient at multiple alternatives than the IF /
THEN / ELSE construct.
You will parse the string, one character at a time:
Get "Y"
Is this a number? Yes N1 = number
Is this an action? Yes V1 = operator
Is this a character? Yes L1 = label
and so on until all of the numbers, characters and operators have been
assigned.
Levels of parentheses makes the code more complex but not more complicated.
Turbo Pascal used to have the code for such an application as part of its
examples. Pascal offered some nice features to make this kind of operation
slick. If you can find a website Turbo Pascal you might be able to find a copy
of the calculator demo and take a look at how they parsed numbers. You might
also be able to find a calculator program in one of the QB program archives
that exist around on the web.