[ create a new paste ] login | about

Link: http://codepad.org/JJ6lUm1b    [ raw code | output | fork ]

C, pasted on Oct 6:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 1000
#define BIT 15
#define MASK 0x7fff
#define BUFF10 8000
#define SIGN(x) (((x) >> (BIT - 1)) ? 1 : 0)
typedef unsigned int uintN[N];

void shift_div(int *mod, uintN q)
{
  int cy, i;

  cy = 0;
  for (i = 0; i < N; i++) {
    q[i] = q[i] << 1;
    if (cy)
      q[i] = q[i] | 0x01;
    cy = (q[i] >> BIT);
    q[i] = q[i] & MASK;
  }
  *mod = *mod << 1;
  if (cy)
    *mod = *mod | 0x01;
}

void copy_uintN(uintN a, uintN b)
{
  int i;
  for (i = 0; i < N; i++)
    a[i] = b[i];
}

void clear_uintN(uintN n)
{
  int i;
  for (i = 0; i < N; i++)
    n[i] = 0;
}

void div10(uintN n, uintN q, int *mod)
{
  int i;
  copy_uintN(q, n);
  *mod = 0;
  for (i = 0; i < N * BIT; i++) {
    shift_div(mod, q);
    if (*mod >= 10) {
      q[0] = q[0] | 0x01;
      *mod = *mod - 10;
    }
  }
}

int iszero_uintN(uintN n)
{
  int i;
  for (i = 0; i < N; i++)
    if (n[i] != 0)
      return 0;
  return 1;
}

void output10(uintN m)
{
  int mod10[BUFF10];
  uintN q, n;
  int i, mod;

  copy_uintN(n, m);
  for(i = 0;;) {
    div10(n, q, &mod);
    if (iszero_uintN(q) && mod == 0)
      break;
    mod10[i++] = mod;
    copy_uintN(n, q);
  }
  i--;
  if (i < 0) {
/*     printf("0\n"); */
    putchar('0');
    return;
  }
  while (mod10[i] == '0') {
    printf("mod10[%d] = %d\n", i, mod10[i]);
    --i;
  }
  for (;i >= 0; --i)
    putchar('0' + mod10[i]);
  putchar('\n');
}

int shift_mul(uintN n)
{
  int cy, i;

  cy = 0;
  for (i = 0; i < N; i++) {
    n[i] = n[i] << 1;
    if (cy)
      n[i] = n[i] | 0x01;
    cy = (n[i] >> BIT);
    n[i] = n[i] & MASK;
  }
  return cy;
}

/*-----------------------------------------------------------*/
#define BUFFSIZE 3 /* >= 2 */
char *getline(FILE *fp)
{
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p;
  int fEOL;
  if ((outbuff_malloc = malloc(1)) == NULL) {
    return NULL;
  }
  *outbuff_malloc = '\0';
  fEOL = 0;
  do {
    if (fgets(inbuff, BUFFSIZE, fp) == NULL)
      break;
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n') {
      *(p - 1) = '\0';
      fEOL = 1;
    }
    if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) ==NULL) {
      free(outbuff_malloc);
      return NULL;
    }
    strcat(tmpbuff, inbuff);
    outbuff_malloc = tmpbuff;
  } while (!fEOL);
  if (strlen(outbuff_malloc) == 0) {
    free(outbuff_malloc);
    return NULL;
  }
  return outbuff_malloc;
}

int main() {
  char *p, *q;
  uintN b;
  int c;
  
  if((q = getline(stdin)) == NULL)
    return -1;
  clear_uintN(b);
  for (p = q; *p != '\0'; p++) {
    if ((c = *p - '0') < 0 || c > 7) {
      printf("not octtal.\n");
      return -1;
    }
    shift_mul(b);
    shift_mul(b);
    shift_mul(b);
    b[0] = b[0] | c;
  }
  free(q);
  output10(b);
  return 0;
}
/* end */


Output:
1
Exited: ExitFailure 255


Create a new paste based on this one


Comments: