#include <stdio.h>#include <string.h>char stack_op[256];unsigned int stack_num[256];int top_op;int top_num;int GetPrecedence(char op, int bInStack);char* GetNumber(char *str, unsigned int *pNum);unsigned int Compute(char* line);int main(int argc, char* argv[]);int GetPrecedence(char op, int bInStack){ switch(op) { case 0: return 1; case '(': return bInStack? 1 : 100; case ')': return 1; case '+': return 2; case '-': return 2; case '*': return 3; } return 0;}char* GetNumber(char *str, unsigned int *pNum){ char *pCh = str + 1; *pNum = 0; while(*pCh != '}') { if(*pCh >= 'A' && *pCh <= 'Z') *pNum = *pNum | (1 << (*pCh - 'A')); ++pCh; } return pCh;}unsigned int Compute(char* line){ char *pCh; unsigned int n1, n2, result; int prec1, prec2; top_op = 0; stack_op[top_op] = '('; top_num = -1; pCh = line; while(1) { if(*pCh == '{') { pCh = GetNumber(pCh, &n1); ++top_num; stack_num[top_num] = n1; } else { prec2 = GetPrecedence(*pCh, 0); prec1 = GetPrecedence(stack_op[top_op], 1); if(prec2 > prec1) { ++top_op; stack_op[top_op] = *pCh; } else { while(prec2 <= prec1 && strchr("*+-", stack_op[top_op]) != NULL) { n1 = stack_num[top_num - 1]; n2 = stack_num[top_num]; switch(stack_op[top_op]) { case '+': result = (n1 | n2); break; case '-': result = (n1 & (~n2)); break; case '*': result = (n1 & n2); break; } --top_num; stack_num[top_num] = result; --top_op; prec1 = GetPrecedence(stack_op[top_op], 1); } if(*pCh == ')') { while(stack_op[top_op] != '(') { --top_op; } --top_op; } else if(*pCh == 0) break; else { ++top_op; stack_op[top_op] = *pCh; } } } ++pCh; } if(top_num == 0) result = stack_num[0]; else result = 0; return result;}int main(int argc, char* argv[]){ unsigned int result, x; char line[256]; while(gets(line) != NULL) { result = Compute(line); printf("{"); for(x = 0; x < 26; x++) { if(result & (1 << x)) printf("%c", x + 'A'); } printf("}n"); } return 0;}