Does this test case break the shunting yard algorithm? "1*2-3/4+5*6-7*8+9/10" - rpn

I have tried numerous times to solve the following using the shunting yard algorithm: 1*2-3/4+5*6-7*8+9/10
Infix Notation:
"1*2-3/4+5*6-7*8+9/10"
Postfix Notation:
[1,2,*,3,4,/,5,6,*,7,8,*,9,10,/,+,-,+,-]
The correct answer is -24.
Every time I solve the postfix notation version of this I get 28.
I have yet to find a working RPN calculator online ... So I have turned to stack overflow.

I am working on my own Shunting Yard algorithm, and so this might not be 100% correct, but I what Im getting is [ 1, 2, *, 3, 4, /, -, 5, 6, *, +, 7, 8, *, -, 9, 10, /, +] The Wikipedia for Shunting Yard Algorithm gives the pseudo code for this parsing, and I dont think I could explain it better. As some alredy said, you might want to look into how precedence and associativity affect parsing.

Related

Shunting-Yard on polynomial

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!

Regex for finding the index of Mathematical Operators

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"))

Calculator with bracket in C++ [duplicate]

This question already has answers here:
Building a math expression evaluator
(2 answers)
Closed 7 years ago.
I'm implementing a calculator in C ++ that respects the priorities of parentheses. I am trying to use std :: string :: find_last_of to find the last occurrence of ( and std :: string :: find to find the first occurrence of ). Once that is done, I reckon the content of the extracted substring and continuing with the rest of the string.
For example:
1 + 1 * (9 * (11-1 + 2))
I find the last occurrence of (, the first of ) and calculating 11-1 + 2. And so it continues.
Is this the right approach to this problem or is there something better?
Thank you.
You can use Reversed Polish Notation:
You will need to convert the expression into reversed polish notation and then implement a stack based algorithm to pop and push to the final answer.
Have a look at the Shunting- Yard algorithm here: To convert the expression into RPN.
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Also Have a look at
Writing a simple equation parser
For help to implement the stack, have a look here:
C++ Calculator using Stacks and Queues
One of the standard approaches to this problem consists of 2 steps:
1) Convert your expression to Reverse Polish notation: https://en.wikipedia.org/wiki/Reverse_Polish_notation using Shunting-yard algorithm https://en.wikipedia.org/wiki/Shunting-yard_algorithm
2) Evaluate converted expression using stack.
The another way to do this:
1) Easy step: Write Backus–Naur Form of your expressions https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form
And after you have 2 options:
2a) Build Finite-State Machine: https://en.wikipedia.org/wiki/Finite-state_machine (exists a lot of tools to do this: ANTLR, ...).
2b) Recursive descent method: https://en.wikipedia.org/wiki/Recursive_descent_parser

Function to count an expression

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

C++ program for conversion of infix expression from user through command line to postfix using stack

Can some help me in the following
Write a c++ program that takes an infix expression from user through command line and convert it into a postfix expression. Stack data structure should use for the implementation of this task.
Regards,
Abdul Nasir
Shunting Yard Algorithm
That's all the help you're getting. If you have a particular problem then state it -- we aren't going to do your homework for you.