Advertisement
bAngelov

Operation Between Numbers

Jan 22nd, 2023 (edited)
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <map>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     void operationsBetweenNumbers(); operationsBetweenNumbers();
  11.     return 0;
  12. }
  13.  
  14. void operationsBetweenNumbers(){
  15.     int a; int b; cin >> a >> b;
  16.     string _operator; cin >> _operator;
  17.     bool isZero = b == 0;
  18.     string noZeroDiv = "Cannot divide " + to_string(a) + " by zero";
  19.     string evenOdd[] = {"even","odd"};
  20.     ostringstream div; div << fixed << setprecision(2) << a / (double)b;
  21.     string division = isZero ? noZeroDiv : div.str();
  22.     //Instances of del and deletion variable were renamed to div, division respectively. Due to closer sounding of del to abbreviation of "delenie" in Bulgarian, such a mistake had occured.
  23.     map<string, string>operators = {{"+", to_string(a + b)}, {"-", to_string(a - b)}, {"*", to_string(a * b)}, {"/", division}, {"%", isZero ? noZeroDiv : to_string(a % b)} };
  24.     if (operators[_operator] == noZeroDiv) {cout << noZeroDiv; return;}
  25.     cout << a <<" "<< _operator <<" "<< b << " = " << operators[_operator];
  26.     if (_operator == "+" || _operator == "-" || _operator == "*") cout << " - " << evenOdd[abs(stoi(operators[_operator])) % 2];
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement