How can I write the code such that
if(token is a operand)
do this
if(token is a operator)
do this
For the operands, the stoi()
function can be used but how can I handle the operators?
Also, How can I tokenize a string with no spaces?
A common way is to do a single-character lookahead --
There are different ways to tokenize operands and operators without relying specifically on white spaces. E.g., for operands you could keep reading characters as long as they are digit/hex or .
, with a small local state machine keep track of the progress (like whether you've seen a .
already).
For operators, if the total number of operators is not too large, you could simply read the required number of characters (assuming they are available from input stream) and do string compares.