0

So I’m trying to make an infix to reverse Polish notation program by pushing the expression into a stack. The program iterates the expression looking for an operator and once it finds it, it pops the 2 values and performs the calculations. However if I try to add multiple if statements a conversion error pops. The line "if express(i) <> "+" works for addition however if I were to extend it by adding multiple conditions " if express(i) <> "+" or "-" or "*"" then it says "Conversion from string "-" to type Boolean is not valid. Could anyone help me with this? Thanks.

Module Module1

Sub Main()
 Dim expres As String
 Console.WriteLine("Enter infix expression")
 expres = Console.ReadLine()
 Dim S As New Stack
 Dim current(1) As Integer
 Dim temp_val As Integer
 For i = 0 To expres.Length - 1
 If expres(i) <> "+" Then
 S.Push(expres(i))
 End If
 If expres(i) = "+" Then
 current(0) = S.Pop().ToString
 current(1) = S.Pop().ToString
 temp_val = current(0) + current(1)
 Console.WriteLine(temp_val)
 S.Push(temp_val)
 End If
 If expres(i) = "-" Then
 current(0) = S.Pop().ToString
 current(1) = S.Pop().ToString
 temp_val = current(0) - current(1)
 Console.WriteLine(temp_val)
 S.Push(temp_val)
 End If
 If expres(i) = "*" Then
 current(0) = S.Pop().ToString
 current(1) = S.Pop().ToString
 temp_val = current(0) * current(1)
 Console.WriteLine(temp_val)
 S.Push(temp_val)
 End If
 If expres(i) = "/" Then
 current(0) = S.Pop().ToString
 current(1) = S.Pop().ToString
 temp_val = current(0) / current(1)
 Console.WriteLine(temp_val)
 S.Push(temp_val)
 End If
 If expres(i) = "^" Then
 current(0) = S.Pop().ToString
 current(1) = S.Pop().ToString
 temp_val = current(0) ^ current(1)
 Console.WriteLine(temp_val)
 S.Push(temp_val)
 End If
 If expres(i) = "~" Then
 current(0) = S.Pop().ToString
 current(1) = S.Pop().ToString
 temp_val = current(0) + current(1)
 Console.WriteLine(temp_val)
 S.Push(temp_val)
 End If
 Next
 Console.ReadLine()
End Sub

End Module

asked Oct 17, 2019 at 8:35
2
  • can you explain the expected logic of if express(i) <> "+" or "-" or "*" more detailed? Commented Oct 17, 2019 at 8:51
  • Use a Select statement instead. Now it simply becomes the Case Else clause. Commented Oct 17, 2019 at 8:53

1 Answer 1

1

you should state the left expression each time you compare, something like

if express(i) <> "+" or express(i) <> "-" or express(i) <> "*"

answered Oct 17, 2019 at 8:44
2
  • @freefaller "curly brackets" usually are referring to braces. I see no braces. Are you referring to the smart quotes (as MS office calls them)? Commented Oct 18, 2019 at 7:35
  • @Mary, yes, typing too quickly and not noticing my mistake... you are entirely correct... I mean curly braces Commented Oct 18, 2019 at 7:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.