204k views
1 vote
Write a function that multiplies two decimal string representations of non-negative integers. The function takes two parameters cx and cy, which are string decimal representations of integers, for example, cx = "1237091" cy = "23015" and returns their product as a string decimal representation: "28471649365". This is the function’s signature: Bool Multiply Integers(char* cx, char* cy, char* res);

User Seamas
by
6.2k points

1 Answer

5 votes

Answer:

See explaination

Step-by-step explanation:

#include<bits/stdc++.h>

using namespace std;

string multiply(char* num1,char* num2)

{

int l1 = strlen(num1);

int l2 = strlen(num2);

if(l1 ==0 || l2 == 0)return "0";

vector<int> result(l1+l2,0);

int in1=0,in2=0;

for(int i=l1-1;i>=0;i--)

{

int carry = 0;

int n1 = num1[i]-'0';

in2 = 0;

for(int j = l2-1;j>=0;j--)

{

int n2 = num2[j]-'0';

int sum = n1*n2 + result[in1+in2] + carry;

carry = sum /10;

result[in1+in2] = sum%10;

in2++;

}

if(carry>0)

{

result[in1+in2]+=carry;

}

in1++;

}

int i = result.size()-1;

while(i>=0&&result[i]==0)

{

i--;

}

if(i == -1)return "0";

string s = "";

while(i>=0){

string ch = "0";

ch[0] = '0'+result[i--];

s += ch;

}

return s;

}

int MultipyCIntegers(char* cx,char* cy,char* res)

{

string result = multiply(cx,cy);

if(strlen(res)!=result.length())return 0;

for(int i=0;i<result.length();i++)

{

if(res[i]!=result[i])

{

return 0;

}

}

return 1;

}

int main()

{

char* s1 = "1235421415454545454545454544";

char* s2 = "1714546546546545454544548544544545";

char* ans = "2118187521397235888154583183918321221520083884298838480662480";

cout<<MultipyCIntegers(s1,s2,ans);

}

User Beowulf
by
5.2k points