Advertisement
abs25

IDK

Sep 20th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <deque>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     unsigned int n;
  10.     deque<int> list = deque<int>();
  11.     string operand1, operand2, operation;
  12.  
  13.     cin >> n;
  14.     while (n)
  15.     {
  16.         cin >> operand1;
  17.         cin >> operation;
  18.         cin >> operand2;
  19.         list.clear();
  20.  
  21.         if (!operation.compare("+"))
  22.         {
  23.             //Get size of booth numbers
  24.             int size;
  25.             int size1 = operand1.size();
  26.             int size2 = operand2.size();
  27.             int diff = 0;
  28.             if (size1 > size2){
  29.                 size = size1;
  30.                 diff = size1 - size2;
  31.                 operand2.insert(0, diff, '0');
  32.             } else {
  33.                 size = size2;
  34.                 diff = size2 - size1;
  35.                 operand1.insert(0, diff, '0');
  36.             }
  37.  
  38.             int carry = 0;
  39.             for (int i = size-1 ; i >= 0; --i) {
  40.                 // Conver chars to numbers
  41.                 int num1 = (int)operand1[i] - '0';
  42.                 int num2 = (int)operand2[i] - '0';
  43.                                
  44.                 // Calculate
  45.                 int result = (num1 + num2 + carry) % 10;
  46.                 carry = (num1 + num2 + carry) / 10;
  47.                 list.push_front(result);
  48.  
  49.                 // If one number is bigger than the other, continue calculating with smaller number as zeroes
  50.                 if (i == 0 && carry > 0) {
  51.                     list.push_front(carry);
  52.                 }
  53.             }
  54.             // Print result
  55.             deque<int>::iterator it = list.begin();
  56.             while (it != list.end())
  57.                 cout << *it++;
  58.             cout << endl;
  59.         }
  60.         else if (!operation.compare("-"))
  61.         {
  62.             // Get size of booth numbers
  63.             int size;
  64.             int size1 = operand1.size();
  65.             int size2 = operand2.size();
  66.             int diff = 0;
  67.             bool negative = false;
  68.             // Calculate which one is bigger and insert zeroes in front of smaller one
  69.             if (size1 > size2)
  70.             {
  71.                 size = size1;
  72.                 diff = size1 - size2;
  73.                 operand2.insert(0, diff, '0');
  74.             }
  75.             else if(size1 < size2)
  76.             {
  77.                 size = size2;
  78.                 diff = size2 - size1;
  79.                 operand1.insert(0, diff, '0');
  80.                 swap(operand1, operand2);
  81.                 negative = true;
  82.             }
  83.             else
  84.             {
  85.                 // Covering case when number lengths are same
  86.                 int i = -1;
  87.                 do
  88.                 {
  89.                     i++;
  90.                     if (operand1[i] > operand2[i])
  91.                     {
  92.                         size = size1;
  93.                         diff = size1 - size2;
  94.                         operand2.insert(0, diff, '0');
  95.                         break;
  96.                     }
  97.                     else if (operand1[i] < operand2[i])
  98.                     {
  99.                         size = size2;
  100.                         diff = size2 - size1;
  101.                         operand1.insert(0, diff, '0');
  102.                         swap(operand1, operand2);
  103.                         break;
  104.                     }
  105.                 } while (operand1[i] == operand2[i]);
  106.             }
  107.  
  108.             for (int i = size - 1; i >= 0; --i) {
  109.                 // Conver chars to numbers
  110.                 int num1 = (int)operand1[i] - '0';
  111.                 int num2 = (int)operand2[i] - '0';
  112.  
  113.                 // Calculate
  114.                 int result;
  115.  
  116.                 // Borrowing
  117.                 int difference = num1 - num2;
  118.                 if (difference < 0)
  119.                 {
  120.                     int j = 1;
  121.                     while (operand1[i - j] == 0)
  122.                     {
  123.                         operand1[i - j] = 9;
  124.                         j++;
  125.                     }
  126.                     operand1[i - j]--;
  127.  
  128.                     num1 += 10;
  129.                 }
  130.                 result = (num1 - num2) % 10;
  131.  
  132.                 list.push_front(result);
  133.             }
  134.             // Remove zeroes at the begining of result
  135.             deque<int>::iterator itFix = list.begin();
  136.             while (itFix != list.end())
  137.             {
  138.                 if (*itFix == 0)
  139.                 {
  140.                     *itFix++;
  141.                     list.pop_front();
  142.                 }
  143.                 else break;
  144.             }
  145.  
  146.             // Print result
  147.             deque<int>::iterator it = list.begin();
  148.             while (it != list.end())
  149.             {
  150.                 if (negative)
  151.                 {
  152.                     negative = false;
  153.                     cout << -(*it++);
  154.                 }
  155.                 else cout << *it++;
  156.             }
  157.            
  158.             cout << endl;
  159.         }
  160.         else if (!operation.compare("*"))
  161.         {
  162.             // Get size of booth numbers
  163.             int size;
  164.             int size1 = operand1.size();
  165.             int size2 = operand2.size();
  166.             int diff = 0;
  167.             vector<int> result(size1 + size2, 0);
  168.             for (int i = size1 - 1; i >= 0; i--)
  169.             {
  170.                 for (int j = size2 - 1; j >= 0; j--)
  171.                 {
  172.                     result[i + j + 1] += (operand2[j] - '0') * (operand1[i] - '0'); //single array to store intermediate values
  173.                 }
  174.             }
  175.             for (int i = size1 + size2-1; i >= 0; i--) {
  176.                 if (result[i] >= 10) {
  177.                     result[i - 1] += result[i] / 10; result[i] %= 10;
  178.                 }
  179.             }
  180.             for (unsigned int i = 0; i < (size1 + size2); i++) {
  181.                 list.push_back(result[i]);
  182.             }
  183.  
  184.             // Remove zeroes at the begining of result
  185.             deque<int>::iterator itFix = list.begin();
  186.             while (itFix != list.end())
  187.             {
  188.                 if (*itFix == 0)
  189.                 {
  190.                     *itFix++;
  191.                     list.pop_front();
  192.                 }
  193.                 else break;
  194.             }
  195.  
  196.             // Print result
  197.             deque<int>::iterator it = list.begin();
  198.             while (it != list.end())
  199.                 cout << *it++;
  200.             cout << endl;
  201.         }
  202.         n--;
  203.     }
  204.     return 0;
  205. }
  206.  
  207.  
  208. //template<class T>
  209. //void Red<T>::enqueue(const Element<T>& x)
  210. //{
  211. //  if (isFull()) return;
  212. // 
  213. //  Q[tail] = x;
  214. //  tail++;
  215. //}
  216. //
  217. //template<class T>
  218. //void Red<T>::dequeue()
  219. //{
  220. //  if (isEmpty()) return;
  221. //  head++;
  222. //}
  223.  
  224.  
  225. //int main()
  226. //{
  227. //  unsigned int n;
  228. //  index size;
  229. //
  230. //  float number;
  231. //  Element<float> *element;
  232. //  string command;
  233. // 
  234. //  cin >> size;
  235. //  Red<float> list = Red<float>(size);
  236. //
  237. //  cin >> n;
  238. //  while (n)
  239. //  {
  240. //      cin >> command;
  241. //      if (!command.compare("ENQUEUE"))
  242. //      {
  243. //          cin >> number;
  244. //          element = new Element<float>(number);
  245. //          list.enqueue(number);
  246. //          cout << "Stanje reda: " << list << endl;
  247. //      }
  248. //      else if (!command.compare("DEQUEUE"))
  249. //      {
  250. //          list.dequeue();
  251. //          cout << "Stanje reda: " << list << endl;
  252. //      }
  253. //      else
  254. //      {
  255. //          cout << "Naredba nije poznata" << endl;
  256. //          continue;
  257. //      }
  258. //      n--;
  259. //  }
  260. //  return 0;
  261. //}
  262. //
  263.  
  264.  
  265.  
  266.  
  267.  
  268. //#include <iostream>
  269. //#include "DPL.h"
  270. //using namespace std;
  271. //
  272. //template<class T>
  273. //void DLList<T>::insert_after(T& k, T& d)
  274. //{
  275. //  Element<T>* iter = head;
  276. //  Element<T>* x = new Element<T>(d);
  277. //  bool found = false;
  278. //  while (iter->next != NULL || !found)
  279. //  {
  280. //      if (iter->d == k)
  281. //      {  
  282. //          if (iter->next != NULL) {
  283. //              x->next = iter->next;
  284. //              x->next->prev = x;
  285. //          }            
  286. //          iter->next = x;
  287. //          x->prev = iter;
  288. //          found = true;
  289. //      }
  290. //      if(iter->next != NULL)
  291. //          iter = iter->next;
  292. //      else break;
  293. //  }
  294. //
  295. //  if (iter->next == NULL && !found)
  296. //  {
  297. //      x->next = head;
  298. //      head->prev = x;
  299. //      head = x;
  300. //  }
  301. //
  302. //  if (x->next == NULL)
  303. //      tail = x;
  304. //}
  305. //
  306. //template<class T>
  307. //void DLList<T>::insert_before(T& k, T& d)
  308. //{
  309. //  Element<T>* iter = tail;
  310. //  Element<T>* x = new Element<T>(d);
  311. //  bool found = false;
  312. //  while (iter->prev != NULL || !found)
  313. //  {
  314. //      if (iter->d == k)
  315. //      {
  316. //          if (iter->prev != NULL)
  317. //          {
  318. //              iter->prev->next = x;
  319. //              x->prev = iter->prev;
  320. //          }
  321. //          x->next = iter;
  322. //          iter->prev = x;
  323. //          found = true;
  324. //      }
  325. //      if (iter->prev != NULL)
  326. //          iter = iter->prev;
  327. //      else break;
  328. //  }
  329. //
  330. //  if (iter->prev == NULL && !found)
  331. //  {
  332. //      x->prev = tail;    
  333. //      tail->next = x;    
  334. //      tail = x;
  335. //  }
  336. //
  337. //  if (x->prev == NULL)
  338. //      head = x;
  339. //}
  340. //
  341. //int main()
  342. //{
  343. //  freopen("test.txt", "r", stdin);
  344. //  DLList<int> list;
  345. //
  346. //  unsigned int n;
  347. //  int kljuc, kljuc2;
  348. //  string naredba;
  349. //
  350. //  cin >> n;
  351. //  while (n)
  352. //  {
  353. //      cin >> naredba;
  354. //      if (!naredba.compare("IF"))
  355. //      {
  356. //          cin >> kljuc;
  357. //          list.insert_front(kljuc);
  358. //          cout << list << endl;
  359. //      }
  360. //      else if (!naredba.compare("IBC"))
  361. //      {
  362. //          cin >> kljuc;
  363. //          list.insert_back(kljuc);
  364. //          cout << list << endl;
  365. //      }
  366. //      else if (!naredba.compare("IA"))
  367. //      {
  368. //          cin >> kljuc >> kljuc2;
  369. //          list.insert_after(kljuc, kljuc2);
  370. //          cout << list << endl;
  371. //      }
  372. //      else if (!naredba.compare("IBF"))
  373. //      {
  374. //          cin >> kljuc >> kljuc2;
  375. //          list.insert_before(kljuc, kljuc2);
  376. //          cout << list << endl;
  377. //      }
  378. //      else if (!naredba.compare("D"))
  379. //      {
  380. //          cin >> kljuc;
  381. //          list.delete_key(kljuc);
  382. //          cout << list << endl;
  383. //      }
  384. //      else
  385. //      {
  386. //          cout << "Naredba nije poznata";
  387. //          continue;
  388. //      }
  389. //      n--;
  390. //  }
  391. //  return 0;
  392. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement