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

/*
 * Filename: MakeMultTableApp.cs
 *
 *     Print a multiplication table.
 *
 *     Compile: csc MakeMultTableApp.cs
 *     Execute: MakeMultTableApp 230 5100
 *
 * Date: 2009/03/07
 * Author: pkim (AT) scripts.pe.kr
 */

using System;

namespace MyTestApplication1 { 

    public class MakeMultTableApp {

        public static void PrintUsing() {
            Console.WriteLine("Using: MakeMultTableApp [number1] [number2]");
            Console.WriteLine("Print a multiplication table for the given two integers.");
        }

        public static void PrintMultTable(long x, long y) {
            long nx = (x >= 0) ? x : - x;
            long ny = (y >= 0) ? y : - y;
            int ntail1 = 0;
            int ntail2 = 0;
            while (nx % 10 == 0) {
                nx = nx / 10;
                ntail1++;
            }
            while (ny % 10 == 0) {
                ny = ny / 10;
                ntail2++;
            }
            long z = nx * ny;
            String strZ = "" + z;
            String strX = "" + nx;
            String strY = "" + ny;
            int n = strY.Length;
            String zeros  = "0000000000000000000000000000000000000000";
            String whites = "                                        ";
            String bars   = "----------------------------------------";
            String line1, line2, line3, line4;
            String loffset = "       ";
            line4 = loffset + strZ;
            line1 = loffset + whites.Substring(0, strZ.Length - strX.Length) + strX;
            line2 = "   x ) " +  whites.Substring(0, strZ.Length- strY.Length) + strY;
            line3 = "     --" +  bars.Substring(0, strZ.Length);
            Console.WriteLine(line1 + zeros.Substring(0, ntail1));
            Console.WriteLine(line2 + zeros.Substring(0, ntail2));
            Console.WriteLine(line3);
            if (strY.Length > 1) {
                int y1;
                String strT;
                for (int i = 0; i < strY.Length; i++) {
                    y1 = Convert.ToInt32(strY.Substring(strY.Length - i - 1, 1));
                    if (y1 != 0) {
                        strT = "" + (nx * y1);
                        Console.WriteLine(loffset + whites.Substring(0, strZ.Length - strT.Length - i) + strT);
                    }
                }
                Console.WriteLine(line3);
            }
            Console.WriteLine(line4 + zeros.Substring(0, ntail1) + zeros.Substring(0, ntail2));
        }

        public static void Main(String[] args) {
            long x, y;
            if (args.Length >= 2) {
                x = Convert.ToInt64(args[0]);
                y = Convert.ToInt64(args[1]);
                Console.WriteLine("");
                PrintMultTable(x, y);
            }
            else {
                PrintUsing();
            }
        }
    }
}



 

컴파일> csc MakeMultTableApp.cs
실행> MakeMultTableApp 230 5100
결과>

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

 

Posted by Scripter
,