초등학교 때 배우던 두 정수의 곱셈표를 만들어 주는 F# 소스이다.
이 소스는 Python 소스를 F# 소스로 변환한 것이라 F#의 명령형 언어 특징을 위주로 짜여져 있다.
* Filename: MakeMultTable.fs
*
* Print a multiplication table.
*
* Compile: fsc MakeMultTable.fs
* Execute: MakeMultTable 230 5100
*
* Date: 2010/07/15
* Author: PH Kim [ pkim ((AT)) scripts.pe.kr ]
*)
#light
exception RuntimeError of string
exception ValueError of string
let println s =
printfn "%O" s
let print s =
printf "%O" s
let printUsage dummy =
println "Using: python makeMultTable.py [number1] [number2]"
println "Print a multiplication table for the given two integers."
let printMultTable x y =
let mutable nx = x
if nx < 0 then
nx <- -nx
let mutable ny = y
if ny < 0 then
ny <- -ny
let mutable ntail1 = 0
let mutable ntail2 = 0
while (nx % 10) = 0 do
nx <- nx / 10
ntail1 <- ntail1 + 1
while ny % 10 = 0 do
ny <- ny / 10
ntail2 <- ntail2 + 1
let z = nx * ny
let strZ = sprintf "%d" z
let strX = sprintf "%d" nx
let strY = sprintf "%d" ny
let n = strY.Length
let zeros = "0000000000000000000000000000000000000000"
let whites = " "
let bars = "----------------------------------------"
let loffset = " "
let line4 = loffset + strZ
let line1 = loffset + (whites.Substring(0, strZ.Length - strX.Length)) + strX
let line2 = " x ) " + (whites.Substring(0, strZ.Length - strY.Length)) + strY
let line3 = " --" + (bars.Substring(0, strZ.Length))
println (line1 + (zeros.Substring(0, ntail1)))
println (line2 + (zeros.Substring(0, ntail2)))
println line3
if strY.Length > 1 then
for i in 0..(strY.Length - 1) do
let y1 = int (strY.Substring(strY.Length - i - 1, 1))
if not (y1 = 0) then
let strT = sprintf "%d" (nx * y1)
println (loffset + (whites.Substring(0, strZ.Length - strT.Length - i)) + strT)
println line3
println (line4 + (zeros.Substring(0, ntail1)) + (zeros.Substring(0, ntail2)))
// Begin here
let cmdArgs = System.Environment.GetCommandLineArgs()
if (Array.length cmdArgs >= 3) then
let x = int (cmdArgs.[1])
let y = int (cmdArgs.[2])
println ""
printMultTable x y
else
printUsage 0
exit(1)
컴파일> fsc MakeMultTable.fs
실행> MakeMultTable 230 5100
결과>
230 x ) 5100 ------ 23 115 ------ 1173000
'프로그래밍 > F#' 카테고리의 다른 글
스트링 리스트에서 스트링 찾기(find) with F# (0) | 2010.07.16 |
---|---|
스트링 배열 정렬(sorting)하기 with F# (0) | 2010.07.16 |
문자열 거꾸로 하기 with F# (0) | 2010.07.15 |
손으로 만드는 나눗셈 계산표 with F# (0) | 2010.07.15 |
7비트 ASCII 코드표 만들기 예제 with F# (0) | 2010.07.15 |