Advertisement
55_maximus_55

Untitled

May 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct d_array {
  6.     int *arr;
  7.     size_t size, used;
  8. } d_array;
  9.  
  10. void free_arr(d_array *a){
  11.     free(a->arr);
  12.     a->used = a->size = 0;
  13. }
  14.  
  15. bool resize_arr(d_array *a){
  16.     a->size *= 2;
  17.     int *pt = a->arr;
  18.     a->arr = realloc(a->arr, a->size * sizeof(int));
  19.     if(!a->arr) {
  20.         free(pt);
  21.         return false;
  22.     }
  23.     return true;
  24. }
  25.  
  26. bool init_arr(d_array *a, size_t init_size){
  27.     a->arr = malloc(init_size * sizeof(int));
  28.     if(!a->arr) return false;
  29.     a->used = 0;
  30.     a->size = init_size;
  31.     return true;
  32. }
  33.  
  34. bool push(d_array *a, int key){
  35.     if(a->used == a->size) if(!resize_arr(a)) return false;
  36.     a->arr[a->used++] = key;
  37.     return true;
  38. }
  39.  
  40.  
  41.  
  42. #define base 10
  43.  
  44. void printBigInt(d_array *a) {
  45.     for (int i = a->used - 1; i >= 0; i--) {
  46.         printf("%d", a->arr[i]);
  47.     }
  48. }
  49.  
  50. bool mulBigInt(d_array *a, int m) {
  51.     for (int j = 0; j < a->used; j++)
  52.         a->arr[j] *= m;
  53.  
  54.     for (int j = 0; j < a->used - 1; j++)
  55.         if (a->arr[j] >= base) {
  56.             a->arr[j+1] += a->arr[j] / base;
  57.             a->arr[j] %= base;
  58.         }
  59.     while (a->arr[a->used - 1] >= base) {
  60.         if (!push(a, a->arr[a->used - 1] / base))
  61.             return false;
  62.         a->arr[a->used - 2] %= base;
  63.     }
  64.     return true;
  65. }
  66.  
  67.  
  68.  
  69. int main() {
  70.     d_array arr;
  71.     if (init_arr(&arr, 16)) {
  72.         push(&arr, 1);
  73.  
  74.         int n;
  75.         scanf("%d", &n);
  76.         for (int i = 2; i <= n; i++) {
  77.             if (!mulBigInt(&arr, i)) exit(EXIT_FAILURE);
  78.         }
  79.         printBigInt(&arr);
  80.         putchar('\n');
  81.  
  82.         free_arr(&arr);
  83.         exit(EXIT_SUCCESS);
  84.     }
  85.     else {
  86.         exit(EXIT_FAILURE);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement