spoj 6 Simple Arithmetics
这道题很烦。。我的代码也很烦。。我想也没人愿意会看
下面是为我找错误提供方便的几个地方:
http://blog.csdn.net/tiaotiaoyly/archive/2008/02/11/2087975.aspx
他总结得很好。。但是对我帮助不大。。呵呵。。不过可以参考一下
开始几个错误还是自己的数据测出来的。。
我最致命的错误是横线的输出
横线的长度其实取决于上下两个数的长度的。。我一直以为是取决于最长数的长度
以下是代码。。刚好利用这个题熟悉了一下大数模板
#include
<
iostream
>
#define MaxN 3500
#define max(a,b) (a > b?a:b)
#define max3(a,b,c) (max(a,max(b,c)))
char str[MaxN],x[MaxN],y[MaxN];
struct HP
{
int len ,s[MaxN];
}a,b,c;
void StrHP( const char * s,HP & x)
{
x.len = ( int )strlen(s);
for ( int i = 1 ;i <= x.len;i ++ )
x.s[i] = s[x.len - i] - ' 0';
}
void Int2HP( int inte,HP & x)
{
if (!inte)
{
x.len = 1 ;
x.s[ 1 ] = 0 ;
return;
}
memset(x.s, 0 ,sizeof(x.s));
for (x.len = 0 ;inte > 0 ;)
{
x.s[ ++ x.len] = inte% 10 ;
inte /= 10 ;
}
}
void Multi( const HP a, const HP b,HP & c)
{
int i,j;
c.len = a.len + b.len;
for (i = 1 ;i <= c.len;i ++ )
c.s[i] = 0 ;
for (i = 1 ;i <= a.len;i ++ )
for (j = 1 ;j <= b.len;j ++ )
c.s[i + j - 1 ] += a.s[i] * b.s[j];
for (i = 1 ;i < c.len;i ++ )
{
c.s[i + 1 ] += c.s[i] / 10 ;
c.s[i]% = 10 ;
}
while (c.s[i])
{
c.s[i + 1 ] = c.s[i] / 10 ;
c.s[i]% = 10 ;
i ++ ;
}
while (i > 1 && !c.s[i])i -- ;
c.len = i;
}
void Plus( const HP a, const HP b,HP & c)
{
int i;
c.s[ 1 ] = 0 ;
for (i = 1 ;i <= a.len||i <= b.len||c.s[i];i ++ )
{
if (i <= a.len)
c.s[i] += a.s[i];
if (i <= b.len)
c.s[i] += b.s[i];
c.s[i + 1 ] = c.s[i] / 10 ;
c.s[i]% = 10 ;
}
c.len = i - 1 ;
if (c.len == 0 )
c.len = 1 ;
}
void Substract( const HP a, const HP b,HP & c)
{
for ( int i = 1 ,j = 0 ;i <= a.len;i ++ )
{
c.s[i] = a.s[i] - j;
if (i <= b.len)
c.s[i] -= b.s[i];
if (c.s[i] < 0 )
{
j = 1 ;
c.s[i] += 10 ;
}
else j = 0 ;
}
c.len = a.len;
while (c.len >= 1 && !c.s[c.len])c.len -- ;
if (!c.len)c.len = 1 ;
}
void PrintHP(HP & x)
{
for ( int i = x.len;i >= 1 ;i -- )
printf( " %d " ,x.s[i]);
}
int main()
{
// freopen( " 1.out " , " w " ,stdout);
int T, len ,len2,i,j;
char * ptr;
char oper;
HP tmp,tmp2;
scanf( " %d " , & T);
while (T -- )
{
len2 = len =- 1 ;
scanf( " %s " ,str);
for (ptr = x,i = j = 0 ;str[i];i ++ )
{
if (str[i] >= ' 0'&&str[i]<='9')
ptr[j ++ ] = str[i];
else
{
oper = str[i];
ptr[j] = ' \0';
j = 0 ;
ptr = y;
}
}
ptr[j] = ' \0';
StrHP(x,a);
StrHP(y,b);
switch(oper)
{
case ' +':
case ' -':
if (oper == ' +')Plus(a,b,c);
else Substract(a,b,c);
len = max3(a.len,b.len + 1 ,c.len); // 带个符号
len2 = max(b.len + 1 ,c.len);
for (i = 0 ;i < len - a.len;i ++ )
putchar( ' ');
PrintHP(a); // 不带回车的print
putchar( ' \n');
for (i = 0 ;i < len - b.len - 1 ;i ++ )
putchar( ' ');
putchar(oper);
PrintHP(b);
putchar( ' \n');
for (i = 0 ;i < len ;i ++ )
i < len - len2?putchar( ' '):putchar('-');
putchar( ' \n');
for (i = 0 ;i < len - c.len;i ++ )
putchar( ' ');
PrintHP(c);
putchar( ' \n');
break;
case ' *':
Multi(a,b,c);
len = max3(a.len,b.len + 1 ,c.len);
for (i = 0 ;i < len - a.len;i ++ )
putchar( ' ');
PrintHP(a);
putchar( ' \n');
for (i = 0 ;i < len - b.len - 1 ;i ++ )
putchar( ' ');
putchar( ' *');
PrintHP(b);
putchar( ' \n');
Int2HP(b.s[ 1 ],tmp);
Multi(a,tmp,tmp2);
len2 = max(b.len + 1 ,tmp2.len);
for (i = 0 ;i < len ;i ++ )
i < len - len2?putchar( ' '):putchar('-');
putchar( ' \n');
for (i = 0 ;i < len - tmp2.len;i ++ )
putchar( ' ');
PrintHP(tmp2);
putchar( ' \n');
for (i = 2 ;i <= b.len;i ++ )
{
Int2HP(b.s[i],tmp);
Multi(a,tmp,tmp2);
for (j = 0 ;j < len - tmp2.len - i + 1 ;j ++ )
putchar( ' ');
PrintHP(tmp2);
putchar( ' \n');
}
if (b.len > 1 )
{
len2 = max(tmp.len + b.len - 1 ,c.len);
for (i = 0 ;i < len ;i ++ )
i < len - len2?putchar( ' '):putchar('-');
putchar( ' \n');
for (i = 0 ;i < len - c.len;i ++ )
putchar( ' ');
PrintHP(c);
putchar( ' \n');
}
break;
}
putchar( ' \n');
}
return 0 ;
}
#define MaxN 3500
#define max(a,b) (a > b?a:b)
#define max3(a,b,c) (max(a,max(b,c)))
char str[MaxN],x[MaxN],y[MaxN];
struct HP
{
int len ,s[MaxN];
}a,b,c;
void StrHP( const char * s,HP & x)
{
x.len = ( int )strlen(s);
for ( int i = 1 ;i <= x.len;i ++ )
x.s[i] = s[x.len - i] - ' 0';
}
void Int2HP( int inte,HP & x)
{
if (!inte)
{
x.len = 1 ;
x.s[ 1 ] = 0 ;
return;
}
memset(x.s, 0 ,sizeof(x.s));
for (x.len = 0 ;inte > 0 ;)
{
x.s[ ++ x.len] = inte% 10 ;
inte /= 10 ;
}
}
void Multi( const HP a, const HP b,HP & c)
{
int i,j;
c.len = a.len + b.len;
for (i = 1 ;i <= c.len;i ++ )
c.s[i] = 0 ;
for (i = 1 ;i <= a.len;i ++ )
for (j = 1 ;j <= b.len;j ++ )
c.s[i + j - 1 ] += a.s[i] * b.s[j];
for (i = 1 ;i < c.len;i ++ )
{
c.s[i + 1 ] += c.s[i] / 10 ;
c.s[i]% = 10 ;
}
while (c.s[i])
{
c.s[i + 1 ] = c.s[i] / 10 ;
c.s[i]% = 10 ;
i ++ ;
}
while (i > 1 && !c.s[i])i -- ;
c.len = i;
}
void Plus( const HP a, const HP b,HP & c)
{
int i;
c.s[ 1 ] = 0 ;
for (i = 1 ;i <= a.len||i <= b.len||c.s[i];i ++ )
{
if (i <= a.len)
c.s[i] += a.s[i];
if (i <= b.len)
c.s[i] += b.s[i];
c.s[i + 1 ] = c.s[i] / 10 ;
c.s[i]% = 10 ;
}
c.len = i - 1 ;
if (c.len == 0 )
c.len = 1 ;
}
void Substract( const HP a, const HP b,HP & c)
{
for ( int i = 1 ,j = 0 ;i <= a.len;i ++ )
{
c.s[i] = a.s[i] - j;
if (i <= b.len)
c.s[i] -= b.s[i];
if (c.s[i] < 0 )
{
j = 1 ;
c.s[i] += 10 ;
}
else j = 0 ;
}
c.len = a.len;
while (c.len >= 1 && !c.s[c.len])c.len -- ;
if (!c.len)c.len = 1 ;
}
void PrintHP(HP & x)
{
for ( int i = x.len;i >= 1 ;i -- )
printf( " %d " ,x.s[i]);
}
int main()
{
// freopen( " 1.out " , " w " ,stdout);
int T, len ,len2,i,j;
char * ptr;
char oper;
HP tmp,tmp2;
scanf( " %d " , & T);
while (T -- )
{
len2 = len =- 1 ;
scanf( " %s " ,str);
for (ptr = x,i = j = 0 ;str[i];i ++ )
{
if (str[i] >= ' 0'&&str[i]<='9')
ptr[j ++ ] = str[i];
else
{
oper = str[i];
ptr[j] = ' \0';
j = 0 ;
ptr = y;
}
}
ptr[j] = ' \0';
StrHP(x,a);
StrHP(y,b);
switch(oper)
{
case ' +':
case ' -':
if (oper == ' +')Plus(a,b,c);
else Substract(a,b,c);
len = max3(a.len,b.len + 1 ,c.len); // 带个符号
len2 = max(b.len + 1 ,c.len);
for (i = 0 ;i < len - a.len;i ++ )
putchar( ' ');
PrintHP(a); // 不带回车的print
putchar( ' \n');
for (i = 0 ;i < len - b.len - 1 ;i ++ )
putchar( ' ');
putchar(oper);
PrintHP(b);
putchar( ' \n');
for (i = 0 ;i < len ;i ++ )
i < len - len2?putchar( ' '):putchar('-');
putchar( ' \n');
for (i = 0 ;i < len - c.len;i ++ )
putchar( ' ');
PrintHP(c);
putchar( ' \n');
break;
case ' *':
Multi(a,b,c);
len = max3(a.len,b.len + 1 ,c.len);
for (i = 0 ;i < len - a.len;i ++ )
putchar( ' ');
PrintHP(a);
putchar( ' \n');
for (i = 0 ;i < len - b.len - 1 ;i ++ )
putchar( ' ');
putchar( ' *');
PrintHP(b);
putchar( ' \n');
Int2HP(b.s[ 1 ],tmp);
Multi(a,tmp,tmp2);
len2 = max(b.len + 1 ,tmp2.len);
for (i = 0 ;i < len ;i ++ )
i < len - len2?putchar( ' '):putchar('-');
putchar( ' \n');
for (i = 0 ;i < len - tmp2.len;i ++ )
putchar( ' ');
PrintHP(tmp2);
putchar( ' \n');
for (i = 2 ;i <= b.len;i ++ )
{
Int2HP(b.s[i],tmp);
Multi(a,tmp,tmp2);
for (j = 0 ;j < len - tmp2.len - i + 1 ;j ++ )
putchar( ' ');
PrintHP(tmp2);
putchar( ' \n');
}
if (b.len > 1 )
{
len2 = max(tmp.len + b.len - 1 ,c.len);
for (i = 0 ;i < len ;i ++ )
i < len - len2?putchar( ' '):putchar('-');
putchar( ' \n');
for (i = 0 ;i < len - c.len;i ++ )
putchar( ' ');
PrintHP(c);
putchar( ' \n');
}
break;
}
putchar( ' \n');
}
return 0 ;
}