Evaluating Mathematical Expression From A String And Inserting It On Stack
I'm trying to build an arbitrary precision calculator. I represent numbers in linked lists (one node is a single digit), and I want to store them in a stack. However, I can't seem
Solution 1:
you could try using eval
?
eval("6*8-2+8-8*9/4")
This would give you 36
EDIT:
If eval isn't feasible, maybe try operator
to convert the string operators to mathematical ones:
importoperator
operations = {
'+' : operator.add,
'-' : operator.sub,
'*' : operator.mul,
'/' : operator.div,
'%' : operator.mod,
'^' : operator.xor
}
Then maybe you could loop through the string and evaluate it that way? (I'll have a think about the looping part now and edit my answer if I get anything good)
Post a Comment for "Evaluating Mathematical Expression From A String And Inserting It On Stack"