다음은 Java용으로 만들어 둔 소스파일 TestForForApp.java(참조: 80컬럼 컨솔에 19단표 출력하기 예제 for Java )를 C용 소스로 고쳐본 것이다.
스트링을 처리하고 전달하기 위해 메모리 할당(allocation)과 해제(free)를 하면서 동적 메모리(dynamic memory)를 이용하고 있다.
소스 변경 없이 Ch 로도 실행시킬 수 있다.

  1. /*
  2.  *  Filename: testForFor.c
  3.  *
  4.  *  Compile:  cl testForFor.c
  5.  *  Execute:  testForFor
  6.  *
  7.  *  Execute without compiling:  ch testForFor.c
  8.  *
  9.  *  Date:  2008. 3. 3.
  10.  */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <memory.h>
  14. // 매개변수 dan으로 지정된 한 단의 결과를 매개변수 t로 전달한다.
  15. // 중요!! t로 전달된 메모리는 사용 후 반드시 해제(free)하여야 한다.
  16. void getDan(char *t[19], int dan) {
  17.     char sa[10], sb[10], sval[10];
  18.     int j;
  19.     // t[0], ... , t[18]에 각각 동적 메모리를 할당한다.
  20.     for (j = 0; j < 19; j++) {
  21.         t[j] = (char *) calloc(21, sizeof(char));
  22.     }
  23.     for (j = 0; j < 19; j++) {
  24.         sprintf(sa, "%d", dan);
  25.         if (strlen(sa) < 2)
  26.             sprintf(sa, " %d", dan);
  27.         sprintf(sb, "%d", j + 1);
  28.         if (strlen(sb) < 2)
  29.             sprintf(sb, " %d", j + 1);
  30.         sprintf(sval, "%d", dan*(j + 1));
  31.         if (strlen(sval) < 2)
  32.             sprintf(sval, "  %d", dan*(j + 1));
  33.         else if (strlen(sval) < 3)
  34.             sprintf(sval, " %d", dan*(j + 1));
  35.         strcpy(t[j], sa);
  36.         strcat(t[j], " x ");
  37.         strcat(t[j], sb);
  38.         strcat(t[j], " = ");
  39.         strcat(t[j], sval);
  40.     }
  41. }
  42. // 19단표를 모두 80컬럼 컨솔에 출력한다.
  43. void printAllNineteenDan() {
  44.     int d[4] = { 2, 7, 11, 16 };      // 각 줄단위 블럭의 첫단
  45.     int counter[4] = { 5, 4, 5, 4 };  // 각 줄단위 블럭에 속한 단의 개수
  46.     char *lines[19];
  47.     char *v[19];
  48.     int i, j, k;
  49.     char arr[18][19][21];
  50.     for (i = 2; i < 20; i++) {
  51.         getDan(v, i);
  52.         for (j = 0; j < 19; j++) {
  53.             strcpy(arr[i - 2][j], v[j]);
  54.         }
  55.     }
  56.     // lines[0], ... , lines[18]에 각각 동적 메모리를 할당한다.
  57.     for (j = 0; j < 19; j++) {
  58.         lines[j] = (char *) malloc(81 * sizeof(char));
  59.     }
  60.     for (k = 0; k < (sizeof(d)/sizeof(int)); k++) {
  61.         // 80 바이트 길이의 한 줄씩 완성
  62.         for (i = 0; i < 19; i++) {
  63.             strcpy(lines[i], arr[d[k]-2][i]);
  64.             for (j = 1; j < counter[k]; j++) {
  65.                 strcat(lines[i], "   ");
  66.                 strcat(lines[i], arr[d[k]-2+j][i]);
  67.             }
  68.         }
  69.         // 80 바이트 길이의 한 줄씩 출력
  70.         for (i = 0; i < 19; i++) {
  71.             printf("%s\n", lines[i]);
  72.         }
  73.         printf("\n");
  74.     }
  75.     // lines[0], ... , lines[18]로 할당받은 모든 동적 메모리를 해제한다.
  76.     for (j = 0; j < 19; j++) {
  77.         free(lines[j]);
  78.     }
  79.     // v[0], ... , v[18]로 할당받은 모든 동적 메모리를 해제한다.
  80.     for (j = 0; j < 19; j++) {
  81.         free(v[j]);
  82.     }
  83. }
  84. // C 언어의 main 함수 : 실행 시작 지점
  85. int main(void) {
  86.     printAllNineteenDan();    // 객체에 속하는 메소드 printDan()을 호출한다.
  87.     return 0;
  88. }


 
실행> ch testForFor.c 
 
또는

컴파일> cl testForFor.c
실행> testForFor

 2 x  1 =   2    3 x  1 =   3    4 x  1 =   4    5 x  1 =   5    6 x  1 =   6
 2 x  2 =   4    3 x  2 =   6    4 x  2 =   8    5 x  2 =  10    6 x  2 =  12
 2 x  3 =   6    3 x  3 =   9    4 x  3 =  12    5 x  3 =  15    6 x  3 =  18
 2 x  4 =   8    3 x  4 =  12    4 x  4 =  16    5 x  4 =  20    6 x  4 =  24
 2 x  5 =  10    3 x  5 =  15    4 x  5 =  20    5 x  5 =  25    6 x  5 =  30
 2 x  6 =  12    3 x  6 =  18    4 x  6 =  24    5 x  6 =  30    6 x  6 =  36
 2 x  7 =  14    3 x  7 =  21    4 x  7 =  28    5 x  7 =  35    6 x  7 =  42
 2 x  8 =  16    3 x  8 =  24    4 x  8 =  32    5 x  8 =  40    6 x  8 =  48
 2 x  9 =  18    3 x  9 =  27    4 x  9 =  36    5 x  9 =  45    6 x  9 =  54
 2 x 10 =  20    3 x 10 =  30    4 x 10 =  40    5 x 10 =  50    6 x 10 =  60
 2 x 11 =  22    3 x 11 =  33    4 x 11 =  44    5 x 11 =  55    6 x 11 =  66
 2 x 12 =  24    3 x 12 =  36    4 x 12 =  48    5 x 12 =  60    6 x 12 =  72
 2 x 13 =  26    3 x 13 =  39    4 x 13 =  52    5 x 13 =  65    6 x 13 =  78
 2 x 14 =  28    3 x 14 =  42    4 x 14 =  56    5 x 14 =  70    6 x 14 =  84
 2 x 15 =  30    3 x 15 =  45    4 x 15 =  60    5 x 15 =  75    6 x 15 =  90
 2 x 16 =  32    3 x 16 =  48    4 x 16 =  64    5 x 16 =  80    6 x 16 =  96
 2 x 17 =  34    3 x 17 =  51    4 x 17 =  68    5 x 17 =  85    6 x 17 = 102
 2 x 18 =  36    3 x 18 =  54    4 x 18 =  72    5 x 18 =  90    6 x 18 = 108
 2 x 19 =  38    3 x 19 =  57    4 x 19 =  76    5 x 19 =  95    6 x 19 = 114

 7 x  1 =   7    8 x  1 =   8    9 x  1 =   9   10 x  1 =  10
 7 x  2 =  14    8 x  2 =  16    9 x  2 =  18   10 x  2 =  20
 7 x  3 =  21    8 x  3 =  24    9 x  3 =  27   10 x  3 =  30
 7 x  4 =  28    8 x  4 =  32    9 x  4 =  36   10 x  4 =  40
 7 x  5 =  35    8 x  5 =  40    9 x  5 =  45   10 x  5 =  50
 7 x  6 =  42    8 x  6 =  48    9 x  6 =  54   10 x  6 =  60
 7 x  7 =  49    8 x  7 =  56    9 x  7 =  63   10 x  7 =  70
 7 x  8 =  56    8 x  8 =  64    9 x  8 =  72   10 x  8 =  80
 7 x  9 =  63    8 x  9 =  72    9 x  9 =  81   10 x  9 =  90
 7 x 10 =  70    8 x 10 =  80    9 x 10 =  90   10 x 10 = 100
 7 x 11 =  77    8 x 11 =  88    9 x 11 =  99   10 x 11 = 110
 7 x 12 =  84    8 x 12 =  96    9 x 12 = 108   10 x 12 = 120
 7 x 13 =  91    8 x 13 = 104    9 x 13 = 117   10 x 13 = 130
 7 x 14 =  98    8 x 14 = 112    9 x 14 = 126   10 x 14 = 140
 7 x 15 = 105    8 x 15 = 120    9 x 15 = 135   10 x 15 = 150
 7 x 16 = 112    8 x 16 = 128    9 x 16 = 144   10 x 16 = 160
 7 x 17 = 119    8 x 17 = 136    9 x 17 = 153   10 x 17 = 170
 7 x 18 = 126    8 x 18 = 144    9 x 18 = 162   10 x 18 = 180
 7 x 19 = 133    8 x 19 = 152    9 x 19 = 171   10 x 19 = 190

11 x  1 =  11   12 x  1 =  12   13 x  1 =  13   14 x  1 =  14   15 x  1 =  15
11 x  2 =  22   12 x  2 =  24   13 x  2 =  26   14 x  2 =  28   15 x  2 =  30
11 x  3 =  33   12 x  3 =  36   13 x  3 =  39   14 x  3 =  42   15 x  3 =  45
11 x  4 =  44   12 x  4 =  48   13 x  4 =  52   14 x  4 =  56   15 x  4 =  60
11 x  5 =  55   12 x  5 =  60   13 x  5 =  65   14 x  5 =  70   15 x  5 =  75
11 x  6 =  66   12 x  6 =  72   13 x  6 =  78   14 x  6 =  84   15 x  6 =  90
11 x  7 =  77   12 x  7 =  84   13 x  7 =  91   14 x  7 =  98   15 x  7 = 105
11 x  8 =  88   12 x  8 =  96   13 x  8 = 104   14 x  8 = 112   15 x  8 = 120
11 x  9 =  99   12 x  9 = 108   13 x  9 = 117   14 x  9 = 126   15 x  9 = 135
11 x 10 = 110   12 x 10 = 120   13 x 10 = 130   14 x 10 = 140   15 x 10 = 150
11 x 11 = 121   12 x 11 = 132   13 x 11 = 143   14 x 11 = 154   15 x 11 = 165
11 x 12 = 132   12 x 12 = 144   13 x 12 = 156   14 x 12 = 168   15 x 12 = 180
11 x 13 = 143   12 x 13 = 156   13 x 13 = 169   14 x 13 = 182   15 x 13 = 195
11 x 14 = 154   12 x 14 = 168   13 x 14 = 182   14 x 14 = 196   15 x 14 = 210
11 x 15 = 165   12 x 15 = 180   13 x 15 = 195   14 x 15 = 210   15 x 15 = 225
11 x 16 = 176   12 x 16 = 192   13 x 16 = 208   14 x 16 = 224   15 x 16 = 240
11 x 17 = 187   12 x 17 = 204   13 x 17 = 221   14 x 17 = 238   15 x 17 = 255
11 x 18 = 198   12 x 18 = 216   13 x 18 = 234   14 x 18 = 252   15 x 18 = 270
11 x 19 = 209   12 x 19 = 228   13 x 19 = 247   14 x 19 = 266   15 x 19 = 285

16 x  1 =  16   17 x  1 =  17   18 x  1 =  18   19 x  1 =  19
16 x  2 =  32   17 x  2 =  34   18 x  2 =  36   19 x  2 =  38
16 x  3 =  48   17 x  3 =  51   18 x  3 =  54   19 x  3 =  57
16 x  4 =  64   17 x  4 =  68   18 x  4 =  72   19 x  4 =  76
16 x  5 =  80   17 x  5 =  85   18 x  5 =  90   19 x  5 =  95
16 x  6 =  96   17 x  6 = 102   18 x  6 = 108   19 x  6 = 114
16 x  7 = 112   17 x  7 = 119   18 x  7 = 126   19 x  7 = 133
16 x  8 = 128   17 x  8 = 136   18 x  8 = 144   19 x  8 = 152
16 x  9 = 144   17 x  9 = 153   18 x  9 = 162   19 x  9 = 171
16 x 10 = 160   17 x 10 = 170   18 x 10 = 180   19 x 10 = 190
16 x 11 = 176   17 x 11 = 187   18 x 11 = 198   19 x 11 = 209
16 x 12 = 192   17 x 12 = 204   18 x 12 = 216   19 x 12 = 228
16 x 13 = 208   17 x 13 = 221   18 x 13 = 234   19 x 13 = 247
16 x 14 = 224   17 x 14 = 238   18 x 14 = 252   19 x 14 = 266
16 x 15 = 240   17 x 15 = 255   18 x 15 = 270   19 x 15 = 285
16 x 16 = 256   17 x 16 = 272   18 x 16 = 288   19 x 16 = 304
16 x 17 = 272   17 x 17 = 289   18 x 17 = 306   19 x 17 = 323
16 x 18 = 288   17 x 18 = 306   18 x 18 = 324   19 x 18 = 342
16 x 19 = 304   17 x 19 = 323   18 x 19 = 342   19 x 19 = 361



 

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
,

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


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