0

I have a switch case based calculator and I want to return the square root result of a number between 0 and 9. Every other function are working, just the sqrt one that I don't know how to output.

<?php
class Calculatrice{
public function calcul($entry = "2+3")
{
   return $this->calculate($entry);
}

private function calculate($data){
    $data_array = str_split($data);
    return $this->operation($data_array);
}
public function operation($array){
    switch ($array[1]) {
        case "+":
            return $this->addition($array[0], $array[2]);
            break;
        case "-":
            return $this->soustraction($array[0], $array[2]);
            break;
        case "*":
            return $this->multiplication($array[0], $array[2]);
            break;
        case "/":
            return $this->division($array[0], $array[2]);
            break;
        case "%":
            return $this->modulo($array[0], $array[2]);
            break;
        case "√":
            return $this->racine($array[0]);
            break;
        default:
            break;
    }
}

public function addition($left, $right){
   return (int)$left + (int)$right;
}
public function soustraction($left, $right){
    return (int)$left - (int)$right;
 }
 public function multiplication($left, $right){
    return (int)$left * (int)$right;
 }
 public function division($left, $right){
    return (float)$left / (float)$right;
 }
 public function modulo($left, $right){
    return (int)$left % (int)$right;
 }
 public function racine($left, $right){
    return (int)sqrt($left);
 }
 }
 ?>

I think it's only about displaying the return statement.

zac
  • 1
  • 1
  • 1
    `racine()` is declared to take 2 parameters, you're calling it with only 1 argument. – Barmar Jul 15 '21 at 00:08
  • How are you writing square root as a string? `calculate()` and `operation()` expect the operator to be the second character, so it should be `5√`. – Barmar Jul 15 '21 at 00:11
  • It won't work if you write the normal mathematical form `√5` – Barmar Jul 15 '21 at 00:13
  • not working either – zac Jul 15 '21 at 00:18
  • `str_split()` doesn't work with multibyte characters like `√`. See the linked question for how to split the string properly. – Barmar Jul 15 '21 at 00:34
  • `√` is a multibyte string, so instead of `str_split()` you must use `mb_str_split()`. Also `racine()` receives one parameter and the `$right` should be omitted. – Mohsen Nazari Jul 15 '21 at 00:36

0 Answers0