/////////////////////////////////////////////////////////// // Проект Task2_1 /////////////////////////////////////////////////////////// // Function.h #ifndef FUNCTION_H #define FUNCTION_H #include class Function { public: virtual ~Function() {} virtual const std::string& GetName() const = 0; virtual void Calculate() = 0; protected: double x; // аргумент }; #endif /* FUNCTION_H */ /////////////////////////////////////////////////////////// // Exp.h #include "Function.h" // Класс для представления функции y = e ^ x class Exp : public Function { public: Exp() : name("e ^ x") {} const std::string& GetName() const { return name; } void Calculate(); protected: std::string name; // мат. обозначение функции }; extern Exp f_exp; /////////////////////////////////////////////////////////// // Exp.cpp #include #include #include "Exp.h" using namespace std; void Exp::Calculate() { cout << "Calculation for function y = " << name << endl; cout << "Enter x = "; cin >> x; cin.get(); cout << "y = " << exp(x) << endl; cin.get(); } // Глобальный объект 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 Calculate(); protected: std::string name; // мат. обозначение функции double a; double b; }; extern Line f_line; /////////////////////////////////////////////////////////// // Line.cpp #include #include "Line.h" using namespace std; void Line::Calculate() { cout << "Calculation for function y = " << name << endl; cout << "Enter a = "; cin >> a; cout << "Enter b = "; cin >> b; cout << "Enter x = "; cin >> x; cin.get(); cout << "y = " << (a * x + b) << endl; cin.get(); } // Глобальный объект Line f_line; /////////////////////////////////////////////////////////// // Menu.h #include #include "Function.h" class Menu { public: Menu(std::vector); Function* SelectObject() const; private: int SelectItem(int) const; std::vector pObj; }; /////////////////////////////////////////////////////////// // Menu.cpp #include #include "Menu.h" using namespace std; Menu::Menu(vector _pObj) : pObj(_pObj) { pObj.push_back(0); // для выбора пункта 'Exit' } Function* Menu::SelectObject() const { int nItem = pObj.size(); cout << "======================================\n"; cout << "Select one of the following function:\n"; for (int i = 0; i < nItem; ++i) { cout << i+1 << ". "; // номер пункта меню на единицу // больше, чем индекс массива pObj if (pObj[i]) cout << pObj[i]->GetName() << endl; else cout << "Exit" << endl; } int item = SelectItem(nItem); return pObj[item - 1]; } int Menu::SelectItem(int nItem) const { cout << "--------------------------------------\n"; int item; while (true) { cin >> item; if ((item > 0) && (item <= nItem) && (cin.peek() == '\n')) { cin.get(); break; } else { cout << "Error (must be number from 1 to " << nItem << "):" << endl; cin.clear(); while (cin.get() != '\n') {}; } } return item; } /////////////////////////////////////////////////////////// // Main.cpp #include #include "Function.h" #include "Exp.h" #include "Line.h" #include "Menu.h" using namespace std; Function* pObjs[] = { &f_exp, &f_line }; vector funcList(pObjs, pObjs + sizeof(pObjs) / sizeof(Function*)); int main() { Menu menu(funcList); while (Function* pObj = menu.SelectObject()) pObj->Calculate(); cout << "Bye!\n"; return 0; } //-------------- конец проекта Task2_1 ---------------- //////////////////////////////////////////////////////////