W
W
web_dev2020-02-17 16:46:57
Java
web_dev, 2020-02-17 16:46:57

Most rational way to define a negative number in a String?

Hello,
here for the sake of interest, for myself I am writing a calculator of a mathematical expression.
And here is an interesting point. Want to beautifully recognize this negative number or operand?
"Brute-force", of course, this can be solved, but I want something beautiful, but nothing smart comes to mind ...

Any ideas? )) Thanks.
For example from such expressions. Determine if this is a negative number, not an operand, and vice versa, is an operand, not a negative number.

"1-1"
"1 -1"
"1- 1"
"1 - 1"
"6 + -( -4)"
"6 + -(4)"


and so on. With brackets - this is not a problem, for this there is a special algorithm that stacks operands and numbers.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mayton2019, 2020-02-17
@mayton2019

There is a good saying about pudding.
In general, to calculate the value of a mathematical expression - it must be calculated. No brute force will most likely help. The expression may require function evaluations. And they must be carried out. How is another question, but it is clear that it is not in the lines.

X
xmoonlight, 2020-02-19
@xmoonlight

Converting from infix notation ( link )

For operations like -x, which are both binary and unary, a modification is needed: when such an operation is found, the system looks at the previous character and determines whether the minus is a binary operation or a unary function. Accordingly, the stack and the NPV need different symbols for the binary and unary minus.

PS: I reposted without shit and sarcasm. You can mark it as a solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question