Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5) Finally, print the result reversely to get the answer of the expression, 1. A

ID: 3599988 • Letter: 5

Question

5) Finally, print the result reversely to get the answer of the expression, 1. Addition of Two Big Integers As we all have known that integers in C have limited range. But sometimes that limitation just has.ta be conquered. And now we are gepnedo it. Input an integer addition expression (addition between two positive integers), with the number of digits of each integer being at most 1,000. Please note that the result could be 1001 digits. Example Other Requirements eAt least have the following functions defined and called in your code: void reverse(dot, array[], 40JRA) {}, to perform in-place reverse of an integer array 40t, gate 4car c) {), to convert a number character to an integer (e.g. convert '1" to 1) * void addfint, numi], at num2],nt as1) ), to perform digit-by-digit addition of nuni and num2 and store the result into ans. 1211112IE11212212222122122222212221222212121 he answer is Hints 1 Get the input expressio 2) Split your input by into two integer arrays, say n as a string. With every array element holding only 1 digit. Be aware of what you need to do about the conversion from a char to an int. Reverse numi and nun2 so that the two numbers are aligned by lower digits. See below: 3) 123456 654321 381 183 4) Perform digit-by-digit addition by using a loop to iterate through num1 and num2 at the same time, and be cautious when there is a carry. See below 654321 738321 183

Explanation / Answer

//Here is the complete code for the above problem


#include <iostream>
#include <string>

using namespace std;

// Returns true if str1 is smaller than str2.
bool isSmaller(string str1, string str2)
{
   // Calculate lengths of both string
   int n1 = str1.length(), n2 = str2.length();

   if (n1 < n2)
       return true;
   if (n2 < n1)
       return false;

   for (int i = 0; i<n1; i++)
       if (str1[i] < str2[i])
           return true;
       else if (str1[i] > str2[i])
           return false;

   return false;
}

// Function for find difference of larger numbers
string findDiff(string str1, string str2)
{
   // Before proceeding further, make sure str1
   // is not smaller
   if (isSmaller(str1, str2))
   {
       swap(str1, str2);
   }

   // Take an empty string for storing result
   string str = "";

   // Calculate length of both string
   int n1 = str1.length(), n2 = str2.length();

   // Reverse both of strings
   reverse(str1.begin(), str1.end());
   reverse(str2.begin(), str2.end());

   int carry = 0;

   // Run loop till small string length
   // and subtract digit of str1 to str2
   for (int i = 0; i<n2; i++)
   {
       // Do school mathematics, compute difference of
       // current digits
       int sub = ((str1[i] - '0') - (str2[i] - '0') - carry);

       // If subtraction is less then zero
       // we add then we add 10 into sub and
       // take carry as 1 for calculating next step
       if (sub < 0)
       {
           sub = sub + 10;
           carry = 1;
       }
       else
           carry = 0;

       str.push_back(sub + '0');
   }

   // subtract remaining digits of larger number
   for (int i = n2; i<n1; i++)
   {
       int sub = ((str1[i] - '0') - carry);
       carry = 0;
       str.push_back(sub + '0');
   }

   // reverse resultant string
   reverse(str.begin(), str.end());

   return str;
}

// Function for finding sum of larger numbers
string findSum(string str1, string str2)
{
   // Before proceeding further, make sure length
   // of str2 is larger.
   if (str1.length() > str2.length())
       swap(str1, str2);

   // Take an empty string for storing result
   string str = "";

   // Calculate lenght of both string
   int n1 = str1.length(), n2 = str2.length();
   int diff = n2 - n1;

   // Initialy take carry zero
   int carry = 0;

   // Traverse from end of both strings
   for (int i = n1 - 1; i >= 0; i--)
   {
       // Do school mathematics, compute sum of
       // current digits and carry
       int sum = ((str1[i] - '0') +
           (str2[i + diff] - '0') +
           carry);
       str.push_back(sum % 10 + '0');
       carry = sum / 10;
   }

   // Add remaining digits of str2[]
   for (int i = n2 - n1 - 1; i >= 0; i--)
   {
       int sum = ((str2[i] - '0') + carry);
       str.push_back(sum % 10 + '0');
       carry = sum / 10;
   }

   // Add remaining carry
   if (carry)
       str.push_back(carry + '0');

   // reverse resultant string
   reverse(str.begin(), str.end());

   return str;
}


// Driver code
int main()
{
   char operation ;
   string expression, number1,number2;
   bool isSmall = false;

   int operation_place = 0 , first_number_start = 0, first_number_end,
       second_number_start,second_number_end;

   cout << "Please input your expression(do not enter space in the expression) :" << endl;

   cin >> expression;

   int lenght_of_expression = expression.length();

   for (char& c : expression)
   {
       if (c == '+' || c == '-')
       {
           operation = c;
           break;
       }
      
       operation_place++;
   }

   if(operation_place == lenght_of_expression)
   {
       cout << "Incorrect or no operator";
       return 0;
   }

   first_number_end = operation_place - 1;
   second_number_start = operation_place + 1;
   second_number_end = lenght_of_expression;

   number1 = expression.substr(first_number_start, (first_number_end - first_number_start) + 1);

   number2 = expression.substr(second_number_start, (second_number_end - second_number_start) + 1);

   if (operation == '+')
   {
       cout << "The answer is : " << findSum(number1, number2);
   }
   else if (operation == '-')
   {
       if (isSmaller(number1, number2))
       {
           isSmall = true;
       }
       if(isSmall)
            cout << "The answer is : -" << findDiff(number1, number2);
       else
           cout << "The answer is : " << findDiff(number1, number2);
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote