I am writing a program that takes a string of a mathematical expression, converts it to postfix notation. Which I have done, I am now trying to figure out how to evaluate this expression. I have left it in the data type of a queue, so my idea is to try and find the index of the first operator, then find the two numbers the come before it, send those to a new function which will evaluate them based on which operator is found (so one function for adding, one for subtracting ... etc). Im having trouble figuring out how to grab the index though.
Im trying to use the Queue method of indexOf and then passing it a regex for those operators. using \\W
y is a Queue. i've never used this type of character code before.
var z = y.indexOf("[\\W]")
i would like it to return the index of the first operator, in the case i currently have it is a "+"
currently that doesent find anything. i've also tried dropping those brackets an example Queue is
Queue(-1, 2, 3, *, +, 10, +)
Queue(1, 2, +)
which does mean i need a way to differ if its just a - or if its tied to the number. These are all Strings inside of the Queue
You have to
y.indexWhere(_.matches("\\W"))
Related
I'm currently working on a project that calculates polynomial expressions (We got this for a task at University). I'm storing a polynomial in a linked list and I've overloaded operators +,*,-,' for polynomials. The most difficult task that I've got is to parse an expression like this e.g. : (3x-(-x-1)^2)(x^3-2x^2+1)''where '' is second derivative. I've been googling a lot and couldn't find antyhing that could parse and make an postfix expression out of this when you have a variable x and numbers, is there a way to modify Shunting-Yard algorithm so that it could work on polynomials?
My node stores coefficient, power, pointer to the previous and next node and my Polynomial class supports every operation thats stated above. The only problem is how can I get a postfix expression out of a string that I put above? Btw. only '(' & ')' parentheses are supported, that makes it a bit easier.
For the example (3x-(-x-1)^2)(x^3-2x^2+1)'' I need to store the result of it in a polynomial that I made.
I can provide more if asked,
Thanks in advance!
I have an expression like below;
abs(sum(Max(abs(ListInput)),Min(V1,V2)))
I want to take out operators which take single argument which is a symbol for a list of arguments , stored somewhere else.
For example in above case;
Max(abs(ListInput))
is what I need.
I am able to do it iteratively by tokenizing and converting the expression in to reverse polish notation.
But things break when operator is ternary.
IF(10 > Max(Abs(ListInput)),5,MAX(ListInput)) // (IF<EXPRESSION>,TRUEVALUE,FALSEVALUE) ; True value and Valse value can also be an expression.
I am trying to figure out to a recursive way to do this , but unable to think through all conditions.
I want to take out operators which take single argument which is a
symbol for a list of arguments … regex
/(abs|sum|max|min)\([^(),]*(\([^(),]*\)[^(),]*)*\)/gi
Online regex tester
If I have a function
let rec function n =
if n<0 then []
else n-2 # function n-2 ;;
I get an error saying that the expression function n-2 is a list of int but it is expecting an int.
How do I concatenate the values to return all the n-2 values above zero as a list?
I cannot use the List module to fold.
Thanks
Your title asks how to concatenate lists, but your question seems rather different.
To concatenate lists, you can use the # operator. In many cases, code that depends on this operator is slower than it needs to be (something to keep in mind for later :-).
Here are some things I see wrong with the code you give:
a. You can't name a function function, because function is a keyword in OCaml.
b. If you use the # operator, you should have lists on both sides of it. As near as I can see, the thing on the left in your code is not a list.
c. Function calls have higher precedence than infix operators. So myfun n - 2 is parsed as (myfun n) - 2. You probably want something closer to myfun (n - 2).
Even with these changes, your code seems to generate a list of integers that are 2 apart, which isn't what you say you want. However, I can't understand what the function is actually supposed to return.
It seems like you are not concatenating lists, but concatenating ints instead. This is done by the :: operator. So your code would look like:
else (n-2)::(fun (n-2))
Although I could see this function possibly not producing the desired output if you put in negative numbers. For example if you pass through n = 1, n-2 will evaluate to -1 which is less than zero.
Is there any function in C++ (included in some library or header file), that would count an expression from string?
Let's say we have a string, which equals 2 + 3 * 8 - 5 (but it's taken from user's keyboard, so we don't know what expression exactly it will be while writing the code), and we want this function co count it, but of course in the correct order (1. power / root 2. times / divide 3. increase / decrease).
I've tried to take all the numbers to an array of ints and operators to an array of chars (okay, actually vectors, because I don't know how many numbers and operators it's going to contain), but I'm not sure what to do next.
Note, that I'm asking if there's any function already written for that, if not I'm going to just try it again.
By count, I'm taking that to mean "evaluate the expression".
You'll have to use a parser generator like boost::spirit to do this properly. If you try hand-writing this I guarantee pain and misery for all involved.
Try looking on here for calculator apps, there are several:
http://boost-spirit.com/repository/applications/show_contents.php
There are also some simple calculator-style grammars in the boost::spirit examples.
Quite surprisingly, others before me have not redirected you to this particular algorithm which is easy to implement and converts your string into this special thing called Reverse Polish Notation (RPN). Computing expressions in RPN is easy, the hard part is implementing Shunting Yard but it is something done many times before and you may find many tutorials on the subject.
Quick overview of the algorithms:
PRN - RPN is a way to write expressions that eliminates the need for parentheses, thus it allows easier computation. Practically speaking, to compute one such expression you walk the string from left to right while keeping a stack of operands. Whenever you meet an operand, you push it onto the stack. Whenever you meet an operation token, you calculate its result on the last 2 operands (if the operation is binary ofcourse, on the last only if it is unary) and push it onto the stack. Rinse and repeat until the end of string.
Shunting Yard really is much harder to simply overview and if I do try to accomplish this task, this answer will end up looking much like the wikipedia article I linked above so I'll save that trouble to both of us.
Tl;DR; Go read the links in the first sentence.
You will have to do this yourself as there is no standard library that handles infix equation resolution
If you do, please include the code for what you are trying and we can help you
So I'm processing math formulas from strings using the shunting-yard algorithm. I pass every string through a function which processes specific strings into values, but occasionally I check against strings that are just strings that should just get passed by - read skip the shunting-yard pass. Should I just use a regular expression to test for all of the symbols and numbers? Or is there a simpler way I might test for this? I guess the inverse would be checking if there are still any letters left in the string?
While I'm sure there is a better answer, I've decided to use the following until something better comes up, I'm working in AS3:
if ( !String( value ).match( /[a-zA-Z]/ ) && !String( value ).match( /^(-?\d+)$/ ) && String( value ).match( /[()\\*+-]/ ) )
{
value = MathParser.parse( value );
}
The first expression verifies that there are no characters - my shunting yard implementation doesn't process special characters or operations, it just does basic math, so if there are any characters remaining, something is wrong.
The second verifies that the string contains mathmatical symbols. There's no point in attempting the operation if there's no math to process. I assume this is unnecessary if I wanted to handle errors inside the shunting-yard processes.
And finally I verify that the value isn't just a standalone number of positive or negative value.
Comments for improvement appreciated.