전체 글 724

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

아래의 소스 코드는 Groovy로도 실행되는 Java 용 소스파일 TestWhileLoop.java를 Groovy 용으로 Groovy 언어 답게 고친 것이다. 소스 파일명: testWhile.groovy /* * Filename: testWhile.groovy * * Purpose: Example using the while loop syntax * while .... * * Execute: groovy testWhile.groovy -200 300 * */ // java.lang.Math 클래스를 명시적으로 import하는 구문이 없어도 // Groovy는 이 클래스를 자동으로 import한다. // 사용법 표시 def printUsage() { println 'Using: groovy testWhil..

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

소스 파일명: TestWhileLoop.java /* * Filename: TestWhileLoop.java * * Purpose: Example using the while loop syntax * while .... * * Compile: javac -d . TestWhileLoop.java * Execute: java TestWhileLoop -200 300 * */ import java.lang.Math; public class TestWhileLoop { // 사용법 표시 public static void printUsage() { System.out.println("Using: java TestWhileLoop [integer1] [integer2]"); System.out.println("T..

if...else... 조건문 사용 예제 for Lua

소스 파일명: testIf.lua --[[ Filename: testIf.lua Purpose: Example using the conditional control structure syntax if .... else ... Execute: lua testIf.lua [number] --]] -- 사용법을 보여주는 함수 function printUsing() print("Using: ruby testIf.rb [number]") print("This determines whether the number is positive or not.") end -- 명령행 인자의 개수가 1이 아니면 사용법을 보여준다. if #arg ~= 1 then printUsing() os.exit(1) end -- 명령행 인자..

프로그래밍/Lua 2008.02.19

if...else... 조건문 사용 예제 for C++

소스 파일명: testIfCPP.cpp #include #include // atof() 함수 사용을 위해 using namespace std; // cout 사용을 위해 // 사용법 표시 함수 void printUsing() { cout ch testCPP.cpp 2.7 2.7000 is a positive number. 실행> ch testCPP.cpp -2.7 -2.7000 is a negative number. 실행> ch testCPP.cpp 0 0 is zero. 이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

프로그래밍/C++ 2008.02.19

if...else... 조건문 사용 예제 for C

소스 파일명: testIf.c #include #include // 사용법 표시 함수 void printUsing() { printf("Using: testIf [number]\n"); printf("This determines whether the number is positive or not.\n"); } // main 함수 int main(int argc, char *argv[]) { float val; if (argc != 2) { printUsing(); exit(1); return 1; } // 명령행 인자의 스트링을 가져와서 // 배정밀도 부동소수점수로 변환하여 // 변수 val에 저장한다. val = atof(argv[1]); // 변수 val에 저장된 값이 양수인지 음수인지 0인지를 //..

프로그래밍/C 2008.02.19

if...else... 조건문 사용 예제 for Python

소스 파일명: testIf.py # coding=euc-kr """ Filename: testIf.py Purpose: Example using the conditional control structure syntax if .... else ... Execute: python testIf.py [number] """ import sys # sys.argv를 위한 수입(import) 구문 # 사용법을 보여주는 함수 def printUsing(): print("Using: python testIf.py [number]") print("This determines whether the number is positive or not.") # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다. if len(sy..

if...else... 조건문 사용 예제 for Ruby

소스 파일명: testIf.rb =begin Filename: testIf.rb Purpose: Example using the conditional control structure syntax if .... else ... Execute: ruby testIf.rb [number] =end # 사용법을 보여주는 함수 def printUsing() print("Using: ruby testIf.rb [number]\n") print("This determines whether the number is positive or not.\n") end # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다. if (ARGV.length != 1) printUsing() exit(1) end # 명령행 인자의 스..

if...else... 조건문 사용 예제 for Java

소스 파일명: TestIfThen.java public class TestIfThen { public static void printUsing() { System.out.println("Using: java TestIfThen [number]"); System.out.println("This determines whether the number is positive or not."); } // C 언어의 main 함수에 준하는 Java 언어의 main 메소드 public static void main(String[] args) { if (args.length != 1) { printUsing(); System.exit(1); } //////////////////////////////////////////..

명령행 인자 처리 예제 for Lua

Lua 언어에서 명령행 인자는 변수 arg로 처리한다. 즉, arg는 Lua 언어에서 하나의 (명령행 인자 처리 변수) 예약어인 셈이다. 이 변수는 명령행 실행시 옵션으로 입력된 인자들을 스트링 값으로 모아둔 리스트 타입의 변수이다. 이 변수가 가리키는 리스트에 속한 아이템의 개수를 구하기 위해서는 전위 단항연산자 #을 선두에 붙이면 된다. 즉 #arg가 리스트에 속한 (아이템의 개수)-1이다. 그런데 Lua 언어에서는 Python 언어에서 처럼, 명령행 실행시 실행될 소스파일명의 인덱스 번호가 0번이므로, 아래의 소스 코드에서 우리가 처리할 명령행 인자들은 1번, 2번, 3번, .... 의 인텍스 번호를 갖는다. (참고로, Lua 언어의 신택스는 테이블 타입의 자료 처리를 기반으로 하고 있으며, 리스트..

프로그래밍/Lua 2008.02.19