MeehoweCK

Untitled

Mar 11th, 2023
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. // main.cpp
  2. #include <iostream>
  3. #include "calculator.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     string operacje[4];
  10.     operacje[0] = "add";
  11.     operacje[1] = "sub";
  12.     operacje[2] = "mul";
  13.     operacje[3] = "div";
  14.  
  15.     cout << calculate(3.5, 2, operacje, 4);
  16.  
  17.     return 0;
  18. }
  19.  
  20. // calculator.h
  21. #ifndef CALCULATOR_H_INCLUDED
  22. #define CALCULATOR_H_INCLUDED
  23. #include <iostream>
  24.  
  25. using namespace std;
  26.  
  27. float add(float x, float y);
  28. float subtract(float x, float y);
  29. float multiply(float x, float y);
  30. float divide(float x, float y);
  31. float calculate(float x, float y, string operations[], unsigned int size);
  32.  
  33. #endif // CALCULATOR_H_INCLUDED
  34.  
  35. // calculator.cpp
  36. #include "calculator.h"
  37.  
  38. float add(float x, float y)
  39. {
  40.     return x + y;
  41. }
  42.  
  43. float subtract(float x, float y)
  44. {
  45.     return x - y;
  46. }
  47.  
  48. float multiply(float x, float y)
  49. {
  50.     return x * y;
  51. }
  52.  
  53. float divide(float x, float y)
  54. {
  55.     return x / y;
  56. }
  57.  
  58. float calculate(float x, float y, string operations[], unsigned int size)
  59. {
  60.     float wynik = 0;
  61.     for(unsigned int i = 0; i < size; ++i)
  62.     {
  63.         if(operations[i] == "add")
  64.             wynik += add(x, y);
  65.         else if(operations[i] == "sub")
  66.             wynik += subtract(x, y);
  67.         else if(operations[i] == "mul")
  68.             wynik += multiply(x, y);
  69.         else if(operations[i] == "div")
  70.             wynik += divide(x, y);
  71.     }
  72.     return wynik;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment