[ create a new paste ] login | about

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

C, pasted on May 30:
#include <stdio.h>
int f(char *p, int *n) {
  int m;
  if (*p != '0' && *p != '1') {
    *n = 0;
    return 1;
  }
  m = f(p + 1, n);
  printf("%c*%d\n", *p, m);
  *n += (*p == '1') ? m : 0;
  return 2 * m;
}

#define N 1024
int main() {
  int n;
  static char buff[N];
  printf("input binary digit.\n");
  fgets(buff, N, stdin);
  printf("transform binary to decimal.\n");
  f(buff, &n);
  printf("value: %d\n", n);
  return 0;
}
/* end */


Output:
1
2
3
input binary digit.
transform binary to decimal.
value: 0


Create a new paste based on this one


Comments: