Advertisement
Guest User

Untitled

a guest
Feb 20th, 2015
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 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.  
  8.  
  9. void _connegbase(int n, int r, int e)
  10. {
  11.     /*
  12.      * Gives 1  1  8  70  1 for digit values which is obviously wrong
  13.      * for -124 converting to base -10
  14.      */
  15.    
  16.     /* Takes the number to be converted n, and the base to convert to r */
  17.     int     m;
  18.    
  19.  
  20.     /* Find minimum multiplicator of |r^e| that is > |n| */
  21.     for (m = 0; m * abs(ipow(r, e)) < abs(n); ++m) {
  22.     ;
  23.     }
  24.  
  25.     buf_update(m);
  26.    
  27.     if (e && n) {
  28.     /* | m * r^e - |n| | passed as n */
  29.     _connegbase(n - (m * ipow(r, e)), r, e-1);
  30.     }
  31.  
  32.     return;
  33. }
  34.  
  35.  
  36. void
  37. connegbase(int n, int r)
  38. {
  39.     int _exp = -1;
  40.     int _pow;
  41.     int _powpos = 0;
  42.     int _npos = 0;
  43.  
  44.  
  45.     if ( r < 0 ) {
  46.  
  47.         do {
  48.  
  49.             _pow = ipow(r, ++_exp);
  50.             _powpos = (_pow>=0)?1:0;
  51.             _npos = (n>=0)?1:0;
  52.            
  53.             if ( abs(_pow) > abs(n) ) {
  54.                
  55.                 if ((!_powpos && !_npos)  || (_powpos && _npos)){
  56.                     break;
  57.                 } else if ((_powpos && !_npos) || (!_powpos && _npos) ) {
  58.                     _exp--;
  59.                     break;
  60.                 }
  61.  
  62.             }
  63.                
  64.         } while (1);
  65.        
  66.         _connegbase(n, r, _exp);
  67.  
  68.     } // r < 0
  69. }
  70.  
  71. int
  72. main(int argc, char **argv)
  73. {
  74.  
  75.     printf("<\n");
  76.     connegbase(1240, -10);
  77.  
  78.     printf("<\n");
  79.     connegbase(124, -10);
  80.  
  81.     printf("<\n");
  82.     connegbase(-1240, -10);
  83.  
  84.     printf("<\n");
  85.     connegbase(-124, -10);
  86.  
  87.     printf("<\n");
  88.     connegbase(0,-10);
  89.    
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement