0

hi can anyone help me with my code, im trying to write a bigInt multiply function but this function got some bugs(it prints additional zero at the left side)
input: 3 2
output 06

and ...

#include <cstring>
#include <iostream>
using namespace std;

const int mmax = 10000;

char result[mmax];
void reverse(char* in)
{
	int len = strlen(in);
	int tail = len - 1;
	for(int i = 0; i < len / 2; i++)
	{
		char temp = in[i];
		in[i] = in[tail];
		in[tail] = temp;
		tail--;

	}
}
void multiply(char* first, char* second)
{
	char a[mmax], b[mmax];
	strcpy(a, first); strcpy(b, second);
	int lena = strlen(a);
	int lenb = strlen(b);
	int lenc = lena + lenb;

	reverse(a), reverse(b);

	for(int i = 0; i <= lenc; i++)
		result[i] = '0';
	result[lenc + 1] = '\0';
	int i, j;
	for(i = 0; i < lenb; i++)
	{
		int keep = 0;
		for(j = 0; j < lena; j++)
		{
			int res = (a[j] - '0') * (b[i] - '0') + keep + result[i + j] -'0';
			keep = res / 10;
			result[i + j] = res % 10 + '0';
		}
		while(keep)
		{
			int res = result[i + j] - '0' + keep;
			result[i + j] = res % 10 + '0';
			keep /= 10;
			j++;
		}
	}
	int kk;
	for(kk = lenc -1  ; i > 0; i--)
	{
		if(result[kk] != '0')
			break;
	}
	result[kk + 1] = '\0';
	reverse(result);
}

int main(void){
	char first[mmax], second[mmax];
	cin >> first >> second;
	multiply(first, second);
	cout << result << endl;
	return 0;
}
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by noobuser
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.