SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6.  
  7. struct BigDecimal {
  8.  
  9.     int size;
  10.     int digits[30];
  11.     int E;
  12.     int sign;
  13.  
  14. };
  15.  
  16. void printArrayResult(int *res, int size) {
  17.     for (int j = 0; j < size; j++) {
  18.         printf("Element[%d] = %d\n", j, res[j]);
  19.     }
  20. }
  21.  
  22. void printNumber(int num) {
  23.     printf("Number = %d\n", num);
  24. }
  25.  
  26. int compare(int *a, int *b, int arraySize, int index) {
  27.  
  28.     int val1 = a[index];
  29.     int val2 = b[index];
  30.  
  31.     if (val1 == val2) {
  32.         return compare(a, b, arraySize, index + 1);
  33.     }
  34.  
  35.     if (val1 > val2) {
  36.         return 1;
  37.     } else {
  38.         return -1;
  39.     }
  40. }
  41.  
  42. void minus(int array1[], int array2[], int size, int returnArray[]) {
  43.  
  44.     int borrow = -1;
  45.  
  46.     for (int i = size - 1; i >= 0; i--) {
  47.  
  48.         if (array1[i] < array2[i]) {
  49.             borrow = i - 1;
  50.             array1[i] += 10;
  51.         }
  52.  
  53.         int res = array1[i] - array2[i];
  54.         if (borrow == i) {
  55.             borrow = -1;
  56.             res--;
  57.         }
  58.         returnArray[i] = res;
  59.     }
  60. }
  61.  
  62. void incrementResult(int resultArray[], int index) {
  63.  
  64.     resultArray[index]++;
  65.     if (resultArray[index] >= 10) {
  66.         incrementResult(resultArray, index - 1);
  67.         resultArray[index] -= 10;
  68.     }
  69. }
  70.  
  71. void divide(int array1[], int array2[], int size, int returnArray[]) {
  72.  
  73.     int compareResult = 0;
  74.     do {
  75.  
  76.         incrementResult(returnArray, size - 1);
  77.  
  78.         minus(array1, array2, size, array1);
  79.         printArrayResult(array1, size);
  80.         compareResult = compare(array1, array2, size, 0);
  81.         printNumber(compareResult);
  82.  
  83.     } while (compareResult > 0);
  84.  
  85. }
  86.  
  87. int main() {
  88.  
  89.     struct BigDecimal number1 = {
  90.             .size = 3,
  91.             .E = 0,
  92.             .sign = 1,
  93.             .digits = {1, 0, 0}
  94.     };
  95.  
  96.     struct BigDecimal number2 = {
  97.             .size = 3,
  98.             .E = 0,
  99.             .sign = 1,
  100.             .digits = {0, 2, 0}
  101.     };
  102.  
  103.     int divRes[number1.size];
  104.     divide(number1.digits, number2.digits, number1.size, divRes);
  105.     printArrayResult(divRes, number1.size);
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top