高精度浮点数的乘法,用模拟的方法可以得出结果,用数组来保存每一位,每位乘以目标乘数得到中间结果,把中间结果相加可得一次乘法结果,循环多次就得到了结果。
在本题中需要注意的是要把数据前后无用的零清理掉,既可以增加计算速度,也可规范最后的显示格式。
//poj1001
//240K 0MS
//求解高精度问题
//解题思路:
// 1.将最终结果和中间过程作为字符串数组进行存储
// 2.翻转所求数存储于结果字符数组ReverseResult[300]中
// 3.如果幂运算次数大于0,将ReverseResult和所求数相乘,幂运算次数减一;否则跳至6步
// 4.每次都将中间结果ReverseResult的一位和所求数相乘,存储在中间结果数组TempResoult[k]中
// 5.将各个中间结果数组依次降位并累加,存储结果于ReverseResult[300]中
// 6.翻转ReverseResult,将结果存储于Result数组中,输出结果
//
//---------------------------------------------------------------------------------
#include <iostream>
using namespace std;
char putin[6] = {0};
char FinalResult[300] = {0}; //最终结果
long int tempResult[300] = {0}; //存储每一位的相乘结果
void ReverseChar(char* Source,char* Result, int poingLoc)
{
//将用作存储中间结果的数组翻转为结果数组并在对应位置加上小数点,参数三为总的小数个数
int ResultLoc = 0;
int signZero = 0;
int i =0;
int signInteger = 0;
int souLength = strlen(Source);
//cout<<"strlen source is: "<<souLength<<endl;
int ZeroNum = 0;
if (poingLoc == 0)
{
for (int j= 0; j< souLength; j++)
{
Result[j] = Source[souLength - j - 1];
}
}
else if (poingLoc > souLength)
{
Result[ZeroNum++] = '.';
for (int j = 0; j< poingLoc - souLength; j++)
{
Result[ZeroNum++] = '0';
}
while (ResultLoc < poingLoc && Source[ResultLoc] == '0')
{
ResultLoc++;
}
for ( i = 0; i<(souLength-ResultLoc); i++)
{
Result[ZeroNum++] = Source[souLength-i-1];
}
}
else if (poingLoc <= souLength)
{
while (ResultLoc < poingLoc && Source[ResultLoc] == '0')
{
ResultLoc++;
}
for ( i = 0; i<(souLength-ResultLoc); i++)
{
if (i == (souLength - poingLoc))
{
Result[ZeroNum++] = '.';
}
Result[ZeroNum++] = Source[souLength-i-1];
}
}
}
void MULmultiply(char* PreResult, long int number)
{
//number为输入的小数的整数形式,preResult为中间结果的反转形式
int tempLength = strlen(PreResult);
long int NextResult = 0;
for (int i=0; i<tempLength; i++)
{
//每次将乘法所得的一次结果于上次的结果相加,并留下最后一位加入preResult中,隐含一次加权的效果
int thisNum = PreResult[i] -'0' ;
tempResult[i] = thisNum * number;
NextResult += tempResult[i];
PreResult[i] =(NextResult % 10) + '0';
NextResult /= 10;
}
while(NextResult != 0)
{
PreResult[tempLength] = (NextResult % 10) + '0';
tempLength++;
NextResult /= 10;
}
}
int main()
{
int mulTimes = 0; //幂运算次数
while(cin>>putin>>mulTimes)
{
char ReverseResult[300] = {0}; //反向结果
long int intPutin = 0;
int PointLoc = 0; //记录小数位数
long int floatToint = 0; //存储输入数据转换后的整数
for (int i=0; i<6;i++) //寻找输入数中的小数个数,并将其转换为一个整数一方便运算
{
if(putin[i] == '.')
PointLoc = 5-i;
else
floatToint = (putin[i]-'0') + floatToint*10;
}
int nLastZero =PointLoc;
intPutin = floatToint;
int k = 0;
int superSign = 0;
while (floatToint != 0)
{ //将转换后的整数转换为字符串并翻转
int charNum = floatToint % 10;
if (PointLoc == 0) //表示此时为整数
{
superSign = 1;
}
if (charNum != 0 || superSign == 1)
{
ReverseResult[k] = '0' + charNum;
k++;
floatToint /= 10;
superSign = 1;
continue;
}
else if (charNum == 0 && superSign == 0)
{
PointLoc--;
floatToint /= 10;
intPutin = floatToint;
}
}
memset(FinalResult, 0, sizeof(FinalResult));
memset(tempResult, 0, sizeof(tempResult));
for (int i = 1; i<mulTimes; i++)
{
MULmultiply(ReverseResult ,intPutin ); //进行多次乘法运算,达到幂运算的效果
}
int pointTotal = PointLoc * mulTimes;
int RightLoc = strlen(ReverseResult) - pointTotal;
ReverseChar(ReverseResult, FinalResult, pointTotal); //将最终结果翻转回来
int sign = 0;
for (int j=0; j<strlen(FinalResult); j++)
{
cout<<FinalResult[j];
}
cout<<endl;
}
return 1;
//system("pause");
}