#Infix to prefix with sin, cos, log etc

27 messages · Page 1 of 1 (latest)

kind helm
#

I am supposed to make a program that takes in an infix turns it to prefix, and then solves it using stack. Suppose if the user enters the infix: (3+4)2/2+4/28 we would get the prefix: +/+3422/428 and the evaluated result: 23. My program works fine with normal operations like +, -, *, and / but it is also supposed to work with sin, cos and log. My stack is a char stack, and i dont understand how im supposed to incorporate the ability to use sin cos and log. Any tips?

  1. Infix to prefix conversion will have 2 Marks.
  2. Getting the result of basic infix equation like (3+46)2/12+4/28 will have 2 Marks.
  3. Getting the result of complex euqation like 3log(100)+sin^2(90)-cos(log(1000))+2-34+12 will have 2 Marks.
  4. Extra functionality of your program based on the usage of customer will have 1 Marks. This part include( The program did not accept ++, --, //,%%) and also the user should be able to use your program without any knowledge or programming!
violet narwhalBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

kind helm
#

The algorithm instructions we have gotten are:

#

Initially reverse the expression given for the infix.
One by one scan of characters.
If character is an operand, then copy it to the output of the prefix notation.
When the character is a parenthesis closing then push it to the stack.
If the character is an opening parenthesis, pop the elements in the stack till we find the closing parenthesis that corresponds.
If a character scanned is operator.
If the operator has greater or equal precedence than the top of the stack, push the operator to the stack.
If the operator has less precedence than the top of the stack, pop the operator and output it to the output of the prefix notation, then check the above condition with the new top of the stack again.
After scanning all the characters, reverse the notation output for the prefix.

pulsar temple
#

Tldr but functions can be handled similar to parenthesis with a unary operator in front

#

Looks like you have binary ops working so unary should come naturally

kind helm
#

How so?

#

are you meaning like turning sin into a character like s, and having that as an operator?

pulsar temple
#

Is your stack made of char? Then yeah in this case you have predefined number of functions that can just be represented with s c t l etc, then mapped back to sin cos etc when you print the postfix or evaluate the postfix

kind helm
#

Any tips on how i would map it back to sin cos etc?

#

and when would i turn the sin into s, cos into c ?

#

when i get the infix? or after ive turned it to prefix?

#

And how would that work with precedence? +, -, *, /, ^ operators all have different precedence

#

Sorry for blasting questions but ive been stuck on it for 3 days 😅

pulsar temple
#

oh wait this is prefix

#

god why lol

kind helm
#

yeee

#

thats the task we got

#

In the below section, there is explanation about how to convert infix to prefix expression. You need to use stack to solve this problem. Consider your program more comprehensive, and it should work for a complex Infix as well. Your program should calculate the final result of Infix and Prefix as well. Suppose if the user enter infix: (3+4)2/2+4/28 it should print the result as 23. Add more functionality to your program based on your understanding.

  1. Infix to prefix conversion will have 2 Marks.

  2. Getting the result of basic infix equation like (3+46)2/12+4/28 will have 2 Marks.

  3. Getting the result of complex euqation like 3log(100)+sin^2(90)-cos(log(1000))+2-34+12 will have 2 Marks.

  4. Extra functionality of your program based on the usage of customer will have 1 Marks. This part include( The program did not accept ++, --, //,%%) and also the user should be able to use your program without any knowledge or programming!

#

My idea was to turn sin into s, cos to c, and log to l

#

but i dont know what to do with the brackets and the number within, like sin(45)

#

how do i later make it so it calculates that

pulsar temple
#

probably just make your stack out of strings then

#

you end up with "sin" "45"

kind helm
#

The task say we have to use characters

#

Algorithm Instruction:

Initially reverse the expression given for the infix.
One by one scan of characters.
If character is an operand, then copy it to the output of the prefix notation.
When the character is a parenthesis closing then push it to the stack.
If the character is an opening parenthesis, pop the elements in the stack till we find the closing parenthesis that corresponds.
If a character scanned is operator.
If the operator has greater or equal precedence than the top of the stack, push the operator to the stack.
If the operator has less precedence than the top of the stack, pop the operator and output it to the output of the prefix notation, then check the above condition with the new top of the stack again.
After scanning all the characters, reverse the notation output for the prefix.

violet narwhalBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.