Answer the question
In order to leave comments, you need to log in
Step by step calculator in C#?
How to implement incremental counting of values in a calculator, for example:
(2+2) / 2 + 2 * 2
4 / 2 + 2 * 2
2 + 2 * 2
2 + 4
6
So that each operation would be output to the console
There was a similar question, but the answer did not find it there.
Answer the question
In order to leave comments, you need to log in
One solution:
Turn the stream of characters into a stream of tokens (a token is NUMBER , OPERATION , OPEN_BRACKET, CLOSE_BRACKET).
Then, one token is put onto the stack.
After the next token, we check whether the top three tokens can be replaced by one according to the rules:
NUMBER OPERATION NUMBER -> NUMBER
OPEN_BRACKET NUMBER CLOSE_BRACKET -> NUMBER.
If possible, we will replace it.
If the replacement is carried out, we check according to the rules again.
When replacing, we display the contents of the stack and the remaining tokens.
We execute until the tokens run out, and one token of the NUMBER type remains on the stack.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question