/*
    第2種情報処理技術者試験    平成12年度・春期・午後 問11〔設問1・設問2〕
                                BohYoh Shibata PREPARATION
*/

#include <string.h>
#define  STRMAX  128

void decmult(char decA[], char decB[], char decC[])
{
    char decT[STRMAX], sign='+';
    int ai, bi, ci=0, ti=0, al=0, bl=0, am, bm, tm;
    int carry=0, shift=0, wk;

    memset(decT, '\0', STRMAX);  /* decT[]を '\0'で初期化する */
    am = strlen(decA);
    bm = strlen(decB);

    /*** 符号の検査 ***/
    if (decA[0] == '+' || decA[0] == '-')       al = 1;
    if (decB[0] == '+' || decB[0] == '-')       bl = 1;
    if ((decA[0] == '-' && decB[0] != '-') ||
        (decA[0] != '-' && decB[0] == '-'))     sign = '-';

    /*** 乗算 ***/
    for (bi = bm - 1; bi >= bl; bi--) {
        for (ai = am - 1; ai >= al; ai--, ti++) {
            wk = (decA[ai] - '0') * (decB[bi] - '0') + carry;
            carry = wk / 10;
            wk %= 10;
            if (decT[ti] != '\0') {  /* 前の演算結果があるか? */
                wk += (decT[ti] - '0');
                if (wk > 9) {
                    carry++;
                    wk %= 10;
                }
            }
            decT[ti] = wk + '0';
        }
        if (carry > 0) {
            decT[ti] = carry + '0';
            carry = 0;
        }
        shift++;
        ti = shift;
    }

    /*** decC[] に格納 ***/
    if (sign == '-') {
        decC[0] = sign;
        ci++;
    }
    tm = strlen(decT);
    for (ti = tm - 1; ti > 0; ti--)
        if (decT[ti] != '0')
            break;
    for ( ; ti >= 0; ti--, ci++)
        decC[ci] = decT[ti];
    decC[ci] = '\0';
}