【TDTX】
【C99】
【编译与运行环境】64位Windows操作系统,TDM-gcc 4.9.2 64bit(-std=c99)编译。
【项目Gitee仓库】C语言-微项目。
【简介】C语言大整数乘法器,纯字符串处理,根据人手算乘法的方式,精确相乘,字符串乘法,最后可以数字格式化输出。
函数 | 作用 |
---|---|
char* add(char a,char b) | 个位数加法运算-直接查加法表-switch方式 |
char* mutip(char a,char b) | 个位数乘法运算-直接查乘法表-switch方式 |
void addToResultForMutip(char* result,char* toAdd) | 乘法器用的大整数字符串加法运算 |
char* makeNoLimitMutip(char* ta,char* tb) | 大整数字符串乘法运算 |
void printAddTables(void) | 输出10×10表格-加法表 |
void printMutiTables(void) | 输出10×10表格-乘法表 |
void printFormatNumber(char* number) | 将数字带逗号分隔的格式化输出 |
void printNumber(char* number) | 将数字原样非格式化输出 |
1.测试数据如下:
其中,第一个数字表示测试的是n位乘n位的运算,第二个数字是C语言采用clock_t方式计算出的时间,如“120,0.016”表示一个120位整数乘以一个120位整数,乘法完成得到积的运行时间为0.016。
30,0.0
40,0.0
60,0.0
80,0.015
100,0.015
120,0.016
140,0.031
160,0.047
180,0.063
200,0.098
220,0.125
240,0.157
280,0.266
320,0.390
360,0.562
400,0.766
440,1.015
480,1.333
560,2.065
640,3.106
960,10.527
1920,86.268
#include
#include
#include
#include
char addResults[2] = {'0','0'};
char mutiResults[2] = {'0','0'};
char* add(char a,char b)
{
//清空结果
addResults[0] = '0';
addResults[1] = '0';
if(a == '0')
{
addResults[0] = '0';
addResults[1] = b;
return addResults;
}
else if(b == '0')
{
addResults[0] = '0';
addResults[1] = a;
return addResults;
}
switch(a)
{
case '1':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '2';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '3';
return addResults;
case '3':
addResults[0] = '0';
addResults[1] = '4';
return addResults;
case '4':
addResults[0] = '0';
addResults[1] = '5';
return addResults;
case '5':
addResults[0] = '0';
addResults[1] = '6';
return addResults;
case '6':
addResults[0] = '0';
addResults[1] = '7';
return addResults;
case '7':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '8':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
}
break;
case '2':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '3';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '4';
return addResults;
case '3':
addResults[0] = '0';
addResults[1] = '5';
return addResults;
case '4':
addResults[0] = '0';
addResults[1] = '6';
return addResults;
case '5':
addResults[0] = '0';
addResults[1] = '7';
return addResults;
case '6':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '7':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
}
break;
case '3':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '4';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '5';
return addResults;
case '3':
addResults[0] = '0';
addResults[1] = '6';
return addResults;
case '4':
addResults[0] = '0';
addResults[1] = '7';
return addResults;
case '5':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '6':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
}
break;
case '4':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '5';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '6';
return addResults;
case '3':
addResults[0] = '0';
addResults[1] = '7';
return addResults;
case '4':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '5':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '6':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '3';
return addResults;
}
break;
case '5':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '6';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '7';
return addResults;
case '3':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '4':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '5':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '6':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '3';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '4';
return addResults;
}
break;
case '6':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '7';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '3':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '4':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '5':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '6':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '3';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '4';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '5';
return addResults;
}
break;
case '7':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '8';
return addResults;
case '2':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '3':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '4':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '5':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
case '6':
addResults[0] = '1';
addResults[1] = '3';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '4';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '5';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '6';
return addResults;
}
break;
case '8':
switch(b)
{
case '1':
addResults[0] = '0';
addResults[1] = '9';
return addResults;
case '2':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '3':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '4':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
case '5':
addResults[0] = '1';
addResults[1] = '3';
return addResults;
case '6':
addResults[0] = '1';
addResults[1] = '4';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '5';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '6';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '7';
return addResults;
}
break;
case '9':
switch(b)
{
case '1':
addResults[0] = '1';
addResults[1] = '0';
return addResults;
case '2':
addResults[0] = '1';
addResults[1] = '1';
return addResults;
case '3':
addResults[0] = '1';
addResults[1] = '2';
return addResults;
case '4':
addResults[0] = '1';
addResults[1] = '3';
return addResults;
case '5':
addResults[0] = '1';
addResults[1] = '4';
return addResults;
case '6':
addResults[0] = '1';
addResults[1] = '5';
return addResults;
case '7':
addResults[0] = '1';
addResults[1] = '6';
return addResults;
case '8':
addResults[0] = '1';
addResults[1] = '7';
return addResults;
case '9':
addResults[0] = '1';
addResults[1] = '8';
return addResults;
}
break;
default:
break;
}
return addResults;
}
char* mutip(char a,char b)
{
//清空结果
mutiResults[0] = '0';
mutiResults[1] = '0';
if(a == '0' || b == '0')
{
mutiResults[0] = '0';
mutiResults[1] = '0';
return mutiResults;
}
if(a == '1')
{
mutiResults[0] = '0';
mutiResults[1] = b;
return mutiResults;
}
else if(b == '1')
{
mutiResults[0] = '0';
mutiResults[1] = a;
return mutiResults;
}
switch(a)
{
case '2':
switch(b)
{
case '2':
mutiResults[0] = '0';
mutiResults[1] = '4';
return mutiResults;
case '3':
mutiResults[0] = '0';
mutiResults[1] = '6';
return mutiResults;
case '4':
mutiResults[0] = '0';
mutiResults[1] = '8';
return mutiResults;
case '5':
mutiResults[0] = '1';
mutiResults[1] = '0';
return mutiResults;
case '6':
mutiResults[0] = '1';
mutiResults[1] = '2';
return mutiResults;
case '7':
mutiResults[0] = '1';
mutiResults[1] = '4';
return mutiResults;
case '8':
mutiResults[0] = '1';
mutiResults[1] = '6';
return mutiResults;
case '9':
mutiResults[0] = '1';
mutiResults[1] = '8';
return mutiResults;
}
break;
case '3':
switch(b)
{
case '2':
mutiResults[0] = '0';
mutiResults[1] = '6';
return mutiResults;
case '3':
mutiResults[0] = '0';
mutiResults[1] = '9';
return mutiResults;
case '4':
mutiResults[0] = '1';
mutiResults[1] = '2';
return mutiResults;
case '5':
mutiResults[0] = '1';
mutiResults[1] = '5';
return mutiResults;
case '6':
mutiResults[0] = '1';
mutiResults[1] = '8';
return mutiResults;
case '7':
mutiResults[0] = '2';
mutiResults[1] = '1';
return mutiResults;
case '8':
mutiResults[0] = '2';
mutiResults[1] = '4';
return mutiResults;
case '9':
mutiResults[0] = '2';
mutiResults[1] = '7';
return mutiResults;
}
break;
case '4':
switch(b)
{
case '2':
mutiResults[0] = '0';
mutiResults[1] = '8';
return mutiResults;
case '3':
mutiResults[0] = '1';
mutiResults[1] = '2';
return mutiResults;
case '4':
mutiResults[0] = '1';
mutiResults[1] = '6';
return mutiResults;
case '5':
mutiResults[0] = '2';
mutiResults[1] = '0';
return mutiResults;
case '6':
mutiResults[0] = '2';
mutiResults[1] = '4';
return mutiResults;
case '7':
mutiResults[0] = '2';
mutiResults[1] = '8';
return mutiResults;
case '8':
mutiResults[0] = '3';
mutiResults[1] = '2';
return mutiResults;
case '9':
mutiResults[0] = '3';
mutiResults[1] = '6';
return mutiResults;
}
break;
case '5':
switch(b)
{
case '2':
mutiResults[0] = '1';
mutiResults[1] = '0';
return mutiResults;
case '3':
mutiResults[0] = '1';
mutiResults[1] = '5';
return mutiResults;
case '4':
mutiResults[0] = '2';
mutiResults[1] = '0';
return mutiResults;
case '5':
mutiResults[0] = '2';
mutiResults[1] = '5';
return mutiResults;
case '6':
mutiResults[0] = '3';
mutiResults[1] = '0';
return mutiResults;
case '7':
mutiResults[0] = '3';
mutiResults[1] = '5';
return mutiResults;
case '8':
mutiResults[0] = '4';
mutiResults[1] = '0';
return mutiResults;
case '9':
mutiResults[0] = '4';
mutiResults[1] = '5';
return mutiResults;
}
break;
case '6':
switch(b)
{
case '2':
mutiResults[0] = '1';
mutiResults[1] = '2';
return mutiResults;
case '3':
mutiResults[0] = '1';
mutiResults[1] = '8';
return mutiResults;
case '4':
mutiResults[0] = '2';
mutiResults[1] = '4';
return mutiResults;
case '5':
mutiResults[0] = '3';
mutiResults[1] = '0';
return mutiResults;
case '6':
mutiResults[0] = '3';
mutiResults[1] = '6';
return mutiResults;
case '7':
mutiResults[0] = '4';
mutiResults[1] = '2';
return mutiResults;
case '8':
mutiResults[0] = '4';
mutiResults[1] = '8';
return mutiResults;
case '9':
mutiResults[0] = '5';
mutiResults[1] = '4';
return mutiResults;
}
break;
case '7':
switch(b)
{
case '2':
mutiResults[0] = '1';
mutiResults[1] = '4';
return mutiResults;
case '3':
mutiResults[0] = '2';
mutiResults[1] = '1';
return mutiResults;
case '4':
mutiResults[0] = '2';
mutiResults[1] = '8';
return mutiResults;
case '5':
mutiResults[0] = '3';
mutiResults[1] = '5';
return mutiResults;
case '6':
mutiResults[0] = '4';
mutiResults[1] = '2';
return mutiResults;
case '7':
mutiResults[0] = '4';
mutiResults[1] = '9';
return mutiResults;
case '8':
mutiResults[0] = '5';
mutiResults[1] = '6';
return mutiResults;
case '9':
mutiResults[0] = '6';
mutiResults[1] = '3';
return mutiResults;
}
break;
case '8':
switch(b)
{
case '2':
mutiResults[0] = '1';
mutiResults[1] = '6';
return mutiResults;
case '3':
mutiResults[0] = '2';
mutiResults[1] = '4';
return mutiResults;
case '4':
mutiResults[0] = '3';
mutiResults[1] = '2';
return mutiResults;
case '5':
mutiResults[0] = '4';
mutiResults[1] = '0';
return mutiResults;
case '6':
mutiResults[0] = '4';
mutiResults[1] = '8';
return mutiResults;
case '7':
mutiResults[0] = '5';
mutiResults[1] = '6';
return mutiResults;
case '8':
mutiResults[0] = '6';
mutiResults[1] = '4';
return mutiResults;
case '9':
mutiResults[0] = '7';
mutiResults[1] = '2';
return mutiResults;
}
break;
case '9':
switch(b)
{
case '2':
mutiResults[0] = '1';
mutiResults[1] = '8';
return mutiResults;
case '3':
mutiResults[0] = '2';
mutiResults[1] = '7';
return mutiResults;
case '4':
mutiResults[0] = '3';
mutiResults[1] = '6';
return mutiResults;
case '5':
mutiResults[0] = '4';
mutiResults[1] = '5';
return mutiResults;
case '6':
mutiResults[0] = '5';
mutiResults[1] = '4';
return mutiResults;
case '7':
mutiResults[0] = '6';
mutiResults[1] = '3';
return mutiResults;
case '8':
mutiResults[0] = '7';
mutiResults[1] = '2';
return mutiResults;
case '9':
mutiResults[0] = '8';
mutiResults[1] = '1';
return mutiResults;
}
break;
default:
break;
}
return mutiResults;
}
void addToResultForMutip(char* result,char* toAdd)
{
//乘法:按位乘再求和的模式下,做加法绝对不会溢出
result[strlen(result)] = '\0';
toAdd[strlen(result)] = '\0';
//puts(result);
char w = '0';
for(int i = strlen(result) - 1;i >= 0;i--)
{
char* t = add(result[i],toAdd[i]);
char tw = t[0];
char* tt = add(t[1],w);
result[i] = tt[1];
char* ttt = add(tt[0],tw);
w = ttt[1];
}
//puts(result);
}
void printAddTables(void)
{
char num[10] = {'0','1','2','3','4','5','6','7','8','9'};
for(int i = 0;i < 10;i ++)
{
for(int j = 0;j < 10;j ++)
{
char* s = add(num[i],num[j]);
printf("%c + %c = %c%c ",num[i],num[j],s[0],s[1]);
}
puts("");
}
}
void printMutiTables(void)
{
char num[10] = {'0','1','2','3','4','5','6','7','8','9'};
for(int i = 0;i < 10;i ++)
{
for(int j = 0;j < 10;j ++)
{
char* s = mutip(num[i],num[j]);
printf("%c x %c = %c%c ",num[i],num[j],s[0],s[1]);
}
puts("");
}
}
void printNumber(char* number)
{
puts(number);
}
void printFormatNumber(char* number)
{
int len = strlen(number);
for(int i = 0;i < len;i++)
{
if(len % 3 == 0)
{
if(i % 3 == 0 && i != 0)
{
printf(",%c",number[i]);
}
else
{
printf("%c",number[i]);
}
}
else
{
if(i < (len % 3))
{
printf("%c",number[i]);
}
else
{
if((i - len % 3) % 3 == 0 && i != 0)
{
printf(",%c",number[i]);
}
else
{
printf("%c",number[i]);
}
}
}
}
}
char* makeNoLimitMutip(char* ta,char* tb)
{
int aWeight = strlen(ta) - 1;
int bWeight = strlen(tb) - 1;
int length = aWeight + bWeight + 3;
char* result = (char*) malloc(sizeof(char) * length);
char* toAdd = (char*) malloc(sizeof(char) * length);
for(int i = 0;i < length;i++)
{
if(i != length - 1)
{
result[i] = '0';
toAdd[i] = '0';
}
else
{
result[i] = '\0';
toAdd[i] = '\0';
}
}
for(int i = 0;i < strlen(ta);i++)
{
//puts("\n------------------------");
for(int j = 0;j < strlen(tb);j++)
{
char* s = mutip(ta[i],tb[j]);
toAdd[length - 1] = '\0';
for(int k = length - 2;k >= 0;k--)
{
if(k > (length - 2 - (bWeight + aWeight)))
{
toAdd[k] = '0';
}
else if(k == (length - 2 - (bWeight + aWeight)))
{
toAdd[k--] = s[1];
toAdd[k] = s[0];
}
else
{
toAdd[k] = '0';
}
}
addToResultForMutip(result,toAdd);
bWeight--;
}
bWeight = strlen(tb) - 1;
aWeight--;
}
free(toAdd);
return result;
}
两数相乘a×b,例如:1234×987654,ta:1234,tb:987654:
由于ta是4位整数、tb是6位整数,所以乘法积位数最多是10位,无非就是可能有0前缀数字,所以就直接当结果是10位。
所以,定义积字符串和临时乘法结果字符串:
int aWeight = strlen(ta) - 1;
int bWeight = strlen(tb) - 1;
int length = aWeight + bWeight + 3;
char* result = (char*) malloc(sizeof(char) * length);
char* toAdd = (char*) malloc(sizeof(char) * length);
result初始化为:0000000000
#include "BigInterNoLimitMutiString.c"
int main(void)
{
char num[10] = {'0','1','2','3','4','5','6','7','8','9'};
char ta[10000] = {'\0'};
char tb[10000] = {'\0'};
gets(ta);
gets(tb);
printf("\ntalen-tblen:%d-%d",strlen(ta),strlen(tb));
clock_t start = clock();
char* result = makeNoLimitMutip(ta,tb);
clock_t end = clock();
printf("\nrun time is :%f\n",(double)(end - start) / CLOCKS_PER_SEC);
puts("\n乘法积:");
for(int i = 0;i < strlen(result);i++)
{
if(result[i] != '0')
{
printFormatNumber(&result[i]);
//puts("");
//printNumber(result);
break;
}
}
free(result);
// puts("");
// printAddTables();
// puts("");
// printMutiTables();
return 0;
}