카타 8급 Basic Mathematical Operations

1 C[ | ]

int basic_op(char op, int value1, int value2) {
  switch(op) {
    case '+': return value1 + value2;
    case '-': return value1 - value2;
    case '*': return value1 * value2;
    case '/': return value1 / value2;
  }
  return -99999999;
}

2 C++[ | ]

int basicOp(char op, int val1, int val2) {
  switch(op) {
    case '+': return val1 + val2;
    case '-': return val1 - val2;
    case '*': return val1 * val2;
    case '/': return val1 / val2;
  }
  return -99999999;
}

3 Java[ | ]

public class BasicOperations
{
  public static Integer basicMath(String op, int v1, int v2)
  {
    switch(op) {
      case "+": return v1+v2;
      case "-": return v1-v2;
      case "*": return v1*v2;
      case "/": return v1/v2;
    }
    return 0;
  }
}
public class BasicOperations
{
  public static Integer basicMath(String op, int v1, int v2)
  {
  switch (op) {
    case "-": return v1 - v2;
    case "+": return v1 + v2;
    case "*": return v1 * v2;
    case "/": {
      if (v2 == 0) throw new IllegalArgumentException("Division by zero");
      return v1 / v2;
    }
    default:
      throw new IllegalArgumentException("Unknown operation: " + op);
    }
  }
}

4 PHP[ | ]

function basicOp($op, $val1, $val2)
{
  switch($op) {
  case '+': return $val1+$val2;
  case '-': return $val1-$val2;
  case '*': return $val1*$val2;
  case '/': return $val1/$val2;
  }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}