⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 大数相乘代码.txt

📁 c语言的一些常见的算法以及思考和改进的文章,写的很不错,花费了很大的精力从网络了搜罗的,希望大家喜欢.
💻 TXT
字号:
原始url:
http://bugeyes.blog.edu.cn/user1/20989/archives/2005/355575.shtml

两整数相乘,一般计算过程(相乘、移位、相加)如下:

4321
×) 999
----------
38889
38889
38889
----------
4316679 

模拟该过程的C语言代码如下(整数在数组中逆序存放,结果是正序输出的,数组中每个元素用来存放整数中的每一位):

#i nclude <conio.h>
#i nclude <stdlib.h>

void product(int x[],int y[],int z[],int n,int m)
{
int i,j;
for(i=0;i<n+m-1;i++)
z[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
z[i+j]=z[i+j]+x[i]*y[j];
}

void chuli(int z[],int result[],int n)
{
int i;
int c=0;
for(i=0;i<n;i++)
{
result[i]=(z[i]+c)%10;
c=(z[i]+c)/10;
}
if(c!=0)
result[i]=c;
else
result[i]=0;
}

void output(int array[],int n)
{
int i;
printf("\n");
for(i=n;i>=0;i--)
printf("%d",array[i]);
}

void main()
{
int x[4]={1,2,3,4},y[3]={9,9,9},z[6],result[7];
int i;
product(x,y,z,4,3);
chuli(z,result,6);
clrscr();
printf("\nThe first digital is:\n");
output(x,3);
printf("\nThe second digital is:\n");
output(y,2);
printf("\nThe result product is:\n");
output(result,6);
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -