#include #include /* for kbhit() */ #include #include #define decprec 78 #define b1prec 127 #define b2prec 256 /* b2prec should be b1prec*2+2 */ /* Function for kbhit() - the gcc way */ /* Only works for the key though... */ int kbhit() { struct timeval wait; int ret; fd_set readfd; FD_ZERO(&readfd); FD_SET(0, &readfd); wait.tv_sec = 0; /* Wait zero seconds */ wait.tv_usec = 0; ret = select(1, &readfd, NULL, NULL, &wait); return(ret); } void main(int argc, char *argv[]) { int i, j, k, l, pflag=1, kcount=0, jcount=0; int d[decprec]; int b1[b1prec]; int b2[b2prec]; int bn[b2prec][decprec]; int lp1[b2prec]; /* Initializations */ if (argc > 2) { printf("Usage: palin [approximate binary startoff]\n"); exit(); } printf("\nHit return at any time to stop...\n"); printf("\nInitializing...\n"); l=-1; for (i=0; i 1) { j=0; for (i=(strlen(argv[1])-1); i>-1; i-=1) { if (argv[1][i] == '1') { b1[j]=1; } else { b1[j]=0; } j++; } l=j-1; /* Strip leading zeros if any exist */ j=l; while ((j>-1) && (b1[j]==0)) { l-=1; j-=1; } /* Now subtract 1 from the binary half to counter the first increment */ i=0; while ((b1[i]==0) && (i < b1prec)) { i++; } b1[i]=0; if (i==l) { l-=1; } if (i != b1prec) { for (j=0; jl) { l++; } for (j=0; j-1; i-=1) { b2[j]=b1[i]; j++; } for (i=0; i<=l; i++) { b2[j]=b1[i]; j++; } /* j is now the length+1 of b2. (b2[0..j-1]) */ /* Convert b2 to decimal d */ for (i=0; i-1; j-=1) { printf("%d", b2[j]); } i-=1; printf(" "); for (j=i; j>-1; j-=1) { printf("%d", d[j]); } printf("\n"); } /* Create b2 binary palindrome #2. */ j=0; for (i=l; i>-1; i-=1) { b2[j]=b1[i]; j++; } b2[j]=0; j++; for (i=0; i<=l; i++) { b2[j]=b1[i]; j++; } /* j is now the length+1 of b2. (b2[0..j-1]) */ /* Convert b2 to decimal d */ for (i=0; i-1; j-=1) { printf("%d", b2[j]); } i-=1; printf(" "); for (j=i; j>-1; j-=1) { printf("%d", d[j]); } printf("\n"); } /* Create b2 binary palindrome #3. */ j=0; for (i=l; i>-1; i-=1) { b2[j]=b1[i]; j++; } b2[j]=1; j++; for (i=0; i<=l; i++) { b2[j]=b1[i]; j++; } /* j is now the length+1 of b2. (b2[0..j-1]) */ /* Convert b2 to decimal d */ for (i=0; i-1; j-=1) { printf("%d", b2[j]); } i-=1; printf(" "); for (j=i; j>-1; j-=1) { printf("%d", d[j]); } printf("\n"); } kcount++; if (kcount==1024) { /* every 1024 loops check to see if kbhit */ kcount=0; jcount++; if (kbhit()) { printf("\n\nBinary half currently worked with:\n\n"); for (j=l; j>-1; j-=1) { printf("%d", b1[j]); } printf("\n\n"); exit(); } if (jcount==16384) { /* every 16777216 loops print current binary half */ jcount=0; if (pflag) { printf("\n"); pflag=0; } for (j=l; j>-1; j-=1) { printf("%d", b1[j]); } printf("\n"); } } } /* End of while(1) loop */ }