Imports System
Module Program
Class elem
Public ope As String
Public lev As Integer
End Class
Sub Main(args As String())
'Dim inpstr As String = "6.1 + 5.2 * 4.3 - 3.4 / 2.5 * 1.6 " '答え 6.1 5.2 4.3 * + 3.4 2.5 / 1.6 * -
'Dim inpstr As String = "( a + b ) * c " '答え a b + c *
'Dim inpstr As String = "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 " '答え 3 4 2 * 1 5 - 2 3 ^ ^ / + =>X
'Dim inpstr As String = "3 + 4 * 2 / ( 1 - 5 ) ^ ( 2 ^ 3 ) " '答え 3 4 2 * 1 5 - 2 3 ^ ^ / +
'Dim inpstr As String = "3 + 4 * 2 / ( 4 - 2 ) " '答え 3 4 2 * 4 2 - / +
'Dim inpstr As String = "( a + b ) ^ 2 " '答え a b + 2 ^
'Dim inpstr As String = "a + b ^ 2 " '答え a b 2 ^ +
'Dim inpstr As String = "a ^ b ^ c " '答え a b c ^ ^ => X
'Dim inpstr As String = "a ^ ( b ^ c ) " '答え a b c ^ ^
Dim inpstr As String = "a / b / c " '答え a b / c /
Dim rpnstr As String = ""
Dim opeStack As New Stack
Dim aelem As New elem
While True
If inpstr.Length = 0 Then
GoTo owari
End If
Dim fSpacePosition As Integer = inpstr.IndexOf(" ")
Dim fstr As String = inpstr.Substring(0, fSpacePosition)
Dim nstr As String = Strings.Right(inpstr, inpstr.Length - fSpacePosition - 1)
If Not (fstr = "+" Or fstr = "-" Or fstr = "*" Or fstr = "/" Or
fstr = "^" Or fstr = "(" Or fstr = ")" Or fstr = "^") Then
'演算子以外の数値は出力する
rpnstr += fstr + " "
Else
If opeStack.Count >= 1 Then
Dim wope As elem = opeStack.Peek
Dim currentlev As Integer
Select Case fstr
Case "^" : currentlev = 3
Case "*" : currentlev = 2
Case "/" : currentlev = 2
Case "+" : currentlev = 1
Case "-" : currentlev = 1
Case ")" : DoPopUntilLeftPara(rpnstr, opeStack)
GoTo nextread
Case "(" : currentlev = 4
Case Else : currentlev = 0
End Select
Do While wope.lev >= currentlev And opeStack.Count >= 1
If wope.ope = "(" Then GoTo pass
rpnstr += wope.ope + " "
opeStack.Pop()
If opeStack.Count > 0 Then
wope = opeStack.Peek
End If
Loop
pass:
Dim belem As New elem
belem.ope = fstr
belem.lev = currentlev
opeStack.Push(belem)
Else
'最初のスタックへの積上げ
Select Case fstr
Case "^" : aelem.lev = 3
Case "*" : aelem.lev = 2
Case "/" : aelem.lev = 2
Case "+" : aelem.lev = 1
Case "-" : aelem.lev = 1
Case Else : aelem.lev = 0
End Select
aelem.ope = fstr
opeStack.Push(aelem)
End If
End If
nextread:
inpstr = nstr
End While
owari:
Dim welem As elem
Do While opeStack.Count > 0
welem = opeStack.Pop
rpnstr += welem.ope + " "
Loop
Console.WriteLine(rpnstr)
Console.WriteLine("Hello World!")
End Sub
Public Function DoPopUntilLeftPara(ByRef rpnstr As String, ByRef opeStack As Stack) As String
Dim w As elem
While True
w = opeStack.Pop
If w.ope = "(" Then
Return rpnstr
Else
rpnstr += w.ope + " "
End If
End While
Return rpnstr
End Function
End Module
月: 2022年4月
Imports System
Module Program
Class elem
Public Property Ope As String
Dim a As String = _Ope
Public Property Lve As Integer
Dim b As Integer = _Lve
End Class
Sub Main(args As String())
Dim a As New elem
a.Ope = "100"
a.Lve = 0
Dim b As New elem With {.Ope = "+", .Lve = 1}
Dim c As New elem With {.Ope = "*", .Lve = 2}
Dim elemstack As New Stack
Dim w As elem
elemstack.Push(a)
elemstack.Push(b)
elemstack.Push(c)
Do While elemstack.Count > 0
w = elemstack.Pop
Console.Write(w.Ope + ", ")
Console.WriteLine(w.Lve)
Loop
Console.WriteLine("Hello World!")
End Sub
End Module
Imports System
Module Program
Class elem
Private _ope As String
Private _lve As Integer
Public Property Ope() As String
Get
Return _ope
End Get
Set(value As String)
_ope = value
End Set
End Property
Public Property Lve() As Integer
Get
Return _lve
End Get
Set(value As Integer)
_lve = value
End Set
End Property
End Class
Sub Main(args As String())
Dim a As New elem
a.Ope = "100"
a.Lve = 0
Dim b As New elem With {.Ope = "+", .Lve = 1}
Dim c As New elem With {.Ope = "*", .Lve = 2}
Dim elemstack As New Stack
Dim w As elem
elemstack.Push(a)
elemstack.Push(b)
elemstack.Push(c)
Do While elemstack.Count > 0
w = elemstack.Pop
Console.Write(w.Ope + ", ")
Console.WriteLine(w.Lve)
Loop
Console.WriteLine("Hello World!")
End Sub
End Module
Imports System
Module Program
Class elem
Public ope As String
Public lev As Integer
End Class
Sub Main(args As String())
Dim inpstr As String = "6.1 + 5.2 * 4.3 - 3.4 / 2.5 * 1.6 "
Dim rpnstr As String = ""
Dim opeStack As New Stack
Dim aelem As New elem
While True
If inpstr.Length = 0 Then
GoTo owari
End If
Dim fSpacePosition As Integer = inpstr.IndexOf(" ")
Dim fstr As String = inpstr.Substring(0, fSpacePosition)
Dim nstr As String = Strings.Right(inpstr, inpstr.Length - fSpacePosition - 1)
If Not (fstr = "+" Or fstr = "-" Or fstr = "*" Or fstr = "/") Then
'演算子以外の数値は出力する
rpnstr += fstr + " "
Else
If opeStack.Count >= 1 Then
Dim wope As elem = opeStack.Peek
Dim currentlev As Integer
Select Case fstr
Case "*" : currentlev = 2
Case "/" : currentlev = 2
Case "+" : currentlev = 1
Case "-" : currentlev = 1
Case Else : currentlev = 0
End Select
Do While wope.lev >= currentlev And opeStack.Count >= 1
rpnstr += wope.ope + " "
opeStack.Pop()
If opeStack.Count > 0 Then
wope = opeStack.Peek
End If
Loop
Dim belem As New elem
belem.ope = fstr
belem.lev = currentlev
opeStack.Push(belem)
Else
'最初のスタックへの積上げ
If fstr = "+" Or fstr = "-" Then
aelem.lev = 1
Else
aelem.lev = 2
End If
aelem.ope = fstr
opeStack.Push(aelem)
End If
End If
inpstr = nstr
End While
owari:
Dim welem As elem
Do While opeStack.Count > 0
welem = opeStack.Pop
rpnstr += welem.ope + " "
Loop
Console.WriteLine(rpnstr)
Console.WriteLine("Hello World!")
End Sub
End Module
Imports System
Module Program
Class elem
Public ope As String
Public lev As Integer
End Class
Sub Main(args As String())
Dim a As New elem With {.ope = "100", .lev = 0}
Dim b As New elem With {.ope = "+", .lev = 1}
Dim elemstack As New Stack
Dim w As elem
elemstack.Push(a)
elemstack.Push(b)
Do While elemstack.Count > 0
w = elemstack.Pop
Console.Write(w.ope + ", ")
Console.WriteLine(w.lev)
Loop
Console.WriteLine("Hello World!")
End Sub
End Module
Imports System
Module Program
Sub Main(args As String())
Dim rpnstr As String = "6.1 5.2 4.3 * + 3.4 2.5 / 1.6 * - "
Dim opeStack As New Stack
While True
If rpnstr.Length = 0 Then
GoTo owari
End If
Dim fSpacePosition As Integer = rpnstr.IndexOf(" ")
Dim fstr As String = rpnstr.Substring(0, fSpacePosition)
Dim nstr As String = Strings.Right(rpnstr, rpnstr.Length - fSpacePosition - 1)
If fstr = "+" Or fstr = "-" Or fstr = "*" Or fstr = "/" Then
Dim y As Double = CDbl(opeStack.Pop)
Dim x As Double = CDbl(opeStack.Pop)
Select Case fstr
Case "+" : opeStack.Push(CStr(x + y))
Case "-" : opeStack.Push(CStr(x - y))
Case "*" : opeStack.Push(CStr(x * y))
Case "/" : opeStack.Push(CStr(x / y))
End Select
Else
opeStack.Push(fstr)
End If
rpnstr = nstr
End While
owari:
Console.WriteLine(opeStack.Pop)
Console.WriteLine("Hello World!")
End Sub
End Module
カテゴリー
逆ポーランド記法の計算式
Imports System
Module Program
Sub Main(args As String())
Dim rpnstr As String
Dim rpnary() As String
Dim ans As Double
rpnstr = "6.1 5.2 4.3 * + 3.4 2.5 / 1.6 * - "
While True
rpnstr = RTrim(rpnstr)
rpnary = rpnstr.Split(" ")
If rpnary.Length = 1 Then
ans = rpnary(0)
GoTo owari
End If
Dim i As Integer = 0
While True
If rpnary(i) = "+" Or rpnary(i) = "-" Or rpnary(i) = "*" Or rpnary(i) = "/" Then
GoTo mikke
End If
i += 1
End While
mikke:
Dim a As Double
Dim b As Double
a = CDbl(rpnary(i - 2))
b = CDbl(rpnary(i - 1))
If rpnary(i) = "+" Then
rpnary(i - 2) = a + b
ElseIf rpnary(i) = "-" Then
rpnary(i - 2) = a - b
ElseIf rpnary(i) = "*" Then
rpnary(i - 2) = a * b
Else
rpnary(i - 2) = a / b
End If
rpnary(i - 1) = ""
rpnary(i) = ""
rpnstr = ""
Dim j As Integer = 0
For j = 0 To i - 2 - 1
rpnstr += rpnary(j) + " "
Next
rpnstr += CStr(rpnary(i - 2)) + rpnary(i - 1) + rpnary(i) + " "
For j = i + 1 To rpnary.Length - 1
rpnstr += rpnary(j) + " "
Next
End While
owari:
Console.WriteLine(ans)
Console.WriteLine("Hello World!")
End Sub