working version: displays only bests results so far.
This commit is contained in:
13
Makefile
13
Makefile
@@ -1,6 +1,7 @@
|
|||||||
SHELL := /bin/bash
|
SHELL := /bin/bash
|
||||||
#CFLAGS := -w -O3
|
CFLAGS := -w -O3 -pg
|
||||||
CFLAGS := -w -g -pg -DDEBUG
|
#CFLAGS := -w -g -pg -DDEBUG
|
||||||
|
#CFLAGS := -w -g -pg -DDEBUG
|
||||||
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||||
export PATH := .:$(PATH)
|
export PATH := .:$(PATH)
|
||||||
|
|
||||||
@@ -13,13 +14,15 @@ OBJS=$(TARGETS:=.o)
|
|||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
lceb: $(OBJS)
|
lceb: $(OBJS)
|
||||||
|
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $^
|
||||||
|
|
||||||
tree: tree.c lceb.h
|
tree: tree.c
|
||||||
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
||||||
oper: oper.c lceb.h
|
oper: oper.c
|
||||||
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
||||||
|
|
||||||
eval.o stack.o best.o: lceb.h
|
lceb.o tree.o oper.o eval.o stack.o best.o: lceb.h
|
||||||
|
tree oper: lceb.h
|
||||||
|
|
||||||
ex2: ex2-c
|
ex2: ex2-c
|
||||||
@$(TIME) ex2-c < $(INPUT)
|
@$(TIME) ex2-c < $(INPUT)
|
||||||
|
42
best.c
42
best.c
@@ -10,7 +10,7 @@ static int target=0;
|
|||||||
static int bestdiff=MAXINT;
|
static int bestdiff=MAXINT;
|
||||||
static int bestops=MAXINT;
|
static int bestops=MAXINT;
|
||||||
|
|
||||||
static BEST bests[1024]; /* TODO: should be dynamic */
|
static BEST bests[1024*10]; /* TODO: should be dynamic */
|
||||||
static int nbests=0;
|
static int nbests=0;
|
||||||
|
|
||||||
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
|
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
|
||||||
@@ -28,39 +28,47 @@ int check_best(res, nops, node, values, ops)
|
|||||||
int *values;
|
int *values;
|
||||||
char *ops;
|
char *ops;
|
||||||
{
|
{
|
||||||
int diff, found=0;
|
int diff, found=0, i=0;
|
||||||
|
|
||||||
diff=DIFF(target, res);
|
diff=DIFF(target, res);
|
||||||
# ifdef DEBUG
|
# ifdef DEBUG1
|
||||||
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||||
# endif
|
# endif
|
||||||
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
|
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
|
||||||
# ifdef DEBUG
|
# ifdef DEBUG
|
||||||
printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
|
// printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||||
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
printf("diff=%d nops=%d %d=", diff, nops, res);
|
||||||
|
print_node(node, TREE_TOP, 0, 4);
|
||||||
|
putchar('\n');
|
||||||
|
//printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||||
# endif
|
# endif
|
||||||
//best=res;
|
//best=res;
|
||||||
|
// clear old bests
|
||||||
|
for (i=0; i<nbests; ++i)
|
||||||
|
free_node(bests[i].root);
|
||||||
bestdiff=diff;
|
bestdiff=diff;
|
||||||
bestops=nops;
|
bestops=nops;
|
||||||
nbests=0;
|
nbests=0;
|
||||||
found=1;
|
found=1;
|
||||||
//return 1;
|
//return 1;
|
||||||
} else if (diff == bestdiff && nops == bestops) {
|
} else if (diff == bestdiff && nops == bestops) {
|
||||||
# ifdef DEBUG
|
# ifdef DEBUG1
|
||||||
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
|
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
|
||||||
|
print_node(node, TREE_TOP, 0, 0);
|
||||||
# endif
|
# endif
|
||||||
found=1;
|
found=2;
|
||||||
}
|
}
|
||||||
if (found) {
|
if (found==1) {
|
||||||
bests[nbests].res=res;
|
bests[nbests].res=res;
|
||||||
bests[nbests].diff=diff;
|
bests[nbests].diff=diff;
|
||||||
|
bests[nbests].nops=nops;
|
||||||
bests[nbests].oper=ops;
|
bests[nbests].oper=ops;
|
||||||
bests[nbests].root=node;
|
bests[nbests].root=dup_node(node);
|
||||||
bests[nbests].values=values;
|
bests[nbests].values=values;
|
||||||
nbests++;
|
nbests++;
|
||||||
return 1;
|
return diff;
|
||||||
}
|
}
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_best(node, values, pops, depth)
|
void print_best(node, values, pops, depth)
|
||||||
@@ -94,7 +102,15 @@ void print_bests()
|
|||||||
int i;
|
int i;
|
||||||
printf("BESTS: diff=%d solutions=%d nops=%d\n", bestdiff, nbests, bestops);
|
printf("BESTS: diff=%d solutions=%d nops=%d\n", bestdiff, nbests, bestops);
|
||||||
for (i=0; i<nbests; ++i) {
|
for (i=0; i<nbests; ++i) {
|
||||||
printf("res = %d : ", bests[i].res);
|
//print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
|
||||||
print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
|
printf("%3d: %d = ", i, bests[i].res);
|
||||||
|
print_node(bests[i].root, TREE_TOP, 0, 0);
|
||||||
|
putchar('\n');
|
||||||
|
//printf("%3d: %d = ", i, bests[i].res);
|
||||||
|
//print_node(bests[i].root, TREE_TOP, 0, 1);
|
||||||
|
//putchar('\n');
|
||||||
|
//printf("%3d: %d = ", i, bests[i].res);
|
||||||
|
//print_node(bests[i].root, TREE_TOP, 0, 4);
|
||||||
|
//putchar('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
eval.c
30
eval.c
@@ -2,30 +2,33 @@
|
|||||||
#include "lceb.h"
|
#include "lceb.h"
|
||||||
|
|
||||||
|
|
||||||
int eval_node(node, depth, pvals, pops)
|
int eval_node(node, depth, pvals, pops, ncalcs)
|
||||||
NODE *node;
|
NODE *node;
|
||||||
int depth;
|
int depth;
|
||||||
int *pvals;
|
int *pvals;
|
||||||
char *pops;
|
char *pops;
|
||||||
|
int *ncalcs;
|
||||||
{
|
{
|
||||||
static int *vals, *val_zero;
|
static int *vals, *val_zero;
|
||||||
static char *ops, *ops_zero;
|
static char *ops, *ops_zero;
|
||||||
static int ncalc;
|
static int totcalc;
|
||||||
static *node_zero;
|
static *node_zero;
|
||||||
int val1, val2, op, res=-1, i;
|
int val1, val2, op, res=-1, i, lcalcs, rcalcs;
|
||||||
|
|
||||||
if (depth == 0) {
|
if (depth == 0) {
|
||||||
val_zero=vals=pvals;
|
val_zero=vals=pvals;
|
||||||
ops_zero=ops=pops;
|
ops_zero=ops=pops;
|
||||||
node_zero=node;
|
node_zero=node;
|
||||||
ncalc=0;
|
totcalc=0;
|
||||||
}
|
}
|
||||||
# ifdef DEBUG1
|
# ifdef DEBUG1
|
||||||
for (i=0; i<=depth; ++i)
|
for (i=0; i<=depth; ++i)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
printf("eval : depth=%d : ", depth);
|
printf("eval : depth=%d : ncalcs=%d", depth, *ncalcs);
|
||||||
if (node->type == TREE_NODE) {
|
if (node->type == TREE_NODE) {
|
||||||
printf("node(%c)\n", *ops);
|
printf("node(%c)\n", *ops);
|
||||||
|
print_node(node, TREE_TOP, 0, 0);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
printf("val(%d)\n", *vals);
|
printf("val(%d)\n", *vals);
|
||||||
}
|
}
|
||||||
@@ -34,16 +37,20 @@ int eval_node(node, depth, pvals, pops)
|
|||||||
node->val=*vals;
|
node->val=*vals;
|
||||||
res=*vals;
|
res=*vals;
|
||||||
vals++;
|
vals++;
|
||||||
|
*ncalcs=0;
|
||||||
|
//printf("leaf=%d\n", res);
|
||||||
} else {
|
} else {
|
||||||
op=*ops;
|
op=*ops;
|
||||||
node->op=*ops;
|
node->op=*ops;
|
||||||
ops++;
|
ops++;
|
||||||
ncalc++;
|
totcalc++;
|
||||||
//printf("NEW node(%s)\n", ops);
|
//printf("NEW node(%s)\n", ops);
|
||||||
val1=eval_node(node->left, depth+1, pvals, ops);
|
val1=eval_node(node->left, depth+1, pvals, ops, &lcalcs);
|
||||||
|
//printf("val1=%d ", val1);
|
||||||
if (val1 <= 0)
|
if (val1 <= 0)
|
||||||
return -1;
|
return -1;
|
||||||
val2=eval_node(node->right, depth+1, pvals, ops);
|
val2=eval_node(node->right, depth+1, pvals, ops, &rcalcs);
|
||||||
|
//printf("val2=%d\n", val2);
|
||||||
if (val2 <= 0)
|
if (val2 <= 0)
|
||||||
return -1;
|
return -1;
|
||||||
switch (op) {
|
switch (op) {
|
||||||
@@ -67,9 +74,12 @@ int eval_node(node, depth, pvals, pops)
|
|||||||
exit(1);
|
exit(1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
*ncalcs=lcalcs+rcalcs+1;
|
||||||
|
}
|
||||||
|
if (res > 0) {
|
||||||
|
if (!check_best(res, *ncalcs, node, val_zero, ops_zero))
|
||||||
|
res=-1;
|
||||||
}
|
}
|
||||||
if (res > 0)
|
|
||||||
check_best(res, ncalc, node_zero, val_zero, ops_zero);
|
|
||||||
# ifdef DEBUG1
|
# ifdef DEBUG1
|
||||||
for (i=0; i<=depth; ++i)
|
for (i=0; i<=depth; ++i)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
|
31
lceb.c
31
lceb.c
@@ -19,7 +19,7 @@ int main(ac, av)
|
|||||||
char **av;
|
char **av;
|
||||||
{
|
{
|
||||||
unsigned target;
|
unsigned target;
|
||||||
STACK *stack;
|
STACK inputstack, *stack;
|
||||||
int i, j, k, stacksize, val, res;
|
int i, j, k, stacksize, val, res;
|
||||||
//char *ops="+-*/", *opscomb;
|
//char *ops="+-*/", *opscomb;
|
||||||
//int len_ops=strlen(ops), ncombs, nops;
|
//int len_ops=strlen(ops), ncombs, nops;
|
||||||
@@ -37,24 +37,22 @@ int main(ac, av)
|
|||||||
stacksize=2*(ac-2)-1;
|
stacksize=2*(ac-2)-1;
|
||||||
nops=ac-2-1;
|
nops=ac-2-1;
|
||||||
|
|
||||||
printf("A\n");
|
|
||||||
gen_combinations(nops);
|
gen_combinations(nops);
|
||||||
printf("A\n");
|
|
||||||
//ncombs=ncombinations(len_ops, nops);
|
//ncombs=ncombinations(len_ops, nops);
|
||||||
ncombs=n_combs();
|
ncombs=n_combs();
|
||||||
printf("A\n");
|
|
||||||
print_combs();
|
print_combs();
|
||||||
printf("target=%d\nstacksize=%d\nops_comb=%d\n", target, stacksize, ncombs);
|
printf("target=%d\nstacksize=%d\nops_comb=%d\n", target, stacksize, ncombs);
|
||||||
set_target(target);
|
set_target(target);
|
||||||
//printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
|
//printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
|
||||||
stack=new_stack(stacksize, "Main Stack", 1);
|
//stack=new_stack(stacksize, "Main Stack", 1);
|
||||||
|
strcpy(inputstack.name, "initial");
|
||||||
for (i=2; i<ac; ++i) {
|
for (i=2; i<ac; ++i) {
|
||||||
val=atoi(av[i]);
|
val=atoi(av[i]);
|
||||||
push_stack(stack, val);
|
push_stack(&inputstack, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
//print_stack(stack, 1);
|
//print_stack(stack, 1);
|
||||||
gen_stacks(stack); // printf("sorting stack...\n");
|
gen_stacks(&inputstack); // printf("sorting stack...\n");
|
||||||
print_stacks();
|
print_stacks();
|
||||||
//mergesort_stack(stack->stack, 0, stack->last-1);
|
//mergesort_stack(stack->stack, 0, stack->last-1);
|
||||||
//print_stack(stack, 1);
|
//print_stack(stack, 1);
|
||||||
@@ -72,24 +70,33 @@ int main(ac, av)
|
|||||||
nstacks=n_stacks();
|
nstacks=n_stacks();
|
||||||
ntrees=n_trees();
|
ntrees=n_trees();
|
||||||
printf("nstacks=%d\nncombs=%d\nntrees=%d\n", nstacks, ncombs, ntrees);
|
printf("nstacks=%d\nncombs=%d\nntrees=%d\n", nstacks, ncombs, ntrees);
|
||||||
|
//for (k=0; k<nstacks; ++k) {
|
||||||
|
// stack=nth_stack(k);
|
||||||
|
// printf("%%%%%%%%%%%%%%%%%%\n");
|
||||||
|
// print_stack(stack, 0);
|
||||||
|
// printf("%%%%%%%%%%%%%%%%%%\n");
|
||||||
|
//}
|
||||||
for (i=0; i<ntrees; ++i) {
|
for (i=0; i<ntrees; ++i) {
|
||||||
tree=nth_tree(i);
|
tree=nth_tree(i);
|
||||||
for (j=0; j<ncombs; ++j) {
|
for (j=0; j<ncombs; ++j) {
|
||||||
comb=nth_comb(j);
|
comb=nth_comb(j);
|
||||||
for (k=0; k<nstacks; ++k) {
|
for (k=0; k<nstacks; ++k) {
|
||||||
|
int ncalcs=0;
|
||||||
stack=nth_stack(k);
|
stack=nth_stack(k);
|
||||||
|
//printf("%%%%%%%%%%%%%%%%%%\n");
|
||||||
eval=eval_node(tree->head, 0, stack->stack, comb);
|
//print_stack(stack, 0);
|
||||||
if (eval > 0) {
|
//printf("%%%%%%%%%%%%%%%%%%\n");
|
||||||
|
eval=eval_node(tree->head, 0, stack->stack, comb, &ncalcs);
|
||||||
#ifdef DEBUG1
|
#ifdef DEBUG1
|
||||||
|
if (eval > 0) {
|
||||||
printf("============================== %d, %d, %d\n", i, j, k);
|
printf("============================== %d, %d, %d\n", i, j, k);
|
||||||
print_tree(tree, 0);
|
print_tree(tree, 0);
|
||||||
print_comb(j);
|
print_comb(j);
|
||||||
print_stack(stack, 0);
|
print_stack(stack, 0);
|
||||||
printf("eval=%d\n", eval);
|
printf("eval=%d - calcs=%d\n", eval, ncalcs);
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//opscomb=combination(ops, nops, i);
|
//opscomb=combination(ops, nops, i);
|
||||||
|
8
lceb.h
8
lceb.h
@@ -17,7 +17,7 @@ typedef struct stack {
|
|||||||
int size;
|
int size;
|
||||||
int last;
|
int last;
|
||||||
struct stack *next;
|
struct stack *next;
|
||||||
int *stack;
|
int stack[MAXINPUT+1];
|
||||||
} STACK;
|
} STACK;
|
||||||
|
|
||||||
#define TREE_UNDEF (-1) /* should not happen */
|
#define TREE_UNDEF (-1) /* should not happen */
|
||||||
@@ -64,6 +64,7 @@ extern void print_node(NODE *node, char side, int depth, int details);
|
|||||||
extern void print_tree(TREE *tree, int details);
|
extern void print_tree(TREE *tree, int details);
|
||||||
extern void print_trees(int details);
|
extern void print_trees(int details);
|
||||||
extern TREE *new_tree(char *name);
|
extern TREE *new_tree(char *name);
|
||||||
|
extern NODE *dup_node(NODE *src);
|
||||||
extern NODE *build_tree(int *desc, int size);
|
extern NODE *build_tree(int *desc, int size);
|
||||||
extern void gen_tree(int *seq, int n, int nb1, int nb0);
|
extern void gen_tree(int *seq, int n, int nb1, int nb0);
|
||||||
extern TREE *nth_tree(int n);
|
extern TREE *nth_tree(int n);
|
||||||
@@ -73,6 +74,7 @@ extern int n_trees();
|
|||||||
extern void print_stack(STACK *stack, int details);
|
extern void print_stack(STACK *stack, int details);
|
||||||
extern void print_stacks();
|
extern void print_stacks();
|
||||||
extern int keep_stack(STACK *stack);
|
extern int keep_stack(STACK *stack);
|
||||||
|
//extern STACK *new_stack(int size, char *name, int keep);
|
||||||
extern STACK *new_stack(int size, char *name, int keep);
|
extern STACK *new_stack(int size, char *name, int keep);
|
||||||
extern int *push_stack(STACK *stack, int val);
|
extern int *push_stack(STACK *stack, int val);
|
||||||
extern int *pop_stack(STACK *stack);
|
extern int *pop_stack(STACK *stack);
|
||||||
@@ -100,10 +102,10 @@ extern char *nth_comb(int n);
|
|||||||
//extern int eval_cell(STACKELT *pos);
|
//extern int eval_cell(STACKELT *pos);
|
||||||
//extern int eval_stack(STACK *stack);
|
//extern int eval_stack(STACK *stack);
|
||||||
/* tree version */
|
/* tree version */
|
||||||
extern int eval_node(NODE *node, int depth, int *pvals, char *pops);
|
extern int eval_node(NODE *node, int depth, int *pvals, char *pops, int *ncalcs);
|
||||||
|
|
||||||
/* best.c */
|
/* best.c */
|
||||||
extern void set_garget (int n);
|
extern void set_target (int n);
|
||||||
extern int check_best(int res, int nops, NODE *node, int *values, char *ops);
|
extern int check_best(int res, int nops, NODE *node, int *values, char *ops);
|
||||||
extern void print_best(NODE *node, int *values, char *pops, int depth);
|
extern void print_best(NODE *node, int *values, char *pops, int depth);
|
||||||
extern void print_bests();
|
extern void print_bests();
|
||||||
|
95
stack.c
95
stack.c
@@ -4,7 +4,10 @@
|
|||||||
#include "lceb.h"
|
#include "lceb.h"
|
||||||
|
|
||||||
static STACK *stacks=NULL;
|
static STACK *stacks=NULL;
|
||||||
static int nstacks=NULL;
|
static STACK *allstacks=NULL;
|
||||||
|
static int nstacks=0;
|
||||||
|
static int totalstacks=0;
|
||||||
|
static int laststack=0;
|
||||||
|
|
||||||
void print_stack(stack, details)
|
void print_stack(stack, details)
|
||||||
STACK *stack;
|
STACK *stack;
|
||||||
@@ -27,13 +30,17 @@ void print_stack(stack, details)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print_stacks()
|
void print_stacks()
|
||||||
{
|
{
|
||||||
STACK *stack=stacks;
|
int i;
|
||||||
printf("nstacks=%d\n", nstacks);
|
//STACK *stack=stacks;
|
||||||
while (stack) {
|
printf("nstacks=%d laststack=%d totalstacks=%d\n", nstacks, laststack, totalstacks);
|
||||||
print_stack(stack, 1);
|
//while (stack) {
|
||||||
stack=stack->next;
|
for (i=0; i<laststack; ++i) {
|
||||||
|
printf("stack %d=", i);
|
||||||
|
//stack=stack->next;
|
||||||
|
print_stack(nth_stack(i), 1);
|
||||||
|
//stack=stack->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +50,7 @@ int keep_stack(stack)
|
|||||||
stack->next=stacks;
|
stack->next=stacks;
|
||||||
stacks=stack;
|
stacks=stack;
|
||||||
nstacks++;
|
nstacks++;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
STACK *new_stack(size, name, keep)
|
STACK *new_stack(size, name, keep)
|
||||||
@@ -54,28 +62,40 @@ STACK *new_stack(size, name, keep)
|
|||||||
int *pelt;
|
int *pelt;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
stack=malloc(sizeof (STACK));
|
if (!allstacks) {
|
||||||
stack->stack=NULL;
|
totalstacks=ALLOCSIZE;
|
||||||
stack->size=0;
|
printf("new_stack %d: allocating %d stacks\n", laststack, totalstacks);
|
||||||
|
allstacks=malloc(totalstacks*sizeof (STACK));
|
||||||
|
}
|
||||||
|
if (laststack==totalstacks) {
|
||||||
|
totalstacks+=ALLOCSIZE;
|
||||||
|
printf("new_stack %d: resizing stacks array to %d\n", laststack, totalstacks);
|
||||||
|
allstacks=realloc(stacks, totalstacks*sizeof (STACK));
|
||||||
|
}
|
||||||
|
|
||||||
|
stack=allstacks+laststack;
|
||||||
|
printf("new_stack %d/%d: address=%p\n", laststack, totalstacks, stack);
|
||||||
|
laststack++;
|
||||||
|
//malloc(sizeof (STACK));
|
||||||
|
//stack->stack=NULL;
|
||||||
|
//stack->size=0;
|
||||||
stack->last=0;
|
stack->last=0;
|
||||||
//stack->eval=0;
|
//stack->eval=0;
|
||||||
strncpy(stack->name, name? name: "No name", sizeof(stack->name));
|
strncpy(stack->name, name? name: "No name", sizeof(stack->name));
|
||||||
if (size) {
|
//if (size) {
|
||||||
pelt=malloc(size*sizeof(int));
|
//pelt=malloc(size*sizeof(int));
|
||||||
stack->size=size;
|
stack->size=MAXINPUT;
|
||||||
stack->stack=pelt;
|
//stack->stack=pelt;
|
||||||
if (keep)
|
if (keep)
|
||||||
keep_stack(stack);
|
keep_stack(stack);
|
||||||
for (i=0; i<size; ++i) {
|
for (i=0; i<MAXINPUT; ++i) {
|
||||||
//(pelt+i)->nop=0;
|
//(pelt+i)->nop=0;
|
||||||
//(pelt+i)->curop=0;
|
//(pelt+i)->curop=0;
|
||||||
//(pelt+i)->op[0]=End;
|
//(pelt+i)->op[0]=End;
|
||||||
pelt[i]=-1;
|
stack->stack[i]=-1;
|
||||||
//(pelt+i)->next=i+1;
|
//(pelt+i)->next=i+1;
|
||||||
//(pelt+i+1)->prev=i;
|
//(pelt+i+1)->prev=i;
|
||||||
}
|
}
|
||||||
//(pelt+i-1)->next=-1;
|
|
||||||
}
|
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,20 +194,35 @@ int gen_stacks(stack)
|
|||||||
char name[80];
|
char name[80];
|
||||||
int last=stack->last;
|
int last=stack->last;
|
||||||
int n=1;
|
int n=1;
|
||||||
STACK *new=stack;
|
STACK *new;
|
||||||
int exists=1;
|
int exists=1;
|
||||||
|
|
||||||
printf("sorting stack...\n");
|
printf("sorting stack...\n");
|
||||||
|
//printf("before sort: ");
|
||||||
|
//print_stack(stack, 0);
|
||||||
mergesort_stack(stack->stack, 0, last-1);
|
mergesort_stack(stack->stack, 0, last-1);
|
||||||
|
//printf("after sort: ");
|
||||||
|
//print_stack(stack, 0);
|
||||||
|
// push initial stack
|
||||||
|
//printf("++++++++++++++++ Adding main stack... ");
|
||||||
|
new=dup_stack(stack, "Main stack");
|
||||||
|
//keep_stack(new);
|
||||||
|
//print_stacks();
|
||||||
//print_stack(stack, 1);
|
//print_stack(stack, 1);
|
||||||
while (exists) {
|
while (exists) {
|
||||||
sprintf(name, "Stack copy %d", n);
|
sprintf(name, "Stack copy %d", n);
|
||||||
new=dup_stack(new, name);
|
//new=dup_stack(new, name);
|
||||||
exists=permute_stack(new->stack, new->last);
|
exists=permute_stack(stack->stack, stack->last);
|
||||||
|
//printf("Permute : ");
|
||||||
|
//print_stack(stack, 0);
|
||||||
if (exists) {
|
if (exists) {
|
||||||
//print_stack(new, 1);
|
printf("++++++++++++++++ Adding stack... ");
|
||||||
keep_stack(new);
|
new=dup_stack(stack, name);
|
||||||
|
// print_stack(new, 0);
|
||||||
|
//keep_stack(new);
|
||||||
}
|
}
|
||||||
|
// printf("---------------------- Stack... ");
|
||||||
|
// print_stacks();
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,15 +259,15 @@ void mergesort_stack(array, left, right)
|
|||||||
STACK *nth_stack(n)
|
STACK *nth_stack(n)
|
||||||
int n;
|
int n;
|
||||||
{
|
{
|
||||||
int i;
|
//int i;
|
||||||
STACK *stack=stacks;
|
//STACK *stack=stacks;
|
||||||
for (i=0; i<n && stack; ++i) {
|
//for (i=0; i<n && stack; ++i) {
|
||||||
stack=stack->next;
|
// stack=stack->next;
|
||||||
}
|
//}
|
||||||
return stack;
|
return allstacks+n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int n_stacks()
|
int n_stacks()
|
||||||
{
|
{
|
||||||
return nstacks;
|
return laststack;
|
||||||
}
|
}
|
||||||
|
104
tree.c
104
tree.c
@@ -60,7 +60,62 @@ void print_node(node, side, depth, details)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (details) {
|
if (!node)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (details) {
|
||||||
|
case 1:
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
printf("%c ", node->op);
|
||||||
|
} else {
|
||||||
|
printf("%d ", node->val);
|
||||||
|
}
|
||||||
|
print_node(node->left, TREE_LEFT, depth+1, details);
|
||||||
|
print_node(node->right, TREE_RIGHT, depth+1, details);
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
print_node(node->left, TREE_LEFT, depth+1, details);
|
||||||
|
print_node(node->right, TREE_RIGHT, depth+1, details);
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
printf("%c ", node->op);
|
||||||
|
} else {
|
||||||
|
printf("%d ", node->val);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
if (!depth) {
|
||||||
|
printf("(%c", node->op);
|
||||||
|
//printf("(op");
|
||||||
|
} else {
|
||||||
|
//printf(" (op");
|
||||||
|
printf(" (%c", node->op);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf(" %d", node->val);
|
||||||
|
}
|
||||||
|
print_node(node->left, TREE_LEFT, depth+1, details);
|
||||||
|
print_node(node->right, TREE_RIGHT, depth+1, details);
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
printf(")");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
printf("(");
|
||||||
|
}
|
||||||
|
print_node(node->left, TREE_LEFT, depth+1, details);
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
printf(" %c ", node->op);
|
||||||
|
} else {
|
||||||
|
printf("%d", node->val);
|
||||||
|
}
|
||||||
|
print_node(node->right, TREE_RIGHT, depth+1, details);
|
||||||
|
if (node->type==TREE_NODE) {
|
||||||
|
printf(")");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
/* left padding */
|
/* left padding */
|
||||||
for (i=0; i<=depth; ++i)
|
for (i=0; i<=depth; ++i)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
@@ -69,17 +124,11 @@ void print_node(node, side, depth, details)
|
|||||||
printf("op=%c val=%d eval=%d", node->op, node->val, node->eval);
|
printf("op=%c val=%d eval=%d", node->op, node->val, node->eval);
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
} else {
|
|
||||||
if (node->type==TREE_NODE) {
|
|
||||||
printf("* ");
|
|
||||||
} else {
|
|
||||||
printf("x ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (node->left)
|
|
||||||
print_node(node->left, TREE_LEFT, depth+1, details);
|
print_node(node->left, TREE_LEFT, depth+1, details);
|
||||||
if (node->right)
|
|
||||||
print_node(node->right, TREE_RIGHT, depth+1, details);
|
print_node(node->right, TREE_RIGHT, depth+1, details);
|
||||||
|
break;;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_tree(tree, details)
|
void print_tree(tree, details)
|
||||||
@@ -87,16 +136,18 @@ void print_tree(tree, details)
|
|||||||
int details;
|
int details;
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
if (details) {
|
switch (details) {
|
||||||
printf("Tree [%s] nodes:%d leaves:%d depth:%d\n",
|
case 2:
|
||||||
tree->name, tree->nodes, tree->leaves, tree->depth);
|
print_node(tree->head, TREE_TOP, 0, details);
|
||||||
|
break;
|
||||||
print_node(tree->head, TREE_TOP, 0, 1);
|
case 0:
|
||||||
} else {
|
case 1:
|
||||||
printf("Tree [%s] : ", tree->name);
|
case 3:
|
||||||
print_node(tree->head, TREE_TOP, 0, 0);
|
case 4:
|
||||||
// for (i=0; )
|
//printf("Tree [%s] : ", tree->name);
|
||||||
|
print_node(tree->head, TREE_TOP, 0, details);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,10 +252,12 @@ void gen_tree(seq, n, nb1, nb0)
|
|||||||
if((nb1 + nb0) == 2*n) { /* end */
|
if((nb1 + nb0) == 2*n) { /* end */
|
||||||
seq[2*n] = 0;
|
seq[2*n] = 0;
|
||||||
ntrees++;
|
ntrees++;
|
||||||
|
# ifdef DEBUG1
|
||||||
printf("tree %d desc=", ntrees);
|
printf("tree %d desc=", ntrees);
|
||||||
for (i=0; i<=2*n; ++i)
|
for (i=0; i<=2*n; ++i)
|
||||||
printf("%d", seq[i]);
|
printf("%d", seq[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
# endif
|
||||||
sprintf(name, "Tree %d", ntrees);
|
sprintf(name, "Tree %d", ntrees);
|
||||||
tree=new_tree(name);
|
tree=new_tree(name);
|
||||||
tree->head=build_tree(seq, 2*n+1);
|
tree->head=build_tree(seq, 2*n+1);
|
||||||
@@ -243,11 +296,20 @@ main(ac, av)
|
|||||||
int ac;
|
int ac;
|
||||||
char **av;
|
char **av;
|
||||||
{
|
{
|
||||||
int n;
|
int n, details=0;
|
||||||
|
|
||||||
int array[1024];
|
int array[1024];
|
||||||
|
if (ac<2 || ac>3) {
|
||||||
|
fprintf(stderr, "usage: %s nodes [type]\n", *av);
|
||||||
|
fprintf(stderr, "type can be 0 (RPN, default, 1 (polish), 2 (details), 3 (lisp notation), 4 (parenthesed notation)\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
if (ac==3) {
|
||||||
|
details=atoi(av[2]);
|
||||||
|
}
|
||||||
n=atoi(av[1]);
|
n=atoi(av[1]);
|
||||||
gen_tree(array, n, 0, 0);
|
gen_tree(array, n, 0, 0);
|
||||||
print_trees(1);
|
print_trees(details);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user