ضرب دو عدد 20 رقمی

سلام.

کسی source code برنامه ای رو داره که دو تا عدد 20 رقمی رو در هم ضرب کند ؟

با سپاس.
 

aira

Member
من اينو تو هاردم داشتم..قبلا از اينترنت گرفته بودم...فكر كنم بدردت بخوره
کد:
//     
// Name: Multiply 2 big numbers
// Description:Multiply 2 big numbers wh
//     ich normal data types cannot handle.
// By: Nguyen Ngoc Giang
//
// Inputs:2 integer numbers
//
// Returns:Product of 2 numbers, and the
//     number of digits of this product.
//
// Side Effects:-Each input number shoul
//     d have less than 500 digits (actually, i
//     t can be more further, but for the purpo
//     se of memory, I adjust at 500).
-The program employs the use of itoa function. Some of C++ compilers (e.g. ANSI C) may not support this function. It works well in Visual C++.
//
//This code is copyrighted and has// limited warranties   

#include <iostream.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/*--------------------------------------- Function prototypes -------------------------*/
void reverseArray(int*, int*, int);
void print(int*, int);
void product(char*, char*, int*&, int*);
void create(int*&);
void destroy(int*&);
/*--------------------------------------- Function definition -------------------------*/
void reverseArray(int* source, int* destination, int length)


    {
    	int i;
    	for (i = 0; i < length; i++)
    		destination[length-i-1] = source[i];
}

void print(int* num, int length)


    {
    	if (num[0] != 0)
    		cout << num[0];
    	for (int i = 1; i < length; i++)
    		cout << num[i];
}

void create(int*& num, int length)


    {
    	num = new int[length];
}

void destroy(int*& num)


    {
    	delete[] num;
}

void product(char s1[], char s2[], int*& actualResult, int* actualLength)


    {
    	int *num1, *num2, lengthTempResult;
    	int *n1, *n2;
    	char temp[2];
    	int i, j, k;
    	int length1, length2;
    	int** tempResult;
    	int *result;
    	length1 = strlen(s1);
    	length2 = strlen(s2);
    		
    	//store 2 numbers in strings of integers
    	create(n1, length1);


        	for (i = 0; i < length1; i++) {
        		strncpy(temp, s1 + i, 1);
        		n1[i] = atoi(temp);
        	}
        	create(n2, length2);


            	for (i = 0; i < length2; i++) {
            		strncpy(temp, s2 + i, 1);
            		n2[i] = atoi(temp);
            	}
            	create(num1, length1);
            	create(num2, length2);
            	reverseArray(n1, num1, length1);
            	reverseArray(n2, num2, length2);
            	destroy(n1);
            	destroy(n2);
            	
            	lengthTempResult = length1 + length2;
            	//allocate memory for tempResult
            	tempResult = new int*[lengthTempResult];
            	for (i = 0; i < lengthTempResult; i++)
            		create(tempResult[i], length2);
            	//allocate memory for result
            	create(result, lengthTempResult);
            	//allocate memory for actualResult;
            	create(actualResult, lengthTempResult);
            	


                	for (j = 0; j < length2; j++) {
                		int rem = 0;
                		//fill 0s in the tempResult
                		for (k = 0; k < j; k++)
                			tempResult[k][j] = 0;	
                		//calculate in reverse order


                    		for (i = 0; i < length1; i++) {
                    			tempResult[i + j][j] = (num1[i] * num2[j] + rem) % 10;
                    			rem = (num1[i] * num2[j] + rem) / 10;
                    		}
                    		//fill 0s in the last part of tempResult
                    		tempResult[length1 + j][j] = rem;
                    		for (k = length1 + j + 1; k < lengthTempResult; k++)
                    			tempResult[k][j] = 0;
                    	}
                    	destroy(num1);
                    	destroy(num2);
                    	
                    	//initialize result[]
                    	for (i = 0; i < lengthTempResult; i++)
                    		result[i] = 0;
                    	//calculate result[]
                    	int rem = 0;


                        	for (i = 0; i < lengthTempResult; i++) {
                        		for (j = 0; j < length2; j++)
                        			result[i] += tempResult[i][j];
                        		result[i] += rem;
                        		rem = result[i] / 10;
                        		result[i] = result[i] % 10;
                        	}
                        	//deallocate memory for tempResult
                        	for (i = 0; i < lengthTempResult; i++)
                        		destroy(tempResult[i]);
                        	delete[] tempResult;
                        	reverseArray(result, actualResult, lengthTempResult);
                        	destroy(result);
                        	*actualLength = lengthTempResult;
                    }

                    void main(void)


                        {
                        	
                        	char s1[500];
                        	char s2[500];
                        	int *result;
                        	int length;
                        	cout << "A program to calculate the product of 2 big numbers" << endl
                        		 << " (each has less than 500 digits)" << endl << endl;
                        	cout << "Enter the first number ";
                        	cin >> s1;
                        	cout << "Enter the second number ";
                        	cin >> s2;


                            	if (s1[0] == '0' || s2[0] == '0') {
                            		cout << "Input error" << endl;
                            		return;
                            	}
                            	product(s1, s2, result, &length);
                            	cout << endl << "The product is ";
                            	print(result, length);
                            	cout << endl;
                            	cout << "It has " << length - (result[0] == 0) << " digits" << endl;
                            	
                            	destroy(result);
                        }
 

جدیدترین ارسال ها

بالا