#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 6 //小数的位数(含小数点)
//将字符转化为数字
unsigned int change (char s[LENGTH], unsigned int s1[LENGTH - 1])
{
int ss[LENGTH - 1]; //ss 放未逆置的整数
memset (ss, 0, sizeof(ss));
int k = 0 ;
for (int i = 0; i < LENGTH && s[i]; ++i) //考虑特殊数据如:0.0001
{
if (s[i] != '.')
ss[k++] = s[i] - '0';
}
for (int j = 0;j < LENGTH - 1; j++)
{
s1[j] = ss[LENGTH - 2 - j];
}
int m = 0;
while ( (s[m] != '.') && s[m] )
++m;
return LENGTH - 1 - m; //小数点位数
}
//大数乘法运算
//函数返回 s2
void mu1 (unsigned int s1[LENGTH - 1],unsigned int s2[130])
{
int ss[130];
memset ( ss, 0, sizeof(ss) );
for ( int i = 0; i < LENGTH - 1; i++)
for (int j = 0;j < 130; j++) //难点:因为返回新的s2之后位数会增加 最多时 5* 25 = 125
ss [i + j] += s1[i] * s2[j];
//将 两个大数相乘得的积ss中进行进位处理后放到s2 中
int c = 0;
for (int i = 0;i < 130;i++)
{
s2[i] = (c + ss[i]) % 10;
c = (c + ss[i]) / 10;
}
}
int main()
{
int n;
char s[LENGTH]; //要处理的幂 R
unsigned int s1[LENGTH - 1]; //将 R 转化成数字
unsigned int s2[130];
while(scanf ("%s%d", s, &n) != EOF)
{
memset (s1, 0, sizeof (s1));
memset (s2, 0, sizeof (s2));
int j = change (s, s1); //得到小数点所在位置
change (s,s2); //得到s2 和 s1 进行幂运算
for ( int i = 1; i < n; i ++)
mu1 (s1,s2);
//在s2中前面的代表小数位,后面的代表整数位,
//所以关键是通过数值关系找到小数点的位置
//例:0.1010 * 0.1010 = 0.01020100
int m = 129;//去掉前导0
while ( (!s2[m]) && m)
--m;
int k = 0; //去掉尾0
while ( ( !s2[k] ) && (k < 130))
++k;
//输出整数位
for (int i = m; i >= n * j; i--)
printf ("%d",s2[i]);
//输出小数点
if ( j && n * j >= k + 1)
printf (".");
for (int i = n*j -1; i >= k; --i)
printf ("%d", s2[i]);
printf ("\n");
}
return 0;
// system ("pause");
}
posted on 2010-08-29 11:35
雪黛依梦 阅读(1887)
评论(0) 编辑 收藏 引用 所属分类:
大数