/*
第2種情報処理技術者試験 平成12年度・春期・午後 問11〔設問3〕
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;
int ap, bp, tp;
memset(decT, '\0', STRMAX); /* decT[]を '\0'で初期化する */
am = ap = strlen(decA);
bm = bp = 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--) {
if (decB[bi] == '.') {
bp = bi + 1;
bi--;
}
for (ai = am - 1; ai >= al; ai--, ti++) {
if (decA[ai] == '.') {
ap = ai + 1;
ai--;
}
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);
tp = (am - ap) + (bm - bp) - 1;
for (ti = tm - 1; ti > tp + 1; ti--)
if (decT[ti] != '0')
break;
for ( ; ti >= 0; ti--, ci++) {
if (ti == tp)
decC[ci++] = '.';
decC[ci] = decT[ti];
}
decC[ci] = '\0';
}