#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct BiTNode { char date[4]; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; int creB(BiTree &Q) { Q = new BiTNode; if (~scanf_s("%s", Q->date, 4) && Q->date[0] != '#') { if (Q->date[0] == '#') Q = NULL; else { creB(Q->lchild); creB(Q->rchild); } return 1; } Q = NULL; return -1; } double get_num(char *a) { int length; int i; double result = 0; if (a[0] == 'A') return 1; else if (a[0] == 'J') return 11; else if (a[0] == 'Q') return 12; else if (a[0] == 'K') return 13; else { length = strlen(a); for (i = 0; i < length; i++) { result = result * 10 + a[i] - '0'; } } return result; } double priB(BiTree Q) { double num = 0; if (Q == NULL) return num; if (Q->date[0] == '/' || Q->date[0] == '*' || Q->date[0] == '-' || Q->date[0] == '+') { double i, j; i = priB(Q->lchild); j = priB(Q->rchild); switch (Q->date[0]) { case '+': num = i + j; break; case '-': num = i - j; break; case '/': if (i == 0 || j == 0) return 0; num = i / j; break; case '*': num = i * j; break; } } else return get_num(Q->date); return num; } void priA(BiTree Q) { if (Q == NULL) return; if (Q->date[0] == '/' || Q->date[0] == '*' || Q->date[0] == '-' || Q->date[0] == '+') { printf("("); priA(Q->lchild); printf("%s", Q->date); priA(Q->rchild); printf(")"); } else printf("%s", Q->date); } int main() { BiTree Q; while (creB(Q) == 1) { double k; k = priB(Q); if (k ==24||k== 23.999999999999989) { priA(Q); printf("=%d ", 24); } else printf("NO "); } return 0; }