谁能看懂这段代码?散分!

gimmyfox 2002-11-01 10:06:14
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Question: *
* How to calculate two integers each one has forty bit. *
* Algorithm: *
* Declare two arrays suppose to be array A and B to storage *
* the two integers just as a intacter singly. *
* Get the first number from array B,then use it to multiply *
* the number of array A singly. Declare another array C to *
* storage the value when multiplying. *
* Thinking about the carrying bits. *
* Thinking: *
* Can you use the Linear_List to do this? *
* If they are two floating numbers,so how to do it? *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <conio.h>

//To calculate two 40 bits integers multiply,change 10 to 40
#define THE_ARR_A_SIZE 10 #define ARR_B_SIZE THE_ARR_A_SIZE
#define ARR_C_SIZE (THE_ARR_A_SIZE + ARR_B_SIZE)

void main(void);
void Multiply( short *, short *, short *, short, short);
void Carrying( short *c, short);

void main(void)
{
short A[THE_ARR_A_SIZE] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; // 10
short B[ARR_B_SIZE] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
short C[ARR_C_SIZE];
short i = 0;

for ( NULL; i < ARR_C_SIZE; i++)
{
*(C+i) = 0;
}

Multiply(A, B, C, THE_ARR_A_SIZE, ARR_B_SIZE);
Carrying(C, ARR_C_SIZE);

printf("\nThe Result is: ");

i = 0;
if (C[0] == 0) ++i; //To delete the primacy zero
for ( NULL; i < ARR_C_SIZE; i++)
{
printf("%d", C[i]);
}
getch();
printf("\n");
}

/*
* NOTICE:
* The Array C is not initialed,you should do that. Here
* the array is supposed to be filled zero.
* Algorithm:
*
*/

void Multiply( short A[], short B[], short C[], short A_size, short B_size)
{
short i = 0, j = 0, k = 1; // Assigned k to 1 to leave C[0]
//be null to storage the carrying.

for (j = 0; j<B_size; j++)
{
for (i = 0; i<A_size; i++)
{
C[k] += B[j] * A[i];
k++;
}
k = 2;
k = k + j;
}
}

// To solve the carrying .

void Carrying(short C[],short C_size)
{
short i, tens_place = 0, units_order = 0;
short temp = 0, end_arr;

end_arr = C_size-1;
for (i = end_arr; i >= 0; i--)
{
temp = C[i];
if (C[i] > 9)
{
tens_place = temp / 10;
units_order = temp % 10;
C[i-1] += tens_place;
C[i] = units_order;
}
}
}

帮忙解释一下 在程序旁边加点注释也好
看得我是头都大了
...全文
25 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gimmyfox 2002-11-02
  • 打赏
  • 举报
回复
谢谢!!!

如果还有什么不懂我再来问了

如果我看懂了我一定散分给你
leojay 2002-11-01
  • 打赏
  • 举报
回复
不知道我说清楚没有,你看看吧。
更正与补充:

第一步:81除以10得8余1,最后一位是1,
~~~~~~~~~~~~这是因为我们只保留余数。
第二步:162+8(8是上面的余数,下同)除以10得17余0,倒数第二位是0,
~~~~应该是商
leojay 2002-11-01
  • 打赏
  • 举报
回复
这个好说:
这个程序是计算两个高精度整数的相乘。

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Question: *
* How to calculate two integers each one has forty bit. *
* Algorithm: *
* Declare two arrays suppose to be array A and B to storage *
* the two integers just as a intacter singly. *
* Get the first number from array B,then use it to multiply *
* the number of array A singly. Declare another array C to *
* storage the value when multiplying. *
* Thinking about the carrying bits. *
* Thinking: *
* Can you use the Linear_List to do this? *
* If they are two floating numbers,so how to do it? *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <conio.h>

//To calculate two 40 bits integers multiply,change 10 to 40
#define THE_ARR_A_SIZE 10   //定义数组A的大小是10
#define ARR_B_SIZE THE_ARR_A_SIZE //定义数组B的大小是10

//C是用来放结果的
#define ARR_C_SIZE (THE_ARR_A_SIZE + ARR_B_SIZE) //定义数组C的大小是20

void main(void);
void Multiply( short *, short *, short *, short, short);
void Carrying( short *c, short);

void main(void)
{
short A[THE_ARR_A_SIZE] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; // 10 //初始化A为9999999999
short B[ARR_B_SIZE] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; //初始化B为9999999999
short C[ARR_C_SIZE]; //这就是放最后结果的C
short i = 0;

for ( NULL; i < ARR_C_SIZE; i++) //把数组C清0
{
*(C+i) = 0;
}

Multiply(A, B, C, THE_ARR_A_SIZE, ARR_B_SIZE); //做乘法 但要注意,这个乘法函数没有把进位加进去。
Carrying(C, ARR_C_SIZE); //在这里做进位计算

printf("\nThe Result is: ");

i = 0;
if (C[0] == 0) ++i; //To delete the primacy zero
for ( NULL; i < ARR_C_SIZE; i++) //输出结果
{
printf("%d", C[i]);
}
getch();
printf("\n");
}

/*
* NOTICE:
* The Array C is not initialed,you should do that. Here
* the array is supposed to be filled zero.
* Algorithm:
*
*/

这里的乘法就是用摸拟手算的方法:
但与我们手算不完全相同的是他先不做进位,最后一次完成进位的计算:
比方说我们计算99乘以99:
先列式子:
9 9
× 9 9
-----------
81 81 注意,我们不做进位,大于等于10的部分要保留
81 81
-----------
81 162 81

这是没有进位的结果,我们再做进位:(这部分就是后面Carrying所要做的事情)

81 162 81
第一步:81除以10得8余1,最后一位是1,
第二步:162+8(8是上面的余数,下同)除以10得17余0,倒数第二位是0,
第三步:81+17除以10得9余8,倒数第三位是8,倒数第四位是9,
所以最后的结果是9801。

void Multiply( short A[], short B[], short C[], short A_size, short B_size)
{
short i = 0, j = 0, k = 1; // Assigned k to 1 to leave C[0]
//be null to storage the carrying.

for (j = 0; j<B_size; j++) //j是数组B的下标
{
for (i = 0; i<A_size; i++) //i是数组A的下标
{
C[k] += B[j] * A[i]; //这里完成A[i]与B[j]相乘再加入C中,这里你单步调试一下就知道了。
k++;
}
k = 2; //适当的移位使A[i]与B[j]的乘积放入C的正确的位置中.
k = k + j;
}
}

// To solve the carrying .

void Carrying(short C[],short C_size) //这里做进位
{
short i, tens_place = 0, units_order = 0;
short temp = 0, end_arr;

end_arr = C_size-1;
for (i = end_arr; i >= 0; i--)
{
temp = C[i];
if (C[i] > 9)
{
tens_place = temp / 10; //除以10
units_order = temp % 10; //units_order放余数
C[i-1] += tens_place; //除以10的商加入下一位
C[i] = units_order; //保留余数
}
}
}

33,009

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧