-1

I have to make a scientific calculator. I just need to know how to take input, process it, then display it. One obvious way is using eval but if I use eval, I have to explain exactly what is the source-code behind eval doing. Can someone please tell me where to find the exact working of eval? Or suggest any algorithm for taking inputs.

The following inputs are possible: 5+6/8xsin(0.5)

or it algorithm for making it work like a simple calculator does, one input is displayed at a time and one function works at a time.

  • 7
    I would think abusing `eval` for this would entirely defeat the purpose of the assignment. And it likely just taps into the python parser. I can't see describing that being an easy task. You can use the Shunting Yard algorithm to help parse infix equations. It's not that difficult to implement. – Carcigenicate Feb 17 '18 at 16:53
  • Imagine someone giving something like `"x" * 10**100` as input... And that is by far not the worst that can happen. – mata Feb 17 '18 at 17:03
  • you can see here also https://stackoverflow.com/a/9383764/7352806 – Narendra Feb 17 '18 at 17:07
  • @Carcigenicate I checked out Shunting Yard Algorithm and its implementation in python, it uses a lot features of language that have not been taught. Can't use those. – Khadija Shujaat Feb 17 '18 at 17:20
  • 1
    `eval` runs the entire Python parser -- a *much more complex* parser than the simplest one that would run `5+6/8xsin(0.5)` correctly. That's not amenable to a brief and simple explanation, much less an explanation that can be given and understood by someone who hasn't been taught enough to use the conventional algorithm for the purpose at hand. – Charles Duffy Feb 17 '18 at 18:03
  • 1
    @KhadijaShujaat I have to say, if you've been tasked with creating an expression parser, and they haven't even taught you how to use a stack, your teacher has failed you. Expression parsing is not an easy thing by any means, and the Shunting Yard algorithm is really the easiest first step that I know of. It will give you a prefix notation expression, which is really easy to parse. – Carcigenicate Feb 17 '18 at 19:24
  • 2
    Unless the point of the assignment was to just abuse `eval`, in which case the "explain how eval works" part is stupid, since the only reason you'd abuse eval is if your didn't know how to create a parser yourself. – Carcigenicate Feb 17 '18 at 19:31

1 Answers1

0

All I had to evalulate was a mathematical expression, which could be done by this simple parsing algorithm too. https://news.ycombinator.com/item?id=284842