尝试写了一个高精度小数乘法,遇到了很奇怪的情况。代码里没有随机生成的部分,但是每次输出的结果都不同?完全不能理解,求大佬解答。以下是代码,用的Dev C++ 5.11
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int max_integer = 100, max_decimals = 100;
class Float
{
public:
//构造函数 0
Float() {}
//构造函数 1
Float(char input[])
{
int i = -1, j = -1;
bool deci_flag = false;
for (unsigned int k=0; k < strlen(input); k++)
{
if (input[k] == '.')
deci_flag = true;
else
{ if (!deci_flag && input[k] != '\0')
integer[++i] = input[k];
else if (j <= max_decimals - 3)
decimals[++j] = input[k];
}
}
}
//友元函数声明
friend ostream& operator << (ostream&, Float);
friend Float operator + (Float, Float);
friend Float operator * (Float, Float);
friend void operator * (Float&, char);
private:
char integer[max_integer] = {'0'};
char decimals[max_decimals] = {'0'};
};
//重载流插入运算符
ostream& operator << (ostream &output, Float input)
{
output<<input.integer<<'.'<<input.decimals;
return output;
}
//加法
Float operator + (Float input_1, Float input_2)
{
Float output;
char a2[max_decimals], b2[max_decimals];
strcpy(a2, input_1.decimals), strcpy(b2, input_2.decimals);
//小数加法
int calt;
bool jinwei_2[max(strlen(a2), strlen(b2)) + 1] = {false};
for (int i = max(strlen(a2), strlen(b2)) - 1; i>=0; i--)
{ if (a2[i] == '\0' || b2[i] == '\0')
a2[i] = a2[i] + b2[i];
else
{
calt = a2[i] + b2[i] - '0'*2 + jinwei_2[i+1];
a2[i] = calt % 10 + '0';
jinwei_2[i] = calt / 10;
}
}
//整数加法
char c[max_integer] = {'\0'};
char a1[max_integer], b1[max_integer];
if (strlen(input_1.integer) > strlen(input_2.integer))
{ strcpy(a1, input_1.integer);
for (unsigned int i=0; i < strlen(input_1.integer) - strlen(input_2.integer); i++)
c[i] = '0';
strcat(c, input_2.integer);
strcpy(b1, c);
}
else
{ strcpy(a1, input_2.integer);
for (unsigned int i=0; i < strlen(input_2.integer) - strlen(input_1.integer); i++)
c[i] = '0';
strcat(c, input_1.integer);
strcpy(b1, c);
}
bool jinwei_1[strlen(a1)+1] = {false};
jinwei_1[strlen(a1)] = jinwei_2[0];
for (int i = strlen(a1) - 1; i>=0; i--)
{ calt = a1[i] + b1[i] - '0'*2 + jinwei_1[i+1];
a1[i] = calt % 10 + '0';
jinwei_1[i] = calt / 10;
}
//赋值
strcpy(output.decimals, a2);
if (jinwei_1[0] == false)
strcpy(output.integer, a1);
else
{ output.integer[0] = '1';
strcat(output.integer, a1);
}
return output;
}
//单位乘法
void operator * (Float &input, char mult)
{
if (mult == '0')
{ Float zero;
input = zero;
return;
}
int calt;
int jinwei_2[strlen(input.decimals)+1] = {0};
for (int i = strlen(input.decimals) - 1; i>=0; i--)
{ calt = (input.decimals[i] - '0') * (mult - '0') + jinwei_2[i+1];
input.decimals[i] = calt % 10 + '0';
jinwei_2[i] = calt / 10;
}
int jinwei_1[strlen(input.integer) + 1] = {0};
jinwei_1[strlen(input.integer)] = jinwei_2[0];
char store[max_integer] = {'\0'};
for (int i = strlen(input.integer) - 1; i>=0; i--)
{ calt = (input.integer[i] - '0') * (mult - '0') + jinwei_1[i+1];
store[i] = calt % 10 + '0';
jinwei_1[i] = calt / 10;
}
if (jinwei_1[0] == 0)
strcpy(input.integer, store);
else
{ char trans[max_integer] = {char(jinwei_1[0] + '0')};
strcat(trans, store);
strcpy(input.integer, trans);
}
}
//乘法
Float operator * (Float input_1, Float input_2)
{
Float output;
//取出 input1的数
char connect[max_integer + max_decimals] = {'\0'};
strcpy(connect, input_1.integer);
strcat(connect, ".");
strcat(connect, input_1.decimals);
int point; char exchange;
char move[max_integer + max_decimals];
//整数部分乘法
for (int i = strlen(input_2.integer) - 1; i>=0; i--)
{ strcpy(move, connect);
point = strlen(input_1.integer);
for (unsigned int j=0; j < strlen(input_2.integer) - 1 - i; j++)
{ if (move[point + 1] == '\0')
move[point + 1] = '0';
exchange = move[point];
move[point] = move[point + 1];
move[point + 1] = exchange;
point++;
}
Float *add = new Float(move);
*add * input_2.integer[i];
output = output + *add;
delete add;
}
return output;
}
int main()
{
char n1[] = "0.1212", n2[] = "10";
Float a(n1), b(n2), c;
cout<<a*b<<'*';
return 0;
}