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
|
// this function determines the larger number of two numbers and returns the larger number as max
vector <char> compare1 (const vector <char> &number1, const vector <char> &number2)
{
// assign max to the larger number and min to the smaller number
int lmax = 0;
vector<char>max(lmax);
// if the second number is longer than the first number, and hence larger
if (static_cast<int>(number2.size()) > static_cast<int>(number1.size()) )
{
lmax = static_cast<int>(number2.size());
max = number2;
}
// if the first number is longer than the second number, and hence larger
if (static_cast<int>(number1.size()) > static_cast<int>(number2.size()) )
{
lmax = static_cast<int>(number1.size());
max = number1;
}
// if the two numbers have the same length, compare integer in each position from the front of the vector until a larger integer is found, and hence the larger number can be determined
if (static_cast<int>(number1.size()) == static_cast<int>(number2.size()) )
{
lmax = static_cast <int> (number1.size());
for (i = 0; i < number1.size(); i++)
{
if (number1[i] > number2[i])
{
max = number1;
}
if (number1[i] < number2[i])
{
max = number2;
}
// quit loop once the larger number is determined
if (number1[i] != number2[i])
break;
if (number1[i] == number2[i])
{
max = number1;
}
}
}
return max;
} // end of compare1 function
// same compare function, but returns the smaller number as min
vector <char> compare2 (const vector <char> &number1, const vector <char> &number2)
{
// assign max to the larger number and min to the smaller number
int lmin = 0;
vector<char>min(lmin);
// if the second number is longer than the first number, and hence larger
if (static_cast<int>(number2.size()) > static_cast<int>(number1.size()) )
{
lmin = static_cast<int>(number1.size());
min = number1;
}
// if the first number is longer than the second number, and hence larger
if (static_cast<int>(number1.size()) > static_cast<int>(number2.size()) )
{
lmin = static_cast<int>(number2.size());
min = number2;
}
// if the two numbers have the same length, compare integer in each position from the front of the vector until a larger integer is found, and hence the larger number can be determined
if (static_cast<int>(number1.size()) == static_cast<int>(number2.size()) )
{
lmin = static_cast <int> (number1.size());
for (i = 0; i < number1.size(); i++)
{
if (number1[i] > number2[i])
{
min = number2;
}
if (number1[i] < number2[i])
{
min = number1;
}
// quit loop once the larger number is determined
if (number1[i] != number2[i])
break;
if (number1[i] == number2[i])
{
min = number2;
}
}
}
return min;
} // end of compare2 function
vector <char> multiply (const vector <char> &number1, const vector <char> &number2)
{
vector <char> result1;
vector <char> result2;
int pretimes = 0;
int carry = 0;
vector<char>max;
vector<char>min;
max = compare1(number1,number2);
min = compare2(number1,number2);
int k = min.size() - 1;
for (i = max.size() - 1; i >= 0; i--)
{
pretimes = (max[i] - 48) * (min[k] - 48) + carry;
carry = pretimes / 10;
result1.push_back (pretimes % 10 + '0');
}
if (carry != 0)
result1.push_back (carry + '0');
for (k = min.size() - 1; k >= 0; k--)
{
for (i = max.size() - 1; i >=0; i--)
{
carry = 0;
pretimes = (max[i] - 48) * (min[k-1] - 48) + carry;
carry = pretimes / 10;
result2.push_back (pretimes % 10 + '0');
}
if (carry != 0)
result2.push_back (carry + '0');
result1.push_back ('0');
result1 = addition(result1, result2);
}
if (carry != 0)
result1.push_back (carry + '0');
// reverse order of result so it's in the right order
result1 = flip(result1);
return result1;
} // end of multiply function
|