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

/*
 * Filename: makeMultTable.go
 *
 *     Print a multiplication table.
 *
 *     Execute: go run makeMultTable.go 230 5100
 *
 *    or
 *
 *     Compile: go build makeMultTable.go
 *     Execute: ./makeMultTable 230 5100
 *
 * Date: 2012/06/28
 * Author: pkim (AT) scripts.pe.kr
 */
 
package main

import (
    "fmt"
    "os"
    "strconv"
)
   
func println(s string) {
    fmt.Printf("%s\n", s)
}

func print(s string) {
    fmt.Printf("%s", s)
}

func main() {
    var x, y int64
    if len(os.Args) >= 3 {
        x = atoi(os.Args[1])
        y = atoi(os.Args[2])
        print("\n")
        printMultTable(x, y)
    } else {
        printUsing()
   }
}

func atoi(s string) int64 {
    var n int
    n, _ = strconv.Atoi(s)
    return int64(n)
}

func printUsing() {
    print("Using: makeMultTable [number1] [number2]\n")
    print("Print a multiplication table for the given two integers.\n");
}

func printMultTable(x, y int64) {
    var nx, ny int64
    var z int64
    var ntail1 int
    var ntail2 int
    var y1 int
    var i int
    var strX string
    var strY string
    var strZ string
    var strT string
    var zeros string
    var whites string
    var bars string
    var line1 string
    var line2 string
    var line3 string
    var line4 string
    var loffset string
    var buf string

    nx = x
    if x < 0 { nx = -x }
    ny = y
    if y < 0 { ny = - y }
    ntail1 = 0
    ntail2 = 0
    for nx % 10 == 0 {
        nx = nx / 10
        ntail1 += 1
    }
    for ny % 10 == 0 {
        ny = ny / 10
        ntail2 += 1
    }
    z = nx * ny
    strZ = fmt.Sprintf("%d", z)
    strX = fmt.Sprintf("%d", nx)
    strY = fmt.Sprintf( "%d", ny)
    zeros = "0000000000000000000000000000000000000000"
    whites = "                                        "
    bars = "----------------------------------------"
    loffset =  "       "
    line4 = fmt.Sprintf("%s%d", loffset, nx*ny)
    buf = zeros[:ntail1 + ntail2]
    line4 += buf
    line1 = loffset
    buf = whites[:len(strZ) - len(strX)]
    buf += strX
    line1 += buf
    buf =  zeros[:ntail1]
    line1 += buf

    line2 = "   x ) "
    buf = whites[:len(strZ) - len(strY)]
    buf += strY
    line2 += buf
    buf = zeros[:ntail2]
    line2 += buf

    line3 = "     --"
    buf = bars[:len(strZ)]
    line3 += buf

    println(line1)
    println(line2)
    println(line3)
    if len(strY) > 1 {
        for i = 0; i < len(strY); i++ {
         buf = string(strY[len(strY) - i - 1])
            y1 = int(atoi(buf))
            if y1 != 0 {
                strT = fmt.Sprintf("%d", nx * int64(y1))
                line2 = loffset
                buf = whites[:len(strZ) - len(strT) - i]
                buf += strT
                line2 += buf
                println(line2)
            }
        }
        println(line3)
    }
    println(line4)
}



실행> go run makeMultTable.go 230 5100
또는

컴파일> go build makeMultTable.go
실행> ./makeMultTable 230 5100
결과>

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

 


 

Posted by Scripter
,