Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cmath>
- using namespace std;
- long int add(long int, long int);
- int range_check(int, long int);
- long int remainder(int, long int);
- struct num_data
- {
- int size;
- long int value;
- };
- int main()
- {
- int list_index = 0;
- int list_count = 0;
- int size;
- int list_size_start, list_size_end;
- int x8, x4, x2, x1;
- int size_diff;
- int large_size, small_size;
- string a, b;
- string answer;
- cout << "Please enter Num1: ";
- cin >> a;
- cout << "Please enter Num2: ";
- cin >> b;
- cout << "Number 1: " << a << endl;
- cout << "Number 2: " << b << endl;
- if(b.size() > a.size())
- {
- large_size = b.size();
- size = large_size;
- //cout << size;
- small_size = a.size();
- size_diff = large_size - small_size;
- for(int size_index = 0; size_index < size_diff; size_index++)
- {
- a = '0' + a;
- }
- }
- else if(a.size() > b.size())
- {
- large_size = a.size();
- size = large_size;
- //cout << size;
- small_size = b.size();
- size_diff = large_size - small_size;
- for(int size_index = 0; size_index < size_diff; size_index++)
- {
- b = '0' + b;
- }
- }
- else
- {
- size = a.size();
- }
- list_size_end = list_size_start = size;
- x8 = floor(size / 8);
- size = size - (x8 * 8);
- list_index += x8;
- //cout << x8 << endl;
- x4 = floor(size / 4);
- size = size - (x4 * 4);
- list_index += x4;
- //cout << x4 << endl;
- x2 = floor(size / 2);
- size = size - (x2 * 2);
- list_index += x2;
- //cout << x2 << endl;
- x1 = floor(size / 1);
- size = size - (x1 * 1);
- list_index += x1;
- //cout << x1 << endl;
- num_data num_list[list_index];
- long int val1[list_index];
- long int val2[list_index];
- long int result[list_index];
- string value[list_index];
- if(x8 != 0)
- {
- int index = 0;
- for(; index < x8; list_count++, index++)
- {
- string temp1, temp2;
- stringstream stream1, stream2, stream3;
- if(index == 0)
- {
- list_size_start -= 8;
- }
- else
- {
- list_size_start -= 8;
- list_size_end -= 8;
- }
- temp1 = a.substr(list_size_start, 8);
- //cout << "Index Start: " << list_size_start << endl;
- //cout << "Temp1: " << temp1 << endl;
- temp2 = b.substr(list_size_start, 8);
- //cout << "Temp2: " << temp2 << endl;
- stream1 << temp1;
- stream1 >> val1[list_count];
- stream2 << temp2;
- stream2 >> val2[list_count];
- result[list_count] = add(val1[list_count], val2[list_count]);
- //cout << result[list_count] << endl;
- num_list[list_count].size = 8;
- num_list[list_count].value = result[list_count];
- //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
- }
- }
- if(x4 != 0)
- {
- int index = 0;
- for(; index < x4; list_count++, index++)
- {
- string temp1, temp2;
- stringstream stream1, stream2, stream3;
- if(index == 0)
- {
- list_size_start -= 4;
- }
- else
- {
- list_size_start -= 4;
- list_size_end -= 4;
- }
- temp1 = a.substr(list_size_start, 4);
- //cout << "Index Start: " << list_size_start << endl;
- //cout << "Temp1: " << temp1 << endl;
- temp2 = b.substr(list_size_start, 4);
- //cout << "Temp2: " << temp2 << endl;
- stream1 << temp1;
- stream1 >> val1[list_count];
- stream2 << temp2;
- stream2 >> val2[list_count];
- result[list_count] = add(val1[list_count], val2[list_count]);
- //cout << result[list_count] << endl;
- num_list[list_count].size = 4;
- num_list[list_count].value = result[list_count];
- //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
- }
- }
- if(x2 != 0)
- {
- int index = 0;
- for(; index < x2; list_count++, index++)
- {
- string temp1, temp2;
- stringstream stream1, stream2, stream3;
- if(index == 0)
- {
- list_size_start -= 2;
- }
- else
- {
- list_size_start -= 2;
- list_size_end -= 2;
- }
- temp1 = a.substr(list_size_start, 2);
- //cout << "Index Start: " << list_size_start << endl;
- //cout << "Temp1: " << temp1 << endl;
- temp2 = b.substr(list_size_start, 2);
- //cout << "Temp2: " << temp2 << endl;
- stream1 << temp1;
- stream1 >> val1[list_count];
- stream2 << temp2;
- stream2 >> val2[list_count];
- result[list_count] = add(val1[list_count], val2[list_count]);
- //cout << result[list_count] << endl;
- num_list[list_count].size = 2;
- num_list[list_count].value = result[list_count];
- //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
- }
- }
- if(x1 != 0)
- {
- int index = 0;
- for(; index < x1; list_count++, index++)
- {
- string temp1, temp2;
- stringstream stream1, stream2, stream3;
- if(index == 0)
- {
- list_size_start -= 1;
- }
- else
- {
- list_size_start -= 1;
- list_size_end -= 1;
- }
- temp1 = a.substr(0, 1);
- //cout << "Index Start: " << list_size_start << endl;
- //cout << "Temp1: " << temp1 << endl;
- temp2 = b.substr(0, 1);
- //cout << "Temp2: " << temp2 << endl;
- stream1 << temp1;
- stream1 >> val1[list_count];
- stream2 << temp2;
- stream2 >> val2[list_count];
- result[list_count] = add(val1[list_count], val2[list_count]);
- //cout << result[list_count] << endl;
- num_list[list_count].size = 1;
- num_list[list_count].value = result[list_count];
- //cout << "List " << list_count << ": " << num_list[list_count].value << endl;
- }
- }
- int index = 0;
- for(; index < list_count; index++)
- {
- int check;
- stringstream stream;
- check = range_check(num_list[index].size, num_list[index].value);
- if(check == 1)
- {
- if(index != list_count - 1)
- {
- num_list[index].value = remainder(num_list[index].size, num_list[index].value);
- num_list[index+1].value += 1;
- }
- //cout << "Value: " << num_list[index].value << endl;
- }
- stream << num_list[index].value;
- stream >> value[index];
- }
- index = 0;
- for(; index < list_count; index++)
- {
- answer = value[index] + answer;
- }
- cout << "Answer: " << answer << endl;
- cin.get();
- cin.get();
- return 0;
- }
- long int add(long int num1, long int num2)
- {
- long int result;
- result = num1 + num2;
- return result;
- }
- int range_check(int size, long int value)
- {
- long int max_limit = pow(10, size);
- int check = 0;
- if(value > max_limit)
- {
- check = 1;
- }
- return check;
- }
- long int remainder(int size, long int value)
- {
- long int max_limit = pow(10, size);
- long int remainder;
- if(value > max_limit)
- {
- remainder = value - max_limit;
- }
- return remainder;
- }
Add Comment
Please, Sign In to add comment