초등학교 때 배우는 두 정수의 곱셈표를 만들어 주는 Boo 소스이다.
소스 코드는 Python 언어로 작성된 것과 거의 같다. 단지 여기서는 스트링을 int 타입이나 long 타입으로 변환하는 구문만 언급한다.
int.Parse(스트링) // 스트링을 int 타입으로 변환하기
long.Parse(스트링) // 스트링을 long 타입으로 변환하기
# Filename: makeMultTable.boo
#
# Print a multiplication table.
#
# Execute: booi makeMultTable.boo 230 5100
#
# Date: 2009/04/04
# Author: PH Kim [ pkim (AT) scripts.pe.kr ]
import System
def printUsing():
print "Using: booi makeMultTable.boo [number1] [number2]"
print "Print a multiplication table for the given two integers."
def printMultTable(x as long, y as long):
nx as long = x
if nx < 0:
nx = -nx
ny as long = y
if ny < 0:
ny = -ny
ntail1 as int = 0
ntail2 as int = 0
while nx % 10 == 0:
nx = nx / 10
ntail1 = ntail1 + 1
while ny % 10 == 0:
ny = ny / 10
ntail2 = ntail2 + 1
z = nx * ny
strZ = "" + z
strX = "" + nx
strY = "" + ny
n = len(strY)
zeros = "0000000000000000000000000000000000000000"
whites = " "
bars = "----------------------------------------"
loffset = " "
line4 = loffset + strZ
line1 = loffset + whites[0: len(strZ) - len(strX)] + strX
line2 = " x ) " + whites[0: len(strZ) - len(strY)] + strY
line3 = " --" + bars[0: len(strZ)]
print line1 + zeros[0: ntail1]
print line2 + zeros[0: ntail2]
print line3
if len(strY) > 1:
for i in range(0, len(strY)):
y1 = int.Parse(strY[len(strY) - i - 1: len(strY) - i])
if y1 != 0:
strT = "" + (nx * y1)
print loffset + whites[0: len(strZ) - len(strT) - i] + strT
print line3
print line4 + zeros[0: ntail1] + zeros[0: ntail2]
if len(argv) >= 2:
x = long.Parse(argv[0])
y = long.Parse(argv[1])
print
printMultTable(x, y)
else:
printUsing()
#
# Print a multiplication table.
#
# Execute: booi makeMultTable.boo 230 5100
#
# Date: 2009/04/04
# Author: PH Kim [ pkim (AT) scripts.pe.kr ]
import System
def printUsing():
print "Using: booi makeMultTable.boo [number1] [number2]"
print "Print a multiplication table for the given two integers."
def printMultTable(x as long, y as long):
nx as long = x
if nx < 0:
nx = -nx
ny as long = y
if ny < 0:
ny = -ny
ntail1 as int = 0
ntail2 as int = 0
while nx % 10 == 0:
nx = nx / 10
ntail1 = ntail1 + 1
while ny % 10 == 0:
ny = ny / 10
ntail2 = ntail2 + 1
z = nx * ny
strZ = "" + z
strX = "" + nx
strY = "" + ny
n = len(strY)
zeros = "0000000000000000000000000000000000000000"
whites = " "
bars = "----------------------------------------"
loffset = " "
line4 = loffset + strZ
line1 = loffset + whites[0: len(strZ) - len(strX)] + strX
line2 = " x ) " + whites[0: len(strZ) - len(strY)] + strY
line3 = " --" + bars[0: len(strZ)]
print line1 + zeros[0: ntail1]
print line2 + zeros[0: ntail2]
print line3
if len(strY) > 1:
for i in range(0, len(strY)):
y1 = int.Parse(strY[len(strY) - i - 1: len(strY) - i])
if y1 != 0:
strT = "" + (nx * y1)
print loffset + whites[0: len(strZ) - len(strT) - i] + strT
print line3
print line4 + zeros[0: ntail1] + zeros[0: ntail2]
if len(argv) >= 2:
x = long.Parse(argv[0])
y = long.Parse(argv[1])
printMultTable(x, y)
else:
printUsing()
실행> python makeMultTable.py 230 5100
결과>
230 x ) 5100 ------ 23 115 ------ 1173000
'프로그래밍 > Boo' 카테고리의 다른 글
삼각형 출력 예제를 통한 여러 가지 소스 비교 with Boo (0) | 2009.04.08 |
---|---|
클래스 상속(subclassing) 예제 with Boo (0) | 2009.04.04 |
Boo 언어로 공백 문자 없이 연속적으로 출력하려면 (0) | 2009.04.04 |
문자열 거꾸로 하기 with Boo (0) | 2009.04.04 |
7비트 ASCII 코드표 만들기 예제 with Boo (0) | 2009.04.03 |