Advertisement
mrAnderson33

перевод чисел в разные СС( unix)

Mar 18th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include<ctype.h>
  5.  
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10.  
  11.  
  12. int gorner(char*, unsigned);
  13. char * invertgorner(unsigned,unsigned );
  14. char * cc(char * , unsigned ,unsigned);
  15.  
  16.  
  17. int main(int argc, char * argv[])
  18. {
  19.     if (argc < 3){
  20.                     fprintf(stderr,"please write the name files!\n");
  21.                     exit(1);
  22.                   }
  23.     int  fd_in = open(argv[1],O_RDONLY);
  24.     unsigned fd_out = open(argv[2],O_CREAT|O_RDWR,S_IRWXU|S_IROTH);
  25.     char * buf = malloc(sizeof(char) * 256);
  26.     char * buf2 = malloc(sizeof(char) * 256);
  27.     char *b = buf;
  28.     char  symb;
  29.     int i = 0 ;
  30.     unsigned flag = 0;
  31.    int   num = 0;
  32.     unsigned base_in = 0;
  33.     unsigned base_out = 2;
  34.     if (fd_in == -1)
  35. {
  36.     fprintf(stderr,"error! file with this name does not exist!\n ");
  37.         exit(1);
  38. }
  39.    do
  40. {
  41.     read (fd_in,&symb,sizeof(char));
  42.     if (symb =='(') flag = 1;
  43.     else if((symb!=')' ) && (symb!= ';')) {
  44.     if  (flag== 0) *b++ = symb;
  45.     else base_in = base_in * 10 +  symb - '0'; }
  46. }
  47.     while(symb != ';');
  48.  
  49.  
  50.     for(base_out ;base_out <= 16;base_out*=2)
  51.     {
  52.       sprintf(buf2,"%s(%u)=%s(%u)\n",buf,base_in,cc(buf,base_in,base_out),base_out);
  53.       write(fd_out,(void *) buf2, strlen(buf2));
  54.     if(base_out == 8)
  55.         {
  56.             base_out = 10;
  57.             sprintf(buf2,"%s(%u)=%s(%u)\n",buf,base_in,cc(buf,base_in,base_out),base_out);
  58.              write(fd_out,(void *) buf2, strlen(buf2));
  59.             base_out = 8;
  60.  
  61.         }
  62.     }
  63.  
  64.     close(fd_in);
  65. return 0;
  66. }
  67.  
  68.  
  69. int gorner(char * num , unsigned base)
  70. {
  71. int res = 0;
  72. while(*num) res =isdigit(*num) ? (res * base + (*num++) - '0') : (res * base + toupper(*num++))-'A'+10;
  73. return res;
  74. }
  75.  
  76. char * invertgorner(unsigned num, unsigned base)
  77. {
  78.     unsigned n = num;
  79.     char * r =  malloc (sizeof(char)* 256);
  80.     while (n)
  81.     {
  82.         r++;
  83.         n /= base;
  84.     }
  85.     while (num)
  86.     {
  87.         *--r = (((num % base) < 10) ? ((num % base) + '0') : ((num % base) - 10 + 'A'));
  88.         num /= base;
  89.     }
  90.     return r;
  91. }
  92.  
  93.  
  94. char * cc(char * num, unsigned base_in, unsigned base_out)
  95. {
  96.     if (base_in == 10)return invertgorner(gorner(num, base_in), base_out);
  97.     return invertgorner(gorner(num, base_in), base_out);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement