By GokiSoft.com| 21:12 02/12/2022|
Học Laravel

Route: Giải phương trình bậc 2 (ax2 + bx + c = 0) bằng Laravel - Lập trình laravel BT1694

Giải phương trình bậc 2 (ax2 + bx + c = 0) bằng Laravel - Lập trình laravel

Yêu cầu tạo route như sau

/ptb2/ax2+bx+c => trong đó a, b, c giá trị thay đổi.

Yêu cầu xử lý logic trong controller và hiển thị kết quả trong view.

Liên kết rút gọn:

https://gokisoft.com/1694

Bình luận

avatar
Đỗ Huy Hòa [community,C2110L]
2022-06-08 06:38:51


#ptb2.blade.php


@extends('layouts.app')

@section('content')
    <div class="container">

        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header bg-warning">{{ __('Giải phương trình') }}</div>

                    <div class="card-body">
                        <div class="card">
                            <div class="card-header bg-secondary"> {{ $pt }}</div>
                        </div>
                        <div class="card-body">
                            <div>{{ $result }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection


#HomeController.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
    public function testbt(Request $request, $a, $b)
    {
        $result = '';
        if ($a == 0 && $b == 0) {
            $result = 'Phuong trinh vo sao nghiem';
        } else if ($a == 0) {
            $result =  'Phuong trinh vo nghiem';
        } else {
            $nghiem = -$b / $a;
            $result = 'Phuong trinh co nghiem: x=  ' . round($nghiem, 2);
        }
        return $result;
    }
    public function ptb2(Request $request, $a, $b, $c)
    {
        $pt = 'Phương trình bậc 2: ' . $a . 'x2 + ' . $b . 'x + ' . $c . ' = 0';
        $delta = $b * $b - 4 * $a * $c;
        if ($delta < 0) {
            $result = 'Phuong trinh vo nghiem';
        } else if ($delta == 0) {
            $nghiem = -$b / (2 * $a);
            $result =  'Phuong trinh co nghiem kep:  ' . round($nghiem, 2);;
        } else {
            $nghiem1 = (-$b + sqrt($delta)) / (2 * $a);
            $nghiem2 = (-$b - sqrt($delta)) / (2 * $a);
            $result = 'Phuong trinh co nghiem: Nghiem 1 =  ' . round($nghiem1, 2) . ' va Nghiem 2 = ' . round($nghiem2, 2);
        }
        return view('ptb2', compact('result', 'pt'));
    }
}


#web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HotelController;
use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/helloworld', function () {
    return view('BT1685.test');
});
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/student', function () {
    return view('BT1685.student');
});
Route::get('/ptb1/a/{a}/b/{b}', [App\http\Controllers\HomeController::class, 'testbt']);

// Route::group(['prefix' => '/hotel'], function () {
//     Route::get('/view', [HotelController::class, 'showAll'])->name('showAll');

//     Route::get('/detail', [HotelController::class, 'showDetail'])->name('showDetail');
// });
Route::prefix('hotel')->group(function () {
    Route::get('/view', [HotelController::class, 'showAll'])->name('showAll');

    Route::get('/detail', [HotelController::class, 'showDetail'])->name('showDetail');
});
Route::get('/ptb2/{a}x2+{b}x+{c}', [App\http\Controllers\HomeController::class, 'ptb2']);


avatar
vuong huu phu [T2008A]
2021-06-22 13:47:20



<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
// });
// Route::get('helloworld',function(){
//  return "Hello Phu";
// });
// Route::get('PTB1/{a}/{b}',function($a,$b){
// if ($a == 0) {
//     if ($b == 0) {
//         echo "Vo so nghiem";
//     }elseif ($b!=0) {
//         echo "Vo nghiem";
//     }
// }elseif($a!=0){
//     $x = -$b/$a;
//     echo "X = " .$x;
// }
// });
Route::get('/ptb2/{a}/{b}/{c}',[App\Http\Controllers\PTbac2::class, 'tptbac2']);
Auth::routes();
// Route::get('/student/display', [App\Http\Controllers\Studentcontroller::class, 'showStudent']);


// Auth::routes();
// Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');



<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PTbac2 extends Controller
{
    public function tptbac2($a,$b,$c){
$dt = $b * $b - 4 * $a* $b;
if ($dt > 0) {
    $x1 = (- $b + sqrt ( $dt )) / (2 * $a);
    $x2 = (- $b - sqrt ( $dt )) / (2 * $a); 
    $kq = "X1= ".$x1." X2 = ".$x2;
}elseif ($dt == 0) {
    $x = (- $b / (2 * $a));
    $kq = "PT co nghiem kep la: ".$x;
}else{
    $kq = "PT vo nghiem";
}
         return view('ptbachai', compact('kq')) ;                         
        }
    }




<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<h1>Ket qua cua phuong trinh bac 2 la</h1>
<?php
echo $kq; 
 ?>
</body>
</html>


avatar
Nguyễn Tiến Đạt [T2008A]
2021-06-21 09:39:01



public function PTB2(Request $request,$a,$b,$c)
    {
        if($a == 0){
            if($b == 0){
                if($c == 0) $result= 'PTVSN';
                else $result = 'PTVN';
            } 
            else $result = 'x = '. -$c/$b;
        }
        else{
            $delta = $b*$b - 4*$a*$c;
            if($delta < 0) $result = 'PTVN';
            else if($delta == 0) $result = 'x = '. -$b/(2*$a);
            else{
                $result = 'x1 = '. number_format(( (-$b + sqrt($delta)) / (2*$a) ), 2) . ', ' . 'x2 = ' . number_format(( (-$b - sqrt($delta)) / (2*$a) ), 2);
            }
        } 
        return view('lesson01/ptb2')->with([
            'result' => $result,
            'a' => $a,
            'b' => $b,
            'c' => $c
        ]);
    }


avatar
Luong Dinh Dai [T1907A]
2020-06-29 02:21:23


#web.php


<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */

Route::get('/helloworld ', function () {
	return '<h1><center>Hello Luong Dinh Dai</center><h1>';
});
Route::get('/PTB1/a/{x}/b/{y} ', function ($x, $y) {
	echo $x . 'x+' . $y . '=0' . '<br/>';
	$result = -$y / $x;
	echo "result = " . $result;

})->where(['a' => '[0-9]+', 'b' => '[0-9]+']);

Route::get('/student/display', 'StudentController@showStudent');

Route::get('/', function () {
	//dd('Oki!!!');
	return view('welcome');
});
Route::get('/test', function () {
	return view('test');
});
Route::get('/home', function () {
	return 'wellcome home';
});
Route::get('/product', function () {
	return 'wellcome product';
});
Route::get('/service', function () {
	return 'wellcome service';
});

Route::get('/new/{para}', function ($para) {
	return 'Bai viet so ' . $para;
});
Route::get('/url', 'UrlController@testUrl');

Route::get('/input-user', 'UrlController@showInputUser');

Route::get('/url/{href_param}', 'UrlController@testUrl2');
Route::get('/url/{href_param}', 'UrlController@testUrl2');
Route::get('/{href_param}.html', function ($href_param) {
	return $href_param;
});

Route::get('/{param}/{href_param}.html', function ($param, $href_param) {
	return $param . '<br/>' . $href_param;
});

Route::get('/{param0}/{param1}/{href_param}.{ext}', function ($param0, $param1, $href_param, $ext) {
	return $param0 . ' >> ' . $param1 . ' >> ' . $href_param . ' >> ' . $ext;
});
Route::get('/ptb2/{valueA}x2+{valueB}x+{valueC}', 'CtrPTB2@giaipt');


#CtrPTB2.php


<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CtrPTB2 extends Controller {

	public function giaipt(Request $request) {
		$a = $request->valueA;
		$b = $request->valueB;
		$c = $request->valueC;

		if ($a == 0) {
			if ($b == 0) {
				if ($c == 0) {
					$result = 'pt vo so nghiem';
					return view('ketqua', ['result' => $result]);
				}
				$result = 'pt vo nghiem';
				return view('ketqua', ['result' => $result]);

			}
			$result = 'pt co 1 nghiem x = ' . (-$c / $b);
			return view('ketqua', ['result' => $result]);
		} else {
			$delta = ($b * $b) - (4 * $a * $c);
			if ($delta > 0) {
				$x1 = (-$b + sqrt($delta)) / (2 * $a);
				$x2 = (-$b - sqrt($delta)) / (2 * $a);
				$result = 'x1 = ' . $x1 . '|| x2 = ' . $x2;
				return view('ketqua', ['result' => $result]);
			} else if ($delta == 0) {
				$x1 = (-$b / (2 * $a));
				$result = 'x1 = x2 = ' . $x1;
				return view('ketqua', ['result' => $result]);
			} else {
				$result = 'vo nghiem';
				return view('ketqua', ['result' => $result]);
			}
		}

	}
}

?>


#ketqua.blade.php


<!DOCTYPE html>
<html>
<head>
	<title>Hello world</title>
</head>
<body>
	<h1>Ket Qua PTB2
	</h1>
	<h1>{{ $result}}</h1>
</body>
</html>


avatar
Phí Văn Long [T1907A]
2020-06-25 15:16:58

ptb2.php


<?php 	
		Route::get('/ptb2/{a}x2+{b}x+{c}','Data\pbt2Controller@tinhptb2')
		->where([
			'a'=>'[-0-9]+',
			'b'=>'[-0-9]+',
			'c'=>'[-0-9]+'
		]);
 ?>

pbt2Controller



<?php

namespace App\Http\Controllers\Data;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class pbt2Controller extends Controller
{
    public function tinhptb2(Request $request,$a,$b,$c){
    	$result = '';
    	if ($a == 0 ) {
    		if ($b == 0) {
    			 $result = 'PTVN';
    		}else{
    			 $result = 'PT co 1 nghiem :'.'x = '.(- $c / $b);
    		}
    		return view('pbt2')->with([
    			'result'=>$result,
    			'a'=>$a,
    			'b'=>$b,
    			'c'=>$c
    		]);
    	}
    	//delta
    	$delta = $b * $b - 4  * $a * $c;
    	$x1 = "";
    	$x2 = "";
    	//xet nghiem phuong trinh
    	if ($delta > 0) {
    		$x1 = (- $b + sqrt ( $delta )) / (2 * $a);
            $x2 = (- $b - sqrt ( $delta )) / (2 * $a);
            $result="Phương trình có 2 nghiệm là: " . "x1 = " . $x1 . " và x2 = " . $x2;
    	}elseif ($delta == 0) {
    		$x1 = (- $b / (2 * $a));
            $result = "Phương trình có nghiệm kép: x1 = x2 = " . $x1;
    	}else{
    		 $result="Phương trình vô nghiệm!";
    	}
    	return view('pbt2')->with([
    		'result' =>$result,
    		'a'=>$a,
    		'b'=>$b,
    		'c'=>$c
    	]);

    }
}

pbt2.blade.php



<!DOCTYPE html>
<html>
<head>
	<title>{{$a}}x2+{{$b}}x+{{$c}} = {{$result}}</title>
</head>
<body>
	<h1 style="color: red">Ket qua : {{$result}}</h1>
</body>
</html>


avatar
thienphu [T1907A]
2020-06-25 02:48:01


#Controller_ptb2.php


<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Controller_ptb2 extends Controller {

	public function giaipt(Request $request) {
		$a = $request->a;
		$b = $request->b;
		$c = $request->c;

		if ($a == 0) {
			if ($b == 0) {
				if ($c == 0) {
					$result = 'pt vo so nghiem';
					return view('showPTB2', ['result' => $result]);
				}
				$result = 'pt vo nghiem';
				return view('showPTB2', ['result' => $result]);

			}
			$result = 'pt co 1 nghiem x = ' . (-$c / $b);
			return view('showPTB2', ['result' => $result]);
		} else {
			$delta = ($b * $b) - (4 * $a * $c);
			if ($delta > 0) {
				$x1 = (-$b + sqrt($delta)) / (2 * $a);
				$x2 = (-$b - sqrt($delta)) / (2 * $a);
				$result = 'x1 = ' . $x1 . '|| x2 = ' . $x2;
				return view('showPTB2', ['result' => $result]);
			} else if ($delta == 0) {
				$x1 = (-$b / (2 * $a));
				$result = 'x1 = x2 = ' . $x1;
				return view('showPTB2', ['result' => $result]);
			} else {
				$result = 'vo nghiem';
				return view('showPTB2', ['result' => $result]);
			}
		}

	}
}

?>


#showPTB2.blade.php


<!DOCTYPE html>
<html>
<head>
	<title>Hello world</title>
</head>
<body>
	<h1>Hello Smileahjhj</h1>
	<h1>{{ $result}}</h1>
</body>
</html>



<?php

use Illuminate\Support\Facades\Route;

Route::get('/ptb2/{a}x2+{b}x+{c}', 'Controller_ptb2@giaipt')->where(
	'a'=>'[0-9]+',
	'b'=>'[0-9]+',
	'c'=>'[0-9]+'
);


avatar
Đỗ Văn Huấn [T1907A]
2020-06-24 16:28:14

view


<!DOCTYPE html>
<html>
<head>
	<title>Registation Form * Form Tutorial</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
      
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
			<h4 style="color:red;">Ket qua: {{$ketqua}}</h4>
			</div>
			<div class="panel-body">
				<form method="post" action="{{route('insertData')}}">
                {{ csrf_field() }}
					<div class="form-group">
					  <label for="a">Enter a:</label>
					  <input required="true" type="number" class="form-control" id="a" name="a">
					</div>
					<div class="form-group">
					  <label for="b">Enter b:</label>
					  <input required="true" type="number" class="form-control" id="b" name="b">
					</div>
					<div class="form-group">
					  <label for="c">Enter c:</label>
					  <input required="true" type="number" class="form-control" id="c" name="c">
					</div>
					<button class="btn btn-success">Giai phuong trinh</button>
				</form>		
			</div>
		</div>
	</div>
</body>
</html>

controller




    public function inputpPtb2(Request $request)
    {
        return view('ptb2');
    }

    public function ptb2(Request $request)
    {
        $a = $request->a;
        $b = $request->b;
        $c = $request->c;
        $x1 = $x2 = '';
        $ketqua = '';
        if ($a == 0) {
            if ($b == 0) {
                if ($c == 0) {
                    $ketqua = 'pt vo so nghiem';
                    return view('ptb2', ['ketqua' => $ketqua]);
                }
                $ketqua = 'pt vo nghiem';
                return view('ptb2', ['ketqua' => $ketqua]);
            }
            $ketqua = 'pt co 1 nghiem x= ' . (-$c / $b);
            return view('ptb2', ['ketqua' => $ketqua]);
        } else {
            $delta = ($b * $b) - (4 * $a * $c);
            if ($delta > 0) {
                $x1 = (-$b + sqrt($delta)) / (2 * $a);
                $x2 = (-$b - sqrt($delta)) / (2 * $a);
                $ketqua = 'x1 = '.$x1.', x2 = '.$x2;
                return view('ptb2', ['ketqua' => $ketqua]);
            } else if ($delta == 0) {
                $x1 = (-$b / (2 * $a));
                $ketqua = 'x1 = x2 = '.$x1;
                return view('ptb2', ['ketqua' => $ketqua]);
            } else {
                $ketqua = 'pt vo nghiem';
                return view('ptb2', ['ketqua' => $ketqua]);
            }
        }
    }
}
route




Route::group(['prefix'=>'/ptb2'],function(){
    Route::post('/inputData','Manage@inputpPtb2')->name('Inputdata');

    Route::post('/insertData','Manage@ptb2')-> name('insertData');
    
});


avatar
Trương Công Vinh [T1907A]
2020-06-24 15:58:24

web.php


<?php

use Illuminate\Support\Facades\Route;
Route::get('/ptb2/{a}x^2+{b}x+{c}','ptb2@giaiPTB2')
->where([
    'a'=>'[-0-9]+',
    'b'=>'[-0-9]+',
    'c'=>'[-0-9]+'
    ]);
controller ptb2.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ptb2 extends Controller
{
    //
    function giaiPTB2( Request $request, $a, $b, $c) {
        $result = '';
        if ($a == "")
            $a = 0;
        if ($b == "")
            $b = 0;
        if ($c == "")
            $c = 0;
            $result = "Phương trình: " . $a . "x2 + " . $b . "x + " . $c . " = 0";
            return view('ptb2')->with([
                'result'=> $result,
                'a'=>$a,
                'b'=>$b,
                'c'=>$c
            ]);
        if ($a == 0) {
            if ($b == 0) {
                $result = "Phương trình vô nghiệm!";
            } else {
                $result="Phương trình có một nghiệm: " . "x = " . (- $c / $b);
            }
            return view('ptb2')->with([
                'result'=> $result,
                'a'=>$a,
                'b'=>$b,
                'c'=>$c
            ]);
        }
        // tính delta
        $delta = $b * $b - 4 * $a * $c;
        $x1 = "";
        $x2 = "";
        // tính nghiệm
        if ($delta > 0) {
            $x1 = (- $b + sqrt ( $delta )) / (2 * $a);
            $x2 = (- $b - sqrt ( $delta )) / (2 * $a);
            $result="Phương trình có 2 nghiệm là: " . "x1 = " . $x1 . " và x2 = " . $x2;
        } else if ($delta == 0) {
            $x1 = (- $b / (2 * $a));
            $result = "Phương trình có nghiệm kép: x1 = x2 = " . $x1;
        } else {
            $result="Phương trình vô nghiệm!";
        }
        return view('ptb2')->with([
            'result'=> $result,
            'a'=>$a,
            'b'=>$b,
            'c'=>$c
        ]);
    }
}

views ptb2.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
     <h1><center>PT : {{$a}}x^2+{{$b}}x+{{$c}}=0 </center></h1>
<br>       <h2>=>  {{$result}}</h2>
    
</body>
</html>