Addition with extremely huge numbers in C++
In C++ you the data type with the largest range is the long long, with a maximum limit of ~19 digits. This means that you can`t make additions with numbers that result a larger number. I know this is a crazy thing, why would you need to calculate with such huge numbers ? I think never..., but the method is very useful and ingenious.Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(){
string nr;
bool validnr = false;
while(!validnr){
cout << "Enter the first number: " << endl;
cin >> nr;
validnr = true;
//Check the number for invalid characters
for(int i=0;i<nr.length();i++){
if((int) nr[i] < 48 || (int) nr[i] > 57){
//48 is the ASCII code of number 0
//57 is the ASCII code of number 9
validnr = false;
}
}
if(!validnr){
cout << endl << "Please enter a valid number!" << endl;
}
}
string nr2;
validnr = false;
//Do the same for the second number
while(!validnr){
cout << "Enter the second number: " << endl;
cin >> nr2;
validnr = true;
for(int i=0;i<nr2.length();i++){
if((int) nr2[i] < 48 || (int) nr2[i] > 57){
validnr = false;
}
}
if(!validnr){
cout << endl << "Please enter a valid number!" << endl;
}
}
int digitsnr;
//Make the two digits to have the same size
//by adding 0`s in front of it
//This is important because we can`t add a digit with nothing
if(nr.length() > nr2.length()){
digitsnr = nr.length() - 1;
while(nr.length() != nr2.length()){
nr2 = "0" + nr2;
}
}
else{
digitsnr = nr2.length() - 1;
while(nr.length() != nr2.length()){
nr = "0" + nr;
}
}
string result;
char digit;
int remainder = 0; //If the result of two digits is more than 10
//we need to add 1 to the following digit(e.g: 5 + 7 = 12, 12 is made from 2 digits
for(int i=digitsnr;i>=0;i--){ //Loop through each digit
if(remainder + nr[i] + nr2[i] - 96 > 9){
digit = remainder + nr[i] + nr2[i] - 48 - 10; //We are working with ASCII codes
result = digit + result;
remainder = 1; //We have to increase the following digit
}
else{
digit = remainder + nr[i] + nr2[i] - 48;
remainder = 0;
result = digit + result;
}
}
if(remainder){
result = "1" + result;
//Add the last remaining if required
}
cout << endl << "Result = " << result << endl;
system("pause");
}
The numbers are stored in strings, each character of the strings is checked to avoid wrong characters. Each character is checked using it`s ASCII code, (for example 0 has the 47 code number, 1 has the 49 code). After the numbers are validated the two numbers are added digit by digit starting from the last number:#include <string>
using namespace std;
int main(){
string nr;
bool validnr = false;
while(!validnr){
cout << "Enter the first number: " << endl;
cin >> nr;
validnr = true;
//Check the number for invalid characters
for(int i=0;i<nr.length();i++){
if((int) nr[i] < 48 || (int) nr[i] > 57){
//48 is the ASCII code of number 0
//57 is the ASCII code of number 9
validnr = false;
}
}
if(!validnr){
cout << endl << "Please enter a valid number!" << endl;
}
}
string nr2;
validnr = false;
//Do the same for the second number
while(!validnr){
cout << "Enter the second number: " << endl;
cin >> nr2;
validnr = true;
for(int i=0;i<nr2.length();i++){
if((int) nr2[i] < 48 || (int) nr2[i] > 57){
validnr = false;
}
}
if(!validnr){
cout << endl << "Please enter a valid number!" << endl;
}
}
int digitsnr;
//Make the two digits to have the same size
//by adding 0`s in front of it
//This is important because we can`t add a digit with nothing
if(nr.length() > nr2.length()){
digitsnr = nr.length() - 1;
while(nr.length() != nr2.length()){
nr2 = "0" + nr2;
}
}
else{
digitsnr = nr2.length() - 1;
while(nr.length() != nr2.length()){
nr = "0" + nr;
}
}
string result;
char digit;
int remainder = 0; //If the result of two digits is more than 10
//we need to add 1 to the following digit(e.g: 5 + 7 = 12, 12 is made from 2 digits
for(int i=digitsnr;i>=0;i--){ //Loop through each digit
if(remainder + nr[i] + nr2[i] - 96 > 9){
digit = remainder + nr[i] + nr2[i] - 48 - 10; //We are working with ASCII codes
result = digit + result;
remainder = 1; //We have to increase the following digit
}
else{
digit = remainder + nr[i] + nr2[i] - 48;
remainder = 0;
result = digit + result;
}
}
if(remainder){
result = "1" + result;
//Add the last remaining if required
}
cout << endl << "Result = " << result << endl;
system("pause");
}
123 + 56 =
9
79
179
Is that simple, but if something it`s not clear please don`t hesitate, ASK ME!9
79
179


11:39 AM
Csabi

6 comments:
Although you did a good job, it is not very "imaginative". This is a technique that is used a lot for arbitrary sized numbers.
This script was a part of a homework, where we calculated the value of 1 + 2 + 2^2 + 2^3 + ... + 2^63
Google for 'C++ bignum'. There are libraries for this. Perhaps some of them are open-source, which would be useful to see how this is usually implemented. Using strings isn't very efficient.
This is a common technique for arbitrary numbers, don't find something innovative.
That's very good. Just a question: doesn't a string have a limit, too?
I`m not sure, but I think a string has no limit, it`s limited only by your memory size
Post a Comment