Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cstdlib>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <deque>
  10. #include <queue>
  11. #include <string>
  12. #include <cstring>
  13. #include <map>
  14. #include <stack>
  15. #include <set>
  16.  
  17. using namespace std;
  18. typedef unsigned long long ll;
  19. const int INF = 2147483647;
  20.  
  21. bool compare(vector<int> a, vector<int> b) {
  22.     // Returns true if a >= b
  23.     if (a == b) return true;
  24.     if (a.size() != b.size()) return (a.size() > b.size());
  25.     assert(a.size() == b.size());
  26.     // Compares most significant digit
  27.     if (a[a.size() - 1] != b[b.size() - 1]) return (a[a.size() - 1] > b[b.size() - 1]);
  28.     // Most significant digit same
  29.     // Compare next significant digit
  30.     return compare(vector<int>(a.begin(), a.end() - 1), vector<int>(b.begin(), b.end() - 1));
  31. }
  32.  
  33. vector<int> add(vector<int> a, vector<int> b) {
  34.     if (!compare(a, b)) swap(a, b);
  35.     // Now a >= b
  36.     for (int i = 0; i < b.size(); i ++) {
  37.         // Naive addition
  38.         a[i] += b[i];
  39.         // Deals with carry
  40.         if (a[i] > 9) {
  41.             a[i] -= 10;
  42.             // Ensures there are enough digits
  43.             if (i + 1 < a.size())
  44.                 a[i + 1] ++;
  45.             else
  46.                 a.push_back(1);
  47.         }
  48.     }
  49.     return a;
  50. }
  51.  
  52. void print(vector<int> a) {
  53.     // Print digit by digit
  54.     for (int i = a.size() - 1; i >= 0; i --) {
  55.         cout << a[i];
  56.     }
  57.     cout << endl;
  58. }
  59.  
  60. vector<int> readInt() {
  61.     // Assuming reading from stdin
  62.     vector<int> a;
  63.     string s;
  64.     cin >> s;
  65.     for (char c : s) {
  66.         // char reads character as it's ASCII code
  67.         // ASCII code of characters '0' to '9' is 48(0x30) to 57(0x39)
  68.         // Subtract 0x30 from ASCII code to get digit value
  69.         a.push_back((int) (c - 48));
  70.     }
  71.     reverse(a.begin(), a.end());
  72.     return a;
  73. }
  74.  
  75. int main() {
  76.     // Code currently only supports *positive integers*
  77.     // 1234 represented as {4, 3, 2, 1}
  78.     // 0 represented as {0}
  79.     vector<int> a = readInt();
  80.     vector<int> b = readInt();
  81.     cout << "a's value: ";
  82.     print(a);
  83.     cout << "b's value: ";
  84.     print(b);
  85.     cout << "Is a > b? " << (compare(a, b) ? "Yes" : "No") << endl;
  86.     cout << "Is b > a? " << (compare(b, a) ? "Yes" : "No") << endl;
  87.     cout << "a + b = ";
  88.     print(add(a, b));
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement