code.cpp 436 B

12345678910111213
  1. /* Create a function that takes two numbers and a
  2. * mathematical operator + - / * and will perform
  3. * a calculation with the given numbers.
  4. */
  5. int calculator(int num1, char op, int num2) {
  6. switch (op){
  7. case '*': return num1 * num2;
  8. // condition ? value_if_true : value_if_false <- ternary statement example
  9. case '/': return (num2==0) ? 0 : num1/num2;
  10. case '+': return num1 + num2;
  11. case '-': return num1 - num2;
  12. }
  13. }