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
you should state the left expression each time you compare, something like
if express(i) <> "+" or express(i) <> "-" or express(i) <> "*"
Try this one
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 tempVal As Integer
For i = 0 To expres.Length - 1
If Array.IndexOf({"+", "-", "*", "/", "^", "~"}, expres(i)) = -1 Then
s.Push(expres(i))
End If
current(0) = s.Pop().ToString
current(1) = s.Pop().ToString
Select Case expres(i)
Case "+"
tempVal = current(0) + current(1)
Case "-"
tempVal = current(0) - current(1)
Case "*"
tempVal = current(0) * current(1)
Case "/"
tempVal = current(0) / current(1)
Case "^"
tempVal = current(0) ^ current(1)
Case "~"
tempVal = current(0) + current(1)
End Select
Console.WriteLine(tempVal)
s.Push(tempVal)
Next
Console.ReadLine()
End Sub
End Module
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.