RunTime Termination for the Multiplication Function for Extremely Big Integers

Good Day Everyone!
I have a code that adds and multiplies really large integers but it seems that my multiplication function (string multiplyLargeNumbers(string num1, string num2);)has a problem. I want to put a string initializer (multiplicationlevel = "";) after this following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
if (temps != ""){
         
         zeromulti += 1;
         for ( int z = zeromulti; z > 0; --z) {multiplicationlevel += "0";}
         
         temps2 = addLargeNumbers(multiplicationlevel, temps);
         temps = temps2;
         
         
         }
         
    else if ( temps == "") { temps = multiplicationlevel; }


Unfortunately the program terminates unexpectedly (Runtime termination) when I multiply big numbers (e.g. 22 * 2). The program adds but it doesn't multiply.
I would like to reinitialize multiplicationlevel in order to avoid the old value to be included in the new string.

The whole code is this:

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
#include <iostream>
#include <string>
#include <stack>

using namespace std;
//Acts as an helper function for large numeric string addition
void deleteLeadingZeros(string& num){

	//delete any starting 0's
	if(num[0] == '0'){
		unsigned int strtCpyIndex = 0;
		strtCpyIndex = num.find_first_not_of("0");
		string temp = num.substr(strtCpyIndex);
		num = temp;
	}
}
//adds leading 0's for the smaller string
void equalizeLength(string& num1 , string& num2, char pad = '0'){

	if(num1.size() < num2.size()){
		unsigned int diff = num2.size() - num1.size();
		string temp;
		while(diff--) //add starting zeros
			temp += pad;
		temp += num1;
		num1 = temp;
	}
	else if(num2.size() < num1.size()){
		unsigned int diff = num1.size() - num2.size();
		string temp;
		while(diff--) //add starting zeros
			temp += pad;
		temp += num2;
		num2 = temp;
	}
}

string addLargeNumbers(string num1, string num2)
{	

	//simple error check
/*	if(num1.empty())
		return num2;
	else if(num2.empty())
		return num1;
*/ // not sure if needed	
//check for valid information passed here if you want


	//holds out result of addition
	stack<short> addResult; 
	//addResult in string version
	string returnResult = "";	
	
	//delete any starting 0's	
	//example if num1 = 001
	//after the call below num = 1
	deleteLeadingZeros(num1);
	deleteLeadingZeros(num2);

	//match size by adding in leading 0's for the smaller number
	//example num1 = 100 and num2 = 50
	//after the call num1 = 100 and num2 = 050
	equalizeLength(num1,num2);
	
	int lastAddElem = num1.size()- 1;

	bool hasCarry = false;

	short result = 0; //holds result of each integer addition

	//start calculation
	for(int i = lastAddElem; i >= 0; --i)
	{
		result = (num1[i] - '0') + (num2[i] - '0') ; //convert to decimal and add
		
		if(result < 10 && !hasCarry)
			addResult.push(result);
		else
		{			
			if(hasCarry){
				result += 1; //account for the carry				
				hasCarry = result < 10 ? false : true;
				addResult.push(result%10);
			}
			else
			{
				hasCarry = true;
				addResult.push(result % 10 );
			}
		}

	}
	//check for highest bit carry
	if(hasCarry)
		addResult.push(result/10);

	//extract data
	while(addResult.size()){
		returnResult += (addResult.top() + '0');		
		addResult.pop();
	}

	return returnResult;
}

string multiplyLargeNumbers(string num1, string num2)
{	
    
string temps = "", temps2 = "";
int zeromulti = 0;
short rmndr;
short carry;
	//simple error check
/*	if(num1.empty())
		return num2;
	else if(num2.empty())
		return num1;
*/ // not sure if needed	
//check for valid information passed here if you want


	//holds out result of multiplication
	stack<short> multiplyResult; 
	
	string multiplicationlevel = "";	
	
	//delete any starting 0's	
	//example if num1 = 001
	//after the call below num = 1
	deleteLeadingZeros(num1);
	deleteLeadingZeros(num2);

	//match size by adding in leading 0's for the smaller number
	//example num1 = 100 and num2 = 50
	//after the call num1 = 100 and num2 = 050
	equalizeLength(num1,num2);
	
	int lastAddElem = num1.size()- 1;

	bool hasCarry = false;

	
    
    short result = 0; //holds result of each integer addition

	//start calculation
	for(int i = lastAddElem; i >= 0; --i)
	{
		for(int t = lastAddElem; t >= 0; --t)
        {
                result = 0;
                result = (num1[t] - '0') * (num2[i] - '0'); //convert to decimal and multiply                
		
        
        if(result < 10 && !hasCarry)
			{multiplyResult.push(result);}
//		else
//		{			
			else if(hasCarry){
				result += carry; //account for the carry				
				hasCarry = result < 10 ? false : true;
				rmndr = (result/10);
				rmndr = (result - (rmndr*10));
				multiplyResult.push(rmndr);
				carry = result/10;
                }
//			}
			else
			{
				hasCarry = true;
				rmndr = result/10;
				rmndr = (result - (rmndr*10));
				multiplyResult.push(rmndr);
				carry = result/10;

			}
        
        
        }

	if(hasCarry)
		{multiplyResult.push(carry);}
	
    
//	else if ( multiplicationlevel != "" ) { multiplicationlevel = "0"; }
	
    while(multiplyResult.size()){
		multiplicationlevel += (multiplyResult.top() + '0');		
		multiplyResult.pop();
                            }
  
	
	
	
	if (temps != ""){
         
         zeromulti += 1;
         for ( int z = zeromulti; z > 0; --z) {multiplicationlevel += "0";}
         
         temps2 = addLargeNumbers(multiplicationlevel, temps);
         temps = temps2;
         
         
         }
         
    else if ( temps == "") { temps = multiplicationlevel; }
//    multiplicationlevel = "";
    multiplicationlevel.erase (multiplicationlevel.begin(), multiplicationlevel.end());

//    if (multiplicationlevel != "") {cout << "Check Temps:" << temps << endl;}
    
    }
//    deleteLeadingZeros(temps);

	return temps;
}


int main()
{	
    
	string num1,num2;
	while(true){
		cout<<"Enter 2 integer for addition and multiplication: ";
		cin >> num1 >> num2;
		cout << num1 << "+" << num2 <<" = "<<addLargeNumbers(num1,num2)<<endl;
		
		cout << num1 << "*" << num2 <<" = " << multiplyLargeNumbers(num1, num2) << endl;
	}
	
}


I would extremely appreciate any help. Thank You Very Much! :)
Last edited on
Topic archived. No new replies allowed.