カテゴリー
Visual Basic

逆ポーランド記法の計算、Stack使用しました。

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

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

inserted by FC2 system