Advertisement
Spocoman

06. Operations Between Numbers

Sep 5th, 2023 (edited)
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. Решение с тернарен оператор:
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int firstNumber, secondNumber;
  9.     cin >> firstNumber >> secondNumber;
  10.  
  11.     char mathOperator;
  12.     cin >> mathOperator;
  13.  
  14.     string evenOrOdd;
  15.  
  16.     double result;
  17.  
  18.     if (mathOperator == '+' || mathOperator == '-' || mathOperator == '*') {
  19.         result =
  20.             mathOperator == '+' ? firstNumber + secondNumber :
  21.             mathOperator == '-' ? firstNumber - secondNumber : firstNumber * secondNumber;
  22.  
  23.         cout << firstNumber << ' ' << mathOperator << ' ' << secondNumber << " = " << result << " - " << (result % 2 == 0 ? "even" : "odd") << endl;
  24.     }
  25.     else {
  26.         if (secondNumber == 0) {
  27.             cout << "Cannot divide " << firstNumber << " by zero" << endl;
  28.         }
  29.         else {
  30.             if (mathOperator == '/') {
  31.                 result = firstNumber * 1.0 / secondNumber;
  32.  
  33.                 cout.setf(ios::fixed);
  34.                 cout.precision(2);
  35.             }
  36.             else {
  37.                 result = firstNumber % secondNumber;
  38.             }
  39.  
  40.             cout << firstNumber << ' ' << mathOperator << ' ' << secondNumber << " = " << result << endl;
  41.         }
  42.     }
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement