Advertisement
AmidamaruZXC

Untitled

Nov 2nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. int clearInputBuffer() {
  7.     int ch;
  8.     while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
  9.     return ch;
  10. }
  11.  
  12. void toBinary(int n)
  13. {
  14.     int d;
  15.     if (n == 0)
  16.         return;
  17.     toBinary(n / 10);
  18.     d = n % 10;
  19.     switch (d)
  20.     {
  21.     case 0:
  22.         printf("0000");
  23.         break;
  24.     case 1:
  25.         printf("0001");
  26.         break;
  27.     case 2:
  28.         printf("0010");
  29.         break;
  30.     case 3:
  31.         printf("0011");
  32.         break;
  33.     case 4:
  34.         printf("0100");
  35.         break;
  36.     case 5:
  37.         printf("0101");
  38.         break;
  39.     case 6:
  40.         printf("0110");
  41.         break;
  42.     case 7:
  43.         printf("0111");
  44.         break;
  45.     case 8:
  46.         printf("1000");
  47.         break;
  48.     case 9:
  49.         printf("1001");
  50.     }
  51. }
  52.  
  53. void toDec(char* n)
  54. {
  55.     int len = 0, temp, i = 0;
  56.     char* digit = (char*)malloc(4);
  57.     for (i = 0; n[i] != '\0'; i++)
  58.         len++;
  59.     i = 0;
  60.     while (n[i] != '\0')
  61.     {
  62.         temp = 0;
  63.         while (temp != 4)
  64.         {
  65.             digit[temp] = n[i];
  66.             i++;
  67.             temp++;
  68.         }
  69.         if (strncmp(digit, "0000", 4) == 0)
  70.             printf("0");
  71.         if (strncmp(digit, "0001", 4) == 0)
  72.             printf("1");
  73.         if (strncmp(digit, "0010", 4) == 0)
  74.             printf("2");
  75.         if (strncmp(digit, "0011", 4) == 0)
  76.             printf("3");
  77.         if (strncmp(digit, "0100", 4) == 0)
  78.             printf("4");
  79.         if (strncmp(digit, "0101", 4) == 0)
  80.             printf("5");
  81.         if (strncmp(digit, "0110", 4) == 0)
  82.             printf("6");
  83.         if (strncmp(digit, "0111", 4) == 0)
  84.             printf("7");
  85.         if (strncmp(digit, "1000", 4) == 0)
  86.             printf("8");
  87.         if (strncmp(digit, "1001", 4) == 0)
  88.             printf("9");
  89.     }
  90.  
  91. }
  92.  
  93. void main(int argc, char* argv[])
  94. {
  95.     setlocale(LC_ALL, "rus");
  96.     int action = -1;
  97.     while (action != 0)
  98.     {
  99.         printf("1. Перевести из десятичного в двоично-десятичное\n");
  100.         printf("2. Перевести из двоично-десятичного в десятичное\n");
  101.         printf("0. Выйти\n");
  102.         printf("Выберите действие: ");
  103.         int n;
  104.         char s[100];
  105.         char* p;
  106.         scanf("%d", &action);
  107.         switch (action)
  108.         {
  109.         case 0:
  110.             break;
  111.         case 1:
  112.             printf("Введите десятичное число: ");
  113.             scanf("%d", &n);
  114.             toBinary(n);
  115.             printf("\n");
  116.             break;
  117.         case 2:
  118.             printf("Введите двоично-десятичное число: ");
  119.             clearInputBuffer();
  120.             p = gets(s);
  121.             toDec(p);
  122.             printf("\n");
  123.             break;
  124.         default:
  125.             printf("Некорректный ввод.\n");
  126.             break;
  127.         }
  128.     }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement