초등학교 때 배우는 두 정수의 곱셈표를 만들어 주는 C++ 소스이다.

/*
 * Filename: makeMultTableCPP.cpp
 *
 *     Print a multiplication table.
 *
 *     Compile: cl /EHsc makeMultTableCPP.cpp
 *     Execute: makeMultTableCPP 230 5100
 *
 * Date: 2009/03/07
 * Author: pkim (AT) scripts.pe.kr
 */

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void printUsing();
void printMultTable(int x, int y);

int main(int argc, char *argv[]) {
    long x, y;
    if (argc >= 3) {
        x = atoi(argv[1]);
        y = atoi(argv[2]);
        cout << endl;
        printMultTable(x, y);
    }
    else {
        printUsing();
    }
}

void printUsing() {
    cout << "Using: makeMultTable [number1] [number2]" << endl;
    cout << "Print a multiplication table for the given two integers." << endl;
}

void printMultTable(int x, int y) {
    char strX[80];
    char strY[80];
    char strZ[80];
    char strT[80];
    char zeros[80];
    char whites[80];
    char bars[80];
    char line1[80];
    char line2[80];
    char line3[80];
    char line4[80];
    char loffset[20];
    char buf[80];

    long nx = (x >= 0) ? x : - x;
    long ny = (y >= 0) ? y : - y;
    int ntail1 = 0;
    int ntail2 = 0;
    while (nx % 10L == 0L) {
        nx = nx / 10L;
        ntail1++;
    }
    while (ny % 10L == 0L) {
        ny = ny / 10L;
        ntail2++;
    }
    long z = nx * ny;
    sprintf(strZ, "%ld", z);
    sprintf(strX, "%ld", nx);
    sprintf(strY, "%ld", ny);
    int n = strlen(strY);
    strcpy(zeros, "0000000000000000000000000000000000000000");
    strcpy(whites, "                                        ");
    strcpy(bars, "----------------------------------------");
    strcpy(loffset, "       ");
    sprintf(line4, "%s%ld", loffset, nx*ny);
    strncpy(buf, zeros, ntail1 + ntail2);
    buf[ntail1 + ntail2] = '\0';
    strcat(line4, buf);
    strcpy(line1, loffset);
    strncpy(buf, whites, strlen(strZ) - strlen(strX));
    buf[strlen(strZ) - strlen(strX)] = '\0';
    strcat(buf, strX);
    strcat(line1, buf);
    strncpy(buf, zeros, ntail1);
    buf[ntail1] = '\0';
    strcat(line1, buf);

    strcpy(line2, "   x ) ");
    strncpy(buf, whites, strlen(strZ) - strlen(strY));
    buf[strlen(strZ) - strlen(strY)] = '\0';
    strcat(buf, strY);
    strcat(line2, buf);
    strncpy(buf, zeros, ntail2);
    buf[ntail2] = '\0';
    strcat(line2, buf);

    strcpy(line3, "     --");
    strncpy(buf, bars, strlen(strZ));
    buf[strlen(strZ)] = '\0';
    strcat(line3, buf);

    cout << line1 << endl;
    cout << line2 << endl;
    cout << line3 << endl;
    if (strlen(strY) > 1) {
        for (int i = 0; i < strlen(strY); i++) {
         buf[0] = strY[strlen(strY) - i - 1];
         buf[1] = '\0';
            int y1 = atoi(buf);
            if (y1 != 0) {
                sprintf(strT, "%ld", nx * y1);
                strcpy(line2, loffset);
                strncpy(buf, whites, strlen(strZ) - strlen(strT) - i);
                buf[strlen(strZ) - strlen(strT) - i] = '\0';
                strcat(buf, strT);
                strcat(line2, buf);
                cout << line2 << endl;
            }
        }
        cout << line3 << endl;
    }
    cout << line4 << endl;
}





컴파일> cl /EHsc makeMultTableCPP.cpp
실행> makeMultTableCPP 230 5100
결과>

          230
   x )   5100
     ------
         23
       115
     ------
       1173000

 

크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,