Guest User

Untitled

a guest
Jun 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. long int add(long int, long int);
  9. int range_check(int, long int);
  10. long int remainder(int, long int);
  11.  
  12. struct num_data
  13. {
  14.     int size;
  15.     long int value;
  16. };
  17.  
  18. int main()
  19. {
  20.     int list_index = 0;
  21.     int list_count = 0;
  22.     int size;
  23.     int list_size_start, list_size_end;
  24.     int x8, x4, x2, x1;
  25.  
  26.     int size_diff;
  27.     int large_size, small_size;
  28.  
  29.     string a, b;
  30.     string answer;
  31.  
  32.     cout << "Please enter Num1: ";
  33.     cin  >> a;
  34.  
  35.     cout << "Please enter Num2: ";
  36.     cin  >> b;
  37.  
  38.     cout << "Number 1: " << a << endl;
  39.     cout << "Number 2: " << b << endl;
  40.  
  41.     if(b.size() > a.size())
  42.     {
  43.         large_size = b.size();
  44.         size = large_size;
  45.  
  46.         //cout << size;
  47.  
  48.         small_size = a.size();
  49.         size_diff = large_size - small_size;
  50.  
  51.         for(int size_index = 0; size_index < size_diff; size_index++)
  52.         {
  53.             a = '0' + a;
  54.         }
  55.     }
  56.     else if(a.size() > b.size())
  57.     {
  58.         large_size = a.size();
  59.         size = large_size;
  60.  
  61.         //cout << size;
  62.  
  63.         small_size = b.size();
  64.         size_diff = large_size - small_size;
  65.  
  66.         for(int size_index = 0; size_index < size_diff; size_index++)
  67.         {
  68.             b = '0' + b;
  69.         }
  70.     }
  71.     else
  72.     {
  73.         size = a.size();
  74.     }
  75.     list_size_end = list_size_start = size;
  76.  
  77.     x8 = floor(size / 8);
  78.     size = size - (x8 * 8);
  79.     list_index += x8;
  80.  
  81.     //cout << x8 << endl;
  82.  
  83.     x4 = floor(size / 4);
  84.     size = size - (x4 * 4);
  85.     list_index += x4;
  86.  
  87.     //cout << x4 << endl;
  88.  
  89.     x2 = floor(size / 2);
  90.     size = size - (x2 * 2);
  91.     list_index += x2;
  92.  
  93.     //cout << x2 << endl;
  94.  
  95.     x1 = floor(size / 1);
  96.     size = size - (x1 * 1);
  97.     list_index += x1;
  98.  
  99.     //cout << x1 << endl;
  100.  
  101.     num_data num_list[list_index];
  102.     long int val1[list_index];
  103.     long int val2[list_index];
  104.     long int result[list_index];
  105.     string value[list_index];
  106.  
  107.     if(x8 != 0)
  108.     {
  109.         int index = 0;
  110.         for(; index < x8; list_count++, index++)
  111.         {
  112.             string temp1, temp2;
  113.             stringstream stream1, stream2, stream3;
  114.  
  115.             if(index == 0)
  116.             {
  117.                 list_size_start -= 8;
  118.             }
  119.             else
  120.             {
  121.                 list_size_start -= 8;
  122.                 list_size_end -= 8;
  123.             }
  124.             temp1 = a.substr(list_size_start, 8);
  125.             //cout << "Index Start: " << list_size_start << endl;
  126.             //cout << "Temp1: " << temp1 << endl;
  127.             temp2 = b.substr(list_size_start, 8);
  128.             //cout << "Temp2: " << temp2 << endl;
  129.  
  130.             stream1 << temp1;
  131.             stream1 >> val1[list_count];
  132.  
  133.             stream2 << temp2;
  134.             stream2 >> val2[list_count];
  135.  
  136.             result[list_count] = add(val1[list_count], val2[list_count]);
  137.  
  138.             //cout << result[list_count] << endl;
  139.  
  140.             num_list[list_count].size = 8;
  141.             num_list[list_count].value = result[list_count];
  142.             //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
  143.         }
  144.     }
  145.  
  146.     if(x4 != 0)
  147.     {
  148.         int index = 0;
  149.         for(; index < x4; list_count++, index++)
  150.         {
  151.             string temp1, temp2;
  152.             stringstream stream1, stream2, stream3;
  153.  
  154.             if(index == 0)
  155.             {
  156.                 list_size_start -= 4;
  157.             }
  158.             else
  159.             {
  160.                 list_size_start -= 4;
  161.                 list_size_end -= 4;
  162.             }
  163.             temp1 = a.substr(list_size_start, 4);
  164.             //cout << "Index Start: " << list_size_start << endl;
  165.             //cout << "Temp1: " << temp1 << endl;
  166.             temp2 = b.substr(list_size_start, 4);
  167.             //cout << "Temp2: " << temp2 << endl;
  168.  
  169.             stream1 << temp1;
  170.             stream1 >> val1[list_count];
  171.  
  172.             stream2 << temp2;
  173.             stream2 >> val2[list_count];
  174.  
  175.             result[list_count] = add(val1[list_count], val2[list_count]);
  176.  
  177.             //cout << result[list_count] << endl;
  178.  
  179.             num_list[list_count].size = 4;
  180.             num_list[list_count].value = result[list_count];
  181.             //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
  182.         }
  183.     }
  184.  
  185.     if(x2 != 0)
  186.     {
  187.         int index = 0;
  188.         for(; index < x2; list_count++, index++)
  189.         {
  190.             string temp1, temp2;
  191.             stringstream stream1, stream2, stream3;
  192.  
  193.             if(index == 0)
  194.             {
  195.                 list_size_start -= 2;
  196.             }
  197.             else
  198.             {
  199.                 list_size_start -= 2;
  200.                 list_size_end -= 2;
  201.             }
  202.             temp1 = a.substr(list_size_start, 2);
  203.             //cout << "Index Start: " << list_size_start << endl;
  204.             //cout << "Temp1: " << temp1 << endl;
  205.             temp2 = b.substr(list_size_start, 2);
  206.             //cout << "Temp2: " << temp2 << endl;
  207.  
  208.             stream1 << temp1;
  209.             stream1 >> val1[list_count];
  210.  
  211.             stream2 << temp2;
  212.             stream2 >> val2[list_count];
  213.  
  214.             result[list_count] = add(val1[list_count], val2[list_count]);
  215.  
  216.             //cout << result[list_count] << endl;
  217.  
  218.             num_list[list_count].size = 2;
  219.             num_list[list_count].value = result[list_count];
  220.             //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
  221.         }
  222.     }
  223.  
  224.     if(x1 != 0)
  225.     {
  226.         int index = 0;
  227.         for(; index < x1; list_count++, index++)
  228.         {
  229.             string temp1, temp2;
  230.             stringstream stream1, stream2, stream3;
  231.  
  232.             if(index == 0)
  233.             {
  234.                 list_size_start -= 1;
  235.             }
  236.             else
  237.             {
  238.                 list_size_start -= 1;
  239.                 list_size_end -= 1;
  240.             }
  241.             temp1 = a.substr(0, 1);
  242.             //cout << "Index Start: " << list_size_start << endl;
  243.             //cout << "Temp1: " << temp1 << endl;
  244.             temp2 = b.substr(0, 1);
  245.             //cout << "Temp2: " << temp2 << endl;
  246.  
  247.             stream1 << temp1;
  248.             stream1 >> val1[list_count];
  249.  
  250.             stream2 << temp2;
  251.             stream2 >> val2[list_count];
  252.  
  253.             result[list_count] = add(val1[list_count], val2[list_count]);
  254.  
  255.             //cout << result[list_count] << endl;
  256.  
  257.             num_list[list_count].size = 1;
  258.             num_list[list_count].value = result[list_count];
  259.             //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
  260.         }
  261.     }
  262.  
  263.     int index = 0;
  264.  
  265.     for(; index < list_count; index++)
  266.     {
  267.         int check;
  268.         stringstream stream;
  269.         check = range_check(num_list[index].size, num_list[index].value);
  270.  
  271.         if(check == 1)
  272.         {
  273.             if(index != list_count - 1)
  274.             {
  275.                 num_list[index].value = remainder(num_list[index].size, num_list[index].value);
  276.                 num_list[index+1].value += 1;
  277.             }
  278.             //cout << "Value: " << num_list[index].value << endl;
  279.         }
  280.         stream << num_list[index].value;
  281.         stream >> value[index];
  282.     }
  283.  
  284.     index = 0;
  285.     for(; index < list_count; index++)
  286.     {
  287.         answer = value[index] + answer;
  288.     }
  289.  
  290.     cout << "Answer: " << answer << endl;
  291.  
  292.     cin.get();
  293.     cin.get();
  294.     return 0;
  295. }
  296.  
  297. long int add(long int num1, long int num2)
  298. {
  299.     long int result;
  300.     result = num1 + num2;
  301.     return result;
  302. }
  303.  
  304. int range_check(int size, long int value)
  305. {
  306.     long int max_limit = pow(10, size);
  307.     int check = 0;
  308.  
  309.     if(value > max_limit)
  310.     {
  311.         check = 1;
  312.     }
  313.     return check;
  314. }
  315.  
  316. long int remainder(int size, long int value)
  317. {
  318.     long int max_limit = pow(10, size);
  319.     long int remainder;
  320.  
  321.     if(value > max_limit)
  322.     {
  323.         remainder = value - max_limit;
  324.     }
  325.     return remainder;
  326. }
Add Comment
Please, Sign In to add comment