소스 파일명: testIf.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 사용법 표시 함수
  4. void printUsing() {
  5.     printf("Using: testIf [number]\n");
  6.     printf("This determines whether the number is positive or not.\n");
  7. }
  8. // main 함수
  9. int main(int argc, char *argv[]) {
  10.     float val;
  11.     if (argc != 2) {
  12.         printUsing();
  13.         exit(1);
  14.         return 1;
  15.     }
  16.     // 명령행 인자의 스트링을 가져와서
  17.     // 배정밀도 부동소수점수로 변환하여
  18.     // 변수 val에 저장한다.
  19.     val = atof(argv[1]);
  20.     // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  21.     // 판단하는 if...else... 조건문
  22.     if (val > 0.0)
  23.         printf("%g is a positive number.\n", val);
  24.     else if (val < 0.0)
  25.         printf("%g is a negative number.\n", val);
  26.     else
  27.         printf("%g is zero.\n", val);
  28.     return 0;
  29. }

컴파일> cl testIf.c

실행> testIf
Using: testIf [number]
This determines whether the number is positive or not.

실행> testIf 1.234
1.234 is a positive number.

실행> testIf -1.234
-1.234 is a negative number.

실행> testIf 0
0 is zero.


※ Ch를 사용하면 위의 소스 코드를 (컴파일 과정 없이) 그대로 실행시킬 수 있다.

실행> ch testIf.c
Using: testIf [number]
This determines whether the number is positive or not.

실행> ch testIf.c 1.01
1.01 is a positive number.

실행> ch testIf.c -1.01
-1.01 is a negative number.

실행> ch testIf.c 0
0 is zero.



Posted by Scripter
,

소스 파일명: testIf.py

  1. # coding=euc-kr
  2. """
  3.    Filename: testIf.py
  4.    Purpose:  Example using the conditional control structure syntax
  5.                  if .... else ...
  6.    Execute: python testIf.py [number]
  7. """
  8. import sys    # sys.argv를 위한 수입(import) 구문
  9. # 사용법을 보여주는 함수
  10. def printUsing():
  11.    print("Using: python testIf.py [number]")
  12.    print("This determines whether the number is positive or not.")
  13. # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
  14. if len(sys.argv) != 2:
  15.     printUsing()
  16.     sys.exit(1)
  17. # 명령행 인자의 스트링을 가져와서
  18. # 배정밀도 부동소수점수로 변환하여
  19. # 변수 val에 저장한다.
  20. val = float(sys.argv[1])
  21. # 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  22. # 판단하는 if...else... 조건문
  23. if val > 0.0:
  24.     print("%g is a positive number." % val)
  25. elif val < 0.0:
  26.     print("%g is a negative number." % val)
  27. else:
  28.     print("%g is zero." % val)


실행> python testIf.py
Using: python testIf.py [number]
This determines whether the number is positive or not.

실행> python testIf.py 6.25
6.25 is a positive number.

실행> python testIf.py -6.25
-6.25 is a negative number.

실행> python testIf.py 0
0 is zero.


※ 위의 소스 코드는 Jython으로도 수정 없이 그대로 실행된다.

실행> jython testIf.py
Using: python testIf.py [number]
This determines whether the number is positive or not.

실행> jython testIf.py 1.02
1.02 is a positive number.

실행> jython testIf.py -1.02
-1.02 is a negative number.

실행> jython testIf.py 0
0 is zero.


위의 소스 코드는 ipy 명령에 의하여 IronPython에서도 수정 없이 그대로 실행된다.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

소스 파일명: testIf.rb

  1. =begin
  2.    Filename: testIf.rb
  3.    Purpose:  Example using the conditional control structure syntax
  4.                  if .... else ...
  5.    Execute: ruby testIf.rb [number]
  6. =end
  7. # 사용법을 보여주는 함수
  8. def printUsing()
  9.    print("Using: ruby testIf.rb [number]\n")
  10.    print("This determines whether the number is positive or not.\n")
  11. end
  12. # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
  13. if (ARGV.length != 1)
  14.     printUsing()
  15.     exit(1)
  16. end
  17. # 명령행 인자의 스트링을 가져와서
  18. # 배정밀도 부동소수점수로 변환하여
  19. # 변수 val에 저장한다.
  20. val = ARGV[0].to_f
  21. # 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  22. # 판단하는 if...else... 조건문
  23. if (val > 0.0)
  24.     print("#{val} is a positive number.\n")
  25. elsif (val < 0.0)
  26.     print("#{val} is a negative number.\n")
  27. else
  28.     print("#{val} is zero.\n")
  29. end


실행> ruby testIf.rb
Using: ruby testIf.rb [number]
This determines whether the number is positive or not.

실행> ruby testIf.rb 1.2
1.2 is a positive number.

실행> ruby testIf.rb -1.2
-1.2 is a negative number.

실행> ruby testIf.rb 0
0.0 is zero.



※ 위의 소스 코드는 JRuby로도 (수정 없이) 그대로 실행된다.

실행> jruby testIf.rb
Using: ruby testIf.rb [number]
This determines whether the number is positive or not.

실행> jruby testIf.rb 3.14
3.14 is a positive number.

실행> jruby testIf.rb -3.14
-3.14 is a negative number.

실행> jruby testIf.rb 0
0.0 is zero.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

소스 파일명: testIf.groovy

  1. /*
  2.  *  Filename: testIf.groovy
  3.  *
  4.  *  Purpose:  Example using the conditional control structure syntax
  5.  *                if .... else ...
  6.  *
  7.  *  Execute: groovy testIf.groovy [number]
  8.  */
  9. def printUsing() {
  10.    println("Using: groovy testIf.groovy [number]")
  11.    println("This determines whether the number is positive or not.")
  12. }
  13. if (args.length != 1) {
  14.     printUsing()
  15.     System.exit(1)
  16. }
  17. ////////////////////////////////////////////////
  18. // 명령행 인자의 스트링을 가져와서
  19. // 배정밀도 부동소수점수로 변환하여
  20. // 변수 val에 저장한다.
  21. def val = args[0].toDouble()
  22. // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  23. // 판단하는 if...else... 조건문
  24. if (val > 0.0)
  25.     println("$val is a positive number.")
  26. else if (val < 0.0)
  27.     println("$val is a negative number.")
  28. else
  29.     println("$val is zero.")



실행> groovy testIf.groovy
Using: groovy testIf.groovy [number]
This determines whether the number is positive or not.

실행> groovy testIf.groovy 5
5 is a positive number.

실행> groovy testIf.groovy -5
-5 is a negative number.

실행> groovy testIf.groovy 0
0.0 is zero.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

소스 파일명: TestIfThen.java

  1. public class TestIfThen {
  2.     public static void printUsing() {
  3.         System.out.println("Using: java TestIfThen [number]");
  4.         System.out.println("This determines whether the number is positive or not.");
  5.     }
  6.     // C 언어의 main 함수에 준하는 Java 언어의 main 메소드
  7.     public static void main(String[] args) {
  8.         if (args.length != 1) {
  9.             printUsing();
  10.             System.exit(1);
  11.         }
  12.         ////////////////////////////////////////////////
  13.         // 명령행 인자의 스트링을 가져와서
  14.         // 배정밀도 부동소수점수로 변환하여
  15.         // 변수 val에 저장한다.
  16.         double val = Double.parseDouble(args[0]);
  17.         // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  18.         // 판단하는 if...else... 조건문
  19.         if (val > 0.0)
  20.             System.out.println(val + " is a positive number.");
  21.         else if (val < 0.0)
  22.             System.out.println(val + " is a negative number.");
  23.         else
  24.             System.out.println(val + " is zero.");
  25.     }
  26. }

컴파일> javac -d . TestIfThen.java

실행> java TestIfThen
Using: java TestIfThen [number]
This determines whether the number is positive or not.

실행> java TestIfThen 2.1
2.1 is a positive number.

실행> java TestIfThen -2.1
-2.1 is a negative number.

실행> java TestIfThen 0
0.0 is zero.



Groovy를 사용하면 위의 소스를 (컴파일 과정 없이) 그대로 실행시킬 수 있다. 그대신 파일 확장자명을 groovy로 하여 저장하여야 한다. 아래는 파일명을 TestIfThen.groovy로 저장하여 실행시칸 경우이다.


실행> groovy TestIfThen.groovy
Using: java TestIfThen [number]
This determines whether the number is positive or not.

실행> groovy TestIfThen.groovy 2.1
2.1 is a positive number.

실행> groovy TestIfThen.groovy -2.1
-2.1 is a negative number.

실행> groovy TestIfThen.groovy 0
0.0 is zero.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

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

Lua 언어에서 줄 단위 주석문 기호는 --이다. 소스 코드의 어느 줄이든 -- 및 그 우측 부분은 모두 주석으로 처리된다. 이 기호는 Python/Ruby 언어의 줄 단위 주석문 기호 #에 해당하며, C/C++/Java/Groovy 언어의 줄 단위 주석문 기호 //에 해당한다.



소스 파일명: testArguments.lua
  1. -- 명령행 인자(command-line argument) 개수 출력
  2. print( "Count of arguments: " .. #arg )
  3. sum = 0.0
  4. for i = 1, #arg do
  5.     -- 스트링을 부동소수점수로 변환하여 누적
  6.     sum = sum + tonumber(arg[i])
  7. end
  8. -- 누적된 값을 출력
  9. print( "The sum of arguments is " .. sum)


실행> lua testArguments.lua 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> lua testArguments.lua 1 2 3 4.5
Count of arguments: 4
The sum of arguments is 10.5





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,
Ruby 언어에서는 명령행 인자를 처리하는 변수로 ARGV를 미리 지정하여 놓았다.


소스 파일명: testArguments.rb
  1. # 명령행 인자(command-line argument) 개수 출력
  2. print("Count of arguments: #{ARGV.length}\n")
  3. sum = 0.0
  4. for i in 0...ARGV.length
  5.     # 스트링을 부동소수점수로 변환하여 누적
  6.     sum += ARGV[i].to_f
  7. end
  8. # 누적된 값을 출력
  9. 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 testArguments.rb 1 2 3 4.2
Count of arguments: 4
The sum of arguments is 10.2


※ 위의 소스 코드는 JRuby로도 수정 없이 그대로 실행된다.

실행> jruby testArguments.rb 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> jruby testArguments.rb 1 2 3 4.2
Count of arguments: 4
The sum of arguments is 10.2





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

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[2], .... 들에 해당하며, Java 언어의 args[0], args[1], args[2], ... 들에 해당한다.

다음 소스의 앞 부분에

    #! /usr/bin/python
    # -*- encoding: euc-kr -*-

를 적어둔 것은 소스 코드에서 (주석문에) ASCII 가 아닌 문자(여기서는 한글 euc-kr)를 소스 코드에서 사용하고 있기 때문이다. 이 두 줄은

    #! /usr/bin/python
    # coding: euc-kr

으로 하여도 된다. 만일 소스 코드 파일을 utf-8 인코딩으로 저장하였다면 euc-kr 대신 utf-8로 하여야 할 것이다. (참고로, Ubuntu 플랫폼에서는 소스파일 저장시 반드시 utf-8 인코딩으로 저장하여야 한다.)


소스파일명: testArguments.py

  1. #!/usr/bin/python
  2. # -*- encoding: euc-kr -*-
  3. import sys
  4. # 명령행 인자(command-line argument) 개수 출력
  5. print "Count of arguments: %d" % (len(sys.argv)-1)
  6. sum = 0.0
  7. for i in range(1, len(sys.argv)):
  8.     # 스트링을 부동소수점수로 변환하여 누적
  9.     sum += float(sys.argv[i])
  10. # 누적된 값을 출력
  11. print "The sum of arguments is %g" % sum


실행> python testArguments.py 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> python testArguments.py 1 2 3 4.1
Count of arguments: 4
The sum of arguments is 10.1


※ 위의 소스 코드는 Jython으로도 수정 없이 그대로 실행된다.

실행> jython testArguments.py 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> jython testArguments.py 1 2 3 4.1
Count of arguments: 4
The sum of arguments is 10.1


※ 위의 소스 코드는 IronPython으로도 수정 없이 그대로 실행된다.

실행> ipy testArguments.py 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> ipy testArguments.py 1 2 3 4.1
Count of arguments: 4
The sum of arguments is 10.1




Posted by Scripter
,
소스 파일명: testArgumentsCPP.cpp
  1. #include <iostream>  // cout 함수와 endl 문자의사용을 위해
  2. #include <cmath>     // atof 함수 사용을 위해
  3. using namespace std;
  4. // argc는 명령행 인자 개수, argv는 명령행 인자 문자열의 배열
  5. int main(int argc, char *argv[]) {
  6.     int i;
  7.     double sum = 0.0;    // 초기화
  8.     // 명령행 인자(command-line argument) 개수 출력
  9.     cout << "Count of arguments: " << argc << endl;
  10.     for (i = 0; i < argc; i++) {
  11.         // 스트링을 배정밀도 부동소수점수로 변환하여 누적
  12.         sum += atof(argv[i]);
  13.     }
  14.     // 배정밀도 부동소수점수 값을 cout로 출력
  15.     cout << "The sum of arguments is " << sum << endl;
  16.     return 0;
  17. }


컴파일> cl -GX testArgumentsCPP.cpp

실행> testArgumentsCPP 1 2 3 4
Count of arguments: 5
The sum of arguments is 10

실행> testArgumentsCPP 1 2 3 4.1
Count of arguments: 5
The sum of arguments is 10.1





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,


소스 파일명: testArguments.c

  1. #include <stdio.h>   // printf 함수 사용을 위해
  2. #include <math.h>    // atof 함수 사용을 위해
  3. // argc는 명령행 인자 개수, argv는 명령행 인자 문자열의 배열
  4. int main(int argc, char *argv[]) {
  5.     int i;
  6.     double sum = 0.0;    // 초기화
  7.     // 명령행 인자(command-line argument) 개수 출력
  8.     printf("Count of arguments: %d\n", argc);
  9.     for (i = 0; i < argc; i++) {
  10.         // 스트링을 배정밀도 부동소수점수로 변환하여 누적
  11.         sum += atof(argv[i]);
  12.     }
  13.     // 배정밀도 부동소수점수 값을 %g로 출력
  14.     printf("The sum of arguments is %g\n", sum);
  15.     return 0;
  16. }


컴파일> cl testArguments.c

실행> testArguments 1 2 3 4
Count of arguments: 5
The sum of arguments is 10

실행> testArguments 1 2 3 4.2
Count of arguments: 5
The sum of arguments is 10.2


 


Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,