Курсовая работа по дисциплине Объектноориентированное программирование Наименование задачи
Скачать 348.58 Kb.
|
Блок-схема алгоритмаКод программыПрограммная реализация алгоритмов для решения задачи представлена ниже.Файл AppClass.cpp #include "BaseClass.h" #include "AppClass.h" #include "ExtraClass.h" AppClass::AppClass(BaseClass * obj): BaseClass(obj){}; bool AppClass::buildObjTree(){ string way, objname; BaseClass * root, *obj; //cin >> rootname; cin >> way; //setObj(rootname); setObj(way); root = this; int classnum; while(true){ //cin >> rootname; cin >> way; //if(rootname == "endtree"){ if(way == "endtree"){ cout << "Object tree"; //cout << "";//??? printNew(""); return true; found"; } cin >> objname >> classnum; //root = findName(rootname); root = getByWay(way); if(!root){//если головной объект не найден cout << "Object tree"; printNew(""); cout << endl << "The head object " << way << " is not return false; } switch(classnum){ case(2): case(3): case(4): case(5): obj = new Branch2(root, objname); break; obj = new Branch3(root, objname); break; obj = new Branch4(root, objname); break; obj = new Branch5(root, objname); break; case(6): } } } obj = new Branch6(root, objname); break; int AppClass::execApp(){ string way, com; while (true){ cin >> com; if(com == "END"){ return 0; >getObj(); } cin >> way; if(com == "FIND"){ cout << endl << way; if(getByWay(way)){ cout << " Object name: "<< getByWay(way)- } else{ } } cout << " Object is not found"; if(com == "SET"){ if(getByWay(way)){ setCurrent(getByWay(way)); cout << endl << "Object is set: " << getCurrent()->getObj(); } else{ cout << endl << "Object is not found: " << getCurrent()->getObj() << " " << way; continue; } } } /*cout << "Object tree"; printNew(); string objname; int newstate; while(cin >> objname >> newstate){ BaseClass * t = findName(objname); t->setState(newstate); } cout << endl << "The tree of objects and their readiness"; printNewState(); //cout << getObj(); //printObj(); */ ///return 0; } Файл AppClass.h #ifndef AppClass_h #define AppClass_h #include "BaseClass.h" #include "Branch2.h" #include "Branch3.h" #include "Branch4.h" #include "Branch5.h" #include "Branch6.h" class AppClass: public BaseClass{ public: }; #endif AppClass(BaseClass* obj = nullptr); bool buildObjTree(); int execApp(); Файл BaseClass.cpp #include "BaseClass.h" BaseClass::BaseClass(BaseClass * obj, string name){ setObj(name); root_ptr = obj; current = this;//новое if(obj != nullptr){ root_ptr->objToSub(this); } } BaseClass::BaseClass(){ for(int i = 0; i < subobj.size(); ++i){ delete subobj[i]; } } void BaseClass::setObj(string name){ this->objname = name; } string BaseClass::getObj(){ return this->objname; } void BaseClass::printObj(){ if(subobj.size() > 0){ cout << endl << getObj(); for(int i = 0; i < subobj.size(); i++){ cout << " " << subobj[i]->getObj(); } for(int i = 0; i < subobj.size(); i++){ subobj[i]->printObj(); } } } void BaseClass::setRootPtr(BaseClass * obj){ if(this->root_ptr != nullptr){ root_ptr->delFromSub(this); } this->root_ptr = obj; if(obj != nullptr){ root_ptr->objToSub(this); } } BaseClass * BaseClass::getRootPtr(){ return root_ptr; } void BaseClass::objToSub(BaseClass * obj){ if(obj != nullptr){ subobj.push_back(obj); } } void BaseClass::delFromSub(BaseClass * obj) { for(int i = 0; i < subobj.size(); i++){ if(subobj[i] == obj){ subobj.erase(subobj.begin() + i); break; } } } BaseClass * BaseClass::findName(string name){ BaseClass * obj_obj; if(objname == name){ return this; } for(int i = 0; i < subobj.size(); i++){ obj_obj = subobj[i]->findName(name); if(obj_obj != nullptr){ return obj_obj; } } return nullptr; } int BaseClass::countLen(){ if(root_ptr == nullptr){ } else{ } } /* return 0; return root_ptr->countLen()+1; void BaseClass::printNew(){ cout << endl; for(int i = 0; i < countLen(); ++i){ cout << " "; } cout << getObj(); for(int i = 0; i < subobj.size(); i++){ subobj[i]->printNew(); } } *////////////////////////////////////////////////// void BaseClass::printNew(string line){ cout << endl << line << getObj(); for(BaseClass * obj : subobj){ obj->printNew(line + " "); } } /////////////////////////////////////////////////// void BaseClass::printNewState(){ cout << endl; for(int i = 0; i < countLen(); ++i){ cout << " "; } cout << getObj()<<' '; if(state == 0){ cout << "is not ready"; } else{ } cout << "is ready"; for(BaseClass * obj : subobj){ obj->printNewState(); } } int BaseClass::getState(){ return this->state; } bool BaseClass::setState(int state){ if(state == 0){ if(root_ptr != nullptr && root_ptr->getState() == 0){ return true; } else{ } } else{ for(BaseClass * obj : subobj){ obj->setState(0); } this->state = 0; return true; if(root_ptr != nullptr && root_ptr->getState() == 0){ return false; } else{ } } } this->state = state; return true; //новое BaseClass * BaseClass::getByWay(string way){ string name; int slash; if(way == "/"){ return this; } if(way[0] == '/' && way[1] == '/'){ name = way.substr(2); return findName(name); } if(way == "."){ return current; } slash = way.find('/', 1); if(way[0] == '/'){ if(slash != -1){ name = way.substr(1, slash - 1); for(BaseClass * obj : subobj){ if(obj->getObj() == name){ return obj- >getByWay(way.substr(slash)); } } } else{ name = way.substr(1); for(BaseClass * obj : subobj){ if(obj->getObj() == name){ return obj; } } } } if(way[0] != '/' && way.find('/') != -1){ return current->getByWay('/' + way); } return nullptr; } void BaseClass::setCurrent(BaseClass * current){ this->current = current; } BaseClass * BaseClass::getCurrent(){ return current; } Файл BaseClass.h #ifndef BaseClass_h #define BaseClass_h #include using namespace std; class BaseClass { private: int state;//31 string objname;//411 BaseClass * root_ptr;//411 vector void objToSub(BaseClass * obj);//411 void delFromSub(BaseClass * obj);//411 public: }; #endif BaseClass * current;//новое BaseClass(BaseClass * obj, string name = "empty");//41 BaseClass();//411 void setRootPtr(BaseClass * obj);//411 void setObj(string name);//411 string getObj();//411 void printObj();//411 BaseClass * getRootPtr();//411 BaseClass * findName(string name);//411 //void printNew();//31 void printNew(string line);//31 void printNewState();//31 bool setState(int state);//31 int getState();//31 BaseClass * getByWay(string way);//новое BaseClass * getCurrent();//новое void setCurrent(BaseClass * current);//новое Файл Branch2.cpp Файл Branch2.h #ifndef Branch2_h #define Branch2_h #include "BaseClass.h" class Branch2: public BaseClass{ public: Branch2(BaseClass* root, string name) :BaseClass(root, name){}; }; #endif Файл Branch3.cpp Файл Branch3.h #ifndef Branch3_h #define Branch3_h #include "BaseClass.h" class Branch3: public BaseClass{ public: Branch3(BaseClass* root, string name) :BaseClass(root, name){}; }; #endif Файл Branch4.cpp Файл Branch4.h #ifndef Branch4_h #define Branch4_h #include "BaseClass.h" class Branch4: public BaseClass{ public: Branch4(BaseClass* root, string name) :BaseClass(root, name){}; }; #endif Файл Branch5.cpp Файл Branch5.h #ifndef Branch5_h #define Branch5_h #include "BaseClass.h" class Branch5: public BaseClass{ public: Branch5(BaseClass* root, string name) :BaseClass(root, name){}; }; #endif Файл Branch6.cpp Файл Branch6.h #ifndef Branch6_h #define Branch6_h #include "BaseClass.h" class Branch6: public BaseClass{ public: Branch6(BaseClass* root, string name) :BaseClass(root, name){}; }; #endif Файл ExtraClass.cpp #include "ExtraClass.h" ExtraClass::ExtraClass(BaseClass * obj, string name): BaseClass(obj, name){} Файл ExtraClass.h #ifndef ExtraClass_h #define ExtraClass_h #include "BaseClass.h" class ExtraClass: public BaseClass{ public: ExtraClass(BaseClass * obj, string name); }; #endif Файл main.cpp #include "BaseClass.h" #include "AppClass.h" #include "ExtraClass.h" int main() { AppClass app(nullptr); if(!app.buildObjTree()){ return 0; } return app.execApp(); } |