static void * worker_run(void *arg) { worker_t *w = (worker_t *) arg; msg_t *msg; int continue_running = 1; ERL_NIF_TERM answer; w->alive = 1; while(continue_running) { msg = queue_pop(w->q); if(msg->type == msg_stop) { continue_running = 0; } else { answer = make_answer(msg, evaluate_msg(msg, w)); // printf("%d receive\n", w->id); enif_send(NULL, &(msg->pid), msg->env, answer); } if(msg->res!=NULL) { enif_release_resource(msg->res); } msg_destroy(msg); } w->alive = 0; return NULL; }
int solve(Field f, Position p, int flip, int rotate, int i, int nowscore) { if (i >= number_of_stones) { return 0; } int u = i; if (flip == 1) { u += fliped; } u += rot[rotate / 90]; std::vector<Position> next_positions; int score = 0; if (score = put_stone(f, p, u, next_positions)) { score += nowscore; if (score > max_score) { max_score = score; max_score_field = f; fprintf(stderr, "max score %d!\n", score); } f.answer[i] = make_answer(p, flip, rotate); printf("score: %d\n", score); dump_field(f); print_answer(f.answer); int rot_buf[] = {0, 90, 180, 270}; for (auto && f_p : next_positions) { for (int j = i + 1; j < number_of_stones; ++j) { for (auto && s_p : stones[j].fills) { for (int k = 0; k < 4; ++k) { solve(f, Position{f_p.y - s_p.y, f_p.x - s_p.x}, 0, rot_buf[k], j, score); solve(f, Position{f_p.y - s_p.y, f_p.x - s_p.x}, 1, rot_buf[k], j, score); } } } } } return 0; }
static void * esqlite_connection_run(void *arg) { esqlite_connection *db = (esqlite_connection *) arg; esqlite_command *cmd; int continue_running = 1; db->alive = 1; while(continue_running) { cmd = queue_pop(db->commands); if(cmd->type == cmd_stop) continue_running = 0; else enif_send(NULL, &cmd->pid, cmd->env, make_answer(cmd, evaluate_command(cmd, db))); command_destroy(cmd); } db->alive = 0; return NULL; }
static void * esqlite_connection_run(void *arg) { esqlite_connection *db = (esqlite_connection *) arg; esqlite_command *cmd; int continue_running = 1; while(continue_running) { cmd = queue_pop(db->commands); if(cmd->type == cmd_stop) { continue_running = 0; } else if(cmd->type == cmd_notification) { enif_send(NULL, &db->notification_pid, cmd->env, cmd->arg); } else { enif_send(NULL, &cmd->pid, cmd->env, make_answer(cmd, evaluate_command(cmd, db))); } command_destroy(cmd); } return NULL; }
int main(int argc, char* argv[]) { int y1, m1, y2, m2; int ans[3]; int input[3]; int* records; int max_trial; int i, j; srand(time(NULL)); make_answer(ans); max_trial = (int)(log2(ans[0]) + log2(ans[1]) + log2(ans[2])); records = (int*)malloc(sizeof(int)*3*max_trial); printf("日付あてゲーム\n"); printf("正解の年/月/日を推測してください\n"); printf("年は 1 ~ %d の間です\n", MAX_YEAR); printf("答え: %5d 年 %2d 月 %2d 日\n", ans[0], ans[1], ans[2]); for (i = 0; i < max_trial; i++) { printf("%d 回目 (残り %d 回)\n", i+1, max_trial-i); while (1) { printf("年: "); scanf("%d", input); if (input[0] < 1 || input[0] > MAX_YEAR) printf("そんな年ねーよ\n"); else break; } while (1) { printf("月: "); scanf("%d", input+1); if (input[1] < 1 || input[1] > 12) printf("そんな月ねーよ\n"); else break; } while (1) { printf("日: "); scanf("%d", input+2); if (input[2] < 1 || input[2] > monthdays(input[0], input[1])) printf("そんな日ねーよ\n"); else break; } records[i] = input[0]; records[i+1] = input[1]; records[i+2] = input[2]; int chk = chk_answer(ans, input); if (chk == 0) { printf("正解!\n"); break; } printf("不正解...\n"); } printf("入力の履歴:\n"); for (j = 0; j <= i; j++) { printf("%d 回目\n", j+1); printf("%5d 年 %2d 月 %2d 日\n", records[j], records[j+1], records[j+2]); printf("正解との差\n"); printf("%5d, %2d, %2d\n", ans[0]-records[j], ans[1]-records[1], ans[2]-records[2]); } free(records); return 0; }