编程之美读书笔记2.6—精确表达浮点数

来源:互联网 发布:excel2013数据验证 编辑:程序博客网 时间:2024/04/29 19:22

解答一:

用于小整数,将无限循环小数0.a1a2...an(b1b2...bm)分为非循环部分和循环部分。

X=((a1a2...an)*(10^m-1)+b1b2...bm)/((10^m-1)*10^n)


#include <iostream>using namespace std;#include <iostream>  using namespace std;  long long  gcd(long long  x,long long y)  //最大公约数{if(x<y)return gcd(y,x);if(y==0)return x;else{if(x%2==0)   //偶数{if(y%2==0){                return (gcd(x>>1,y>>1)<<1);}else{    return gcd(x>>1,y);}        }else{if(y%2==0){return  gcd(x,y>>1);}else{return gcd(y,x-y);}}    }} int main(){long long c=11,a=613, b=7777; // 整数部分c,非循环小数a,循环小数b  if (a==0 && b==0)    //纯整数cout << c;else{long long up=1;long long down=1;long long temp_a=a;while(temp_a){down*=10;temp_a/=10;}up=down*c+a;if(b!=0){long long m = 1;        long long temp_b=b;    while(temp_b)   {    m*=10;    temp_b/=10;   }   up=up*(m-1)+b;   down=down*(m-1);}long long fac = gcd(up, down);cout << up/fac << "/" << down/fac << endl;}}


解答二:(未仔细看)

用于大整数,定义了大整数类型,以及对应的加减乘除、比较移位运算。

#include <iostream>
#include <cstring>
#include <string>
using namespace std;


// 大整数类型
#define MAXLEN 1000
struct HP {int len, s[MAXLEN];};


void PrintHP(HP x) 
{
for (int i=x.len; i>=1; i--)
cout << x.s[i];
}


// 字符串转大整数
void Str2HP(const char *s, HP &x)
{
x.len = strlen(s);
for (int i=1; i<=x.len; i++)
x.s[i] = s[x.len-i] - '0';
if (x.len == 0)
{
x.len = 1;
x.s[1] = 0;
}
}


// 大整数的加法
void Plus(const HP a, const HP b, HP &c)
{
int i; c.s[1] = 0;
// 大整数a,b的加法操作和结果c的进位操作
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.s[i]==0,所以取前一位
c.len = i-1; 
if (c.len == 0) c.len = 1;
}


// 大整数的减法
void Subtract(const HP a, const HP b, HP &c)
{
int i, j;
for (i=1,j=0; i<=a.len; i++)
{
// j表示是否要对高位进行借位
c.s[i] = a.s[i] - j;
if (i <= b.len) c.s[i] -= b.s[i];
if (c.s[i] < 0) 
{
// 向高位借位,补10
j = 1;
c.s[i] += 10;
}
else j = 0;
}
c.len = a.len;
while (c.len > 1 && !c.s[c.len]) c.len--;
}


// 大整数的比较
int HPCompare(const HP &x, const HP &y)
{
if (x.len > y.len) return 1;
if (x.len < y.len) return -1;
int i = x.len;
while (i>1 && (x.s[i]==y.s[i])) i--;
return x.s[i] - y.s[i];
}


// 大整数的乘法
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++;}
// 确保最高位不为0
while (i>1 && !c.s[i]) i--;
c.len = i;
}


// 大整数的除法
void Divide(const HP a, const HP b, HP &c, HP &d)
{
int i, j;
// 用余数d存被除数a的前i位数据,用来多次减去除数b,以得到商c
d.len = 1; d.s[1] = 0;
for (i=a.len; i>0; i--)
{
if (!(d.len == 1 && d.s[1] == 0))
{
// i没移一位,余数d也移位
for (j=d.len; j>0; j--)
d.s[j+1] = d.s[j];
d.len++;
}
d.s[1] = a.s[i];
c.s[i] = 0;
// 余数d大于除数b时,才可以进行减操作
while ((j=HPCompare(d,b)) >= 0)
{
Subtract(d, b, d);
c.s[i]++;
if (j == 0) break;
}
}
c.len = a.len;
while (c.len > 1 && c.s[c.len] == 0)
c.len--;
}
// 十进位右移
void RightShift(HP &x, int k)
{
for (int i=1; i<=x.len-k; i++)
x.s[i] = x.s[i+k];
x.len -= k;
if(x.len <= 0)
{
x.len = 1;
x.s[1] = 0;
}
}
// 十进位左移
void LeftShift(HP &x, int k)
{
int i;
for (i=x.len; i>=1; i--)
x.s[i+k] = x.s[i];
for (i=k; i>=1; i--)
x.s[i] = 0;
x.len += k;
}
// 求大整数的最大公约数
void GCD(HP a, HP b, HP &c)
{
if (b.len == 1 && b.s[1] == 0)
{
c.len = a.len;
memcpy(c.s, a.s, (a.len+1)*sizeof(int));
}
else
{
HP m, n;
Divide(a, b, m, n);
GCD(b, n, c);
}
}


int main()
{
string str;
string strc, stra, strb;
cin >> str;
int posc = str.find('.');
int posa = str.find('(');
int posb = str.find(')');
strc = str.substr(0, posc);
if (posc < 0)
cout << strc;
else
{
HP a, b, c;
HP tmp; tmp.len = 1; tmp.s[1] = 1;
// 整数部分
Str2HP(strc.c_str(), c);
stra = str.substr(posc+1, posa-posc-1);
// 非循环部分
Str2HP(stra.c_str(), a);
// up分子,down分母
HP up = c, down = tmp;
// 乘以10^|a|
LeftShift(down, stra.size());
LeftShift(up, stra.size());
Plus(up, a, up);
if (posa >= 0)
{
strb = str.substr(posa+1, posb-posa-1);
// 循环部分
Str2HP(strb.c_str(), b);
HP m = tmp;
LeftShift(m, strb.size());
Subtract(m, tmp, m);
// 乘以10^(|b|-1)
Multi(up, m, up);
Plus(up, b, up);
Multi(down, m, down);
}
// 求分子分母的最大公约数
GCD(down, up, tmp);
HP h;
Divide(down, tmp, down, h);
Divide(up, tmp, up, h);
PrintHP(up); cout << "/";
PrintHP(down); cout << endl;
}
}

 



0 0
原创粉丝点击