#Find the prefix

83 messages · Page 1 of 1 (latest)

plain hingeBOT
#

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 tips on how to ask a good question run !howto ask.

fluid salmon
#

@swift vessel it's not really helpful to do people's assignment for them

swift vessel
#

@fluid salmon It was very simple question that's why i choose to answer it

fluid salmon
#

not simple when you don't understand it

iron ore
#

it's also technically against the rules

#

#rules 5. We're happy to help you learn, however, we do not condone academic dishonesty. We will not take your tests for you or write programs for you. Likewise avoid giving full solutions to problems when helping other members.

swift vessel
#

Sorry i have deleted it

fluid salmon
#

@ocean sequoia what have you tried so far?

ocean sequoia
#

but it is kind of weird

#
    • 3 4 / - 5 2 + 6 1
#

this is wat i got

fluid salmon
#

I think that looks correct

ocean sequoia
#

actually it is wrong

fluid salmon
#

how so?

#

oh yeah the order of the precedence is wrong for * and /

swift vessel
#

So division operator should come first and then multiplication

ocean sequoia
#

and how can i reverse that to get prefix

swift vessel
#

@ocean sequoia We cannot obtain prefix by reversing postfix expression

#

Just think logically

ocean sequoia
#

i am writing this code

#

On internet, most people will do like reverse infix then postfix then reversed postfix to get prefix

swift vessel
#

@ocean sequoia Ohh you want to code it , i thought you are just converting it manually

#

Then you will first have to reverse the expression , convert it to postfix and then reverse it again

ocean sequoia
#

reverse

#

postfix

#

reverse

swift vessel
#

The problem is occurring because of associativity rules

#

When you reverse the expression at first stage, the order of operators with similar precedence also reverses

#

In your case '/' comes left side and '*' goes right side, but here * should evaluate first because in original expression * was left side

#

So, whenever you are converting reversed expression into postfix, you should not pop out operators with similar precedence

#

So, your postfix should be like this :
16+25-43+*/

#

@ocean sequoia You got my point?

ocean sequoia
#

yea i got it

swift vessel
#

great lmk if you have any other doubts with this

ocean sequoia
# swift vessel great lmk if you have any other doubts with this

So i got this problem
Infix : 59+ 107 * 83 7291 / ( 27 - ( 112 * 153 ) * ( 111 + 76 ) )
Reverse : ( ( ( 67 + 111 ) * ( 351 * 211 ) - 72 ) / 19 * 27 * 38 * 701 + 95 )
postfix: 67 111 + 351 211 * * 72 - 19 27 38 701 * * 95 + * /
Reverse postfix: / * + 59 * * 107 83 72 91 - 27 * * 112 153 + 111 76 (This is wat i got)
Prefix(Answer) : + 59 / * * * 107 83 72 91 - 27 * * 112 153 + 111 76

swift vessel
#

@ocean sequoia Can you clearly show me what is the infix expression? because i can't see multiplication operators in your infix expression, but i can see them in postfix expression

#

Ok i think you have wrote it ,but it is not visible to me

ocean sequoia
#

59+ 107 * 83 *72 *91 / ( 27 - ( 112 * 153 ) * ( 111 + 76 ) )

swift vessel
#

Okk it is visible now

ocean sequoia
#

sorry

#
#

here my code

#

u can take a look at it

swift vessel
#

@ocean sequoia Yes sure

ocean sequoia
#

btw it kind basic

#

cuz i just started learning

swift vessel
#

@ocean sequoia it's okay

#

@ocean sequoia Why are you reversing the numbers also

#

don't reverse the numbers , just reverse the expression

#

For example ,in reversed expression 76 should be 76 only

#

It will not be 67

ocean sequoia
#

but it is still fine isnt it ?

swift vessel
#

Yeah I guess so, because you are again reversing the expression

ocean sequoia
#

yea

swift vessel
#

Your postfix of reverse expression is wrong though

#

Wait let me go through the code first

#

Your post fix of reverse expression should be: 67 111 + 351 211 * * 72 - 19 27 38 701 * * */ 95 +

#

Because when you reach at "+" , you will have to pop out '*' and append to the output

#

Because * have greater precedence than +

ocean sequoia
swift vessel
#

let me check

#

@ocean sequoia Why are you checking if operator.top() is empty at line 75 and 116?

ocean sequoia
#

i dont see it

swift vessel
#

Ohh, i think some glitch, after copying it

swift vessel
#

@ocean sequoia It is very complex, but issue i can see is you don't have any loop inside the main loop, which means, whenever any lower precedence operator comes, it will not able to pop out all the operators with higher precedence from the stack

ocean sequoia
#

yea i also think like that

swift vessel
#

In this expression you have to pop out all * operator when + comes

#

You can try to make your code simpler, it will be easy to understand and debug for you also

#

My suggestion is, define one function which will deal with the precedence

ocean sequoia
#

oh i got it

#

thank you

swift vessel
#

for example: if( op=='^'){ return 3} if(op=='*){ return 2 } something like this

plain hingeBOT
#

@ocean sequoia Has your question been resolved? If so, run !solved :)

ocean sequoia
#

!solved

plain hingeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity