소스 파일명: testIfCPP.cpp

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

컴파일> cl /GX testIfCPP.cpp

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

실행> testIfCPP 1.234
1.234 is a positive number.

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

실행> testIfCPP 0
0 is zero.



※ Ch 5.5를 사용하여 컴피일 과정 없이 소스를 실행시키려면 인클루드문

        #incldue <cmath>



        #incldue <math.h>

로 고치면 된다. 그대신 부동소수점수의 출력이 다소 다르다.



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

실행> 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.




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

Posted by Scripter
,

소스 파일명: 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
,