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
1 Answer 1
you should state the left expression each time you compare, something like
if express(i) <> "+" or express(i) <> "-" or express(i) <> "*"
-
@freefaller "curly brackets" usually are referring to braces. I see no braces. Are you referring to the smart quotes (as MS office calls them)?Mary– Mary2019年10月18日 07:35:20 +00:00Commented Oct 18, 2019 at 7:35
-
@Mary, yes, typing too quickly and not noticing my mistake... you are entirely correct... I mean curly bracesfreefaller– freefaller2019年10月18日 07:44:02 +00:00Commented Oct 18, 2019 at 7:44
if express(i) <> "+" or "-" or "*"
more detailed?