초등학교 때 배우는 두 정수의 곱셈표를 만들어 주는 C# 소스이다.

' --------------------------------------------
'  Filename: MakeMultTableVB.bas
'
'     Print a multiplication table.
'
'     Compile: vbc MakeMultTableVB.bas
'     Execute: MakeMultTableVB 230 5100
'
'  Date: 2009/03/07
'  Author: pkim (AT) scripts.pe.kr
' --------------------------------------------

Imports System

Namespace MyTestApplication1

    Class MakeMultTableApp

        Shared Sub PrintUsing()
            Console.WriteLine("Using: MakeMultTableApp [number1] [number2]")
            Console.WriteLine("Print a multiplication table for the given two integers.")
        End Sub

        Shared Sub PrintMultTable(x As long, y As long)
            DIM nx As long = x
            DIM ny As long = y
            If nx < 0 Then nx = - nx
            If ny < 0 Then ny = - ny
            DIM ntail1 As int32 = 0
            DIM ntail2 As int32 = 0
            While nx Mod 10 = 0
                nx = Convert.ToInt64(Math.Floor(nx / 10))
                ntail1 = ntail1 + 1
            End While
            While ny MOD 10 = 0
                ny = Convert.ToInt64(Math.Floor(ny / 10))
                ntail2 = ntail2 + 1
            End While
            Dim z As long = nx * ny
            Dim strZ As String = "" & z
            Dim strX As String = "" & nx
            Dim strY As String = "" & ny
            Dim n As Integer = Len(strY)
            Dim zeros As String  = "0000000000000000000000000000000000000000"
            Dim whites As String = "                                        "
            Dim bars As String   = "----------------------------------------"
            Dim line1 As String, line2 As String, line3 As String, line4 As String
            Dim loffset As String = "       "
            line4 = loffset & strZ
            line1 = loffset & Mid(whites, 1, Len(strZ) - Len(strX)) & strX
            line2 = "   x ) " &  Mid(whites, 1, Len(strZ) - Len(strY)) & strY
            line3 = "     --" &  Mid(bars, 1, Len(strZ))
            Console.WriteLine(line1 & Mid(zeros, 1, ntail1))
            Console.WriteLine(line2 & Mid(zeros, 1, ntail2))
            Console.WriteLine(line3)
            If Len(strY) > 1 Then
                Dim y1 As Integer
                Dim strT As String
                For i As Integer = 0 To Len(strY) - 1
                    y1 = Convert.ToInt32(Mid(strY, Len(strY) - i, 1))
                    If y1 <> 0 Then
                        strT = "" & (nx * y1)
                        Console.WriteLine(loffset & Mid(whites, 1, Len(strZ) - Len(strT) - i) & strT)
                    End If
                Next
                Console.WriteLine(line3)
            End If
            Console.WriteLine(line4 & Mid(zeros, 1, ntail1) & Mid(zeros, 1, ntail2))
        End Sub

        ' Java 언어의 main 메소드에 해당하는 Visual Basic의 Main
        Shared Sub Main(ByVal args() As String)
            DIM x As long, y As long
            If args.Length >= 2 Then
                x = Convert.ToInt64(args(0))
                y = Convert.ToInt64(args(1))
                Console.WriteLine("")
                PrintMultTable(x, y)
            Else
                PrintUsing()
            End If
        End Sub
    End Class
End Namespace




컴파일> vbc MakeMultTableVB.bas
실행> MakeMultTableVB 230 5100
결과>

          230
   x )   5100
     ------
         23
       115
     ------
       1173000

 

크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by Scripter
,