Go Down

Topic: Best way to parse input numbers (Read 82 times) previous topic - next topic


I am looking to implement a calculator, re-inventing the wheel I know but I have just bought a touchscreen for my Arduino Uno and I am using various scripts to see what I can do with it. I have my calculator layout and can touch the keys and print the keys pressed in a textbox, the next step would be to save the inputs and perform the calculation when the = key is pressed.

This is where my programming skills fall down. Should I store the inputs as a string and then remove the numbers before and between operators using regular expressions? Or should I save the numbers and operators as an array? I will have no way of knowing the size of the numbers untill the = key is pressed.

For instance, if the input is "236+29-2=" I would want to parse the number 236, the operator +, the number 29, the operator - and the number 2 and then perform the calculation. What is the best way to do this?


You could watch for operators in the input data from the keypad and when you see one convert the preceding characters to a number.

Alternatively create a big array to hold the input and save all the characters until you detect the = sign.

The examples in Serial Input Basics  - especially the 2nd example and the parse example may give you some ideas.

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up