-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbigproduct.cpp
More file actions
109 lines (103 loc) · 1.76 KB
/
bigproduct.cpp
File metadata and controls
109 lines (103 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Author: Saurabh Odhyan
Program to compute product of very big integers
*/
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
class BIG_PRODUCT {
private:
string a,b;
public:
vector<int> str_intvec(string s)
{
vector<int> a;
for(int i=0;i<s.length();i++)
a.push_back((int)(s[i]-'0'));
return a;
}
vector<int> add(vector<int> x1,vector<int> x2)
{
int s=0,c=0;
for(int k=x2.size();k<x1.size();k++)
x2.push_back(0);
for(int i=0;i<x1.size();i++)
{
s=x1[i]+x2[i]+c;
x2[i]=s%10;
c=s/10;
}
if(s>0)
{
while(c>0)
{
int a=c%10;
x2.push_back(a);
c=c/10;
}
}
return x2;
}
vector<int> multiply(vector<int> a,vector<int> b)
{
vector<int> p,s(max(a.size(),b.size()),0);
for(int i=a.size()-1;i>=0;i--)
{
int c=0;
int y=a.size()-1;
p.clear();
while(y>i)
{
p.push_back(0);
y--;
}
for(int j=b.size()-1;j>=0;j--)
{
int x=a[i]*b[j]+c;
p.push_back(x%10);
c=x/10;
}
if(c!=0)
p.push_back(c);
s=add(p,s);
}
return s;
}
vector<int> format(vector<int> f)
{
vector<int> s;
int x=1,l=0,k=0;
for(int i=f.size()-1;i>=0;i--)
{
if(!(x==1 && f[i]==0))
{
s.push_back(f[i]);
l++;
x=0;
}
}
if(x)
s.push_back(0);
return s;
}
vector<int> getproduct(string s1,string s2)
{
vector<int> a,b,c;
a=str_intvec(s1);
b=str_intvec(s2);
c=multiply(a,b);
return format(c);
}
};
int main()
{
string s1,s2;
BIG_PRODUCT bp;
while(cin>>s1>>s2)
{
vector<int> res=bp.getproduct(s1,s2);
}
return 0;
}