프로그래밍 605

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

명령행 인자 처리 예제 for Ruby and JRuby

Ruby 언어에서는 명령행 인자를 처리하는 변수로 ARGV를 미리 지정하여 놓았다. 소스 파일명: testArguments.rb # 명령행 인자(command-line argument) 개수 출력 print("Count of arguments: #{ARGV.length}\n") sum = 0.0 for i in 0...ARGV.length # 스트링을 부동소수점수로 변환하여 누적 sum += ARGV[i].to_f end # 누적된 값을 출력 print("The sum of arguments is %g\n" % sum) 실행> ruby testArguments.rb 1 2 3 4 Count of arguments: 4 The sum of arguments is 10 실행> ruby testArgument..

명령행 인자 처리 예제 for Python and Jython

Python 언어에서 명령행 인자는 sys.argv 라는 변수로 처리한다. sys.argv는 모듈 sys에 속하는 변수 argv를 의미하며, 이는 명령행 실행시 옵션으로 입력된 인자들을 스트링 값으로 모아둔 리스트형의 변수이다. 모듈 sys을 하기 위해서는 import sys 라는 수입(import) 구문을 소스 선두 부분에 적어주어야 한다. C/C++/Java/Ruby 언어들과는 달리 리스트의 0번의 값(즉 sys.argv[0])은 python 명령 바로 다음에 입력된 (실행될) Python 소스파일명을 가리키므로, 1번부터 처리해야 한다. 즉 Python 언어에서 sys.argv[1], sys.argv[2], sys.argv[3], ... 들이 각각 C 언어의 argv[0], argv[1], argv..

맹령행 인자 처리 예제 for C

소스 파일명: testArguments.c #include // printf 함수 사용을 위해 #include // atof 함수 사용을 위해 // argc는 명령행 인자 개수, argv는 명령행 인자 문자열의 배열 int main(int argc, char *argv[]) { int i; double sum = 0.0; // 초기화 // 명령행 인자(command-line argument) 개수 출력 printf("Count of arguments: %d\n", argc); for (i = 0; i < argc; i++) { // 스트링을 배정밀도 부동소수점수로 변환하여 누적 sum += atof(argv[i]); } // 배정밀도 부동소수점수 값을 %g로 출력 printf("The sum of arg..

프로그래밍/C 2008.02.18