Computer Science Canada

Evaluate

Author:  killa_kid [ Tue Feb 03, 2004 6:08 pm ]
Post subject:  Evaluate

I am working on a graphing program and I would like for the users to be able to input a function of their own (eg: x^3, x+3, (x^4) +3 ). I was wondering if turing had a command that would evalutate a string as a command like in javascript.

If I wanted the function x^2, i would make a string with:
code:
currX**2


Now is there any way to get turing to execute that as is?

Author:  Cervantes [ Tue Feb 03, 2004 6:14 pm ]
Post subject: 

not as far as I know. to do that involves a lot of error proofing... I would suggest just getting the user to change the code where the equation is.

also, though x^2 is good for MSN and text on computers, in Turing its x**2

Author:  killa_kid [ Tue Feb 03, 2004 6:18 pm ]
Post subject: 

Uh, the x^2 is for input since that is how any computer program & calculators takes input of exponents, and in the string you see i do use ** so it would just get execed

Author:  Cervantes [ Tue Feb 03, 2004 6:55 pm ]
Post subject: 

well if you can trust your user to input things correctly then use the strint command or the strreal to convert the string into an integer or real numer, respectively. but they would have to input ** for exponents, not ^

Author:  killa_kid [ Tue Feb 03, 2004 6:56 pm ]
Post subject: 

the strint will parse the string and give me a value?

Author:  Cervantes [ Tue Feb 03, 2004 7:09 pm ]
Post subject: 

you should look at the help file, but anyways,

strint turns a string into an integer. It won't parse it. you have to error proof it before to make sure that the user inputs something that will correctly translate into a number. like entering a 'q' is a nono.

also entering ("x=74*/2") is a nono Confused

Author:  killa_kid [ Tue Feb 03, 2004 7:11 pm ]
Post subject: 

i have read the help file

but based on what u said u made it sound like it would parse

Author:  Cervantes [ Tue Feb 03, 2004 7:26 pm ]
Post subject: 

Cervantes wrote:
well if you can trust your user to input things correctly


have you played around with it? try different things.

it should give you a number as a result, but if the user puts in a q it'll mess up.

Author:  killa_kid [ Tue Feb 03, 2004 7:31 pm ]
Post subject: 

i amde a string with:

"3**2"

then did strreal and i got an error

Author:  santabruzer [ Tue Feb 03, 2004 7:31 pm ]
Post subject: 

because ** is a character

Author:  killa_kid [ Tue Feb 03, 2004 7:40 pm ]
Post subject: 

yes i know, but once again from what i am reading here i was being told i could do something along those lines

Author:  Cervantes [ Tue Feb 03, 2004 7:47 pm ]
Post subject: 

you can't. which is what I said at the very start. you can do a simple version of what you are trying to do, it'll work if you input 5. but no +, -, /, *, or **.

I would recommend just letting the user into the code to type in the equation there.

Author:  AsianSensation [ Tue Feb 03, 2004 10:07 pm ]
Post subject: 

killa_kid wrote:
i amde a string with:

"3**2"

then did strreal and i got an error


streal does not allow conversion of ASCII values different then the arabic numbers, scientific notation, and a + or - sign (I think). To do what you wanted to do, you probably have to index for the operation sign, and then separately strint the numbers and then do the operation on them.

Author:  killa_kid [ Wed Feb 04, 2004 7:15 am ]
Post subject: 

believe me i've been trying and it is really freakin hard

Author:  Cervantes [ Wed Feb 04, 2004 4:28 pm ]
Post subject: 

the last inclass project in gr 10 for me was this graphing assignment and my teacher told us not to worry about that part. just let them type inside the code!

Author:  killa_kid [ Wed Feb 04, 2004 6:08 pm ]
Post subject: 

the point is that i dont want some idiot grade 9 (if they even use it) to mess with my code


believe me there are only like 6 or 7 good comp sci students in each grade

Author:  apomb [ Wed Feb 04, 2004 8:58 pm ]
Post subject: 

Instead of letting them enter the code, just put something like

[code]
put "enter variable"
get (variable)
put (variable ** 2)
[/code]

Author:  santabruzer [ Wed Feb 04, 2004 9:04 pm ]
Post subject: 

dude... read the rest of the thread Rolling Eyes ... anyways.. i'll get a program on it sometime.. i'm pretty close.. so far i got the idea of creating an array for the values and then adding/multiplying the odd values...

Author:  santabruzer [ Wed Feb 04, 2004 9:16 pm ]
Post subject: 

meh.. a stab at it, though i know there are better ways:
code:
var str : array 1 .. 3 of string
for i : 1 .. 3
    get str (i)
end for
if str (2) = "*" and strintok (str (1)) and strintok (str (3)) then
    put strint (str (1)) * strint (str (3))
end if


Author:  AsianSensation [ Wed Feb 04, 2004 11:16 pm ]
Post subject: 

killa_kid wrote:
the point is that i dont want some idiot grade 9 (if they even use it) to mess with my code


believe me there are only like 6 or 7 good comp sci students in each grade


you can always PM someone that you think could handle the proble, and then they could fix your errors. Actually, PM me, I could probably help you out, but right now, all I'm seeing is bunch of if statements to handle the operators.


: