|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
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 |
カテゴリー