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
|
#include <iostream>
using namespace std;
const int SIZE=20;
char overflow;
void InitNumber(char[], char[], int, int);
void AddArrays(char[], char[], char[], char& overflow);
void OutputNumber(char[], char[], char[]);
int main() {
int size, numDigits1, numDigits2;
char digits1[SIZE];
char digits2[SIZE];
char sum[SIZE];
char repeatAnswer;
do{
InitNumber(digits1, digits2, size, size);
AddArrays(digits1, digits2, sum, overflow);
if(overflow != 'y'){
OutputNumber(digits1, digits2, sum);
}
cout << "\nWould you like to add two more numbers (y/n)? ";
cin >> repeatAnswer;
}
while(repeatAnswer == 'y');
}
void InitNumber(char digits1[], char digits2[], int numDigits1, int numDigits2){
int i;
cout << "Please enter your first number\n" << "How many digits are in your number? ";
cin >> numDigits1;
//A loop continues until the user has input the total number of digits specified before
cout << "Please enter your number starting with the LEAST significant digit: ";
for(i = SIZE-1; i >= SIZE - numDigits1; i--) {
cin >> digits1[i];
}
//Stores a 0 in each of the remaining spots of the array
for(i = SIZE - numDigits1 - 1; i >= 0; i--) {
digits1[i] = '0';
}
cout << "Please enter your second number\n" << "How many digits are in your number? ";
cin >> numDigits2;
//A loop continues until the user has input the total number of digits specified before
cout << "Please enter your number starting with the LEAST significant digit: ";
for(i = SIZE-1; i >= SIZE - numDigits2; i--) {
cin >> digits2[i];
}
//Stores a 0 in each of the remaining spots of the array
for(i = SIZE - numDigits2 - 1; i >= 0; i--) {
digits2[i] = '0';
}
}
void AddArrays(char digits1[], char digits2[], char sum[], char& overflow){
int i, tempDigits1, tempDigits2, tempSum, tempOne = 0;
for(i = SIZE-1; i >= 0; i--){
tempDigits1 = digits1[i] - '0';
tempDigits2 = digits2[i] - '0';
tempSum = tempDigits1 + tempDigits2 + tempOne;
if(tempSum >= 10){
switch(tempSum){
case 10:
sum[i] = '0';
tempOne = 1;
break;
case 11:
sum[i] = '1';
tempOne = 1;
break;
case 12:
sum[i] = '2';
tempOne = 1;
break;
case 13:
sum[i] = '3';
tempOne = 1;
break;
case 14:
sum[i] = '4';
tempOne = 1;
break;
case 15:
sum[i] = '5';
tempOne = 1;
break;
case 16:
sum[i] = '6';
tempOne = 1;
break;
case 17:
sum[i] = '7';
tempOne = 1;
break;
case 18:
sum[i] = '8';
tempOne = 1;
break;
case 19:
sum[i] = '9';
tempOne = 1;
break;
default:
break;
}
}
else{
sum[i] = (tempDigits1 + tempDigits2 + tempOne) + '0';
tempOne = 0;
}
}
if((tempDigits1 + tempDigits2 + tempOne) >= 10){
for(i = 0; i < SIZE; i++) {
cout << digits1[i];
}
cout << " + ";
for(i = 0; i < SIZE; i++) {
cout << digits2[i];
}
cout << " = ERROR: Addition Overflow\n";
overflow = 'y';
}
}
void OutputNumber(char digits1[], char digits2[], char sum[]){
int i, tempDigits1, tempDigits2;
tempDigits1 = digits1[0] - '0';
tempDigits2 = digits2[0] - '0';
cout << endl;
for(i = 0; i < SIZE; i++) {
cout << digits1[i] - '0';
}
cout << " + ";
for(i = 0; i < SIZE; i++) {
cout << digits2[i];
}
cout << " = ";
for(i = 0; i < SIZE; i++) {
cout << sum[i];
}
}
|