다음은 초등학교에서 배우는 나눗셈 계산표를 만들어주는 C 소스 코드이다.
나눗셈 계산표를 완성하고 나서 약수, 배수 관계를 알려준다.


  1. /*
  2.  *  Filename: makeDivisionTable.c
  3.  *
  4.  *  Purpose:  Make a division table in a handy written form.
  5.  *
  6.  *  Compile: cl makeDivisionTable.c
  7.  *
  8.  *  Execute: makeDivisionTable 12345 32
  9.  *           makeDivisionTable 500210 61
  10.  *
  11.  *     Date:  2008/05/15
  12.  *   Author:  PH Kim   [ pkim ((AT)) scripts.pe.kr ]
  13.  */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <math.h>
  17. #include <memory.h>
  18. enum { FALSE, TRUE };
  19. void println(char s[]) {
  20.     printf("%s\n", s);
  21. }
  22. void print(char s[]) {
  23.     printf("%s", s);
  24. }
  25. void printUsage() {
  26.     // println("Using: makeDivisionTable [numerator] [denominator]");
  27.     // println("Make a division table in a handy written form.");
  28.     println("사용법: makeDivisionTable [피제수] [제수]");
  29.     println("손으로 작성한 형태의 나눗셈 표를 만들어준다.");
  30. }
  31. // 부동소수점수의 표현이 .0 으로 끝나는 경우 이를 잘라낸다.
  32. // 전체 문자열 표시 너비는 매개변수 width 로 전달받아 처리한다.
  33. char *simplify(double v, int width) {
  34.     int n, len;
  35.     static char t[] = "                               ";
  36.     char tmp[] = "                               ";
  37.     sprintf(t, "%g", v);
  38.     n = strlen(t);
  39.     if ((n > 2) && (t[n - 2] == '.') && (t[n - 1] == '0'))
  40.         t[n-2] = '\0';
  41.     len = strlen(t);
  42.     strcpy(tmp, t);
  43.     if (len < width) {
  44.         strncpy(t, "               ", width - len);
  45.         t[width - len] = '\0';
  46.         strcat(t, tmp);
  47.     }
  48.     return (char *) t;
  49. }
  50. // 발음상 숫자에 맞는 주어 조사 "은", "는"을 결정한다.
  51. void getSuffix(char *buf, long v) {
  52.     long t = v % 10L;
  53.     char suffix[3];
  54.     char tbuf[2];
  55.     char suf[2][3] = { "은",  "는"};
  56.     strcpy(suffix, suf[0]);
  57.     sprintf(tbuf, "%d", t);
  58.     if (strchr("2459", tbuf[0]) != NULL) {
  59.         strcpy(suffix, suf[1]);
  60.     }
  61.     strcpy(buf, suffix);
  62. }
  63. long makeTable(long numer, long denom, long quotient) {
  64.     char sbuf[81];
  65.     char strNumer[40];
  66.     char strDenom[40];
  67.     char strQuotient[40];
  68.     char strTmpR[40];
  69.     char whiteChars[] = "                                                                                 ";
  70.     char underbarChars[]  = "_________________________________________________________________________________";
  71.     char barChars[]  = "---------------------------------------------------------------------------------";
  72.     char offsetSpaces[81];
  73.     char biasSpaces[81];
  74.     char spaces[81];
  75.     char uline[81];
  76.     char sline[81];
  77.     int i;
  78.     int lenN;
  79.     int lenD;
  80.     int lenQ;
  81.     int offsetLeft;
  82.     int bias;
  83.     long tmpR;
  84.     long tmpSub;
  85.     char oneDigit;
  86.     sprintf(strNumer, "%ld", numer);
  87.     sprintf(strDenom, "%ld", denom);
  88.     sprintf(strQuotient, "%ld", quotient);
  89.     lenN = strlen(strNumer);
  90.     lenD = strlen(strDenom);
  91.     lenQ = strlen(strQuotient);
  92.     offsetLeft = 3 + lenD + 3;
  93.     bias = lenN - lenQ;
  94.     strncpy(offsetSpaces, whiteChars, offsetLeft);
  95.     offsetSpaces[offsetLeft] = '\0';
  96.     strncpy(biasSpaces, whiteChars, bias);
  97.     biasSpaces[bias] = '\0';
  98.     strncpy(uline, underbarChars, lenN + 2);
  99.     uline[lenN + 2] = '\0';
  100.     strncpy(sline, barChars, lenN);
  101.     sline[lenN] = '\0';
  102.     printf("%s%s%ld\n", offsetSpaces, biasSpaces, quotient);
  103.     sprintf(sbuf, offsetSpaces);
  104.     sbuf[offsetLeft - 2] = '\0';
  105.     printf("%s%s\n", sbuf, uline);
  106.     printf("   %s ) %s", strDenom, strNumer);
  107.     strncpy(strTmpR, strNumer, bias + 1);
  108.     strTmpR[bias + 1] = '\0';
  109.     tmpR = atoi(strTmpR);
  110.     tmpSub = 0L;
  111.     for (i = 0; i < lenQ; i++) {
  112.         if (strQuotient[i] == '0') {
  113.             if (i + 1 < lenQ) {
  114.                 oneDigit = strNumer[bias + i + 1];
  115.                 printf("%c", oneDigit);
  116.                 sprintf(sbuf, "%c", oneDigit);
  117.                 strcat(strTmpR, sbuf);
  118.                 tmpR = atoi(strTmpR);
  119.             }
  120.         }
  121.         else {
  122.             println("");
  123.             tmpSub = (long)(strQuotient[i] - '0') * denom;
  124.             printf("%s%s\n", offsetSpaces, simplify(tmpSub, bias + i + 1));
  125.             printf("%s%s\n", offsetSpaces, sline);
  126.             tmpR = tmpR - tmpSub;
  127.             if (tmpR == 0L && i + 1 < lenQ) {
  128.                 strncpy(sbuf, whiteChars, bias + i + 1);
  129.                 sbuf[bias + i + 1] = '\0';
  130.                 printf("%s%s", offsetSpaces, sbuf);
  131.             }
  132.             else {
  133.                 printf("%s%s", offsetSpaces, simplify(tmpR, bias + i + 1));
  134.             }
  135.             sprintf(strTmpR, "%ld", tmpR);
  136.             if (i + 1 < lenQ) {
  137.                 oneDigit = strNumer[bias + i + 1];
  138.                 printf("%c", oneDigit);
  139.                 sprintf(sbuf, "%c", oneDigit);
  140.                 strcat(strTmpR, sbuf);
  141.                 tmpR = atoi(strTmpR);
  142.             }
  143.         }
  144.     }
  145.     println("");
  146.     return tmpR;
  147. }
  148. // C/C++/Ch 언어의 실행 시작 지점
  149. int main(int argc, char *argv[]) {
  150.     int i;
  151.     int SIZE = argc - 2;
  152.     long a, b, q, r, k;
  153.     char mbuf[3];
  154.     if (argc < 3) {
  155.         printUsage();
  156.         exit(1);
  157.     }
  158.     a = atoi(argv[1]);
  159.     b = atoi(argv[2]);
  160.     if (a <= 0L) {
  161.         printf("피제수: %ld\n", a);
  162.         println("피제수는 양의 정수라야 합니다.");
  163.         exit(1);
  164.     }
  165.     if (b <= 0L) {
             printf("제수: %ld\n", b);
  166.          println("제수는 양의 정수라야 합니다.");
  167.          exit(1);
  168.     }
  169.     q = a / b;
  170.     r = a % b;
  171.     printf("나눗셈 %ld ÷ %ld 의 결과: ", a, b);
  172.     printf("몫: %ld, ", q);
  173.     printf("나머지: %ld\n", r);
  174.     println("");
  175.     k = makeTable(a, b, q);
  176.     if (k == r) {
  177.         printf("\n나머지: %ld\n", k);
  178.     }
  179.     if (k == 0L) {
  180.         printf("%ld = %ld x %ld\n", a, b, q);
  181.         getSuffix(mbuf, a);
  182.         printf("%ld%s %ld의 배수(mupltiple)이다.\n", a, mbuf, b);
  183.         getSuffix(mbuf, b);
  184.         printf("%ld%s %ld의 약수(divisor)이다.\n", b, mbuf, a);
  185.     }
  186.     else {
  187.         getSuffix(mbuf, a);
  188.         printf("%ld = %ld x %ld + %ld\n", a, b, q, r);
  189.         printf("%ld%s %ld의 배수(mupltiple)가 아니다.\n", a, mbuf, b);
  190.     }
  191.     return 0;
  192. }




실행> makeDivisionTable 500210 61

나눗셈 500210 ÷ 61 의 결과: 몫: 8200, 나머지: 10

          8200
      ________
   61 ) 500210
        488
        ------
         122
         122
        ------
            10

나머지: 10
500210 = 61 x 8200 + 10
500210은 61의 배수(mupltiple)가 아니다.



 

Posted by Scripter
,