전체 글 726

현재 시각 알아내기 with Go

현재 시각을 컨솔에 보여주는 간단한 애플리케이션의 Go 언어 소스 코드이다. UTC란 1970년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초 단위의 총 시간을 의미한다. * UTC(Universal Time Coordinated, 협정세계시, 協定世界時) /* * Filename: testCTime.go * * Compile: go build testCTime.go * Execute: ./testCTime * * Execute without compiling: go run testCTime.go * * Date: 2012. 6. 18. */ package main import ( "fmt" "time" "math" ) func main() { var weekNames []string = []st..

프로그래밍/Go 2012.06.17

조립제법(Horner의 방법) 예제 with Go

다항식 p(x) 를 1차 다항식 x - a 로 나눌 때의 몫과 나머지를 구하는 조립제법을 Go 언어로 구현해 보았다. 조립제법(synthetic division)은 일명 Horner의 방법이라고도 불리우는데, 이는 다항식 p(x)의 x = a 일 때의 값 p(a)을 계산하는 가장 빠른 알고리즘이기도 하다. p(x) = (x - a)q(x) + r 여기서 r은 나머지이며 r = p(a) 이다. 또 q(x)는 몫이다. [참고] * 온라인으로 조립제법 표 만들기 손으로 계산하는 조립제법 표 * 온라인으로 구하는 다항식의 도함수: 조립제법을 이용한 다항식의 도함수 아래의 소스파일은 go run 명령을 사용하면 컴파일 과정 없이 그대로 실행된다. (실행 예: go run testSyntheticDivision.g..

프로그래밍/Go 2012.06.16

80컬럼 컨솔에 19단표 출력하기 예제 with Go

다음은 C 언어로 작성된 소스파일 testForFor.c(참조: 80컬럼 컨솔에 19단표 출력하기 예제 for C and Ch)를 Go 언어용으로 고친 것이다. (* 한글이 있는 Go 소스파일은 UTF8 인코딩으로 저장해야 한다.) /* * Filename: testForFor.go * * Compile: go build testForFor.go * Execute: testForFor * * Execute without compiling: go run testForFor.go * * Date: 2012. 6. 16. */ package main import ( "fmt" ) // dan으로 전달된 (구구단의) 한 단의 출력할 내용을 // 스트링으로 만들어 반환하는 함수 func getDan(dan int..

프로그래밍/Go 2012.06.16

(최대공약수 구하기) while... 반복문 예제 with Go

Go 언어에는 while 반복문이 없으므로 for 반복문을 (while 반복문 처럼) 쓰면 된다. 소스 파일명: testWhile.go // Filename: testWhile.go // // Execute: go run testWhile.go 625 1000 // or // Compile: go build testWhile.go // Execute: ./testWhile 625 1000 package main import ( "fmt" "os" "strconv" ) // 사용법 표시 함수 func printUsing() { fmt.Printf("Using: testWhile [integer1] [integer2]\n"); fmt.Printf("This finds the greatest common di..

프로그래밍/Go 2012.06.15

if...else... 조건문 사용 예제 with Go

소스 파일명: testIf.go // Filename: testIf.go // // Execute: go run testIf.go -1.5 // or // Compile: go build testIf.go // Execute: ./testIf -1.5 package main import ( "fmt" "os" "strconv" ) // 사용법 표시 함수 func printUsing() { fmt.Printf("Using: testIf [number]\n"); fmt.Printf("This determines whether the number is positive or not.\n"); } // main 함수 func main() { var val = 0.0 if (len(os.Args) != 2) { p..

프로그래밍/Go 2012.06.15

맹령행 인자 처리 예제 with Go

Go 소스 파일에 한글이 포함되어 있으면 UTF8 인코딩으로 저장해야 한다. 소스 파일명: testArguments.go // Filename: testArguments.go // // Execute: go run testArguments.go 1 2 3 // or // Compile: go build testArguments.go // Execute: ./testArguments 1 2 3 package main import ( "fmt" // fmt.Printf 함수 사용을 위해 "os" // os.Args 사용을 위해 "strconv" // strconv.ParseFloat 함수 사용을 위해 ) func main() { var sum = 0.0; var y = 0.0 for i, x := range..

프로그래밍/Go 2012.06.15

Haskell 언어로 행렬 곱셈하는 예제

Haskell 언어로 행렬 곱셈하는 간단한 소스 (정수로 이루어진 행렬, 분수로 이루어진 행렬, 부동소수점수로 이루어진 행렬, 복소수로 이루어진 행렬 들릐 곱셈을 처리합니다.) -- Filename: testMatrixMultiplication.hs -- -- 참조: http://rosettacode.org/wiki/Matrix_multiplication#Haskell module Main where import Data.Complex import Data.Ratio import Text.Printf import Data.List -- 행렬 곱셉 함수 mmult :: Num a => [[a]] -> [[a]] -> [[a]] mmult a b = [ [ sum $ zipWith (*) ar bc | bc

Haskell 언어로 복소수 계산과 분수 계산 쉽게 하기

Haskell 언어로도 (Python 언어 처럼) 복소수 게산과 분수 게산을 쉽게 할 수 있습니다,. (1) Haskell 언어에서 복소수를 사용하려면 import Data.Complex 구문이 있어야 합니다. 복소수의 표현은 실수부 :+ 허수부 입니다. (2) Haskell 언어에서 분수를 사용하려면 import Data.Ratio 구문이 있어야 합니다. 분수의 표현은 분자 % 분모 입니다. (3) C 언어의 printf 나 Python 언어의 print 처럼 포맷 출력을 하려면 import Text.Printf 구문이 있어야 합니다. 아래에서 진한 글자체로 된 부분만 입력하고 엔터키를 누르면 됩니다. (ghci 는 Glasgow Haskell 의 인터프리터이고,. ghc는 Glasgow Haskell..

이차방정식 풀이 with Haskell

GHC 의 runhaskell 명령으로 소스 파일을 직접 실행해도 되고, ghc 명령으로 컴파일하여 생성된 실행파일을 실행해도 된다. -- Filename: solveQuadratic.hs -- Solve a quadratic equation. -- -- Compile: ghc solveQuadratic.hs -- Execute: solveQuadratic 1 3 2 -- -- Or -- -- Execute: runhaskell solveQuadratic.hs 1 3 2 {- Compile: ghc solveQuadratic.hs Execute & Result: solveQuadratic 1 0 4 Quadratic Equation: 1.0x^2 + 0.0x + 4.0 = 0 Discriminant D ..