/////////////////////////////////////////////////////////// // Проект Task2_3 /////////////////////////////////////////////////////////// // AString.h #ifndef ASTRING_H #define ASTRING_H #include class AString { public: virtual ~AString() {} virtual const std::string& GetName() const = 0; virtual const std::string& GetVal() const = 0; virtual int GetSize() const = 0; }; #endif //ASTRING_H /////////////////////////////////////////////////////////// // SymbString.h #include #include "AString.h" class SymbString : public AString { public: SymbString(std::string _name) : name(_name) {} SymbString(std::string _name, std::string _val) : name(_name), val(_val) {} const std::string& GetName() const { return name; } const std::string& GetVal() const { return val; } int GetSize() const { return val.size(); } private: std::string name; std::string val; }; /////////////////////////////////////////////////////////// // HexString.h #include #include "AString.h" const std::string alph = "0123456789ABCDEF"; bool IsHexStrVal(std::string); class HexString : public AString { public: HexString(std::string _name) : name(_name) {} HexString(std::string, std::string); const std::string& GetName() const { return name; } const std::string& GetVal() const { return val; } int GetSize() const { return val.size(); } private: std::string name; std::string val; }; /////////////////////////////////////////////////////////// // HexString.cpp #include #include "HexString.h" using namespace std; bool IsHexStrVal(string _str) { for (int i = 0; i < _str.size(); ++i) if (-1 == alph.find_first_of(_str[i])) return false; return true; } HexString::HexString(string _name, string _val) : name(_name) { if (IsHexStrVal(_val)) val = _val; } /////////////////////////////////////////////////////////// // Action.h #ifndef ACTION_H #define ACTION_H #include "AString.h" class Action { public: virtual ~Action() {} virtual void Operate(AString*) = 0; virtual const std::string& GetName() const = 0; protected: long GetDecimal(AString* pObj) const; }; #endif /* ACTION_H */ /////////////////////////////////////////////////////////// // Action.cpp #include #include "Action.h" #include "HexString.h" using namespace std; long Action::GetDecimal(AString* pObj) const { if (dynamic_cast(pObj)) { long dest; string source = pObj->GetVal(); sscanf(source.c_str(), "%lX", &dest); return dest; } else { cout << "Action not supported." << endl; return -1; } } /////////////////////////////////////////////////////////// // ShowStr.h #include "Action.h" class ShowStr : public Action { public: ShowStr() : name("Show string value") {} void Operate(AString*); const std::string& GetName() const { return name; } private: std::string name; // обозначение операции }; extern ShowStr show_str; /////////////////////////////////////////////////////////// // ShowStr.cpp #include #include "ShowStr.h" using namespace std; void ShowStr::Operate(AString* pObj) { cout << pObj->GetName() << ": "; cout << pObj->GetVal() << endl; cin.get(); } // Глобальный объект ShowStr show_str; ///////////////////////////////////////////////////// // ShowDec.h #include "Action.h" class ShowDec : public Action { public: ShowDec() : name("Show decimal value") {} void Operate(AString*); const std::string& GetName() const { return name; } private: std::string name; // обозначение операции }; extern ShowDec show_dec; ///////////////////////////////////////////////////// // ShowDec.cpp #include #include "ShowDec.h" #include "HexString.h" using namespace std; void ShowDec::Operate(AString* pObj) { cout << pObj->GetName() << ": "; long decVal = GetDecimal(pObj); if (decVal != -1) cout << GetDecimal(pObj); cout << endl; cin.get(); } // Глобальный объект ShowDec show_dec; ///////////////////////////////////////////////////// // ShowBin.h #include "Action.h" class ShowBin : public Action { public: ShowBin() : name("Show binary value") {} void Operate(AString*); const std::string& GetName() const { return name; } private: std::string GetBinary(AString*) const; std::string name; // обозначение операции }; extern ShowBin show_bin; ///////////////////////////////////////////////////// // ShowBin.cpp #include #include "ShowBin.h" #include "ShowDec.h" #include "AString.h" using namespace std; void ShowBin::Operate(AString* pObj) { cout << pObj->GetName() << ": "; cout << GetBinary(pObj) << endl; cin.get(); } string ShowBin::GetBinary(AString* pObj) const { int nBinDigit = 4 * pObj->GetSize(); char* binStr = new char[nBinDigit + 1]; for (int k = 0; k < nBinDigit; ++k) binStr[k] = '0'; binStr[nBinDigit] = 0; long decVal = GetDecimal(pObj); if (-1 == decVal) return string(""); int i = nBinDigit - 1; while (decVal > 0) { binStr[i--] = 48 + (decVal % 2); decVal >>= 1; } string temp(binStr); delete [] binStr; return temp; } // Глобальный объект ShowBin show_bin; ///////////////////////////////////////////////////// // Factory.h #ifndef FACTORY_H #define FACTORY_H #include #include "AString.h" class Factory { friend class Menu; public: Factory() {} void AddObject(); void DeleteObject(); private: std::vector pObj; }; #endif //FACTORY_H ///////////////////////////////////////////////////// // Factory.cpp #include #include "Factory.h" #include "Menu.h" #include "SymbString.h" #include "HexString.h" using namespace std; #define MAX_LEN_STR 100 void Factory::AddObject() { cout << "--------------------------------------\n"; cout << "Select object type:\n"; cout << "1. Symbolic string" << endl; cout << "2. Hexadecimal string" << endl; int item = Menu::SelectItem(2); string name; cout << "Enter object name: "; cin >> name; cin.get(); cout << "Enter object value: "; char buf[MAX_LEN_STR]; cin.getline(buf, MAX_LEN_STR); string value = buf; AString* pNewObj; switch (item) { case 1: pNewObj = new SymbString(name, value); break; case 2: if (!IsHexStrVal(value)) { cout << "Error!" << endl; return; } pNewObj = new HexString(name, value); break; } pObj.push_back(pNewObj); cout << "Object added." << endl; } void Factory::DeleteObject() { int nItem = pObj.size(); if (!nItem) { cout << "There are no objects." << endl; cin.get(); return; } cout << "......................................\n"; cout << "Delete one of the following Object:\n"; for (int i = 0; i < nItem; ++i) cout << i + 1 << ". " << pObj[i]->GetName() << endl; int item = Menu::SelectItem(nItem); string objName = pObj[item - 1]->GetName(); pObj.erase(pObj.begin() + item - 1); cout << "Object " << objName << " deleted." << endl; cin.get(); } ///////////////////////////////////////////////////// // Menu.h #include #include "AString.h" #include "Action.h" #include "Factory.h" typedef enum { AddObj, DelObj, WorkWithObj, Exit } JobMode; class Menu { public: Menu(std::vector); JobMode SelectJob() const; AString* SelectObject(const Factory&) const; Action* SelectAction(const AString*) const; static int SelectItem(int); private: std::vector pAct; }; ///////////////////////////////////////////////////// // Menu.cpp #include #include "AString.h" #include "SymbString.h" #include "HexString.h" #include "Menu.h" using namespace std; Menu::Menu(vector _pAct) : pAct(_pAct) {} JobMode Menu::SelectJob() const { cout << "======================================\n"; cout << "Select one of the following job mode:\n"; cout << "1. Add object" << endl; cout << "2. Delete object" << endl; cout << "3. Work with object" << endl; cout << "4. Exit" << endl; int item = SelectItem(4); return (JobMode)(item - 1); } AString* Menu::SelectObject(const Factory& fctry) const { int nItem = fctry.pObj.size(); if (!nItem) { cout << "There are no objects." << endl; cin.get(); return 0; } cout << "......................................\n"; cout << "Select one of the following Object:\n"; for (int i = 0; i < nItem; ++i) { cout << i + 1 << ". "; cout << fctry.pObj[i]->GetName() << endl; } int item = SelectItem(nItem); return fctry.pObj[item - 1]; } Action* Menu::SelectAction(const AString* pObj) const { if (!pObj) return 0; int nItem = pAct.size(); cout << ". . . . . . . . . . . . . . . . . . .\n"; cout << "Select one of the following Action:\n"; for (int i = 0; i < nItem; ++i) { cout << i + 1 << ". "; cout << pAct[i]->GetName() << endl; } int item = SelectItem(nItem); return pAct[item - 1]; } int Menu::SelectItem(int nItem) { // Возьмите код аналогичной функции из проекта Task2_1 } ///////////////////////////////////////////////////// // Main.cpp #include #include "AString.h" #include "SymbString.h" #include "HexString.h" #include "Action.h" #include "ShowStr.h" #include "ShowDec.h" #include "ShowBin.h" #include "Factory.h" #include "Menu.h" using namespace std; Action* pActs[] = { &show_str, &show_dec, &show_bin }; vector actionList(pActs, pActs + sizeof(pActs)/sizeof(Action*)); int main() { Factory factory; Menu menu(actionList); JobMode jobMode; while ((jobMode = menu.SelectJob()) != Exit ) { switch (jobMode) { case AddObj: factory.AddObject(); break; case DelObj: factory.DeleteObject(); break; case WorkWithObj: AString* pObj = menu.SelectObject(factory); Action* pAct = menu.SelectAction(pObj); if (pAct) pAct->Operate(pObj); break; } cin.get(); } cout << "Bye!\n"; return 0; } //-------------- конец проекта Task2_3 ---------------- //////////////////////////////////////////////////////////