Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char*change_binary(int num)
  6. {
  7. int i, k=0,temp = num, cnt = 0, *arr = NULL; char *str = NULL;
  8. while (num != 0)
  9. {
  10. temp = num;
  11. temp %= 2;
  12. str = (char *)realloc(str, sizeof(char)*(k + 1));
  13. *(str + k) = temp + '0';
  14. k++;
  15. num /= 2;
  16. }
  17. *(str + k) = NULL;
  18.  
  19. puts(str);
  20. return str;
  21. }
  22. void main()
  23. {
  24. FILE *f1, *f2; int i, j = 0, k = 0, *arr = NULL,*intarr=NULL, temp = -1; char *newarr = NULL,tempr[5];
  25. fopen_s(&f1, "Text.txt", "rt");
  26. fopen_s(&f2, "res.txt", "w");
  27. if (f1 == NULL)
  28. exit(1);
  29. for (i = 0; temp!=0; i++)
  30. {
  31. arr = (int *)realloc(arr,sizeof(int)*(i+1));
  32. if (arr == NULL)
  33. exit(1);
  34. fscanf_s(f1, "%d", &temp);
  35. *(arr + i) = temp;
  36. }
  37.  
  38.  
  39. if (f1 == NULL)
  40. exit(1);
  41. for (i = 0; i <16; i++)
  42. {
  43. if (arr[i] % 2 == 0)
  44. fprintf_s(f2, "%-3d", arr[i]);
  45. else
  46. {
  47. int num2, num = arr[i];
  48. num2 = arr[i];
  49. k = 0;
  50. while (num != 0)
  51. {
  52. temp = num;
  53. temp %= 2;
  54. newarr = (char *)realloc(newarr, sizeof(char)*(k + 1));
  55. intarr = (int *)realloc(intarr, sizeof(int)*(k + 1));
  56. *(newarr + k) = temp + '0';
  57. tempr[k] = *(newarr + k);
  58. k++;
  59. num /= 2;
  60.  
  61. }
  62. *(newarr + k) = NULL;
  63.  
  64.  
  65.  
  66. printf("The number in dec is : %d \n and in binary is: \n", num2);
  67. printf("%b", num2);
  68. puts(newarr);
  69.  
  70. }
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77. for (i = 0; i < k; i++)
  78. {
  79.  
  80. printf("%-3d", *(newarr + i));
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement