// KS converter binary into KS-string in stack representation // Oleg Mazonka 2003 #include using namespace std; void readCode(); void readInput(); void evaluate(); void extractResult(); void deleteTree(); struct node; node * root = 0; void dump(node * n=root); int main(){ try{ readCode(); readInput(); dump(); deleteTree(); }catch(const char * s){ cout<t ) return false; else if( t!=A ) return true; return left->equal(n->left) && right->equal(n->right); } }; int getBitCin(){ char a='\0'; while(1){ cin.get(a); if(!cin) return -1; if( a== '0' ) return 0; if( a== '1' ) return 1; } return 0; // never } int (*getBit)() = getBitCin; void readNode(node ** pn){ int b = (*getBit)(); switch(b){ case -1: throw "\nsyntax error\n"; case 1: *pn = new node(node::A); readNode(&(*pn)->left); readNode(&(*pn)->right); break; case 0: int b2 = (*getBit)(); if( b2==0 ) *pn = new node(node::S); else if( b2==1 ) *pn = new node(node::K); else throw "\nerror: expecting 0 or 1\n"; break; } } void readCode(){ readNode(&root); } int getBitString_counter = 0; const char * getBitString_string = 0; int getBitString(){ switch(getBitString_string[getBitString_counter++]){ case '1': return 1; case '0': return 0; default: return -1; } } void initString(const char *s){ getBitString_counter = 0; getBitString_string = s; getBit = getBitString; } void readNodeFrom(node ** n, const char *s) { initString(s); readNode(n); getBit = getBitCin; } const char * opP = "11001100101001100101011100101001100101100110001010110101"; const char * opD = "10101"; const char * opQ = "10110110001"; const char * opI = "11000101"; const char * opU = "1111100000111001011100001001100000101110011001010011001010111001" "0110010111001011001100010101110010110011001010011001011100101100" "1100010101110010110011000101110010101110000101110011001100010110" "1001010111001011001010111001011100001100110010100110010101001010" "0011010111000101"; const char * op0 = "01"; const char * op1 = "10001"; const char * opY = "11100000111001011100001001100000101"; void readInput(){ int b = (*getBit)(); if( b == -1 ) return; node *r = new node(node::A); r->left = root; root = r; while( 1 ){ node * a1 = new node(node::A); node * a2 = new node(node::A); r->right = a1; a1->left = a2; //a2->left**P readNodeFrom(&a2->left,opP); //a2->right**b if( b==1 ){ readNodeFrom(&a2->right,op1); }else{ readNodeFrom(&a2->right,op0); } getBit = getBitCin; b = (*getBit)(); if( b==-1 ){ readNodeFrom(&a1->right,opD); break; } r = a1; } } void deleteTree(){ delete root; } const char * recogn(node*); void dump(node * n){ if( n==0 ) throw "\ninternal error: 0-node\n"; const char *c = recogn(n); if(c) { cout<t){ case node::A: dump(n->left); if( !(c=recogn(n->right)) ){ cout<<'('; dump(n->right); cout<<')'; }else cout<t == node::K ) return "K"; if( n->t == node::S ) return "S"; return 0; }