Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- string add_big_integer(string A, string B) { // funkcija koja ke ni sobere A + B
- if(A.size() < B.size()) {
- swap(A, B);
- }
- int a_i = (int) A.size() - 1;
- int b_i = (int) B.size() - 1;
- int carry = 0;
- string sum = "";
- while(a_i >= 0 and b_i >= 0) {
- int digit_a = (A[a_i] - '0');
- int digit_b = (B[b_i] - '0');
- int zbir = digit_a + digit_b + carry;
- if(zbir <= 9) {
- sum += (zbir + '0');
- carry = 0;
- }
- else {
- carry = 1;
- zbir -= 10;
- sum += (zbir + '0');
- }
- a_i--;
- b_i--;
- }
- while(a_i >= 0) {
- int digit_a = (A[a_i] - '0');
- int zbir = digit_a + carry;
- if(zbir <= 9) {
- sum += (zbir + '0');
- carry = 0;
- }
- else {
- carry = 1;
- zbir -= 10;
- sum += (zbir + '0');
- }
- a_i--;
- }
- if(carry == 1) {
- sum += "1";
- }
- reverse(sum.begin(), sum.end());
- return sum;
- }
- int main()
- {
- string a, b;
- cin >> a >> b;
- cout << add_big_integer(a, b) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement