Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a String to Variable

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
For example you want a program that allows you to type in the formula, then it prints out the answer.

Such as

INPUT "Please type in your formula :"; formula$

' if type in "A + B - C"
' formula$ = "A + B - C"

A = 1
B = 2
C = 3

'
'How can I get the result
'according to the formula I typed in
'
'Such as:
'Y = A + B - C
'

Print "Result is"; Y



Thanks Very Much for your help
 
Well, I didn't found any use about this but I've been tricked by that one. Perhaps it might have a little something about homeworks but anyways. There's what I did.

REM ALGEBRICAL OPERATIONS
REM ### THIS PROGRAM USE VALUE A=1, B=2.. ..Y=25, Z=26 ###
REM ### AS AKED BY Knowlittle ###

DIM eStack$(100)
DIM dumpStack$(100)
DIM nStack$

SCREEN 0: CLS
INPUT "Formula: ", formula$
formula$ = UCASE$(formula$)

REM ### CHOPPING BLANK SPACES ###

FOR i = 1 TO LEN(formula$)
temp$ = MID$(formula$, i, 1)
IF temp$ <> &quot; &quot; THEN nStack$ = nStack$ + temp$
NEXT i

REM ### STORING THE FORMULA INTO eStack$() ###

eStack$(0) = STR$(LEN(nStack$)) ' Number Of Element In The Stack
FOR i = 1 TO VAL(eStack$(0))
eStack$(i) = MID$(nStack$, i, 1)
NEXT i

REM ### AFFECTING VALUE TO ALGEBRICS ###

FOR i = 1 TO VAL(eStack$(0))
SELECT CASE eStack$(i)
CASE CHR$(56) TO CHR$(90): eStack$(i) = STR$(ASC(eStack$(i)) - 64)
CASE ELSE: eStack$(i) = eStack$(i)
END SELECT
NEXT i

REM ### PRIORITY ###
REM ### SEARCHING FOR OPERATIONS SIGNS ###

REM ### FINDING EXP ###

DO
REM ### FINDING ^ ###

FOR i = 1 TO VAL(eStack$(0))
IF eStack$(i) = CHR$(94) THEN
extract% = i
EXIT FOR
END IF
NEXT i

REM ### PROCEDING WITH THE OPERATION ###

IF extract% <> 0 THEN
tempNo1% = VAL(eStack$(extract% - 1))
tempNo2% = VAL(eStack$(extract% + 1))
answer% = tempNo1% ^ tempNo2%

REM ### UPDATING STACK WITHOUT THE ^ OPERATION ###

eStack$(extract% - 1) = STR$(answer%)
eStack$(extract%) = &quot;&quot;
eStack$(extract% + 1) = &quot;&quot;
FOR i = 1 TO 100
IF eStack$(i) <> &quot;&quot; THEN
counter% = counter% + 1
dumpStack$(counter%) = eStack$(i)
END IF
NEXT i
FOR i = 0 TO 100: eStack$(i) = &quot;&quot;: NEXT i
FOR i = 1 TO counter%: eStack$(i) = dumpStack$(i): NEXT i
eStack$(0) = STR$(counter%)

ELSE
counter% = 0
extract% = 0
FOR i = 0 TO 100: dumpStack$(i) = &quot;&quot;: NEXT i
EXIT DO
END IF

REM ### CLEARING VARIABLES ###

counter% = 0
extract% = 0
FOR i = 0 TO 100: dumpStack$(i) = &quot;&quot;: NEXT i

LOOP


REM ### FINDING MUL AND DIV ###

DO
REM ### FINDING * OR / ###

FOR i = 1 TO VAL(eStack$(0))
SELECT CASE eStack$(i)
CASE CHR$(42)
extract% = i
EXIT FOR
CASE CHR$(47)
extract% = i
EXIT FOR
CASE ELSE
END SELECT
NEXT i

REM ### PROCEDING WITH THE OPERATION ###

IF extract% <> 0 THEN
tempNo1% = VAL(eStack$(extract% - 1))
tempNo2% = VAL(eStack$(extract% + 1))
operator$ = eStack$(extract%)
SELECT CASE operator$
CASE CHR$(42): answer% = tempNo1% * tempNo2%
CASE CHR$(47): answer% = tempNo1% / tempNo2%
CASE ELSE
END SELECT

REM ### UPDATING STACK WITHOUT THE * OR / OPERATION ###

eStack$(extract% - 1) = STR$(answer%)
eStack$(extract%) = &quot;&quot;
eStack$(extract% + 1) = &quot;&quot;
FOR i = 1 TO 100
IF eStack$(i) <> &quot;&quot; THEN
counter% = counter% + 1
dumpStack$(counter%) = eStack$(i)
END IF
NEXT i
FOR i = 0 TO 100: eStack$(i) = &quot;&quot;: NEXT i
FOR i = 1 TO counter%: eStack$(i) = dumpStack$(i): NEXT i
eStack$(0) = STR$(counter%)

REM ### SETTING NEW STACK SIZE INTO eStack$(0) ###

FOR i = 1 TO 100
IF dumpStack$(i) = &quot;&quot; THEN
eStack$(0) = STR$(i - 1)
EXIT FOR
END IF
NEXT i

ELSE
counter% = 0
extract% = 0
FOR i = 0 TO 100: dumpStack$(i) = &quot;&quot;: NEXT i
EXIT DO
END IF

REM ### CLEARING VARIABLES ###

counter% = 0
extract% = 0
FOR i = 0 TO 100: dumpStack$(i) = &quot;&quot;: NEXT i

LOOP


REM ### FINDING ADD AND SUB ###

DO
REM ### FINDING + OR - ###

FOR i = 1 TO VAL(eStack$(0))
SELECT CASE eStack$(i)
CASE CHR$(43)
extract% = i
EXIT FOR
CASE CHR$(45)
extract% = i
EXIT FOR
CASE ELSE
END SELECT
NEXT i

REM ### PROCEDING WITH THE OPERATION ###

IF extract% <> 0 THEN
tempNo1% = VAL(eStack$(extract% - 1))
tempNo2% = VAL(eStack$(extract% + 1))
operator$ = eStack$(extract%)
SELECT CASE operator$
CASE CHR$(43): answer% = tempNo1% + tempNo2%
CASE CHR$(45): answer% = tempNo1% - tempNo2%
CASE ELSE
END SELECT

REM ### UPDATING STACK WITHOUT THE + OR - OPERATION ###

eStack$(extract% - 1) = STR$(answer%)
eStack$(extract%) = &quot;&quot;
eStack$(extract% + 1) = &quot;&quot;
FOR i = 1 TO 100
IF eStack$(i) <> &quot;&quot; THEN
counter% = counter% + 1
dumpStack$(counter%) = eStack$(i)
END IF
NEXT i
FOR i = 0 TO 100: eStack$(i) = &quot;&quot;: NEXT i
FOR i = 1 TO counter%: eStack$(i) = dumpStack$(i): NEXT i
eStack$(0) = STR$(counter%)

ELSE
counter% = 0
extract% = 0
FOR i = 1 TO 100: dumpStack$(i) = &quot;&quot;: NEXT i
EXIT DO
END IF

REM ### CLEARING VARIABLES ###

counter% = 0
extract% = 0
FOR i = 1 TO 100: dumpStack$(i) = &quot;&quot;: NEXT i

LOOP

PRINT &quot;answer: &quot;; eStack$(1)

Few! A sweaty thing but the computer didn't got me on that one. Or maybe not..It's pretty scaring, not?
 
A mistake here! Remove:
REM ### SETTING NEW STACK SIZE INTO eStack$(0) ###
FOR i = 1 TO 100
IF dumpStack$(i) = &quot;&quot; THEN
eStack$(0) = STR$(i - 1)
EXIT FOR
END IF
NEXT i

It was a part of the prototype. eStack$(0) isn't use at run time anyway.
 
Double mistake! eStack$(0) IS use at run time but. But remove the prototype:
REM ### SETTING NEW STACK SIZE INTO eStack$(0) ###
FOR i = 1 TO 100
IF dumpStack$(i) = &quot;&quot; THEN
eStack$(0) = STR$(i - 1)
EXIT FOR
END IF
NEXT i

Now we're set on!
 
Oak, Thanks very very much for your help.

Probably the only thing I can do in return is helping back this community when I become a more experience programmer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top