写写我的大数乘法

课题还是没有想好,便重新看了Algorithms(2006),大数乘法一直没有自己实现过,就写了写,仅仅是整数的实现:
PS:书籍还是看原版的好,被逼的无奈看了英文原版的ALGORITHMS,自己的看法是原版认识的单词必然会少一点,但理解作者的思想要比较容易,能得到更多启发。我之前也是怕原版,看了几行就想换中文的,其实读英文远没有想象的那么难,只要开了头,后面跟看中文差别不是很大。各位还是努力看看原版吧,会有不同收获的。
#include<iostream>
using namespace std;

#define MAX_mulf 100
#define MAX_muls 100
#define MAX_result 500
int similar_binary(int ,int);
int judge_num(int num[],int n);
int move_matrix_left(int num[],int n,int max);
void matrix_multiply_num(int num[],int para);
void matrix_multiply_matrix(int faced[],int mutiply[]);
void matrix_add_matrix(int first[],int second[]);
void copy_arr(int result[],int source[],int lenth);
void out_put(int num[],int max);
void transfer_char_num(char mul[],int num[]);

int main()
{
	int test[MAX_mulf];
	int prepare[MAX_muls];
	memset(test,0,sizeof(test));
	memset(prepare,0,sizeof(prepare));
	char multia[MAX_mulf];
	char multib[MAX_muls];
	printf("please input two big numbers seperated by the space key: \n");
	scanf("%s %s",multia,multib);
	transfer_char_num(multia,test);
	//out_put(test,MAX_mulf);
	transfer_char_num(multib,prepare);
	//out_put(prepare,MAX_muls);
	
	matrix_multiply_matrix(test,prepare);
	system("pause");
	return 1;
}

/* output the array to judge */
void out_put(int num[],int max)
{
	int i = judge_num(num,max);
	i--;
	while(i >=0)
		{
			cout<<num[i];
			i--;
		}
	cout<<endl;
}
/* judge the lenth of the array */
int judge_num(int num[],int n)
{
	int i = n-1;
	while(num[i] == 0)
	{
		i--;
	}
	i++;
	return i;
}

/* from char to num */
void transfer_char_num(char mul[],int num[])
{
		int i = 0;
		int count = 0;
		while(mul[i] != '\0')
		{
			i++;
		}
		count = i;
		count--;
		i = 0;
		while(i<=count)
		{
			num[i] = mul[count-i] - '0';
			i++;
		}
}
/* move array right ---- move number left */
int move_matrix_left(int num[],int n,int max)
{
	int head = judge_num(num,max);
	head--;
	if(head+n>=max)
		return 0;
	while(head >=  0)
	{
		num[head+n] = num[head];
		head--;
	}
	n = n-1;
	while(n >= 0)
		{
			num[n] = 0;
			n--;
		}

}

/* copy one array to another */
void copy_arr(int result[],int source[],int lenth)
{
	lenth--;
	while(lenth >= 0)
	{
		result[lenth] = source[lenth];
		lenth--;
	}
}
void matrix_multiply_num(int num[],int para)
{
	int i = judge_num(num,MAX_mulf);
	i--;
	int temp;
	int up_position = 0;
	for(int j=0; j<=i;j++)
	{
		num[j] = num[j] * para;
		num[j] += up_position;
		temp = num[j];
		num[j] = temp%10;
		up_position = temp/10;
		
	}
}
/* compared to the demical addition */
void matrix_add_matrix(int first[],int second[])
{
	int end_first = judge_num(first,MAX_mulf) - 1;
	int end_second = judge_num(second,MAX_muls) - 1;
	int up_position = 0;
	int last;
	int flag = 0;
	int temp;
	if(end_first>end_second)
		{
			last = end_second;
			flag = 1;
		}
	else
		{
			last = end_first;
		}
	
			for(int i=0;i<=last;i++)
			{
				first[i] += second[i];
				first[i] += up_position;
				temp = first[i];
				first[i] = temp%10;
				up_position = temp/10;
			}


			if(!flag)
				for(int i=last+1;i<=end_second;i++)
					first[i] = second[i];

			  int i = last+1;	
			   for(;;i++)
				{
					first[i] += up_position;
					if(first[i]>9)
					{
						temp = first[i];
						first[i] = temp%10;
						up_position = temp/10;
					}else
						break;
			    }
			
			
}


void matrix_multiply_matrix(int faced[],int mutiply[])
{
	int result[MAX_result];
	memset(result,0,sizeof(result));
	int temp[MAX_mulf];
	memset(temp,0,sizeof(temp));
	int end_first = judge_num(faced,MAX_mulf) - 1;
	int end_second = judge_num(mutiply,MAX_muls) - 1;
	int head = end_second;
	
	while(head >= 0)
	{
		if(head<end_second)
			move_matrix_left(result,1,MAX_mulf);
		if(mutiply[head] != 0)
		{
			int  num_deal = mutiply[head];
			copy_arr(temp,faced,end_first);
			matrix_multiply_num(temp,mutiply[head]);
			matrix_add_matrix(result,temp);
		}
		head--;
		
	}
	out_put(result,MAX_result);
}
/*int similar_binary( int width, int height)
{
	if(height == 0)
		return 0;
	else
	{
      int z;
	  
	z =similar_binary(width,height>>1);
	
	z = 2*z;
	if(height&1)
		z+=width;
	return z;
	}
}*/



你可能感兴趣的:(System,input,UP,Matrix,output,Numbers)