高精度数组的集大成
做的时候在和高中同学叙叙旧,差点寄掉
代码如下:
#include<stdio.h>
void expand(int len);
const char hexadecimal[17] = "0123456789ABCDEF";
int result[20], mid[20], l_result[100];
int main(void)
{
char tmp;
int len, length = 0, len2 = 1;
scanf("%c %d", &tmp, &len);
for(int i = 0; i <= 15; i++)
if(hexadecimal[i] == tmp)
{
mid[len - 1] = i;
break;
}
while(mid[len - 1] <= 15)
{
int ext = 0, i;
expand(len);
for(i = 0; i < len; i++)
{
ext += result[i] + mid[i];
result[i] = ext % 16;
ext /= 16;
}
while(ext)
{
ext += result[i];
result[i++] = ext % 16;
ext /= 16;
}
length = (length > i) ? length : i;
mid[len - 1]++;
}
//转换
for(int i = length - 1; i >= 0; i--)
{
int ext = 0, j;
for(j = 0; j < len2; j++)
{
ext += l_result[j] * 16;
l_result[j] = ext % 10;
ext /= 10;
}
while(ext)
{
ext += l_result[j];
l_result[j++] = ext % 10;
ext /= 10;
}
len2 = (len2 > j) ? len2 : j;
ext = result[i], j = 0;
while(ext)
{
ext += l_result[j];
l_result[j++] = ext % 10;
ext /= 10;
}
len2 = (len2 > j) ? len2 : j;
}
//输出
for(int i = len2 - 1; i >= 0; i--)
printf("%d", l_result[i]);
return 0;
}
void expand(int len)
{
for(int i = len - 2, j = 1; i >= 0; i--, j++)
mid[i] = (mid[len - 1] + j) % 16;
return;
}
写的有点草,进制的转换方法我之前写过,就不赘述了