Skip to content

Instantly share code, notes, and snippets.

@hainp2604
Created February 4, 2014 12:40
Show Gist options
  • Save hainp2604/8802903 to your computer and use it in GitHub Desktop.
Save hainp2604/8802903 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SIZE 10
int main(int argc, char *argv[])
{
// Expected result:
// |00** |
// |00* *|
// |00 **|
// |0 0**|
int test = 123;
abacus(test);
printf("\n");
// Expected result:
// |00* *|
// |00 **|
// |0 0**|
// | 00**|
test = 1234;
abacus(test);
return 0;
}
int abacus(int value) {
char str[SIZE];
char left[SIZE];
char right[SIZE];
char* tmpl = "00000*****";
int v = value;
// Convert int to string
sprintf(str, "%d", v);
int len = strlen(str);
int i, d;
for (i = 0; i < (SIZE - len); i++) {
printf("|%s |\n", tmpl);
}
d = 0;
for (i = len - 1; i >= 0; i--) {
int tmp = d * pow(10, i + 1);
double p = pow(10, i);
v = v - tmp;
d = v / p;
strncpy(left, tmpl, SIZE - d);
strncpy(right, &tmpl[SIZE - d], d);
// Copy from lewtds ;)
left[SIZE - d] = 0;
right[d] = 0;
printf("|%s %s|\n", left, right);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment