Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I made a program to calculate an INFIX expression input by the user. How do I do

ID: 3815354 • Letter: I

Question

I made a program to calculate an INFIX expression input by the user. How do I do the same thing for POSTFIX?

This is my code to calculate an INFIX:

.data
stack: .space 160
infix: .space 41
prompt1: .asciiz " Please enter an expression to calculate or 'E' to end: "

.text
start:
li $v0, 4
la $a0, prompt1
syscall

li $v0, 8           #Get user input
la $a0, infix           #store string in infix
li $a1, 41
syscall
la $s0, infix           #pointer to input
la $s1, stack           #pointer to stack

lb $t0, ($s0)           #$t0 contains the value at the pointer
beq $t0,'E', end       #exit if E is entered

move_to_stack:
lb $t0, ($s0)

L2:
li $t2, 0            #sum in this register
li $t3, 0           #counter in this register
addi $s0, $s0, 1       #point to next byte in string
beq $t0, ' ', print      
beq $t0, 0x00, print
beq $t0, ')', pop
sw $t0, ($s1)           #convert byte into word and store in stack

addi $s1, $s1, 4        #point to next word in stack
j move_to_stack

pop:
subi $s1, $s1, 4       #pop last item in stack
lw $t1, ($s1)           #store in $t1
beq $t1, $s4, jump
beq $t1, '(', calculate       #pop untill '(' is encountered
beq $t1, '+', move_op
beq $t1, '-', move_op
beq $t1, '*', move_op
beq $t1, '/', move_op

jump:
#blt $t1, 48, move_op       #to check if the value entered is a number or not, and if it is not then its an operation, go to move_op
jal convert           #convert ascii number to decimal
add $t2, $t2, $t1       #add to sum the multi digit numbers
addi $t3, $t3, 1       #incriment the number of digits of the number
j pop               #pop next element

move_op:           #to store the operation in a register
move $s3, $t1           #move operation to $s3
move $s2, $t2           #move the sum of the digits of the number y into $s2, so x is in $t2
move $t2, $zero
move $t3, $zero
j pop               #continue to pop the next number

calculate:
la $s5, stack
beq $s3, '+', addition       #x+y
beq $s3, '-', subtraction   #x-y
beq $s3, '*', multiplication   #x*y
beq $s3, '/' division       #x/y
j start

addition:
add $s4, $t2, $s2
beq $s1, $s5, skip
addi $s4, $s4, 48

skip:  
sw $s4, ($s1)           #store $s4 into stack $s1
addi $s1, $s1, 4       #point to next element in stack
j move_to_stack  

subtraction:
sub $s4, $t2, $s2       #x-y and store in s4
beq $s1, $s5, skip1
addi $s4, $s4, 48

skip1:
sw $s4, ($s1)           #store $s4 into stack $s1
addi $s1, $s1, 4       #point to next element in stack
j move_to_stack          

multiplication:
mult $t2, $s2           #x*y
mflo $s4           #store result in s4
beq $s1, $s5, skip2
addi $s4, $s4, 48
skip2:
sw $s4, ($s1)           #store $s4 into stack $s1
addi $s1, $s1, 4       #point to next element in stack
j move_to_stack          

division:          
div $t2, $s2           #x/y
mflo $s4           #store answer in s4
beq $s1, $s5, skip3
addi $s4, $s4, 48

skip3:
sw $s4, ($s1)           #store $s4 into stack $s1
addi $s1, $s1, 4       #point to next element in stack
j move_to_stack          

print:
lw $a0, stack
li $v0, 1
syscall
j start

end:
li $v0, 10
syscall

convert:
li $t4, 0           #second counter
subi $t1, $t1, 48       #convert ascii number to decimal

L1:
beq $t4, $t3, finish       #number of times to multiple by power of 10
addi $t4, $t4, 1       #increment counter to loop $t3 times
mul $t1, $t1, 10       #multiple by 10
mflo $t1

j L1

finish:
jr $ra               #number is converted

How do I edit this code to find the POSTFIX? Thanks

Explanation / Answer

from pythonds.basic.stack import Stack

def infixToPostfix(infixexpr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()

for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)

while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)

print(infixToPostfix("A * B + C * D"))
print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )"))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote