Advertisement
Guest User

Pos and neg bases

a guest
Feb 20th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define ipow(n, e) (int)pow(n, e)
  6. #define buf_update(n) printf("buf: %d\n", n)
  7. #define error(msg) fprintf(stderr, "%s", (msg) )
  8.  
  9.  
  10. void
  11. _conbase_negbase(int n, int r, int e)
  12. {
  13.     int m;
  14.    
  15.     /* Find minimum multiplicator of |r^e| that is > |n| */
  16.     for (m = 0; m * abs(ipow(r, e)) < abs(n); ++m) {
  17.         ;
  18.     }
  19.  
  20.     buf_update(m);
  21.    
  22.     if (e && n) {
  23.         /* Must not use absolute values here ! */
  24.         _conbase_negbase(n - (m * ipow(r, e)), r, e - 1);
  25.     }
  26.  
  27.     return;
  28. }
  29.  
  30. void
  31. _conbase_posbase (int n, int r)
  32. {
  33.     if ( n ) {
  34.    
  35.         _conbase_posbase(n / r, r);
  36.         buf_update(n % r);
  37.        
  38.     }
  39. }
  40.  
  41.  
  42. void
  43. conbase(int n, int r)
  44. {
  45.     int _exp = -1;
  46.     int _pow;
  47.     int _powpos = 0;
  48.     int _npos = 0;
  49.  
  50.  
  51.     if ( r < 0 ) {
  52.  
  53.         do {
  54.  
  55.             _pow = ipow(r, ++_exp);
  56.             _powpos = (_pow>=0)?1:0;
  57.             _npos = (n>=0)?1:0;
  58.            
  59.             if ( abs(_pow) > abs(n) ) {
  60.                
  61.                 if ((!_powpos && !_npos)  || (_powpos && _npos)){
  62.                     break;
  63.                 } else if ((_powpos && !_npos) || (!_powpos && _npos) ) {
  64.                     _exp--;
  65.                     break;
  66.                 }
  67.  
  68.             }
  69.                
  70.         } while (1);
  71.        
  72.     _conbase_negbase(n, r, _exp);
  73.  
  74.     }// r < 0
  75.     else if ( r > 1 ) {
  76.      
  77.     _conbase_posbase(n, r);
  78.    
  79.     }// r > 1
  80.     else {
  81.    
  82.     error("conbase: bad base.\n");
  83.    
  84.     }// r == 0 || r == 1
  85. }
  86.  
  87. int
  88. main(int argc, char **argv)
  89. {
  90.  
  91.     printf("<\n");
  92.     conbase(1240, -10);
  93.  
  94.     printf("<\n");
  95.     conbase(124, -10);
  96.  
  97.     printf("<\n");
  98.     conbase(-1240, -10);
  99.  
  100.     printf("<\n");
  101.     conbase(-124, -10);
  102.  
  103.     printf("<\n");
  104.     conbase(-10, -10);
  105.    
  106.     /* Positive base */
  107.     printf("<\n");
  108.     conbase(1240, 10);
  109.  
  110.     printf("<\n");
  111.     conbase(124, 10);
  112.  
  113.     printf("<\n");
  114.     conbase(10, 10);
  115.  
  116.     printf("<\n");
  117.     conbase(127, 8);
  118.  
  119.     printf("<\n");
  120.     conbase(32767, 2);
  121.    
  122.     printf("<\n");
  123.     conbase(-32768, 2);
  124.    
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement