Skip to content

Instantly share code, notes, and snippets.

@libraplanet
Last active September 28, 2015 01:48
Show Gist options
  • Save libraplanet/1365590 to your computer and use it in GitHub Desktop.
Save libraplanet/1365590 to your computer and use it in GitHub Desktop.
adam
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define TRUE (1)
#define FALSE (0)
typedef struct
{
unsigned int pair;
unsigned int birth_boy;
unsigned int birth_girl;
unsigned int fixed_boy;
unsigned int fixed_girl;
}DATA;
void output2(const char *pref, const char *cap, int boy, int girl)
{
int total = boy + girl;
int parboy, pargirl;
if(total == 0)
{
parboy = pargirl = 0;
}
else
{
parboy = boy * 100 / total;
pargirl = 100 - parboy;
}
printf("%s%s%d boy , %d girl. (%d%% : %d%%)\n", pref, cap, boy, girl, parboy, pargirl);
}
void output(DATA* data, int root, int cur)
{
if(data == NULL)
{
printf("\n\n\n");
}
else
{
const char *str[2] = {"", "new pair!"};
const char *pref[2][2] = {{"", "\x1b[1A"}, {"", "\x1b[K"}};
printf("%s%s%s%s", pref[0][cur], pref[0][cur], pref[0][cur], pref[0][cur]);
printf("%s%d pair. %s\n", pref[1][cur], data->pair, str[root]);
output2(pref[1][cur], " birth : ", data->birth_boy, data->birth_girl);
output2(pref[1][cur], " fixed : ", data->fixed_boy, data->fixed_girl);
output2(pref[1][cur], " total : ", data->birth_boy + data->fixed_boy, data->birth_girl + data->fixed_girl);
}
}
void refresh(DATA* data, int root, int runnable)
{
if(runnable == TRUE)
{
output(data, root, TRUE);
usleep(500000);
}
}
void birth(DATA* data, int root, int console)
{
if((rand() % 2) == 0)
{
data->birth_boy++;
refresh(data, root, console);
}
else
{
data->birth_girl++;
refresh(data, root, console);
birth(data, FALSE, console);
}
return;
}
void run(DATA* data)
{
#if CONSOLE_MODE
output(NULL, FALSE, TRUE);
#endif
while(!0)
{
data->fixed_boy += data->birth_boy;
data->fixed_girl += data->birth_girl;
data->birth_boy = 0;
data->birth_girl = 0;
#if !CONSOLE_MODE
if(data->pair >= 10000000)
{
output(data, FALSE, FALSE);
break;
}
#endif
data->pair++;
#if CONSOLE_MODE
birth(data, TRUE, TRUE);
#else
birth(data, TRUE, FALSE);
#endif
}
}
void init(DATA* data)
{
memset(data, 0, sizeof(*data));
}
int main(void)
{
DATA data;
init(&data);
printf("hellow gcc!\n");
srand((unsigned)time(NULL));
run(&data);
printf("fin.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment