Advertisement
xBloodY

showdouble

Oct 3rd, 2022
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "stdlib.h"
  4.  
  5.  
  6. union u {
  7.     char data[sizeof(long double)];
  8.     long double ldb;
  9.     double db;
  10.     float fl;
  11. };
  12.  
  13. int main() {
  14.     union u X;
  15.     memset(X.data, 0, sizeof(long double));
  16.     char t[10] = {0};
  17.     char elem = 0;
  18.     int space_chek = 1, start = 0;
  19.     scanf("%s", &t);
  20.     if (strstr(t, "float") != NULL) {
  21.         scanf("%f", &X.fl);
  22.         start = sizeof(float) * 8;
  23.     }
  24.     else if (strstr(t, "double") == t) {
  25.         scanf("%lf", &X.db);
  26.         start = sizeof(double) * 8;
  27.     }
  28.     else {
  29.         scanf("%s", &t);
  30.         scanf("%Lf", &X.ldb);
  31.         start = sizeof(long double) * 8;
  32.     }
  33.  
  34.     for (int i = --start; i >= 0; i--) {
  35.         int ind1 = i/8, ind2 = i%8;
  36.         elem = X.data[ind1] >> ind2 & 1;
  37.         printf("%d", elem);
  38.         if (space_chek % 4 == 0)
  39.             printf(" ");
  40.         space_chek++;
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement