/////////////////////////////////////////////////////////// // Проект Task2_2 /////////////////////////////////////////////////////////// // Function.h #ifndef FUNCTION_H #define FUNCTION_H #include class Function { public: virtual ~Function() {} void SetArg(double arg) { x = arg; } virtual void SetCoeff() = 0; virtual double GetVal() const = 0; virtual const std::string& GetName() const = 0; protected: double x; // аргумент }; #endif /* FUNCTION_H */ /////////////////////////////////////////////////////////// // Exp.h #include #include "Function.h" // Класс для представления функции y = e ^ x class Exp : public Function { public: Exp() : name("e ^ x") {} const std::string& GetName() const { return name; } void SetCoeff() {} double GetVal() const { return exp(x); } private: std::string name; // мат. обозначение функции }; extern Exp f_exp; /////////////////////////////////////////////////////////// // Exp.cpp #include "Exp.h" // Глобальный объект Exp f_exp; /////////////////////////////////////////////////////////// // Line.h #include "Function.h" // Класс для представления функции y = a * x + b class Line : public Function { public: Line() : name("a * x + b") {} const std::string& GetName() const { return name; } void SetCoeff(); double GetVal() const { return (a * x + b); } private: std::string name; // мат. обозначение функции double a; double b; }; extern Line f_line; /////////////////////////////////////////////////////////// // Line.cpp #include #include "Line.h" using namespace std; void Line::SetCoeff() { cout << "Enter a = "; cin >> a; cout << "Enter b = "; cin >> b; } // Глобальный объект Line f_line; /////////////////////////////////////////////////////////// // Action.h #ifndef ACTION_H #define ACTION_H #include "Function.h" class Action { public: virtual ~Action() {} virtual void Operate(Function*) = 0; virtual const std::string& GetName() const = 0; }; #endif /* ACTION_H */ /////////////////////////////////////////////////////////// // Calculation.h #include "Action.h" class Calculation : public Action { public: Calculation() : name("Calculation") {} const std::string& GetName() const { return name; } void Operate(Function*); private: std::string name; // обозначение операции }; extern Calculation calculation; /////////////////////////////////////////////////////////// // Calculation.cpp #include #include "Calculation.h" using namespace std; void Calculation::Operate(Function* pFunc) { cout << "Calculation for function y = "; cout << pFunc->GetName() << endl; pFunc->SetCoeff(); double x; cout << "Enter x = "; cin >> x; cin.get(); pFunc->SetArg(x); cout << "y = " << pFunc->GetVal() << endl; cin.get(); } // Глобальный объект Calculation calculation; /////////////////////////////////////////////////////////// // Tabulation.h #include "Action.h" class Tabulation : public Action { public: Tabulation() : name("Tabulation") {} const std::string& GetName() const { return name; } void Operate(Function*); private: std::string name; // обозначение операции }; extern Tabulation tabulation; /////////////////////////////////////////////////////////// // Tabulation.cpp #include #include #include "Tabulation.h" using namespace std; void Tabulation::Operate(Function* pFunc) { cout << "Tabulation for function y = "; cout << pFunc->GetName() << endl; pFunc->SetCoeff(); double x_beg, x_end, x_step; cout << "Enter x_beg = "; cin >> x_beg; cout << "Enter x_end = "; cin >> x_end; cout << "Enter x_step = "; cin >> x_step; cin.get(); cout << "-----------------------" << endl; cout << " x y" << endl; cout << "-----------------------" << endl; double x = x_beg; while (x <= x_end) { pFunc->SetArg(x); cout << setw(6) << x << setw(14) << pFunc->GetVal() << endl; x += x_step; } cin.get(); } // Глобальный объект Tabulation tabulation; /////////////////////////////////////////////////////////// // AnyAction.h #include "Action.h" class AnyAction : public Action { public: AnyAction() : name("Any action") {} const std::string& GetName() const { return name; } void Operate(Function*); private: std::string name; // обозначение операции }; extern AnyAction any_action; /////////////////////////////////////////////////////////// // AnyAction.cpp #include #include "AnyAction.h" using namespace std; void AnyAction::Operate(Function*) { cout << "Your advertising might be here!" << endl; cin.get(); } // Глобальный объект AnyAction any_action; /////////////////////////////////////////////////////////// // Menu.h #include #include "Function.h" #include "Action.h" class Menu { public: Menu(std::vector, std::vector); Function* SelectObject() const; Action* SelectAction(Function*) const; private: int SelectItem(int) const; std::vector pObj; std::vector pAct; }; /////////////////////////////////////////////////////////// // Menu.cpp #include #include "Menu.h" using namespace std; Menu::Menu(vector _pObj, vector _pAct) : pObj(_pObj), pAct(_pAct) { pObj.push_back(0); // для выбора пункта 'Exit' } Function* Menu::SelectObject() const { // Возьмите код аналогичной функции из проекта Task2_1 } Action* Menu::SelectAction(Function* pObj) const { 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) const { // Возьмите код аналогичной функции из проекта Task2_1 } /////////////////////////////////////////////////////////// // Main.cpp #include #include "Function.h" #include "Exp.h" #include "Line.h" #include "Action.h" #include "Calculation.h" #include "Tabulation.h" #include "AnyAction.h" #include "Menu.h" using namespace std; Function* pObjs[] = { &f_exp, &f_line }; vector funcList(pObjs, pObjs + sizeof(pObjs)/sizeof(Function*)); Action* pActs[] = { &calculation, &tabulation, &any_action }; vector operList(pActs, pActs + sizeof(pActs) / sizeof(Action*)); int main() { Menu menu(funcList, operList); while (Function* pObj = menu.SelectObject()) { Action* pAct = menu.SelectAction(pObj); pAct->Operate(pObj); } cout << "Bye!\n"; return 0; } //-------------- конец проекта Task2_2 ---------------- //////////////////////////////////////////////////////////