Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------------------------------
- # PLIK1 - MathModule.h
- #pragma once
- class MathModule
- {
- public:
- double divide(double a, double b);
- double power(double a, double b);
- double abs(double a);
- bool isDivisible(double a, double b);
- double graphic_function(double a);
- };
- ------------------------------------------------------------------------------------------
- # PLIK 2 - MathModule.cpp
- #include "MathModule.h"
- #include <cmath>
- double MathModule::divide(double a, double b)
- {
- return a / b;
- }
- double MathModule::power(double a, double b)
- {
- return pow(a, b);
- }
- double MathModule::abs(double a)
- {
- return std::abs(a);
- }
- bool MathModule::isDivisible(double a, double b)
- {
- return fmod(a, b) == 0;
- }
- double MathModule::graphic_function(double a)
- {
- if (a < -5) {
- return 0;
- }
- else if (a <= -1 && a >= -2) {
- return abs(a) - 1;
- }
- else if (a > -1 && a <= 0) {
- return a + 1;
- }
- else if (a > 0 && a < 2) {
- return 1;
- }
- else if (a >= 2 && a <= 5) {
- return 2;
- }
- else {
- return 0;
- }
- }
- ------------------------------------------------------------------------------------------
- # PLIK 3 - main.cpp
- #include "MathModule.h"
- #include <iostream>
- #include <array>
- int nTestErrors = 0;
- bool checkError(bool condition, std::string errorMessage)
- {
- if (!condition)
- {
- std::cout << errorMessage << '\n';
- ++nTestErrors;
- }
- return condition;
- }
- int main()
- {
- double a, b;
- int choice;
- std::array<std::string, 5> functions = { "1. Divide", "2. Power", "3. Abs", "4. isDivisible", "5. Graphic function" };
- MathModule mathModule;
- checkError(mathModule.divide(4, 2) == 2, "Wrong division result.");
- checkError(mathModule.power(2, 4) == 16, "Wrong power result.");
- checkError(mathModule.abs(-4) == 4, "Wrong abs result.");
- checkError(mathModule.isDivisible(4, 2), "Wrong isDivisible result.");
- if (nTestErrors > 0) return 0;
- std::cout << "Choose the number of the function:" << "\n" << std::endl;
- for (int i = 0; i < functions.size(); i++)
- {
- std::cout << functions[i] << std::endl;
- }
- std::cout << "\n" << "Choice: ", std::cin >> choice;
- switch (choice)
- {
- case 1:
- std::cout << "Dividend: ", std::cin >> a;
- std::cout << "Divider: ", std::cin >> b;
- std::cout << "Result: " << mathModule.divide(a, b);
- break;
- case 2:
- std::cout << "Base: ", std::cin >> a;
- std::cout << "Power: ", std::cin >> b;
- std::cout << "Result: " << mathModule.power(a, b);
- break;
- case 3:
- std::cout << "Number: ", std::cin >> a;
- std::cout << "Result: " << mathModule.abs(a);
- break;
- case 4:
- std::cout << "Dividend: ", std::cin >> a;
- std::cout << "Divider: ", std::cin >> b;
- std::cout << "Result: " << std::boolalpha << mathModule.isDivisible(a, b);
- break;
- case 5:
- std::cout << "X = ", std::cin >> a;
- std::cout << "Result: Y = " << mathModule.graphic_function(a);
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement