Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses. Obtain the postfix expression of the infix expression Step 1.Reverse the postfix expression to get the prefix expression

NotificationsYou must be signed in to change notification settings

SAZZAD-AMT/Infix-to-Prefix-Convertion-by-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation


Postfix

Step 1: Add '')" to the end of the infix expressionStep 2: Push(o nto the stackStep 3: Repeat until each character in the infix notation is scannedIF a(is encountered, push it on the stackIF an operand (whetheradigit oracharacter) is encountered, add it postfix expression.IF a ")" is encountered, thena. Repeatedly pop from stack and add it to the postfix expression until a "(" is encountered.b. Discard the "(".That is, remove the(from stack and do not add it to the postfix expressionIF an operator O is encountered, thena. Repeatedly pop from stack and add each operator ( popped from the stack) to the postfix expression which has the same precedence orahigher precedence than Ob. Push the operator to the stack[END OF IF]Step 4: Repeatedly pop from the stack and add it to the postfix expression until the stack is emptyStep 5: EXIT

Prefix

Step 1: Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses.Step 2: Obtain the postfix expression of the infix expression Step 1.Step 3: Reverse the postfix expression to get the prefix expression

CODE

def isOperator(c):    return (not (c >= 'a' and c <= 'z') and not(c >= '0' and c <= '9') and not(c >= 'A' and c <= 'Z')) def getPriority(C):    if (C == '-' or C == '+'):        return 1    elif (C == '*' or C == '/'):        return 2    elif (C == '^'):        return 3    return 0 def infixToPrefix(infix):    operators = []    operands = []     for i in range(len(infix)):                if (infix[i] == '(' ):            operators.append(infix[i])         elif (infix[i] == ')'):            while (len(operators)!=0 and (operators[-1] != '(' )):                op1 = operands[-1]                operands.pop()                op2 = operands[-1]                operands.pop()                op = operators[-1]                operators.pop()                tmp = op + op2 + op1                operands.append(tmp)            operators.pop()        elif (not isOperator(infix[i])):            operands.append(infix[i] + "")         else:            while (len(operators)!=0 and getPriority(infix[i]) <= getPriority(operators[-1])):                op1 = operands[-1]                operands.pop()                 op2 = operands[-1]                operands.pop()                 op = operators[-1]                operators.pop()                 tmp = op + op2 + op1                operands.append(tmp)            operators.append(infix[i])     while (len(operators)!=0):        op1 = operands[-1]        operands.pop()         op2 = operands[-1]        operands.pop()         op = operators[-1]        operators.pop()         tmp = op + op2 + op1        operands.append(tmp)    return operands[-1]while(1):    s = input("Infix Expression : ")    print("Prefix Expression : ", infixToPrefix(s))    Step 1: Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses.Step 2: Obtain the postfix expression of the infix expression Step 1.Step 3: Reverse the postfix expression to get the prefix expression

About

Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses. Obtain the postfix expression of the infix expression Step 1.Reverse the postfix expression to get the prefix expression

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp