ResistanceJke

lab2 normal vid

Dec 8th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <Windows.h>
  8. #include <iostream>
  9.  
  10. using namespace std;   
  11.  
  12. char* Dvoichnaya(int x)
  13. {
  14.     char* t = (char*)malloc(100);
  15.     int i = 0;
  16.     do
  17.     {
  18.         t[i++] = x % 2 + 48;
  19.         x = x / 2;
  20.     } while (x != 0);
  21.     t[i] = '\0';
  22.     int k = strlen(t) - 2; 
  23.     int m = k / 2;
  24.     for (int i = 0; i <= m; i++)
  25.     {
  26.         char tmp = t[i];
  27.         t[i] = t[k - i + 1];
  28.         t[k - i + 1] = tmp;
  29.     }
  30.     return t;
  31.     free(t);
  32. }
  33.  
  34. char* DvoichnayaDrob(float x)
  35. {
  36.     char* t = (char*)malloc(100);
  37.     int i = 0;
  38.     do
  39.     {
  40.         x *= 2;
  41.         if (x >= 1) { t[i++] = '1'; x -= 1; }
  42.         else { t[i++] = '0'; }
  43.     } while (i != 32);
  44.     t[i] = '\0';
  45.     return t;
  46.     free(t);
  47. }
  48.  
  49. int main()
  50. {
  51.     SetConsoleCP(1251);
  52.     SetConsoleOutputCP(1251);
  53.  
  54.     int che = 0, k = 1;
  55.     int num;
  56.     int i = 0;
  57.     int step = 0, ish, a, ch = 0;
  58.     int ot = 0;
  59.     float str, po = 0;
  60.     char finch[100], final[33];
  61.     int z;
  62.     scanf("%e", &str);
  63.     if (str >= 0) { final[0] = '0'; }
  64.     else { final[0] = '1'; }
  65.     str = fabs(str);
  66.     char* otm = (char*)malloc(100), * pom = (char*)malloc(100);
  67.     ot = str;
  68.     po = str - ot;
  69.     otm = Dvoichnaya(ot);
  70.     pom = DvoichnayaDrob(po);
  71.     for (int i = 0; i <= strlen(otm); i++) { finch[i] = otm[i]; }
  72.     finch[strlen(otm)] = '.';
  73.     for (int i = 0; i <= strlen(pom); i++) { finch[i + strlen(otm) + 1] = pom[i]; }
  74.     for (int i = 0; finch[1] != '.'; i++, step++)
  75.     {
  76.         char tmp = 0;
  77.         tmp = finch[strlen(otm) - i];
  78.         finch[strlen(otm) - i] = finch[strlen(otm) - i - 1];
  79.         finch[strlen(otm) - i - 1] = tmp;
  80.     }
  81.     step += 127;
  82.     if (step > 127) { otm = Dvoichnaya(step); }
  83.     else { for (i = 0; i <= 7; i++) { otm[i] = '0'; otm[8] = '\0'; } }
  84.     for (int i = 0; i <= strlen(otm); i++) {
  85.         final[i + 1] = otm[i];
  86.     }
  87.     for (int i = 0; (i <= strlen(finch - 2)) && (i <= 23); i++) {
  88.         final[i + 9] = finch[i + 2];
  89.     }
  90.     final[32] = '\0';
  91.     cout << "\n";
  92.     printf("Число во внутреннем представлении компьютера: %s\n", final);
  93.     cout << "\n";
  94.     if (final[0] == '1')
  95.         cout << "Знак: " << "          " << final[0] << " (отрицательный)";
  96.     if (final[0] == '0')
  97.         cout << "Знак: " << "          " << final[0] << " (положительный)";
  98.     cout << "\n";
  99.     cout << "\n";
  100.     cout << "Экспонента: " << "\t";
  101.     for (int i = 1; i < 9; i++)
  102.     {
  103.         cout << final[i];
  104.     }
  105.     cout << "\n";
  106.     cout << "\n";
  107.     cout << "Мантисса: " << "\t";
  108.     for (int i = 9; i < 32; i++)
  109.     {
  110.         cout << final[i];
  111.     }
  112.     cout << "\n" << endl;
  113.     return 0;
  114. }
Add Comment
Please, Sign In to add comment