#include <string.h>
typedef unsigned char UCHAR
typedef unsigned short USHORT
#define SIZE128 //一个大整数用个字节保存,最多表示位大整数
#define SIZE_10 2 * SIZE
typedef struct BigNum //大整数结构
{
UCHAR data[SIZE] //空间为(SIZE * sizeof(UCHAR)),就是SIZE个字节
}BigNum
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _BIGNUM_H_
#define _BIGNUM_H_
UCHAR atox(char ch) //将一个十六进制的字符(4位)转位数字,转换失败返回xff
BigNum Init(char* str) //初始化大整数,str为十六进制字符卖睁串
int GetByte(BigNum bignum) //判断有多少个字节不为
BigNum MovByteLetf(BigNum bignum, int n) //左移n个字节
BigNum MovByteRight(BigNum bignum, int n) //右移n个手枝字节
int Cmp(BigNum bignum_a, BigNum bignum_b) //大整数比较大小,>返回,<返回-1,==返回
BigNum Add(BigNum bignum_a, BigNum bignum_b) //大整数加法
BigNum Sub(BigNum bignum_a, BigNum bignum_b) //大整数减法
//BigNum Mul(BigNum bignum_a, UCHAR uchar) //大整数乘中薯岁法UCHAR
BigNum Mul(BigNum bignum_a, BigNum bignum_b) //大整数乘法
BigNum Div(BigNum bignum_a, BigNum bignum_b) //大整数除法
BigNum Mod(BigNum bignum_a, BigNum bignum_b) //大整数模运算
BigNum Pow(BigNum bignum, int n) //大整数乘方运算
void Print_16(BigNum bignum) //打印十六进制大整数
void Print_10(BigNum bignum) //打印十进制大整数
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BigNum.c : 定义控制台应用程序的入口点。
//
UCHAR atox(char ch) //将一个十六进制的字符(4位)转位数字,转换失败返回xff
{
UCHAR res
if (ch >= '0' &&ch <= '9')
{
res = ch - '0'
}
else if (ch >= 'a' &&ch <= 'f')
{
res = ch - 'a' + 10
}
else if (ch >= 'A' &&ch <= 'F')
{
res = ch - 'A' + 10
}
else
{
printf("change error!\n")
return 0xff
}
return res
}
int GetByte(BigNum bignum) //判断有多少个字节不为
{
int ByteOfBigNum = SIZE
while ((bignum.data[ByteOfBigNum - 1] == 0) &&(ByteOfBigNum >0))
{
ByteOfBigNum--
}
return ByteOfBigNum
}
BigNum MovByteLetf(BigNum bignum, int n) //左移n个字节
{
int i
int bignum_len = GetByte(bignum)
for (i = bignum_len - 1i >= 0i--)
{
if (i + n >= SIZE)
{
continue
}
bignum.data[i + n] = bignum.data[i]
}
for (i = n - 1i >= 0i--)
{
bignum.data[i] = 0
}
return bignum
}
BigNum MovByteRight(BigNum bignum, int n) //右移n个字节
{
int i
int bignum_len = GetByte(bignum)
for (i = 0i <bignum_leni++)
{
if (i + n >= SIZE)
{
bignum.data[i] = 0
continue
}
bignum.data[i] = bignum.data[i + n]
}
return bignum
}
BigNum Init(char* str) //初始化大整数,str为十六进制字符串
{
BigNum bignum
int len = (int)strlen(str)
int i
int j = 0
if (len >2 * SIZE)
{
len = 2 * SIZE
}
for (i = len - 1i >0i -= 2)
{
bignum.data[j] = atox(str[i]) + atox(str[i - 1]) * 16
j++
}
if (i == 0)
{
bignum.data[j] = atox(str[i])
j++
}
while (j <SIZE)
{
bignum.data[j] = 0
j++
}
return bignum
}
int Cmp(BigNum bignum_a, BigNum bignum_b) //大整数比较大小,>返回,<返回-1,==返回
{
int bignum_a_len = GetByte(bignum_a)
int bignum_b_len = GetByte(bignum_b)
int max = bignum_a_len >bignum_b_len ? bignum_a_len : bignum_b_len
int i
for (i = max - 1i >= 0i--)
{
if (bignum_a.data[i] >bignum_b.data[i])
{
return 1
}
if (bignum_a.data[i] <bignum_b.data[i])
{
return -1
}
}
return 0
}
BigNum Add(BigNum bignum_a, BigNum bignum_b) //大整数加法
{
BigNum bignum_c
USHORT temp
UCHAR carry = 0
int i
for (i = 0i <SIZEi++)
{
temp = bignum_a.data[i] + bignum_b.data[i] + carry
bignum_c.data[i] = temp &0x00ff
carry = (temp >>8) &0xff
}
return bignum_c
}
BigNum Sub(BigNum bignum_a, BigNum bignum_b) //大整数减法
{
BigNum bignum_c
USHORT temp
UCHAR carry = 0
int i
for (i = 0i <SIZEi++)
{
temp = bignum_a.data[i] - bignum_b.data[i] - carry
bignum_c.data[i] = temp &0x00ff
carry = (temp >>15) &0x01
}
return bignum_c
}
BigNum Mul(BigNum bignum_a, BigNum bignum_b) //大整数乘法
{
BigNum bignum_c = Init("0")
USHORT temp
UCHAR carry
int i, j
for (i = 0i <SIZEi++)
{
carry = 0
for (j = 0j <SIZEj++)
{
temp = bignum_a.data[i] * bignum_b.data[j] + bignum_c.data[j + i] + carry
bignum_c.data[j + i] = temp &0x00ff
carry = (temp >>8) &0xff
}
}
return bignum_c
}
BigNum Div(BigNum bignum_a, BigNum bignum_b) //大整数除法
{
BigNum bignum_c = Init("0")
BigNum B
int bignum_a_len
int bignum_b_len
int bignum_c_len
if (Cmp(bignum_b, bignum_c) == 0)
{
printf("错误!除数为\n")
return bignum_c
}
bignum_a_len = GetByte(bignum_a)
bignum_b_len = GetByte(bignum_b)
bignum_c_len = bignum_a_len - bignum_b_len
while (bignum_c_len >= 0)
{
B = MovByteLetf(bignum_b, bignum_c_len)
while (Cmp(bignum_a, B) != -1)
{
bignum_a = Sub(bignum_a, B)
bignum_c.data[bignum_c_len]++
}
bignum_c_len--
}
return bignum_c
}
BigNum Mod(BigNum bignum_a, BigNum bignum_b) //大整数模运算
{
BigNum bignum_c = Init("0")
BigNum B
int bignum_a_len
int bignum_b_len
int bignum_c_len
if (Cmp(bignum_b, bignum_c) == 0)
{
printf("错误!除数为\n")
return bignum_c
}
bignum_a_len = GetByte(bignum_a)
bignum_b_len = GetByte(bignum_b)
bignum_c_len = bignum_a_len - bignum_b_len
while (bignum_c_len >= 0)
{
B = MovByteLetf(bignum_b, bignum_c_len)
while (Cmp(bignum_a, B) != -1)
{
bignum_a = Sub(bignum_a, B)
}
bignum_c_len--
}
return bignum_a
}
BigNum Pow(BigNum bignum, int n) //大整数乘方运算
{
int i
BigNum bignum_res = Init("1")
for (i = 0i <ni++)
{
bignum_res = Mul(bignum_res, bignum)
}
return bignum_res
}
void Print_16(BigNum bignum) //打印十六进制大整数
{
int i
int nFlag = 0
for (i = SIZE - 1i >= 0i--)
{
if (nFlag == 0 &&bignum.data[i] == 0) //前面是的不打印
{
continue
}
else
{
nFlag++
if (nFlag == 1) //首位的不打印,如x01,只打印
{
printf("%x", bignum.data[i])
}
else //后面的要打印
{
printf("%02x", bignum.data[i])
}
}
}
if (nFlag == 0)
{
printf("0")
}
printf("\n")
}
void Print_10(BigNum bignum) //打印十进制大整数
{
int data_10[SIZE_10]
int i = 0
int j
while (Cmp(bignum, Init("0")) == 1)
{
data_10[i] = (Mod(bignum, Init("a"))).data[0]
bignum = (Div(bignum, Init("a")))
i++
}
for (j = i - 1j >= 0j--)
{
printf("%d", data_10[j])
}
printf("\n")
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void GoldNum(int precision)
{
BigNum b1 = Init("2")
BigNum b2 = Init("3")
BigNum b3 = Init("a")
BigNum temp1
BigNum temp2 = b1
int count = 1000
int i = 0
b3 = Pow(b3, precision)
for(i <counti++)
{
temp1 = b2
b2 = Add(b1, b2)
b1 = temp1
temp1 = Mul(b1, b3)
temp1 = Div(temp1, b2)
if(!Cmp(temp1, temp2))
{
Print_10(temp1)
break
}
temp2 = temp1
}
}
int main()
{
GoldNum(100)
return 0
}
这里面的大数加减乘除是直接用这里面的http://wenku.baidu.com/view/f7d13dd2195f312b3169a5d7.html
我只是写了void GoldNum(int precision)函数,precision参数是精度大小
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)