Beyrin

lab2_3

Jun 1st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <malloc.h>
  5. const char *Scale="0123456789ABCDEF";
  6.  
  7. int dig2int(char d, int from)
  8. {
  9.     int i;
  10.     for (i=0; i<from; i++)
  11.     {
  12.  
  13.         if (Scale[i]==d)
  14.             return i;
  15.     }
  16.     return -1;
  17.  
  18. }
  19. char *upcase(char *n)
  20. {
  21.     int z;
  22.    for (z = 0; z != strlen(n); z++)
  23.         n[z] = toupper(n[z]);
  24.  
  25. }
  26. char  *convertq(char *n, int from, int to, int *error)
  27. {
  28.       *error = 0;
  29.       int i,j,length,p,k;
  30.       char *buf;
  31.       length=strlen(n);
  32.       p=0;
  33.       for (i=0; i<length; i++)
  34.         if ( dig2int(n[i], from) != -1)
  35.             p=p*from+dig2int(n[i], from);
  36.         else
  37.         {
  38.             *error = 1;
  39.             break;
  40.         }
  41.       buf=(char *) calloc(100,1);
  42.       k=99;
  43.       length=0;
  44.       while (1)
  45.       {
  46.          j=p%to;
  47.          buf[k--]=Scale[j];
  48.          length++;
  49.          p=p/to;
  50.          if (p==0) break;
  51.       }
  52.       j=0;
  53.       k++;
  54.       while (1)
  55.       {
  56.          buf[j++]=buf[k++];
  57.          if (k == 100) break;
  58.       }
  59.       buf=(char *) realloc(buf,(length+1));
  60.       return buf;
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64.     int *from;
  65.     int error;
  66.     char n[100];
  67.     int input;
  68.  
  69.     do
  70.     {
  71.     printf("Choose wisely:\n");
  72.     printf("1. 16 to 10\n");
  73.     printf("2. 8 to 10\n");
  74.     printf("3. 10 to 2\n");
  75.     printf("4. exit]\n");
  76.     scanf( "%d", &input );
  77.     getchar();
  78.         if (input == 1)
  79.         {
  80.             from = 16;
  81.             char *ans;
  82.             printf("Enter your number: \n");
  83.             scanf("%s", n);
  84.             upcase(n);
  85.             ans = convertq(n, 16, 10, &error);
  86.             if (!error)
  87.                 printf("Result: %s\n", ans);
  88.             else
  89.                 printf("SMT WENT WRONG\n");
  90.         }
  91.         if (input == 2)
  92.         {
  93.             from = 8;
  94.             char *ans;
  95.             printf("Enter your number: \n");
  96.             scanf("%s", n);
  97.             upcase(n);
  98.             ans = convertq(n, 8, 10, &error);
  99.             if (!error)
  100.                 printf("Result: %s\n", ans);
  101.             else
  102.                 printf("SMT WENT WRONG\n");
  103.         }
  104.         if (input == 3)
  105.         {
  106.             from = 10;
  107.             char *ans;
  108.             printf("Enter your number: \n");
  109.             scanf("%s", n);
  110.             upcase(n);
  111.             ans = convertq(n, 10, 2, &error);
  112.             if (!error)
  113.                 printf("Result: %s\n", ans);
  114.             else
  115.                 printf("SMT WENT WRONG\n");
  116.  
  117.         }
  118.         if (input == 4)
  119.         {
  120.             break;
  121.         }
  122.     }
  123.     while(1);
  124.     return 0;
  125.  
  126. }
Add Comment
Please, Sign In to add comment