다음은 초등학교에서 배우는 나눗셈 계산표를 만들어주는 Visual Basic 소스 코드이다.
나눗셈 계산표를 완성하고 나서 약수, 배수 관계를 알려준다.
C# 코드로 작성된 BigInteger.cs 와 함께 컴파일하여 실행시키면 된다.
'
' Purpose: Make a division table in a handy written form.
'
' Compile: csc /target:module BigInteger.cs
' vbc MakeDivisionTableApp.bas /addmodule:BigInteger.netmodule
' Execute: MakeDivisionTableApp 12345 32
' MakeDivisionTableApp 500210 61
'
' Date: 2009/02/14
' Author: PH Kim [ pkim ((AT)) scripts.pe.kr ]
Imports System
Imports ScottGarland ' for C# 2.0 Get at http://www.codeplex.com/biginteger/Release/ProjectReleases.aspx?ReleaseId=16762
Namespace MyTestApplication1
Class MakeDivisionTableApp
Shared Dim ZERO As BigInteger = New BigInteger("0")
Shared Sub printUsage()
' Console.WriteLine("Using: MakeDivisionTableApp [numerator] [denominator]")
' Console.WriteLine("Make a division table in a handy written form.")
Console.WriteLine("사용법: MakeDivisionTableApp [피제수] [제수]")
Console.WriteLine("손으로 작성한 형태의 나눗셈 표를 만들어준다.")
End Sub
Shared Function simplify(v As Double) As String
Dim t As String = "" & Convert.ToString(v)
If t.EndsWith(".0") Then
t = Mid(t, 1, t.Length - 2)
End If
simplify = t
End Function
Shared Function simplify(v As BigInteger, width As Integer) As String
Dim t As String = "" & Convert.ToString(v)
If t.EndsWith(".0") Then
t = Mid(t, 1, t.Length - 2)
End If
Dim len As Integer = t.Length
If len < width Then
t = Mid(" ", 1, width - len) & t
End If
simplify = t
End Function
Function getSuffix(v As BigInteger) As String
Dim t As BigInteger = BigInteger.Modulus(v, New BigInteger("10"))
Dim suffix As String = "은"
If "2459".IndexOf("" & Convert.ToString(t)) >= 0 Then
suffix = "는"
End If
getSuffix = suffix
End Function
Function makeTable(numer As BigInteger, denom As BigInteger, quotient As BigInteger) As BigInteger
Dim strNumer As String = "" & Convert.ToString(numer)
Dim strDenom As String = "" & Convert.ToString(denom)
Dim strQuotient As String = "" & Convert.ToString(quotient)
DIm lenN As Integer = strNumer.Length
DIm lenD As Integer = strDenom.Length
DIm lenQ As Integer = strQuotient.Length
DIm offsetLeft As Integer = 3 + lenD + 3
DIm spaces As String = " "
DIm uline As String = Mid("_________________________________________________________________________________", 1, lenN + 2)
DIm sline As String = Mid("---------------------------------------------------------------------------------", 1, lenN)
DIm bias As Integer = lenN - lenQ
Console.WriteLine(Mid(spaces, 1, offsetLeft) & Mid(spaces, 1, bias) & Convert.ToString(quotient))
Console.WriteLine(Mid(spaces, 1, offsetLeft - 2) & uline)
Console.Write(" " & strDenom & " ) " & strNumer)
Dim strTmpR As String = Mid(strNumer, 1, bias + 1)
Dim tmpR As BigInteger = New BigInteger(strTmpR)
Dim tmpSub As BigInteger = ZERO
Dim oneDigit As String = Nothing
For i As Integer = 0 To lenQ - 1
If Mid(strQuotient, i + 1, 1) = "0" Then
If i + 1 < lenQ Then
oneDigit = Mid(strNumer, bias + i + 1 + 1, 1)
Console.Write(oneDigit)
strTmpR = strTmpR & oneDigit
tmpR = New BigInteger(strTmpR)
End If
Else
Console.WriteLine()
tmpSub = BigInteger.Multiply(New BigInteger(Mid(strQuotient, i + 1, 1)), denom)
Console.WriteLine(Mid(spaces, 1, offsetLeft) & simplify(tmpSub, bias + i + 1))
Console.WriteLine(Mid(spaces, 1, offsetLeft) & sline)
tmpR = BigInteger.Subtract(tmpR, tmpSub)
If tmpR.Equals(ZERO) And i + 1 < lenQ Then
Console.Write(Mid(spaces, 1, offsetLeft) & Mid(spaces, 1, bias + i + 1))
Else
Console.Write(Mid(spaces, 1, offsetLeft) & simplify(tmpR, bias + i + 1))
End If
strTmpR = "" & Convert.ToString(tmpR)
If i + 1 < lenQ Then
oneDigit = Mid(strNumer, bias + i + 1 + 1, 1)
Console.Write(oneDigit)
strTmpR = strTmpR & oneDigit
tmpR = New BigInteger(strTmpR)
End If
End If
Next
Console.WriteLine()
makeTable = tmpR
End Function
' Java 언어의 main 메소드에 해당하는 Visual Basic의 Main
Shared Sub Main(ByVal args() As String)
If args.Length < 2 Then
printUsage()
Environment.Exit(1)
End If
Dim a As BigInteger = Nothing
Dim b As BigInteger = Nothing
Try
a = New BigInteger(args(0))
b = New BigInteger(args(1))
Catch ex As ArgumentOutOfRangeException
Console.WriteLine(ex)
Console.WriteLine("피제수: " & args(0) & ", 제수: " & args(1))
Console.WriteLine("숫자 입력에 오류가 있습니다.")
Environment.Exit(1)
Catch ex As ArgumentNullException
Console.WriteLine(ex)
Catch ex As FormatException
Console.WriteLine(ex)
Console.WriteLine("피제수: " & args(0) & ", 제수: " & args(1))
Console.WriteLine("숫자 입력에 오류가 있습니다.")
Environment.Exit(1)
End Try
If BigInteger.Compare(a, ZERO) <= 0 Then
Console.WriteLine("피제수: " & Convert.ToString(a))
Console.WriteLine("피제수는 양의 정수라야 합니다.")
Environment.Exit(1)
ElseIf BigInteger.Compare(b, ZERO) <= 0 Then
Console.WriteLine("제수: " & Convert.ToString(b))
Console.WriteLine("제수는 양의 정수라야 합니다.")
Environment.Exit(1)
End If
Dim app As MakeDivisionTableApp = New MakeDivisionTableApp()
Dim q As BigInteger = BigInteger.Divide(a, b)
Dim r As BigInteger = BigInteger.Modulus(a, b)
Console.Write("나눗셈 " & Convert.ToString(a) & " ÷ " & Convert.ToString(b) & " 의 결과: ")
Console.Write("몫: " & Convert.ToString(q) & ", ")
Console.WriteLine("나머지: " & Convert.ToString(r))
Console.WriteLine()
Dim k As BigInteger = app.makeTable(a, b, q)
Console.WriteLine()
If k.Equals(r) Then
Console.WriteLine("나머지: " & Convert.ToString(k))
End If
If k.Equals(ZERO) Then
Console.WriteLine(Convert.ToString(a) & " = " & Convert.ToString(b) & " x " & Convert.ToString(q))
Console.WriteLine(Convert.ToString(a) & app.getSuffix(a) & " " & Convert.ToString(b) & "의 배수(mupltiple)이다.")
Console.WriteLine(Convert.ToString(b) & app.getSuffix(b) & " " & Convert.ToString(a) & "의 약수(divisor)이다.")
Else
Console.WriteLine(Convert.ToString(a) & " = " & Convert.ToString(b) & " x " & Convert.ToString(q) & " + " & Convert.ToString(r))
Console.WriteLine(Convert.ToString(a) & app.getSuffix(a) & " " & Convert.ToString(b) & "의 배수(mupltiple)가 아니다.")
End If
End Sub
End Class
End Namespace
컴파일> csc /target:module BigInteger.cs
컴파일> vbc MakeDivisionTableApp.bas /addmodule:BigInteger.netmodule
실행> MakeDivisionTableApp 500210 61
나눗셈 500210 ÷ 61 의 결과: 몫: 8200, 나머지: 10
8200
________
61 ) 500210
488
------
122
122
------
10
나머지: 10
500210 = 61 x 8200 + 10
500210은 61의 배수(mupltiple)가 아니다.
'프로그래밍 > BASIC' 카테고리의 다른 글
스트링 배열 정렬(sorting)하기 with Visual Basic (0) | 2009.04.20 |
---|---|
손으로 계산하는 긴자리 곱셈표 만들기 with Visual Basic (0) | 2009.03.08 |
대화형 모드의 진법(radix) 변환 예제 with Visual Basic (0) | 2009.02.16 |
7비트 ASCII 코드표 만들기 예제 with Visual Basic (0) | 2009.02.16 |
진법(radix) 표 만들기 예제 with Visual Basic (0) | 2009.02.16 |