Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- struct Node {
- int value;
- Node* next = NULL;
- };
- Node* first = NULL;
- Node* second = NULL;
- Node* res = NULL;
- void Push(int value,Node**head) {
- Node* temp = new Node;
- temp->value = value;
- temp->next = (*head);
- (*head) = temp;
- }
- void print(Node* head) {
- Node* temp = head;
- while (temp->next != NULL) {
- cout << temp->value;
- temp = temp->next;
- }
- cout << temp->value;
- }
- string split(string & str) {
- //I haven't taken into consideration the case when the number is minus
- if (str.length() > 10) {
- int len = 10;
- string x1, x2;
- for (int i = 0; i < len; i++)
- x1.push_back(str[i]);
- for (int i = len; i < str.length(); i++)
- x2.push_back(str[i]);
- str = x2;
- return x1;
- }
- else {
- string x1;
- for (int i = 0; i < str.length(); i++)
- x1.push_back(str[i]);
- str.clear();
- return x1;
- }
- }
- int ConvertedToInt(string s) {
- //this should work fine, my only concern that it might take too long.
- int sum = 0;
- if (s[0] == '-') {
- for (int i = 1; i < s.length(); i++) {
- sum = sum * 10 + (s[i] - '0');
- }
- sum = sum * (-1);
- }
- else {
- for (int i = 0; i < s.length(); i++) {
- sum = sum * 10 + (s[i] - '0');
- }
- }
- return sum;
- }
- string RemoveSpaces(string str)
- {
- for (int i = 0; i < str.length();) {
- if (str[i] == ' ') {
- str.erase(str.begin() + i);
- }
- else {
- i++;
- }
- }
- return str;
- }
- Node* SUM(Node*first, Node* second) {
- Node* temp=new Node, * prev = NULL;
- int sum, carry = 0;
- while(first != NULL || second != NULL) //termenating condition
- {
- //the sum will equal the carry + value from first list (if existes) if not put 0
- //, same for second if statment.
- sum = carry + (first ? first->value : 0) + (second ? second->value : 0);
- carry = (sum >= 10000000000) ? 1 : 0; //since we are didviding the numbers' digits by 10
- sum = sum % 10000000000;
- Node* NewNode = new Node;
- NewNode->value = sum;
- temp=NewNode;
- if (res == NULL) res = temp;
- else Push(sum,&res);
- prev = temp; //setting prev for next iteration
- //Moving forward in our lists
- if (first) first = first->next;
- if (second) second = second->next;
- }
- if (carry > 0) {
- Node* tempr = new Node;
- tempr->value = carry;
- tempr->next = NULL;
- temp->next = tempr;
- }
- return res;
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- string s1, s2;
- int L1, L2;
- cin >> L1 >> L2;
- getline(cin, s1);
- getline(cin, s1);
- getline(cin, s2);
- s1=RemoveSpaces(s1);
- s2=RemoveSpaces(s2);
- if (s1.length() > s2.length()) {
- s2.insert(s2.begin() + 0, s1.length()-1, '0');
- }
- if (s1.length() < s2.length()) {
- s1.insert(s1.begin() + 0, s2.length() - 1, '0');
- }
- int temp;
- while (s1.length()>10) {
- temp = ConvertedToInt(split(s1));
- Push(temp,&first);
- }
- temp = ConvertedToInt(split(s1));
- Push(temp, &first);
- while (s2.length() > 10) {
- temp = ConvertedToInt(split(s2));
- Push(temp,&second);
- }
- temp = ConvertedToInt(split(s2));
- Push(temp, &second);
- print(SUM(first, second));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement