Posts: 294
Threads: 9
Joined: Oct 2002
create a function (which is allowed to use many other functions aswell and subs) that evaluates an expression.
i.e:
print evaluate("2*((3*4)+52)") 'returns 128
print evaluate("-10") 'returns -10
print evaluate("-2^8") 'returns -256
print evaluate("-sin(10+31/5) + 8") 'returns something i cant be bothered calculating :p
try to make it work in all cases, and try to remain as simple as can be.
Posts: 3,368
Threads: 195
Joined: Jan 2003
Something like this, but I can't get it to return back to the same line. I don't know if it's possible.
Code:
DECLARE FUNCTION evaluate# (inputstring$)
CLS
PRINT evaluate("5 + 5")
FUNCTION evaluate# (inputstring$)
OPEN "tempcalc.bas" FOR OUTPUT AS #1
PRINT #1, "a# =" + inputstring$
PRINT #1, "OPEN " + CHR$(34) + "tempcal2.bas" + CHR$(34) + " FOR OUTPUT as #2"
PRINT #1, "PRINT #2, a#"
PRINT #1, "CLOSE"
PRINT #1, "SYSTEM"
CLOSE
CHAIN "tempcalc.bas"
OPEN "tempcal2.bas" FOR INPUT AS #1
INPUT #1, evaluate#
CLOSE
KILL "tempcal2.bas"
KILL "tempcalc.bas"
END FUNCTION
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 2,020
Threads: 24
Joined: Jun 2002
i once wrote something of the like. it split everything by parthenthesis, then split everything by sign ("+-/*"), went across from left to right, and then replaced the entire parenthesis with the answer and started over until the entire expression was a single numerical value.
your best friend on this one is going to be "instr", and i can hook you up with some good string manipulation routines i wrote if you like.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 1,956
Threads: 65
Joined: Jun 2003
TO ALL:
This is not a trivial program. I have a similar program and it takes 700 lines of code to evaluate all the SIN,COS,TAN, etc., plus the AND,OR,XOR and PI.
AGAMEMNUS:
Your idea is brillant! You insert the expression into a generated program which you then chain to in order to do all the work.
I think the CHAIN returns to the top of the calling program. I don't remember exactly, but I think you need to set a switch to know if you are just stating you main program or coming back from the chained program. Work on it.
Maybe instead of the chain, you could generate the subordinate program, then do a shell to compile it. Your main program then shells to execute it. The result cound be in a workfile which the main program then reads and displays.
*****
Posts: 1,407
Threads: 117
Joined: Dec 2002
The Basic of Sinclair (i think it was Timex in the USA) Spectrum of the 80's had a function VAL$ that EVALUATED EXPRESSIONS. The whole basic interpreter fitted in 16K (and it was slow as hell). It's a function i ever missed...
Antoni
Posts: 358
Threads: 15
Joined: May 2003
Quote:create a function (which is allowed to use many other functions aswell and subs) that evaluates an expression.
i.e:
print evaluate("2*((3*4)+52)") 'returns 128
print evaluate("-10") 'returns -10
print evaluate("-2^8") 'returns -256
print evaluate("-sin(10+31/5) + 8") 'returns something i cant be bothered calculating :p
try to make it work in all cases, and try to remain as simple as can be.
Why not just use the print function...eg
Code:
CLS
PRINT 2 * ((3 * 4) + 52)
PRINT -10
PRINT -2 ^ 8
PRINT -SIN(10 + 31 / 5) + 8
works just fine for the expressions you posted....
Posts: 2,020
Threads: 24
Joined: Jun 2002
mango, he's talking about user inputted expressions
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 2,765
Threads: 138
Joined: Nov 2002
also it wouldn't be much of a good challenge
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 1,956
Threads: 65
Joined: Jun 2003
MANGO,
Nice try anyway. It's the thought that counts.
*****
Posts: 294
Threads: 9
Joined: Oct 2002
LOL. I figured aga's method would come up sooner or later
. No really..
Its good but slow heh, and it has to keep calling qb, put it this way, QB has a built in expression evaluator, what this challenge is, is to make your own. It might not be easy but it is a challenge
.