Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* Print the decimal representation of N/D with up to
- P digits after the decimal point.
- */
- #define P 60
- void *safeMalloc(int n) {
- void *p = malloc(n);
- if (p == NULL) {
- printf("Error: malloc(%d) failed. Out of memory?\n", n);
- exit(EXIT_FAILURE);
- }
- return p;
- }
- void PrintDecimal(unsigned N, unsigned D)
- {
- unsigned *array;
- unsigned tailing;
- unsigned bitch;
- int firstindex;
- int lastindex;
- int found = 0;
- int intial=2;
- array=malloc(intial*sizeof(int));
- int j=0;
- int k=0;
- unsigned bitch1;
- unsigned tailing1;
- // Print the integer portion.
- printf("%u.", N/D);
- // Take the remainder.
- N %= D;
- array[j]=N;
- j++;
- // printf("this is the first remainder in the array:%d\n",array[0]);
- // printf("this is the remainder%d\n",N);
- for (int i = 1; i < P && (N!=0); ++i)
- {
- // Move to next digit position and print next digit.
- N *= 10;
- // printf("%u", N/D);
- // Take the remainder.
- N %= D;
- for(k=0;k<j;k++)
- {
- if(array[k]==N && found==0){
- // printf("The %dth remainder is: %d\n", k, array[k]);
- firstindex=k;
- found = 1;
- lastindex=j;
- }
- }
- if(found==0){ array[j]=N;}
- // printf("This is the remainder that goes in the array:%d\n",N);
- j++;
- if(intial==j)
- {
- intial=intial*2;
- array=realloc(array, intial*sizeof(int));
- }
- }
- //printf("%d %d\n",firstindex,lastindex);
- // printf("\n%d\n",k);
- for(int p=0;p<firstindex;p++)
- {
- tailing1=array[p];
- bitch1=(tailing1*10)/D;
- printf("%u ",bitch1);
- }
- printf("[");
- for(int p=firstindex;p<lastindex;p++)
- {
- //printf("%u ",array[p]);
- tailing=array[p];
- bitch=(tailing*10)/D;
- printf("%u",bitch);
- }
- printf("]");
- putchar('\n');
- //0196078431372549
- }
- int main()
- {
- int denom,numerator;
- scanf("%d",&denom);
- scanf("%d",&numerator);
- PrintDecimal(denom, numerator);
- putchar('\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement