Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have made a basic app that can derivate a mathematical expression. The current input method looks like this:
JavaScript
G = new Ln(new Add(new X(), new Add(new Constant(2), new Ln(new X()))));
Is there a way to use the string input method which is looks like this:
ln(x+(2+ln(x)))
How could I do that?

I know how basic tokenzier works, I have already made one (github). But I do not know how to convert a tokenzied array into
JavaScript
G = new Ln(new Add(new X(), new Add(new Constant(2), new Ln(new X()))));
format??

I am newbie in the fields of programming.

What I have tried:

You can find my question at stackoverflow: https://stackoverflow.com/q/73425663/19807534[^]
Posted
Updated 22-Aug-22 6:16am

1 solution

1) If I wanted to answer SO questions, I'd answer questions at SO, not CP. Posting so multiple groups do the same work for you is a good way to get peoples backs up - and annoyed people are less likely to help you than happy ones.

2) If you have written a tokenizer, then you've done the donkey work already: now convert the tokens into a stack-based (RPN) form and evaluate them:
ln(x+(2+ln(x)))
Becomes
x 2 x ln + + ln
Now unwind the stack and it's easy to evaluate. Converting infix to RPN (shunting-yard algorithm) | andreinc[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900