Advertisement
Guest User

Lab3

a guest
Nov 12th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------------------------------------------------------------
  2. # PLIK1 - MathModule.h
  3.  
  4. #pragma once
  5.  
  6. class MathModule
  7. {
  8. public:
  9.     double divide(double a, double b);
  10.     double power(double a, double b);
  11.     double abs(double a);
  12.     bool isDivisible(double a, double b);
  13.     double graphic_function(double a);
  14. };
  15.  
  16. ------------------------------------------------------------------------------------------
  17. # PLIK 2 - MathModule.cpp
  18.  
  19. #include "MathModule.h"
  20. #include <cmath>
  21.  
  22. double MathModule::divide(double a, double b)
  23. {
  24.     return a / b;
  25. }
  26.  
  27. double MathModule::power(double a, double b)
  28. {
  29.     return pow(a, b);
  30. }
  31.  
  32. double MathModule::abs(double a)
  33. {
  34.     return std::abs(a);
  35. }
  36.  
  37. bool MathModule::isDivisible(double a, double b)
  38. {
  39.     return fmod(a, b) == 0;
  40. }
  41.  
  42. double MathModule::graphic_function(double a)
  43. {
  44.     if (a < -5) {
  45.         return 0;
  46.     }
  47.     else if (a <= -1 && a >= -2) {
  48.         return abs(a) - 1;
  49.     }
  50.     else if (a > -1 && a <= 0) {
  51.         return a + 1;
  52.     }
  53.     else if (a > 0 && a < 2) {
  54.         return 1;
  55.     }
  56.     else if (a >= 2 && a <= 5) {
  57.         return 2;
  58.     }
  59.     else {
  60.         return 0;
  61.     }
  62. }
  63.  
  64.  
  65. ------------------------------------------------------------------------------------------
  66. # PLIK 3 - main.cpp
  67.  
  68. #include "MathModule.h"
  69. #include <iostream>
  70. #include <array>
  71.  
  72. int nTestErrors = 0;
  73.  
  74. bool checkError(bool condition, std::string errorMessage)
  75. {
  76.     if (!condition)
  77.     {
  78.         std::cout << errorMessage << '\n';
  79.         ++nTestErrors;
  80.     }
  81.     return condition;
  82. }
  83.  
  84. int main()
  85. {
  86.     double a, b;
  87.     int choice;
  88.     std::array<std::string, 5> functions = { "1. Divide", "2. Power", "3. Abs", "4. isDivisible", "5. Graphic function" };
  89.  
  90.     MathModule mathModule;
  91.  
  92.     checkError(mathModule.divide(4, 2) == 2, "Wrong division result.");
  93.     checkError(mathModule.power(2, 4) == 16, "Wrong power result.");
  94.     checkError(mathModule.abs(-4) == 4, "Wrong abs result.");
  95.     checkError(mathModule.isDivisible(4, 2), "Wrong isDivisible result.");
  96.     if (nTestErrors > 0) return 0;
  97.  
  98.     std::cout << "Choose the number of the function:" << "\n" << std::endl;
  99.  
  100.     for (int i = 0; i < functions.size(); i++)
  101.         {
  102.                 std::cout << functions[i] << std::endl;
  103.         }
  104.  
  105.     std::cout << "\n" << "Choice: ", std::cin >> choice;
  106.  
  107.     switch (choice)
  108.         {
  109.     case 1:
  110.         std::cout << "Dividend: ", std::cin >> a;
  111.         std::cout << "Divider: ", std::cin >> b;
  112.         std::cout << "Result: " << mathModule.divide(a, b);
  113.         break;
  114.     case 2:
  115.         std::cout << "Base: ", std::cin >> a;
  116.         std::cout << "Power: ", std::cin >> b;
  117.         std::cout << "Result: " << mathModule.power(a, b);
  118.         break;
  119.     case 3:
  120.         std::cout << "Number: ", std::cin >> a;
  121.         std::cout << "Result: " << mathModule.abs(a);
  122.         break;
  123.     case 4:
  124.         std::cout << "Dividend: ", std::cin >> a;
  125.         std::cout << "Divider: ", std::cin >> b;
  126.         std::cout << "Result: " << std::boolalpha << mathModule.isDivisible(a, b);
  127.         break;
  128.     case 5:
  129.         std::cout << "X = ", std::cin >> a;
  130.         std::cout << "Result: Y = " << mathModule.graphic_function(a);
  131.         break;
  132.         }
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement