PHP 프로그래밍

본문 바로가기
사이트 내 전체검색


PHP 프로그래밍
PHP 프로그래밍

10. 계산기 만들기

페이지 정보

작성자 관리자 댓글 0건 조회 287회 작성일 22-09-23 10:47

본문

10. 계산기 만들기

1. 실습1 

파일명 form.php


<?

include "../header.php";

?>

<h1>계산기</h1>

<h3>학번 : 이름 : </h3>


<form method="post" action="result.php">

<table width=600 border=1>

<tr height=40 align=center>

<td width=200>숫자1</td>

<td width=200>연산자</td>

<td width=200>숫자2</td>

</tr>

<tr height=40 align=center>

<td><input type="text" name="num1" ></td>

<td><input type="text" name="opcode" ></td>

<td><input type="text" name="num2" ></td>

</tr>

<tr height=40 align=center>

<td colspan=3><input type="submit" value="확인"></td>

</tr>

</table>

</form>


<?

include "../footer.php";

?>


파일명 : result.php

<?
include "../header.php";
?>
<h1>계산기1</h1>
<h3>학번 : 이름 : </h3>
<?
$num1 = $_POST['num1'];
$opcode = $_POST['opcode'];
$num2 = $_POST['num2'];

if($opcode == "+") {
$result = $num1 + $num2;
} else if($opcode == "-") {
$result = $num1 - $num2;
} else if($opcode == "*") {
$result = $num1 * $num2;
} else if($opcode == "/") {
$result = $num1 / $num2;
} else {
$result = "error";
}

if($reuslt == "error") {
echo "<p>".$result."</p>";
} else {
echo "<p>".$num1 .$opcode. $num2."=".$result."</p>";
}

?>

<?
include "../footer.php";
?>


2. 실습 2

파일명 : form1.php

<?
include "../header.php";

$num1 = $_POST['num1'];
$opcode = $_POST['opcode'];
$num2 = $_POST['num2'];

?>
<h1>계산기</h1>
<h3>학번 : 이름 : </h3>

<form method="post" action="form1.php">
<table width=600 border=1>
<tr height=40 align=center>
<td width=200>숫자1</td>
<td width=200>연산자</td>
<td width=200>숫자2</td>
</tr>
<tr height=40 align=center>
<td><input type="text" name="num1" ></td>
<td><input type="text" name="opcode" ></td>
<td><input type="text" name="num2" ></td>
</tr>
<tr height=40 align=center>
<td colspan=3><input type="submit" value="확인"></td>
</tr>
</table>
</form>
<?
if($opcode != ""){
?>
<table width=600 border=1>
<tr height=50 align=center>
<td>
<?
if($opcode == "+") {
$result = $num1 + $num2;
} else if($opcode == "-") {
$result = $num1 - $num2;
} else if($opcode == "*") {
$result = $num1 * $num2;
} else if($opcode == "/") {
$result = $num1 / $num2;
} else {
$result = "error";
}

if($reuslt == "error") {
echo "<p>".$result."</p>";
} else {
echo "<p>".$num1 .$opcode. $num2."=".$result."</p>";
}
?>
</td>
</tr>
</table>
<?
}
?>
<?
include "../footer.php";
?>

3. 실습 3
파일명 : form3.php

<?
include "../header.php";
?>
<h1>계산기</h1>
<h3>학번 : 이름 : </h3>

<form method="post" action="result3.php" target="result_window">
<table width=600 border=1>
<tr height=40 align=center>
<td width=200>숫자1</td>
<td width=200>연산자</td>
<td width=200>숫자2</td>
</tr>
<tr height=40 align=center>
<td><input type="text" name="num1" ></td>
<td><input type="text" name="opcode" ></td>
<td><input type="text" name="num2" ></td>
</tr>
<tr height=40 align=center>
<td colspan=3><input type="submit" value="확인"></td>
</tr>
</table>
</form>

<table width=600 border=1>
<tr>
<td><iframe name="result_window" src="" width=100% height=100 border=1></iframe></td>
</tr>
</table>

<?
include "../footer.php";
?>


파일명 : result3.php

<?
$num1 = $_POST['num1'];
$opcode = $_POST['opcode'];
$num2 = $_POST['num2'];

if($opcode == "+") {
$result = $num1 + $num2;
} else if($opcode == "-") {
$result = $num1 - $num2;
} else if($opcode == "*") {
$result = $num1 * $num2;
} else if($opcode == "/") {
$result = $num1 / $num2;
} else {
$result = "error";
}

if($reuslt == "error") {
echo "<p>".$result."</p>";
} else {
echo "<p>".$num1 .$opcode. $num2."=".$result."</p>";
}

?>

4. 실습4

파일명 : form2.php

<?
include "../header.php";
?>
<h1>계산기2</h1>
<h3>학번 : 이름 : </h3>
<script>
function calc() {
f = document.form1;
var num1 = parseInt(f.num1.value);
var opcode = f.opcode.value;
var num2 = parseInt(f.num2.value);
//alert(num1);
var result = 0;
if(opcode == "+") result = num1 + num2; 
else if(opcode == "-") result = num1 - num2; 
else if(opcode == "*") result = num1 * num2; 
else if(opcode == "/") result = num1 / num2; 

result_div = document.getElementById("result");
result_div.innerHTML = result;

}
</script>

<form name="form1" method="post" action="">
<table width=600 border=1>
<tr height=40 align=center>
<td width=200>숫자1</td>
<td width=200>연산자</td>
<td width=200>숫자2</td>
</tr>
<tr height=40 align=center>
<td><input type="text" name="num1" ></td>
<td><input type="text" name="opcode" ></td>
<td><input type="text" name="num2" ></td>
</tr>
<tr height=40 align=center>
<td colspan=3><input type="submit" value="확인" onclick="calc();return false"></td>
</tr>
</table>
</form>
<table width=600 border=1>
<tr height=40 align=center>
<td>
<div id="result" class="">
결과
</div>
</td>
</tr>
</table>
<?
include "../footer.php";
?>

댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.