-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigIntFinal.cpp
218 lines (184 loc) · 4.28 KB
/
BigIntFinal.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
// Pointers to the previous and next digit
struct Node* next;
struct Node* prev;
Node(int);
};
// To initialize the structure with a single digit
Node::Node(int val)
{
data = val;
next = prev = NULL;
}
class HugeIntLL
{
public:
HugeIntLL();
~HugeIntLL();
// To insert a digit in front
void insertInFront(int);
// To insert a digit at the end
void insertInEnd(int);
// To display the large number
void display();
int length();
void add(HugeIntLL*, HugeIntLL*);
void mul(HugeIntLL*, HugeIntLL*);
Node* head;
Node* tail;
int size;
};
// Constructor of the Class
HugeIntLL::HugeIntLL()
{
head = tail = NULL;
size = 0;
}
// To insert at the beginning of the list
void HugeIntLL::insertInFront(int value)
{
Node* temp = new Node(value);
if (head == NULL)
head = tail = temp;
else
{
head->prev = temp;
temp->next = head;
head = temp;
}
size++;
}
// To insert in the end
void HugeIntLL::insertInEnd(int value)
{
Node* temp = new Node(value);
if (tail == NULL)
head = tail = temp;
else
{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
size++;
}
void HugeIntLL::display()
{
Node* temp = head;
while (temp != NULL)
{
cout << temp->data;
temp = temp->next;
}
}
int HugeIntLL::length()
{
return size;
}
/* Uses simple addition method that we
follow using carry*/
void HugeIntLL::add(HugeIntLL* a, HugeIntLL* b)
{
int c = 0, s;
HugeIntLL* a1 = new HugeIntLL(*a);
HugeIntLL* b1 = new HugeIntLL(*b);
// default copy constructor
// Copy Constructor - used to copy objects
this->head = NULL;
this->tail = NULL;
this->size = 0;
while (a1->tail != NULL || b1->tail != NULL)
{
if (a1->tail != NULL && b1->tail != NULL)
{
s = ((a1->tail->data) + (b1->tail->data) + c) % 10;
c = ((a1->tail->data) + (b1->tail->data) + c) / 10;
a1->tail = a1->tail->prev;
b1->tail = b1->tail->prev;
}
else if (a1->tail == NULL && b1->tail != NULL)
{
s = ((b1->tail->data) + c) % 10;
c = ((b1->tail->data) + c) / 10;
b1->tail = b1->tail->prev;
}
else if (a1->tail != NULL && b1->tail == NULL)
{
s = ((a1->tail->data) + c) % 10;
c = ((a1->tail->data) + c) / 10;
a1->tail = a1->tail->prev;
}
// Inserting the sum digit
insertInFront(s);
}
// Inserting last carry
if (c != 0)
insertInFront(c);
}
// Normal multiplication is used i.e. in one to all way
void HugeIntLL::mul(HugeIntLL* a, HugeIntLL* b)
{
int k = 0, i;
HugeIntLL* tpro = new HugeIntLL();
while (b->tail != NULL)
{
int c = 0, s = 0;
HugeIntLL* temp = new HugeIntLL(*a);
HugeIntLL* pro = new HugeIntLL();
while (temp->tail != NULL)
{
s = ((temp->tail->data) * (b->tail->data) + c) % 10;
c = ((temp->tail->data) * (b->tail->data) + c) / 10;
pro->insertInFront(s);
temp->tail = temp->tail->prev;
}
if (c != 0)
pro->insertInFront(c);
for (i = 0; i < k; i++)
pro->insertInEnd(0);
add(this, pro);
k++;
b->tail = b->tail->prev;
pro->head = pro->tail = NULL;
pro->size = 0;
}
}
// Driver code
int main()
{
HugeIntLL* m = new HugeIntLL();
HugeIntLL* n = new HugeIntLL();
HugeIntLL* s = new HugeIntLL();
HugeIntLL* p = new HugeIntLL();
HugeIntLL* d = new HugeIntLL();
HugeIntLL* q = new HugeIntLL();
string s1,s2;
cout<<"1st decimal numbers A :"<<endl;
cin>>s1;
cout<<"2nd decimal number B:"<<endl;
cin>>s2;
for (int i = 0; i < s1.length(); i++)
m->insertInEnd(s1.at(i) - '0');
for (int i = 0; i < s2.length(); i++)
n->insertInEnd(s2.at(i) - '0');
// Creating copies of m and n
HugeIntLL* m1 = new HugeIntLL(*m);
HugeIntLL* n1 = new HugeIntLL(*n);
HugeIntLL* m2 = new HugeIntLL(*m);
HugeIntLL* n2 = new HugeIntLL(*n);
HugeIntLL* m3 = new HugeIntLL(*m);
HugeIntLL* n3 = new HugeIntLL(*n);
cout << "Sum(A+B) :" << endl;
p->add(m1, n1);
p->display();
cout << endl;
cout << "Product(A*B) :" << endl;
s->mul(m, n);
s->display();
cout << endl;
return 0;
}