Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <iostream>
- #include "calculator.h"
- using namespace std;
- int main()
- {
- string operacje[4];
- operacje[0] = "add";
- operacje[1] = "sub";
- operacje[2] = "mul";
- operacje[3] = "div";
- cout << calculate(3.5, 2, operacje, 4);
- return 0;
- }
- // calculator.h
- #ifndef CALCULATOR_H_INCLUDED
- #define CALCULATOR_H_INCLUDED
- #include <iostream>
- using namespace std;
- float add(float x, float y);
- float subtract(float x, float y);
- float multiply(float x, float y);
- float divide(float x, float y);
- float calculate(float x, float y, string operations[], unsigned int size);
- #endif // CALCULATOR_H_INCLUDED
- // calculator.cpp
- #include "calculator.h"
- float add(float x, float y)
- {
- return x + y;
- }
- float subtract(float x, float y)
- {
- return x - y;
- }
- float multiply(float x, float y)
- {
- return x * y;
- }
- float divide(float x, float y)
- {
- return x / y;
- }
- float calculate(float x, float y, string operations[], unsigned int size)
- {
- float wynik = 0;
- for(unsigned int i = 0; i < size; ++i)
- {
- if(operations[i] == "add")
- wynik += add(x, y);
- else if(operations[i] == "sub")
- wynik += subtract(x, y);
- else if(operations[i] == "mul")
- wynik += multiply(x, y);
- else if(operations[i] == "div")
- wynik += divide(x, y);
- }
- return wynik;
- }
Advertisement
Add Comment
Please, Sign In to add comment