Big Integer Class using Dynamic Arrays

I have become overwhelmed an frustrated at these current operator overloads!

Currently I still can not get my +,* or istream operators working at all!

I could sincerely use some help!

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include <cstring>
#include "myint.h"


using namespace std;

MyInt::MyInt(int n)		
// default int constructor
{
	digits = NULL;		// setting array to null
	if(n <= 0)
	{
		setDefault();		
	}
	else
		Int2Array(n);
}

MyInt::~MyInt()		
// destructor
{
	delete[] digits;
}

MyInt::MyInt(const char *cstrg)		
// default cstring constructor 
{
	digits = NULL;
	Cstrg2Array(cstrg);
}

MyInt::MyInt(const MyInt & d)		
// copy constructor 
{
	maxSize = d.maxSize;
	currentSize = d.currentSize;

	digits = new int[d.maxSize];

	for(int i = 0; i < currentSize; i++)
		digits[i] = d.digits[i];
}


void MyInt::Int2Array(int n)
{
	currentSize = 0;	// intializing 
	maxSize = 2;
	int x = n;

		while(x != 0)	// finding length of the number
		{
			x = x / 10;		
			currentSize++;
		}
		maxSize = maxSize + currentSize;	// increasing array size to have 2 extra storage spaces then the number
	
	
	digits = new int[maxSize];		// creating new int array to pointer

	x = n;
	for(int i = 0; i < currentSize; x /=10, i++)	// for loop to assign each array location with a integer 
	{
		digits[i] = (x % 10);		// Mods the least significant digit and places it in the current array slot
	}
}

void MyInt::Cstrg2Array(const char *cstrg)
{
	currentSize = strlen(cstrg);	// reading number of char
	maxSize = currentSize + 2;

	digits = new int[maxSize];

	const char* read = cstrg;	// temp variable 
	for(int i = currentSize -1; i >= '\0'; i--)		// for loop to convert then assign each character to the array of int
	{
		int temp = C2I(*read);

		if(temp == -1)
		{
			setDefault();
			return;
		}
		digits[i] = temp;	// assigning array elements 

		read++;
	}
}


void MyInt::setDefault()	// sets default paramaters with error checking
{
	if(digits != NULL )		// deletes previous array if one was created
	{
		delete[] digits;
	}
	currentSize = 1;
	maxSize = 3;
	digits = new int[maxSize];
	digits[0] = 0;
}

void MyInt::testfunc()
{
	cout<< currentSize << endl;
	cout<< maxSize << endl;
	
	for(int i = 0; i < currentSize; i++)
	{
		cout<< digits[i]<< endl;
	}
}

// Operator overloads

//Member Functions

MyInt& MyInt::operator= (const MyInt & d)
// assigment operator
{
	if(this != &d)	// only makes a copy if variable isnt equal already
	{
		delete[] digits;

	maxSize = d.maxSize;
	currentSize = d.currentSize;

	digits = new int[d.maxSize];

	for(int i = 0; i < currentSize; i++)
		digits[i] = d.digits[i];

	}
	return *this;
}

MyInt& MyInt::operator++()
{
	int i = 0;

	while(digits[i] == 9)
	{
		digits[i] = 0;
		if(i == currentSize -1)
		{
			digits[currentSize] = 1;
			currentSize++;
			maxSize++;
		
			return *this;
		}
		i++;
		
	}
	digits[i]++;
	return *this;


}

MyInt MyInt::operator++(int) 
{
	MyInt temp = *this;
	int i = 0;

	while(digits[i] == 9)
	{
		digits[i] = 0;
		if(i == currentSize -1)
		{
			digits[currentSize] = 1;
			currentSize++;
			maxSize++;
		
			return temp;
		}
		i++;
		
	}
	digits[i]++;
	return temp;

}

MyInt& MyInt::operator--()
{
	int i = 0;

	while(digits[i] == 0)
	{
		digits[i] = 9;
		if(i == currentSize -1)
		{
			digits[i]--;
			return *this;
		}
		i++;
		
	}
	if(digits[i] == 1)
			{
				currentSize--;
				maxSize--;
				return *this;
			}
	digits[i]--;
	return *this;
}

MyInt MyInt::operator--(int)
{
	MyInt temp = *this;
	int i = 0;

	while(digits[i] == 0)
	{
		digits[i] = 9;
		if(i == currentSize -1)
		{
			digits[i]--;
			return temp;
		}
		i++;
		
	}
	if(digits[i] == 1)
			{
				currentSize--;
				maxSize--;
				return temp;
			}
	digits[i]--;
	return temp;
}
// Friend Functions

istream& operator>>(istream& , MyInt& )
{

}


ostream& operator<< (ostream &os, const MyInt &c)
{
	for(int i = c.currentSize -1 ; i >= 0; i--)
	{
		os << c.digits[i];
	}
	
	return os;

}

MyInt operator* (const MyInt& x, const MyInt& y)
{

}

MyInt operator+ (const MyInt& x, const MyInt& y)
{
	MyInt Temp;
	if( x.currentSize > y.currentSize)
	{
		int carry = 0;
		for(int i = x.currentSize -1; i >=0 ; i--)
		{
			int j;
			
			j = ( x.digits[i] + y.digits[i] + carry);
			Temp.digits[i] = (j % 10);
			carry = (j / 10);
			Temp.currentSize++;
			Temp.maxSize++;
		}
	}
	else 
	{
		int carry = 0;
		for(int i = y.currentSize -1; i >=0 ; i--)
		{
			int j;
			
			j = ( x.digits[i] + y.digits[i] + carry);
			Temp.digits[i] = (j % 10);
			carry = (j / 10);
			Temp.currentSize++;
			Temp.maxSize++;
		}
	}

	return Temp;
	
}

bool operator< (const MyInt& x, const MyInt& y)
{
}
bool operator> (const MyInt& x, const MyInt& y)
{
}
bool operator<= (const MyInt& x, const MyInt& y)
{}
bool operator>= (const MyInt& x, const MyInt& y)
{}
bool operator== (const MyInt& x, const MyInt& y)
{}
bool operator!= (const MyInt& x, const MyInt& y)
{}

int MyInt::C2I(char c)
// converts character into integer (returns -1 for error)
{
   if (c < '0' || c > '9')	return -1;	// error
   return (c - '0');				// success
}

char MyInt::I2C(int x)
// converts single digit integer into character (returns '\0' for error)
{
   if (x < 0 || x > 9)		return '\0';	// error
   return (static_cast<char>(x) + '0'); 	// success
}


// Add in operator overload and member function definitions 

Example:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  int size;

  std::cin >> size;

  int *array = new int[size];

  delete [] array;

  return 0;
}

I'm not exactly sure what you are asking, but here's a link that has an example of Dynamic Arrays:

http://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers

***Don't forget to delete and allocate new data
Topic archived. No new replies allowed.