소스 파일명: testWhileCPP.cpp

  1. /*
  2.  * Filename: testWhileCPP.cpp
  3.  *
  4.  * Purpose:  Example using the while loop syntax
  5.  *               while ....
  6.  *
  7.  * Compile:  cl -EHsc testWhileCPP.cpp
  8.  *
  9.  * Execute:  testWhileCPP -200 300
  10. */
  11. #include <iostream>
  12. #include <cmath>
  13. using namespace std;
  14. // 사용법 표시
  15. void printUsage() {
  16.     cout << "Using: testWhileCPP [integer1] [integer2]" << endl;
  17.     cout << "This finds the greatest common divisor of the given two integers." << endl;
  18. }
  19. int main(int argc, char *argv[]) {
  20.     long val1, val2;
  21.     long a, b, q, r, gcd;
  22.     if (argc != 3) {
  23.         printUsage();
  24.         exit(1);
  25.     }
  26.     // 명령행 인자의 두 스트링을 가져와서
  27.     // 긴정수(long) 타입으로 변환하여
  28.     // 변수 val1과 val2에 저장한다.
  29.     val1 = atoi(argv[1]);
  30.     val2 = atoi(argv[2]);
  31.     // a는 |val1|, |val2| 중 큰 값
  32.     a = abs(val1);
  33.     b = abs(val2);
  34.     if (a < b) {
  35.         a = abs(val2);
  36.         b = abs(val1);
  37.     }
  38.     if (b == 0L) {
  39.         cout << "GCD(" << val1 << ", " << val2 << ") = " << a << endl;
  40.         exit(0);
  41.     }
  42.     // -------------------------------------------
  43.     // Euclidean 알고리즘의 시작
  44.     //
  45.     // a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  46.     q = a / b;
  47.     r = a % b;
  48.     // Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  49.     while (r != 0L) {
  50.         a = b;
  51.         b = r;
  52.         q = a / b;
  53.         r = a % b;
  54.     }
  55.     // 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  56.     gcd = b;
  57.     // 최대공약수(GCD)를 출력한다.
  58.     cout << "GCD(" << val1 << ", " << val2 << ") = " << gcd << endl;
  59.     return 0;
  60. }



컴파일:

Command> cl -EHsc testWhileCPP.cpp


실행:

Command> testWhileCPP
Using: testWhileCPP [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> testWhileCPP 200 -300
GCD(200, -300) = 100

Command> testWhileCPP 0 -300
GCD(0, -300) = 300

Command> testWhileCPP 20 -125
GCD(20, -125) = 5

Command> testWhileCPP 121 66
GCD(121, 66) = 11

Command> testWhileCPP -111 -37
GCD(-111, -37) = 37




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

Posted by Scripter
,

소스 파일명: testWhile.c

  1. /*
  2.  * Filename: testWhile.c
  3.  *
  4.  * Purpose:  Example using the while loop syntax
  5.  *               while ....
  6.  *
  7.  * Compile:  cl testWhile.c
  8.  *
  9.  * Execute:  testWhile -200 300
  10. */
  11. #include <stdio.h>
  12. #include <math.h>
  13. // 사용법 표시
  14. void printUsage() {
  15.     printf("Using: testWhile [integer1] [integer2]\n");
  16.     printf("This finds the greatest common divisor of the given two integers.\n");
  17. }
  18. int main(int argc, char *argv[]) {
  19.     long val1, val2;
  20.     long a, b, q, r, gcd;
  21.     if (argc != 3) {
  22.         printUsage();
  23.         exit(1);
  24.     }
  25.     // 명령행 인자의 두 스트링을 가져와서
  26.     // 긴정수(long) 타입으로 변환하여
  27.     // 변수 val1과 val2에 저장한다.
  28.     val1 = atoi(argv[1]);
  29.     val2 = atoi(argv[2]);
  30.     // a는 |val1|, |val2| 중 큰 값
  31.     a = abs(val1);
  32.     b = abs(val2);
  33.     if (a < b) {
  34.         a = abs(val2);
  35.         b = abs(val1);
  36.     }
  37.     if (b == 0L) {
  38.         printf("GCD(%ld, %ld) = %ld\n", val1, val2, a);
  39.         exit(0);
  40.     }
  41.     // -------------------------------------------
  42.     // Euclidean 알고리즘의 시작
  43.     //
  44.     // a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  45.     q = a / b;
  46.     r = a % b;
  47.     // Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  48.     while (r != 0L) {
  49.         a = b;
  50.          b = r;
  51.         q = a / b;
  52.         r = a % b;
  53.     }
  54.     // 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  55.     gcd = b;
  56.     // 최대공약수(GCD)를 출력한다.
  57.     printf("GCD(%ld, %ld) = %ld\n", val1, val2, gcd);
  58.     return 0;
  59. }



컴파일:

Command> cl testWhile.c


실행:

Command> testWhile
Using: testWhile [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> testWhile 200 -300
GCD(200, -300) = 100

Command> testWhile 0 -300
GCD(0, -300) = 300

Command> testWhile 20 -125
GCD(20, -125) = 5

Command> testWhile 121 66
GCD(121, 66) = 11

Command> testWhile -111 -37
GCD(-111, -37) = 37

Posted by Scripter
,

소스 파일명: testWhile.lua

  1. --[[
  2.   Filename: testWhile.lua
  3.   Purpose:  Example using the while loop syntax
  4.                 while ....
  5.   Execute: lua testWhile.lua -200 300
  6. --]]
  7. -- 사용법 표시
  8. function printUsage()
  9.     print "Using: lua testWhile.lua [integer1] [integer2]"
  10.     print "This finds the greatest common divisor of the given two integers."
  11. end
  12. if #arg ~= 2 then
  13.     printUsage()
  14.     os.exit(1)
  15. end
  16. -- 명령행 인자의 두 스트링을 가져와서
  17. -- 정수 타입으로 변환하여
  18. -- 변수 val1과 val2에 저장한다.
  19. val1 = tonumber(arg[1])
  20. val2 = tonumber(arg[2])
  21. -- a는 |val1|, |val2| 중 큰 값
  22. a = math.abs(val1)
  23. b = math.abs(val2)
  24. if a < b then
  25.     a = math.abs(val2)
  26.     b = math.abs(val1)
  27. end
  28. if b == 0 then
  29.     print( "GCD(" .. val1 .. ", " .. val2 .. ") = " .. a )
  30.     os.exit(0)
  31. end
  32. -- Euclidean 알고리즘의 시작
  33. --
  34. -- a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  35. q = a / b
  36. r = a % b
  37. -- Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  38. while r ~= 0 do
  39.     a = b
  40.     b = r
  41.     q = a / b
  42.     r = a % b
  43. end
  44. -- 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  45. gcd = b
  46. -- 최대공약수(GCD)를 출력한다.
  47. print( "GCD(" .. val1 .. ", " .. val2 .. ") = " .. gcd )



실행:

Command> lua testWhile.lua
Using: lua testWhile.lua [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> lua testWhile.lua -200 0
GCD(-200, 0) = 200

Command> lua testWhile.lua -200 300
GCD(-200, 300) = 100

Command> lua testWhile.lua 21 81
GCD(21, 81) = 3

Command> lua testWhile.lua 23 25
GCD(23, 25) = 1




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

Posted by Scripter
,