// default base is decimal to binary // run program with flag to start with different base #include #include typedef struct stack { int * base; int top; int size; } Stack; void getSpace(Stack * s) { if (s->size == 0) { s->size = 1; s->base = (int *) calloc (s->size,sizeof(int)); } else { s->size += 1; s->base = (int *) realloc (s->base, sizeof(int) * s->size); } } void conversion(Stack * s, int from, int to) { while (from != 0) { if (s->size <= s->top) getSpace(s); s->base[s->top++] = from % to; from /= to; } } void print(Stack * s, int from) { printf("origin\t\tbinary\n"); printf("%d\t\t",from); while (s->top != 0) printf("%d",s->base[--s->top]); s->top = 0; putchar('\n'); } int getNumber(void) { int tmp; if (scanf("%d", &tmp) != 1) exit(EXIT_SUCCESS); return tmp; } int main(void) { Stack storage = {NULL, 0, 0}; int from = getNumber(); int to = 2; conversion(&storage,from,to); print(&storage,from); free(storage.base); return 0; }