Advertisement
Guest User

Untitled

a guest
Jan 17th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // div.cpp : Defines the entry point for the console application.
  2. //
  3. #include <stdio.h>
  4.  
  5. int div_(int n, int d);
  6. void binary_out(int b,char *s);
  7.  
  8.  
  9. int main(void)
  10. {
  11.   int n,d,r;
  12.   for(;;)
  13.     {
  14.       printf("Enter numerator and denominator (-1 to quit):");
  15.       scanf("%d %d",&n,&d);
  16.       if((n==-1)||(d==-1))
  17.         break;
  18.       binary_out(n,"<- numerator\n");
  19.       binary_out(d,"<- denominator\n");
  20.       r=div_(n,d);
  21.       printf("===\n");
  22.       binary_out(r,"<- quotient\n");
  23.     }
  24.   return 0;
  25. }
  26.  
  27. void binary_out(int b,char *s)
  28.   {
  29.     int x;
  30.     for(x=(sizeof(b)<<3)-1;x>=0;x--)
  31.       if(b&(1<<x))
  32.         printf("1");
  33.       else
  34.         printf("0");
  35.     printf("(%d) %s",b,s);
  36.   }
  37.  
  38. #define DEBUG_DIV
  39.  
  40. int div_(int n, int d)
  41.   {
  42.     int res,x,t;
  43.     res=0; /* зануляем все разряды */
  44.     for(x=(sizeof(n)<<3)-1;x>=0;x--)/* пройти по всем битам от старшего до младшего */
  45.       {
  46.         t=n>>x;/* сдвигаем, чтобы остались только старшие (sizeof(n)-x) бит */
  47. #ifdef DEBUG_DIV
  48.         printf("************************************************\n");
  49.         binary_out(x," <- x\n");
  50.         binary_out(t," <- t\n\n");
  51. #endif
  52.         if(t>=d)/* если старшие биты больше делителя */
  53.           {
  54. #ifdef DEBUG_DIV
  55.             binary_out(n,"_<- n before\n");
  56.             binary_out(d<<x," <- subtrahend\n");
  57. #endif
  58.             n=n-(d<<x); /* то вычитаем из исходного числа делитель, сдвинутый
  59.             так, чтобы он был под старшими разрядами */
  60. #ifdef DEBUG_DIV
  61.             printf("--------------------------------\n");
  62.             binary_out(n,"<- difference\n\n");
  63.             binary_out(res," <- old res\n");
  64.             binary_out(1<<x,"+ <- adding\n");
  65.             printf("--------------------------------\n");
  66. #endif
  67.             res=res+(1<<x); /* а в результат добавляем единичку в соотетствующем разряде */
  68. #ifdef DEBUG_DIV
  69.             binary_out(res,"<- new res\n");
  70. #endif
  71.           }
  72.         /* ну а если условие не выполняется, то ничего не делаем
  73.         потому что вычитать нечего, а надо записать в очередной разряд
  74.         нолик... но он уже там!*/
  75.       }
  76.     return res;
  77.   }
  78.  
  79. /* clear division -- without debug or comments
  80. int div_(int n, int d)
  81.   {
  82.     int res,x,t;
  83.     res=0;
  84.     for(x=(sizeof(n)<<3)-1;x>=0;x--)
  85.       {
  86.         t=n>>x;
  87.         if(t>=d)
  88.           {
  89.             n=n-(d<<x);
  90.             res=res+(1<<x);
  91.           }
  92.       }
  93.     return res;
  94.   }
  95. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement