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
|
void display (string &number1,string &number2)
{
//Tell user to type in twenty digit
cout<<"Twenty digit number: ";
//get all the numbers until space is found
getline(cin,number1);
//If the length is greater then 20. ouput a message
if (number1.length() >10)
{
number1.clear();
cout<<"Enter Valid Number: ";
cin>>number1;
}
cin.ignore(1000,'\n');
//Tell user to enter in second twenty digit number
cout<<"\n\nTwenty digit number: ";
//get all the numbers until space is found
getline(cin,number2);
//If the length is greater then 20. ouput a message
if (number2.length() >10)
{
number2.clear();
cout<<"Enter Valid Number: ";
cin>>number2;
}
cin.ignore(1000,'\n');
}
void addnumber_formula(string one,string two, int result[],int resultsize)
{
//Declare sum to store the sum of indexes
int sum=0;
int lastnum=0;
int carry=0;
int row=0;
int counter;
//find the length of each string.
int length1, length2;
int max;
length1=one.length();
length2=two.length();
//Determine which one of these is a max
if (length1>length2)
max=length1;
else
max=length2;
//make a for loop to get each letter of a string
for (counter=one.length()-1;counter>=0;counter--)
{
sum=(one[counter]-'0')+(two[counter]-'0')+carry;
//Find the last number of sum
lastnum=sum%10;
//find first number of sum
carry=sum/10;
//store last number in the result
result[row]=lastnum;
row++;
//if max is equal to one then make sure to enter carry in the array
if (counter==1)
{
result[row]=carry;
}
}
}
void print_addition(int result[],int size)
{
for (size=4;size>=0;size--)
cout<<result[size];
}
int _tmain(int argc, _TCHAR* argv[])
{
//Cal he function to get the twenty digit number
string one,two;
display(one, two);
int results[5];
//Call addnumberformula
addnumber_formula(one, two, results,5);
print_addition(results,5);
|