Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*  Print the decimal representation of N/D with up to
  5.     P digits after the decimal point.
  6. */
  7. #define P   60
  8. void *safeMalloc(int n) {
  9.     void *p = malloc(n);
  10.     if (p == NULL) {
  11.         printf("Error: malloc(%d) failed. Out of memory?\n", n);
  12.         exit(EXIT_FAILURE);
  13.     }
  14.     return p;
  15. }
  16.  
  17.  
  18.  
  19. void PrintDecimal(unsigned N, unsigned D)
  20. {
  21.     unsigned *array;
  22.     unsigned tailing;
  23.     unsigned bitch;
  24.     int firstindex;
  25.     int lastindex;
  26.     int found = 0;
  27.     int intial=2;
  28.     array=malloc(intial*sizeof(int));
  29.     int j=0;
  30.     int k=0;
  31.     unsigned bitch1;
  32.     unsigned tailing1;
  33.    
  34.     //  Print the integer portion.
  35.    printf("%u.", N/D);
  36.  
  37.     //  Take the remainder.
  38.     N %= D;
  39.     array[j]=N;
  40.     j++;
  41.    // printf("this is the first remainder in the array:%d\n",array[0]);
  42.   //  printf("this is the remainder%d\n",N);
  43.     for (int i = 1; i < P && (N!=0); ++i)
  44.     {
  45.         //  Move to next digit position and print next digit.
  46.         N *= 10;
  47.        // printf("%u", N/D);
  48.  
  49.         //  Take the remainder.
  50.         N %= D;
  51.        
  52.         for(k=0;k<j;k++)
  53.         {
  54.            
  55.             if(array[k]==N && found==0){
  56.               //  printf("The %dth remainder is: %d\n", k, array[k]);
  57.                 firstindex=k;
  58.                 found = 1;
  59.                 lastindex=j;
  60.                
  61.             }
  62.    
  63.         }
  64.        
  65.         if(found==0){   array[j]=N;}
  66.    //     printf("This is the remainder that goes in the array:%d\n",N);
  67.  
  68.         j++;
  69.         if(intial==j)
  70.         {
  71.             intial=intial*2;
  72.             array=realloc(array, intial*sizeof(int));
  73.         }
  74.  
  75.  
  76.     }
  77.     //printf("%d %d\n",firstindex,lastindex);
  78.    // printf("\n%d\n",k);
  79.     for(int p=0;p<firstindex;p++)
  80.     {
  81.         tailing1=array[p];
  82.         bitch1=(tailing1*10)/D;
  83.         printf("%u ",bitch1);
  84.        
  85.     }
  86.     printf("[");
  87.    for(int p=firstindex;p<lastindex;p++)
  88.    {
  89.     //printf("%u ",array[p]);
  90.        tailing=array[p];
  91.        bitch=(tailing*10)/D;
  92.  
  93.        
  94.      printf("%u",bitch);
  95.  
  96.    }
  97.     printf("]");
  98.    putchar('\n');
  99.  
  100.     //0196078431372549
  101.  
  102. }
  103.  
  104.  
  105. int main()
  106. {
  107.     int denom,numerator;
  108.     scanf("%d",&denom);
  109.     scanf("%d",&numerator);
  110.     PrintDecimal(denom, numerator);
  111.     putchar('\n');
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement