void ArrayOps::evaluate(SymTab & symTab, std::unique_ptr<FuncTab> &funcTab) { auto type = symTab.getValueFor(_id)->type(); if(_op == "append") { if(_test == nullptr) { std::cout << "ArrayOps::evaluate append, no element provided\n"; exit(1); } auto element = _test->evaluate(symTab, funcTab); if( type == TypeDescriptor::NUMBERARRAY ) { if(element->type() == TypeDescriptor::INTEGER) { auto nDesc = dynamic_cast<NumberDescriptor*>(element.get()); auto narray = dynamic_cast<NumberArray*> (symTab.getValueFor(_id).get()); narray->nAppend(nDesc->value.intValue); } else { std::cout << "ArrayOps::append error: members must be of "; std::cout << "the same type\n"; exit(1); } } else if(type == TypeDescriptor::STRINGARRAY) { if(element->type() == TypeDescriptor::STRING) { auto sDesc = dynamic_cast<StringDescriptor*>(element.get()); auto sarray = dynamic_cast<StringArray*> (symTab.getValueFor(_id).get()); sarray->sAppend(sDesc->value); } else { std::cout << "ArrayOps::append error: members must be of "; std::cout << "the same type\n"; exit(1); } } else if (type == TypeDescriptor::NULLARRAY) { if(element->type() == TypeDescriptor::INTEGER) { auto nDesc = dynamic_cast<NumberDescriptor*>(element.get()); if(nDesc == nullptr) { std::cout << "ArrayOps::evaluate error: invalid cast"; exit(1); } std::shared_ptr<NumberArray> narray = std::make_shared<NumberArray>(TypeDescriptor::NUMBERARRAY); narray->nAppend(nDesc->value.intValue); symTab.setValueFor(_id, narray); } else if(element->type() == TypeDescriptor::STRING) { auto sDesc = dynamic_cast<StringDescriptor*>(element.get()); if(sDesc == nullptr) { std::cout << "ArrayOps::evaluate error: invalid cast"; exit(1); } std::shared_ptr<StringArray> sarray = std::make_shared<StringArray>(TypeDescriptor::STRINGARRAY); sarray->sAppend(sDesc->value); symTab.setValueFor(_id, sarray); } else { std::cout << "append() is not supported for this type\n"; exit(1); } } else { std::cout << "append() is not supported for this type\n"; exit(1); } } else if (_op == "pop") { if( type == TypeDescriptor::NUMBERARRAY ) { auto narray = dynamic_cast<NumberArray*> (symTab.getValueFor(_id).get()); if(narray != nullptr) { if(_test == nullptr) narray->nPop(); else { auto element = _test->evaluate(symTab, funcTab); narray->nPopIndex(element.get()); } } } else if(type == TypeDescriptor::STRINGARRAY) { auto sarray = dynamic_cast<StringArray*> (symTab.getValueFor(_id).get()); if(sarray != nullptr) { if(_test == nullptr) sarray->sPop(); else { auto element = _test->evaluate(symTab, funcTab); sarray->sPopIndex(element.get()); } } } else { std::cout << "pop is not supported for this type\n"; exit(1); } } }
int main (void) { char str[100000]; char op; char inputChar; int N; int i = 0; int first, end; Stack frontStack; Stack rearStack; Stack tempStack; scanf("%s", str); sInit(&frontStack); sInit(&rearStack); while(1) { if(str[i] == '\0') break; sPush(&frontStack, str[i]); i++; } scanf("%d", &N); while(N--) { scanf("%c", &op); if(op == 'P') { scanf("%c", &inputChar); sPush(&frontStack, inputChar); } else if(op == 'L') { if(!sIsEmpty(&frontStack)) sPush(&rearStack, sPop(&frontStack)); } else if(op == 'D') { if(!sIsEmpty(&rearStack)) sPush(&frontStack, sPop(&rearStack)); } else if(op == 'B') { if(!sIsEmpty(&frontStack)) sPop(&frontStack); } } sInit(&tempStack); while(1) { if(sIsEmpty(&frontStack)) break; sPush(&tempStack, sPop(&frontStack)); } printf("2"); while(1){ if(sIsEmpty(&tempStack)) break; printf("%c", sPop(&tempStack)); } printf("1"); while(1){ if(sIsEmpty(&rearStack)) break; printf("%c", sPop(&rearStack)); } printf("3"); return 0; }