Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- string multiply_two_numbers_as_string(string a, string b) {
- int len_a = a.size();
- int len_b = b.size();
- int mult[len_a + len_b + 1];
- for(int i = 0; i < len_a + len_b + 1; i++) {
- mult[i] = 0;
- }
- int till_a = 0, till_b = 0;
- if(a[0] == '-') {
- till_a = 1;
- }
- if(b[0] == '-') {
- till_b = 1;
- }
- for(int i = len_a - 1; i >= till_a; i--) {
- for(int j = len_b - 1; j >= till_b; j--) {
- mult[i + j + 1] += (a[i] - '0') * (b[j] - '0');
- }
- }
- for(int i = len_a + len_b - 1; i >= 0; i--) {
- if(mult[i] > 9) {
- mult[i - 1] += (mult[i] / 10);
- mult[i] %= 10;
- }
- }
- int i = 0;
- while(mult[i] == 0) {
- i++;
- }
- string product = "";
- if(till_a != till_b) {
- product += "-";
- }
- for(int j = i; j < len_a + len_b; j++) {
- product += (mult[j] + '0');
- }
- return product;
- }
- bool is_A_smaller_than_B(string A, string B) {
- if(A.size() < B.size()) {
- return true;
- }
- if(A.size() > B.size()) {
- return false;
- }
- for(int i = 0; i < (int) A.size(); i++) {
- if(A[i] < B[i]) {
- return true;
- }
- if(A[i] > B[i]) {
- return false;
- }
- }
- return true;
- }
- string subtract_big_integer(string A, string B) {
- if(is_A_smaller_than_B(A, B)) {
- swap(A, B);
- }
- int sz_a = (int)A.size() - 1;
- int sz_b = (int)B.size() - 1;
- int carry = 0;
- string diff = "";
- while(sz_a >= 0 and sz_b >= 0) {
- int digit_a = (A[sz_a] - '0');
- int digit_b = (B[sz_b] - '0');
- int razlika = digit_a - digit_b - carry;
- if(razlika < 0) {
- razlika += 10;
- carry = 1;
- }
- else {
- carry = 0;
- }
- diff += (razlika + '0');
- sz_a--;
- sz_b--;
- }
- while(sz_a >= 0) {
- int digit_a = (A[sz_a] - '0');
- int razlika = digit_a - carry;
- if(razlika < 0) {
- carry = 1;
- razlika += 10;
- }
- else {
- carry = 0;
- }
- diff += (razlika + '0');
- }
- reverse(diff.begin(), diff.end());
- int i = 0;
- string result = "";
- while(diff[i] == '0') {
- i++;
- }
- for(int j = i; j < diff.size(); j++) {
- result += diff[j];
- }
- return result;
- }
- 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;
- }
- string divide_two_big_numbers_as_string(string a, string b) {
- if(is_A_smaller_than_B(a, b)) {
- swap(a, b);
- }
- string S = "";
- for(int i = 0; i < b.size(); i++) {
- S += a[i];
- }
- if(is_A_smaller_than_B(S, b)) {
- S += a[b.size()];
- }
- string diff = S;
- string pati = "1";
- while(!is_A_smaller_than_B(diff, b)) {
- if(!is_A_smaller_than_B(subtract_big_integer(diff, b), b)) {
- diff = subtract_big_integer(diff, b);
- pati = add_big_integer(pati, "1");
- }
- else {
- break;
- }
- }
- string remainder = "";
- string quotient = "";
- quotient += pati;
- for(int i = (int) S.size(); i < (int) a.size(); i++) {
- remainder = subtract_big_integer(S, multiply_two_numbers_as_string(pati, b));
- remainder += a[i];
- diff = remainder;
- pati = "1";
- while(!is_A_smaller_than_B(diff, b)) {
- if(!is_A_smaller_than_B(subtract_big_integer(diff, b), b)) {
- diff = subtract_big_integer(diff, b);
- pati = add_big_integer(pati, "1");
- }
- else {
- break;
- }
- }
- quotient += pati;
- S = remainder;
- }
- return quotient;
- }
- int main() {
- string a, b;
- a = "32167";
- b = "123";
- cout << divide_two_big_numbers_as_string(a, b) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement