Attachment 'longint.c'

Download

   1 #include <stdio.h>
   2 
   3 #define N 1000
   4 
   5 char x[N];
   6 char y[N];
   7 
   8 int input(char *x) // returns length of number (significant digits)
   9 {
  10     int i = N-1;
  11     int l;
  12     int t;
  13     int c;
  14     int was_not_zero_digit = 0;
  15     c = getchar();
  16     
  17     while((c <= '9') && (c >= '0')){
  18         if( was_not_zero_digit || (c != '0')) {
  19           was_not_zero_digit = 1;   
  20           x[i] = c-'0'; /* digit 0..9 from a digit character */
  21           i--;
  22         }
  23         c = getchar();
  24     }
  25     l = N-1-i; /* length of a number, to return */
  26     
  27     while(i>0) { /* fill up insignificant zeroes */
  28       x[i] = 0;
  29       i--;
  30      } 
  31      
  32      for( i=0; i<=(l-1)/2; i++ )
  33      {
  34        t = x[N-l+i]; 
  35        x[N-l+i] = x[N-1-i];
  36        x[N-1-i] = t;
  37      }
  38   return l;
  39 }
  40 
  41 int output( char *x, int l)
  42 {
  43     int i;
  44     for( i = N-l; i<=N-1; i++ )
  45       printf( "%d", x[i] );
  46     printf("\n" );
  47 }
  48 
  49 int compare( char *x1, int l1, char *x2, int l2)
  50 {
  51     int i; 
  52     if( l1 < l2 )
  53       return -1;
  54     else if ( l1 > l2 )
  55       return 1; 
  56     else { /* l1 == l2 */
  57       i = N-l1; /* first significant digit of both values */
  58       while( (i <= N-1) && (x1[i] == x2[i]) )
  59         i++;
  60       if (i <= N-1) /* there were some different digits */
  61         if (x1[i] < x2[i])
  62           return -1;
  63         else /*x1[i] > x2[i] */
  64           return 1;
  65       else
  66         return 0;
  67     }
  68 }
  69 
  70 int main( int argc, char  **argv )
  71 {
  72     int lx, ly;
  73     int res; 
  74     
  75     lx = input(x);
  76     ly = input(y);
  77     printf( "%d digits: ", lx);
  78     output(x, lx);
  79     printf( "%d digits: ", ly);
  80     output(y, ly);
  81     
  82     res = compare(x, lx, y, ly);
  83     
  84     if( res > 0 )
  85       printf( "x > y\n" );
  86     else if ( res < 0)
  87       printf( "x < y\n" );
  88     else
  89       printf( "x == y\n" );
  90 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.