전체 글 724

80컬럼 컨솔에 19단표 출력하기 예제 (2) for Groovy

다음 소스 코드는 80컬럼 컨솔에 19단표 출력하기 예제 (1) for Groovy 에 올려진 Groovy 소스 코드를 더 Groovy 소스 코드 답게 고친 것이다. Java 문법은 모든 구문에서 문장의 끝은 세미콜론(;)을 붙이게 되어 있다. 그러나 Groovy 문법은 문장의 끝을 알리는 세미콜론(;)을 붙여도 되고 안븉여도 된다. (단, 동일한 줄에 여러 개의 문장이 들어갈 때는 문장과 문장을 구별하는 세미콜론(;)을 사이사이에 넣어야 한다.) 즉, 문장의 끝을 알리는 세미콜론(;) 붙이기는 사용자의 개별 취향에 맡기는 옵션이다.(신택스 슈가, syntax sugar) 함수나 변수의 선언시에 Java 구문처럼 타입을 지정해도 되지만, 타입 대신 단지 def를 함수명이나 변수명 앞에 붙여도 된다. 이렇..

80컬럼 컨솔에 19단표 출력하기 예제 (1) for Groovy

아래는 Java용 소스파일 TestForForApp.java 를 (최소한의 수정으로) Groovy용으로 고친 것이다. 배열(array)을 생성하는 문법에 있어 Java 코드와 Groovy 코드가 서로 다르기 때문이다. 예를 들어, 정수 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 을 갖고 있는 int[] 타입의 배열을 생성하여 변수 arr에 저장하는(참조자 arr이 이를 참조하게 하는) Java 문법은 int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 이다. 하지만 이에 대응하는 Groovy 문법은 int[] arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] as int[]; 이다. 즉. Groovy 문법은 리스트를 생성시..

80컬럼 컨솔에 19단표 출력하기 예제 for Java

/* * Filename: TestForForApp.java * * Compile: javac -d . TestForForApp.java * Execute: java TestForForApp * * Date: 2008. 3. 3. */ public class TestForForApp { // static 선언자가 없으므로 이 메소드는 인스턴스 메소드이다. // 인스턴스 메소드는 static 메소드에서는 직접 호출되지 않는다. // 반드시 생성된 객체를 거쳐서 호출되어 진다. public String[] getDan(int dan) { String[] t = new String[19]; String sa, sb, sval; for (int j = 0; j < 19; j++) { sa = "" + dan; ..

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

소스 파일명: testWhile.c /* * Filename: testWhile.c * * Purpose: Example using the while loop syntax * while .... * * Compile: cl testWhile.c * * Execute: testWhile -200 300 */ #include #include // 사용법 표시 void printUsage() { printf("Using: testWhile [integer1] [integer2]\n"); printf("This finds the greatest common divisor of the given two integers.\n"); } int main(int argc, char *argv[]) { long val1,..

프로그래밍/C 2008.02.21

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

소스 파일명: testWhile.lua --[[ Filename: testWhile.lua Purpose: Example using the while loop syntax while .... Execute: lua testWhile.lua -200 300 --]] -- 사용법 표시 function printUsage() print "Using: lua testWhile.lua [integer1] [integer2]" print "This finds the greatest common divisor of the given two integers." end if #arg ~= 2 then printUsage() os.exit(1) end -- 명령행 인자의 두 스트링을 가져와서 -- 정수 타입으로 변환하여 ..

프로그래밍/Lua 2008.02.21

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

소스 파일명: testWhile.py # coding=euc-kr # Filename: testWhile.py # # Purpose: Example using the while loop syntax # while .... # # Execute: python testWhile.py -200 300 # import sys # 사용법 표시 def printUsage(): print "Using: python testWhile.py [integer1] [integer2]" print "This finds the greatest common divisor of the given two integers." if len(sys.argv) != 3: printUsage() sys.exit(1) # -----------..

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

소스 파일명: testWhile.rb # # Filename: testWhile.rb # # Purpose: Example using the while loop syntax # while .... # # Execute: ruby testWhile.rb -200 300 # # 사용법 표시 def printUsage() print "Using: ruby testWhile.rb [integer1] [integer2]\n" print "This finds the greatest common divisor of the given two integers.\n" end if (ARGV.length != 2) printUsage() exit(1) end # ----------------------------------..

Groovy 언어의 특징을 잘 나타내는 몇 가지 예제들

간단한 Groovy 스크립트:def name='World'; println "Hello $name!" 다소 복잡한 Groovy 스크립트: class Greet { def name Greet(who) { name = who[0].toUpperCase() + who[1..-1] } def salute() { println "Hello $name!" } } g = new Greet('world') // 객체 생성 g.salute() // "Hello World!"를 출력 Apache의 commons.lang 라이브러리를 이용한 스크립트 (한 개의 소스 파일로 저장한다.): class Greet { def name Greet() { } Greet(who) { name = who[0].toUpperCase() +..