SHARE
TWEET

Files2

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