fix Sub/Div swap / fix wrong order for prefix/lisp display

This commit is contained in:
2021-01-25 17:36:40 +01:00
parent add6d02749
commit 5bd19cbe22
4 changed files with 33 additions and 34 deletions

43
eval.c
View File

@@ -66,34 +66,33 @@ int eval_node(node, depth, pvals, pops, ncalcs)
res=val1*val2;
break;
case Sub:
if (val1 != val2) {
if (val1 > val2) {
res=val1-val2;
} else {
# ifdef DEBUG_EVAL2
printf("eval: Sub: swapping val1=%d val2=%d\n", val1, val2);
# endif
res=val2-val1;
}
if (res == val2) /* already found in subtree */
res=-1;
if (val1<val2) {
NODE *tmp=node->left;
# ifdef DEBUG_EVAL2
printf("eval: Sub: swapping val1=%d val2=%d\n", val1, val2);
# endif
node->left=node->right;
node->right=tmp;
}
if (val1 != val2) {
res=val1-val2;
}
if (res == val2) /* already found in subtree */
res=-1;
break;
case Div:
if (val1 >= val2) {
if (val2 != 1 && (val1 % val2 == 0))
res=val1/val2;
if (res == val2) /* already found in subtree */
res=-1;
} else {
if (val1<val2) {
NODE *tmp=node->left;
# ifdef DEBUG_EVAL2
printf("eval: Div: swapping val1=%d val2=%d\n", val1, val2);
printf("eval: Sub: swapping val1=%d val2=%d\n", val1, val2);
# endif
if (val1 != 1 && (val2 % val1 == 0))
res=val2/val1;
if (res == val1) /* already found in subtree */
res=-1;
node->left=node->right;
node->right=tmp;
}
if (val2 != 1 && (val1 % val2 == 0))
res=val1/val2;
if (res == val2) /* already found in subtree */
res=-1;
break;
case Nop:
case End: