-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargeNum2.cc
143 lines (129 loc) · 2.73 KB
/
largeNum2.cc
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
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
#include "largeNum2.h"
using namespace std;
LargeNum2::LargeNum2(){
size=0;
num = toArray( 0, size );
}
LargeNum2::LargeNum2( int initNum ){
size=0;
num = toArray( initNum, size );
}
LargeNum2::LargeNum2( int* num1, int size1 ){
size = size1;
num = num1;
}
LargeNum2::~LargeNum2(){
}
//This methods prints out the number
void LargeNum2::print(){
for( int i =0; i < size; i++){
cout<<num[i];
}
cout<<endl;
}
void LargeNum2::set( int value ){
num = toArray( value, size );
}
//This method takes an int and stores it in an array form
int* LargeNum2::toArray(int num, int& size){
string result;
stringstream ss;
ss << num;
result = ss.str();
size=result.length();
int* num4 = new int[size];
int nums;
for(int i=0; i<size;i++){
nums=result[i]-'0';
num4[i]=nums;
}
return num4;
}
//Implements adding two large numbers
LargeNum2 LargeNum2::sum(LargeNum2 number1, LargeNum2 number2){
int remain=0;
int size1 = number1.size;
int size2 = number2.size;
int* num1 = number1.num;
int* num2 = number2.num;
int j=max(size1, size2)+1;//size of sum array
int* sum = new int[j];//might have one extra 0 before real num
sum[0]=0;
int k=0;
if(size1>size2){
for(int i=size2; i>0;i--){
k=size1-(size2-i);
remain=num1[k-1]+num2[i-1];
if(remain>9){
remain=remain-10;
num1[k-2]++;
}
sum[k+1-1]=remain;
}
for(int i=size1-size2;i>0;i--){
sum[i+1-1]=num1[i-1];
}
}
else{
for(int i=size1; i>0;i--){
k=size2-(size1-i);
remain=num1[i-1]+num2[k-1];
if(remain>9){
remain=remain-10;
if(size1==size2&&i==1){
sum[0]++;
}
else{
num2[k-2]++;
}
}
sum[k]=remain;
}
for(int i=size2-size1;i>0;i--){
sum[i]=num2[i-1];
}
}
if( sum[0] == -1 )
j--;
LargeNum2 sumA( sum, j );
return sumA;
}
//precondition: size1 is greater than size2
//Handles the multiplication of large numbers
int* LargeNum2::multiply(int size1,int size2,int* num1,int* num2,int& size){
size=size1+size2;
int* res = new int[size];
int remain=0;
int remain2=0;
int k=size2;
for(int i=0;i<size;i++){
res[i]=0;
}
//the actual multiplication, no carrying occurs
while(k>0){
for(int i=size1;i>0;i--){
remain=num1[i-1]*num2[k-1]+remain2;
remain2=(int)(remain/10);
if(i!=1){
remain=remain%10;
}
res[size-(size1-i)-1-(size2-k)]+=remain;
}
remain2=0;
k--;
}
remain=0;
remain2=0;
//Carries over so each bucket has an int less than 10
for(int i=size;i>0;i--){
remain=res[i-1]+remain2;
remain2=(int)(remain/10);
remain=remain%10;
res[i-1]=remain;
}
return res;
}