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

/* * Filename: makeMultTableMain.m * * Print a multiplication table. * * Compile: Click Ctrl+F11 on Dev-C++ IDE * Execute: makeMultTable 230 5100 * * Date: 2012/05/01 * Author: pkim (AT) scripts.pe.kr */ #import <Foundation/Foundation.h> #import <stdio.h> #import <string.h> #import <math.h> void printUsing(); void printMultTable(long x, long y); int main(int argc, const char *argv[]) { long x, y; if (argc >= 3) { x = atoi(argv[1]); y = atoi(argv[2]); printf("\n"); printMultTable(x, y); } else { printUsing(); } return 0; } void printUsing() { // printf("Using: makeMultTable [number1] [number2]\n"); // printf("Print a multiplication table for the given two integers.\n"); printf("사용법: makeMultTable [자연수1] [자연수2]\n"); printf("(손으로 계산하는) 곱셈 계산표를 출력해 줍니다.\n"); } void printMultTable(long x, long y) { long nx, ny; long z; int ntail1; int ntail2; int n; int y1; int i; 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]; nx = (x >= 0) ? x : - x; ny = (y >= 0) ? y : - y; ntail1 = 0; ntail2 = 0; while (nx % 10L == 0L) { nx = nx / 10L; ntail1++; } while (ny % 10L == 0L) { ny = ny / 10L; ntail2++; } z = nx * ny; sprintf(strZ, "%ld", z); sprintf(strX, "%ld", nx); sprintf(strY, "%ld", ny); 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); printf("%s\n", line1); printf("%s\n", line2); printf("%s\n", line3); if (strlen(strY) > 1) { for (i = 0; i < strlen(strY); i++) { buf[0] = strY[strlen(strY) - i - 1]; buf[1] = '\0'; 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); printf("%s\n", line2); } } printf("%s\n", line3); } printf("%s\n", line4); }




컴파일은 Dev-C++ 개발도구에서 Ctrl+F11 클릭

실행> makeMultTable 230 5100
결과>

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

 

Posted by Scripter
,