#include #include /* Carol v4 - 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 */ float stack[10]; int sp; 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 i = pop(); push(i/pop()); } #define NUMOPS 4 struct { char name; void (*func)(); } operator[NUMOPS] = { { '+', doplus }, { '-', dominus }, { '*', dotimes }, { '/', dodivide } }; #define NUMNUMS 6 float number[NUMNUMS] = { 1., 5., 6., 7., 8., 9. }; float target; int numcounter[NUMNUMS]; int opcounter[NUMNUMS-1]; 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