Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <deque>
- #include <string>
- #include <vector>
- using namespace std;
- int main()
- {
- unsigned int n;
- deque<int> list = deque<int>();
- string operand1, operand2, operation;
- cin >> n;
- while (n)
- {
- cin >> operand1;
- cin >> operation;
- cin >> operand2;
- list.clear();
- if (!operation.compare("+"))
- {
- //Get size of booth numbers
- int size;
- int size1 = operand1.size();
- int size2 = operand2.size();
- int diff = 0;
- if (size1 > size2){
- size = size1;
- diff = size1 - size2;
- operand2.insert(0, diff, '0');
- } else {
- size = size2;
- diff = size2 - size1;
- operand1.insert(0, diff, '0');
- }
- int carry = 0;
- for (int i = size-1 ; i >= 0; --i) {
- // Conver chars to numbers
- int num1 = (int)operand1[i] - '0';
- int num2 = (int)operand2[i] - '0';
- // Calculate
- int result = (num1 + num2 + carry) % 10;
- carry = (num1 + num2 + carry) / 10;
- list.push_front(result);
- // If one number is bigger than the other, continue calculating with smaller number as zeroes
- if (i == 0 && carry > 0) {
- list.push_front(carry);
- }
- }
- // Print result
- deque<int>::iterator it = list.begin();
- while (it != list.end())
- cout << *it++;
- cout << endl;
- }
- else if (!operation.compare("-"))
- {
- // Get size of booth numbers
- int size;
- int size1 = operand1.size();
- int size2 = operand2.size();
- int diff = 0;
- bool negative = false;
- // Calculate which one is bigger and insert zeroes in front of smaller one
- if (size1 > size2)
- {
- size = size1;
- diff = size1 - size2;
- operand2.insert(0, diff, '0');
- }
- else if(size1 < size2)
- {
- size = size2;
- diff = size2 - size1;
- operand1.insert(0, diff, '0');
- swap(operand1, operand2);
- negative = true;
- }
- else
- {
- // Covering case when number lengths are same
- int i = -1;
- do
- {
- i++;
- if (operand1[i] > operand2[i])
- {
- size = size1;
- diff = size1 - size2;
- operand2.insert(0, diff, '0');
- break;
- }
- else if (operand1[i] < operand2[i])
- {
- size = size2;
- diff = size2 - size1;
- operand1.insert(0, diff, '0');
- swap(operand1, operand2);
- break;
- }
- } while (operand1[i] == operand2[i]);
- }
- for (int i = size - 1; i >= 0; --i) {
- // Conver chars to numbers
- int num1 = (int)operand1[i] - '0';
- int num2 = (int)operand2[i] - '0';
- // Calculate
- int result;
- // Borrowing
- int difference = num1 - num2;
- if (difference < 0)
- {
- int j = 1;
- while (operand1[i - j] == 0)
- {
- operand1[i - j] = 9;
- j++;
- }
- operand1[i - j]--;
- num1 += 10;
- }
- result = (num1 - num2) % 10;
- list.push_front(result);
- }
- // Remove zeroes at the begining of result
- deque<int>::iterator itFix = list.begin();
- while (itFix != list.end())
- {
- if (*itFix == 0)
- {
- *itFix++;
- list.pop_front();
- }
- else break;
- }
- // Print result
- deque<int>::iterator it = list.begin();
- while (it != list.end())
- {
- if (negative)
- {
- negative = false;
- cout << -(*it++);
- }
- else cout << *it++;
- }
- cout << endl;
- }
- else if (!operation.compare("*"))
- {
- // Get size of booth numbers
- int size;
- int size1 = operand1.size();
- int size2 = operand2.size();
- int diff = 0;
- vector<int> result(size1 + size2, 0);
- for (int i = size1 - 1; i >= 0; i--)
- {
- for (int j = size2 - 1; j >= 0; j--)
- {
- result[i + j + 1] += (operand2[j] - '0') * (operand1[i] - '0'); //single array to store intermediate values
- }
- }
- for (int i = size1 + size2-1; i >= 0; i--) {
- if (result[i] >= 10) {
- result[i - 1] += result[i] / 10; result[i] %= 10;
- }
- }
- for (unsigned int i = 0; i < (size1 + size2); i++) {
- list.push_back(result[i]);
- }
- // Remove zeroes at the begining of result
- deque<int>::iterator itFix = list.begin();
- while (itFix != list.end())
- {
- if (*itFix == 0)
- {
- *itFix++;
- list.pop_front();
- }
- else break;
- }
- // Print result
- deque<int>::iterator it = list.begin();
- while (it != list.end())
- cout << *it++;
- cout << endl;
- }
- n--;
- }
- return 0;
- }
- //template<class T>
- //void Red<T>::enqueue(const Element<T>& x)
- //{
- // if (isFull()) return;
- //
- // Q[tail] = x;
- // tail++;
- //}
- //
- //template<class T>
- //void Red<T>::dequeue()
- //{
- // if (isEmpty()) return;
- // head++;
- //}
- //int main()
- //{
- // unsigned int n;
- // index size;
- //
- // float number;
- // Element<float> *element;
- // string command;
- //
- // cin >> size;
- // Red<float> list = Red<float>(size);
- //
- // cin >> n;
- // while (n)
- // {
- // cin >> command;
- // if (!command.compare("ENQUEUE"))
- // {
- // cin >> number;
- // element = new Element<float>(number);
- // list.enqueue(number);
- // cout << "Stanje reda: " << list << endl;
- // }
- // else if (!command.compare("DEQUEUE"))
- // {
- // list.dequeue();
- // cout << "Stanje reda: " << list << endl;
- // }
- // else
- // {
- // cout << "Naredba nije poznata" << endl;
- // continue;
- // }
- // n--;
- // }
- // return 0;
- //}
- //
- //#include <iostream>
- //#include "DPL.h"
- //using namespace std;
- //
- //template<class T>
- //void DLList<T>::insert_after(T& k, T& d)
- //{
- // Element<T>* iter = head;
- // Element<T>* x = new Element<T>(d);
- // bool found = false;
- // while (iter->next != NULL || !found)
- // {
- // if (iter->d == k)
- // {
- // if (iter->next != NULL) {
- // x->next = iter->next;
- // x->next->prev = x;
- // }
- // iter->next = x;
- // x->prev = iter;
- // found = true;
- // }
- // if(iter->next != NULL)
- // iter = iter->next;
- // else break;
- // }
- //
- // if (iter->next == NULL && !found)
- // {
- // x->next = head;
- // head->prev = x;
- // head = x;
- // }
- //
- // if (x->next == NULL)
- // tail = x;
- //}
- //
- //template<class T>
- //void DLList<T>::insert_before(T& k, T& d)
- //{
- // Element<T>* iter = tail;
- // Element<T>* x = new Element<T>(d);
- // bool found = false;
- // while (iter->prev != NULL || !found)
- // {
- // if (iter->d == k)
- // {
- // if (iter->prev != NULL)
- // {
- // iter->prev->next = x;
- // x->prev = iter->prev;
- // }
- // x->next = iter;
- // iter->prev = x;
- // found = true;
- // }
- // if (iter->prev != NULL)
- // iter = iter->prev;
- // else break;
- // }
- //
- // if (iter->prev == NULL && !found)
- // {
- // x->prev = tail;
- // tail->next = x;
- // tail = x;
- // }
- //
- // if (x->prev == NULL)
- // head = x;
- //}
- //
- //int main()
- //{
- // freopen("test.txt", "r", stdin);
- // DLList<int> list;
- //
- // unsigned int n;
- // int kljuc, kljuc2;
- // string naredba;
- //
- // cin >> n;
- // while (n)
- // {
- // cin >> naredba;
- // if (!naredba.compare("IF"))
- // {
- // cin >> kljuc;
- // list.insert_front(kljuc);
- // cout << list << endl;
- // }
- // else if (!naredba.compare("IBC"))
- // {
- // cin >> kljuc;
- // list.insert_back(kljuc);
- // cout << list << endl;
- // }
- // else if (!naredba.compare("IA"))
- // {
- // cin >> kljuc >> kljuc2;
- // list.insert_after(kljuc, kljuc2);
- // cout << list << endl;
- // }
- // else if (!naredba.compare("IBF"))
- // {
- // cin >> kljuc >> kljuc2;
- // list.insert_before(kljuc, kljuc2);
- // cout << list << endl;
- // }
- // else if (!naredba.compare("D"))
- // {
- // cin >> kljuc;
- // list.delete_key(kljuc);
- // cout << list << endl;
- // }
- // else
- // {
- // cout << "Naredba nije poznata";
- // continue;
- // }
- // n--;
- // }
- // return 0;
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement