0

Here is my code for calculator class which have 4 function, add,subract,multiply and divide. which take s two input . I want to make a class than can accept more than 2 input and return answer. I am using laravel for making this calculator.

This is my controller method

 public function calculate()
    {
        $oprator;
        $input = Input::get('ans');
        $logs = new Log;
        $logs->user_id = Auth::id();
        $logs->log_data = $input;
        preg_match_all('/\d+/', $input, $matches);
        if (preg_match('/[\/\'^£$%&*()}{@#~?><>,|=_+¬-]/', $input,$oper))
        {
            $oprator = $oper[0];
        }

       $firstnumber = $matches[0][0];
       $secondnumber = $matches[0][1]; 

       $Maths1= new Maths();
        if($oprator == "+"){ 
            $data=array('answer'=> $Maths1->add($firstnumber,$secondnumber));
            $logs->log_answer = $data['answer'];
            $logs->save();
        return view('calculator',compact('data'));
        } 
        if($oprator == "-"){ 
          $data=array('answer'=> $Maths1->subtract($firstnumber,$secondnumber));
          $logs->log_answer = $data['answer'];
            $logs->save();
        return view('calculator',compact('data')); 
        } 
        if($oprator == "/"){ 
          $data=array('answer'=> $Maths1->divide($firstnumber,$secondnumber));
          $logs->log_answer = $data['answer'];
            $logs->save();
        return view('calculator',compact('data')); 
        } 
        if($oprator == "*"){ 
          $data=array('answer'=> $Maths1->multiply($firstnumber,$secondnumber));
          $logs->log_answer = $data['answer'];
            $logs->save();
        return view('calculator',compact('data'));
        }
    }

}

Here is the oop Maths class for calculation

<?php 

namespace App\Custom;     
 class Maths{


         public  function add($number1,$number2) 
          { 
                   $result =$number1 + $number2; 
                    return $result; 
           } 

          public  function subtract($number1,$number2) 
          { 
                   $result =$number1 - $number2; 
                    return $result;
           } 

          public  function divide($number1,$number2) 
          { 
                   $result =$number1 / $number2; 
                    return $result; 
           } 

          public  function multiply($number1,$number2) 
          { 
                   $result =$number1 * $number2; 
                    return $result;
           } 
} 

Here is my view calculator.blade,php

<!-- experiments with table & button-input  vegaseat  2/13/02 -->
<!-- table, border, cellpadding, cellspacing, input, onClick -->
<!-- uses eval() to do the final calculation -->
<!-- note: eval() traps divide by zero error -->
<html>
<head>
    <title>A Simple Four-Banger</title>

</head>
<body>

<form name="calculator" method="post" action="/result">
<!-- form the display to match the keypad -->
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<table border="4" cellpadding="1" bordercolor="#FFFFFF" bgcolor="#73B27B" cellspacing="2" width="222">
<tr>
<td>


<input type="text" size="25" length="25" value="<?php if(isset($data)){
     echo $data['answer'];
}
?>" id="ans" name="ans" style="background:beige;color:black;">
</td>
</tr>
</table>
<!-- form the keypad with buttons in a table -->
<table border="4" cellpadding="2" bordercolor="#FFFFFF" cellspacing="2" width="150" bgcolor="#73B27B">
<tr>
<td align="center">
<input type="button" value="  7  " name="seven" onClick="document.calculator.ans.value+='7'">
</td>
<td align="center">
<input type="button" value="  8  " name="eight" onClick="document.calculator.ans.value+='8'">
</td>
<td align="center">
<input type="button" value="  9  " name="nine" onClick="document.calculator.ans.value+='9'">
</td>
<td align="center">
<input type="button" value="  /  " name="divide" onClick="document.calculator.ans.value+='/'">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="  4  " name="four" onClick="document.calculator.ans.value+='4'">
</td>
<td align="center">
<input type="button" value="  5  " name="five" onClick="document.calculator.ans.value+='5'">
</td>
<td align="center">
<input type="button" value="  6  " name="six" onClick="document.calculator.ans.value+='6'">
</td>
<td align="center">
<input type="button" value="  *  " name="multiply" onClick="document.calculator.ans.value+='*'">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="  1  " name="one" onClick="document.calculator.ans.value+='1'">
</td>
<td align="center">
<input type="button" value="  2  " name="two" onClick="document.calculator.ans.value+='2'">
</td>
<td align="center">
<input type="button" value="  3  " name="three" onClick="document.calculator.ans.value+='3'">
</td>
<td align="center">
<input type="button" value="  -  " name="subtract" onClick="document.calculator.ans.value+='-'">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="  C  " name="clear" onClick="document.calculator.ans.value=''">
</td>
<td align="center">
<input type="button" value="  0  " name="zero" onClick="document.calculator.ans.value+='0'">
</td>
<td align="center">
<input type="submit" value="  =  " name="equal" >
</td>
<td align="center">
<input type="button" value="  +  " name="add" onClick="document.calculator.ans.value+='+'">
</td>
</tr>
<tr>

<a href="/logs">History</a>

</tr>
</table>
</form>
</body>
</html>

I want to make basic calculator with addition, subraction,divide and multiply in laravel

Rehmat Sayany
  • 67
  • 1
  • 2
  • 12

0 Answers0