#include #include #include /* Carol v5 - a Countdown number-puzzle solver. Adam Sampson, azz@gnu.org changelog: v1 - initial version v2 - added "not found" message v3 - print out the expression rather than just the stack v4 - check that it's given enough arguments v5 - check for "invalid" division, add -a, -i, -r, -n */ float *stack; int sp; int invalid; int numnums = 6; int list_all = 0; int allow_invalid = 0; float range = 0; float pop() { return stack[sp--]; } void push(float i) { stack[++sp] = i; } void doplus() { push(pop()+pop()); } void dominus() { float i = pop(); push(i-pop()); } void dotimes() { push(pop()*pop()); } void dodivide() { float t = pop(); float b = pop(); if ((int) t % (int) b != 0) invalid = 1; push(t / b); } #define NUMOPS 4 struct { char name; void (*func)(); } operator[NUMOPS] = { { '+', doplus }, { '-', dominus }, { '*', dotimes }, { '/', dodivide } }; float *number; float target; int *numcounter; int *opcounter; void onallcombinations(int num, int *counters, int repeat, void (*func)()) { int i, j, k; int ncount = repeat ? repeat : num; while (1) { if (!repeat) { for (j=0; j= (target - range) && result <= (target + range) && (allow_invalid || !invalid)) { printf("(((((%.0f", number[numcounter[numnums-1]]); for (i=0; i