Archive for the ‘舊102課程’ Category

20
五月

程式設計二程式 05/20: Ex12Class.java

   Posted by: admin Tags: , , ,

import java.io.*;
public class Ex12Class {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileIn = "score.txt";
		String fileOut = "sum.txt";
		try
		{
			//In
			FileReader reader = new FileReader(fileIn);
			BufferedReader sr = new BufferedReader();
			//Out
			FileWriter writer = new FileWriter(fileOut);
			PrintWriter pw = new PrintWriter();
 
			String line = sr.readLine();
			while (line != null && line.length() > 0)
			{
				String[] scores = line.split(" ");
				int s1 = Integer.parseInt(scores[0].trim())
				int s2 = Integer.parseInt(scores[1].trim())
				int s3 = Integer.parseInt(scores[2].trim())
				double avg = 1.0 *(s1 + s2 + s3)/3.0; 
				System.out.printf("%2d %2d %2d %7.3f\n", s1, s2, s3, avg);
				        pw.printf("%2d %2d %2d %7.3f", s1, s2, s3, avg);pw.println();
				line = sr.readLine();
			}
			sr.close();		
			pw.close();
		}
		//catch(FileNotFoundException fe)
		{
			System.out.printf(fe.getMessage())
		}
		//catch(IOException ie)
		{
			System.out.printf(ie.getMessage())
		}
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

20
五月

程式設計二程式 05/14: Ex11Class.java

   Posted by: admin Tags: , , ,

public class Ex11Class {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String idStr =  args[0];
		int num = Integer.parseInt(idStr);
		int len = idStr.length();
		int odds = 0;
		for (int i=0; i< len; i++)
		{
			int remainder = num % 2;
			if (remainder == 1)
				odds++;
			//num = num /10;
			num /= 10;
		}
		System.out.printf("There are %d odds and %d evens in My ID %s", odds, len - odds, idStr);
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

22
四月

程式設計二程式 04/22: ex8.php: 驗證碼

   Posted by: admin Tags: , , ,

session_start();
$captcha_message = "請輸入驗證碼";
if (isset($_POST["captcha"]) && trim(strtolower($_POST['captcha'])) != $_SESSION['captcha']) {
        $captcha_message = "驗證碼錯誤!!";
}
=======
<--img src="captcha/captcha.php" id="captcha" />
<--input type="text" name="captcha" id="captcha-form" autocomplete="off" />
<--?php echo $captcha_message;?>

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

<!--?php
if (isset($_POST["Submit"]))
{
	//讀入表單參數
	$email = $_POST["ST-EMail"];
 	$message = $_POST["ST-Message"];
	$vtime = date("Y-m-d H:i:s");
	//寫入資料庫
	$link = mysql_connect("localhost", "root", "");
	mysql_query("SET NAMES 'UTF8'");
    mysql_select_db("sss");
    $query = "INSERT INTO guestbook(email, message, vtime) VALUES ('$email','$message','$vtime')";
    $result = mysql_query($query);
    mysql_close($link);
}
 
?-->
 
<form action="midterm.php" method="post">
<table border="1" width="100%">
<tbody>
<tr>
<td colspan="2">請問您有何建議</td>
</tr>
<tr>
<td colspan="2">EMail:
<input type="text" name="ST-EMail" /></td>
</tr>
<tr>
<td colspan="2">留言:
<input type="text" name="ST-Message" /></td>
</tr>
<tr>
<td></td>
<td width="20%"> 
<input type="submit" name="Submit" value="送出留言" /></td>
</tr>
</tbody></table>
</form>
<h1>留言內容</h1>
<hr />
<?php
//從資料庫讀出投票資料
	$link = mysql_connect("localhost", "root", "");
	mysql_query("SET NAMES 'UTF8'");
    mysql_select_db("sss");
 
    $query = "SELECT COUNT(*) FROM guestbook";
	$result = mysql_query($query);
	$total_records	 = mysql_fetch_row($result);
 
echo '
 
總留言數:'.$total_records[0].' 
 
';
?>
<hr />
<?php
$query="SELECT * FROM guestbook ORDER BY vtime DESC LIMIT 20";
$result=mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
 
?>
 
";
}
?&gt;
<table border="1" width="100%" id="table1">
<tbody>
<tr>
<td>GID</td>
<td>EMail</td>
<td>Message</td>
<td>Visit Time</td>
</tr>
<!--?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "
<tr-->
<tr>
<td>".$row["gid"]."</td>
<td>".$row["email"]."</td>
<td>".$row['message']."</td>
<td>".$row['vtime']."</td>
</tr>
</tbody></table>
<!--?php
mysql_close($link);
?-->

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

<?php
if (isset($_POST["Submit"]))
{
	//讀入表單參數
	$email = $_POST["ST-EMail"];
	$approval = $_POST["ST-Approval"];
 	$message = $_POST["ST-Message"];
	$vtime = date("Y-m-d H:i:s");
	//寫入資料庫
	$link = mysql_connect("localhost", "root", "");
	mysql_query("SET NAMES 'UTF8'");
    mysql_select_db("stvote");
    $query = "INSERT INTO vote_table(email, approval, message, vtime) VALUES ('$email','$approval', '$message','$vtime')";
    $result = mysql_query($query);
    mysql_close($link);
 
	//重新導向網頁
	header('Location: ex7-list.php');
  	exit;
}
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>喜歡或反對伏冒的投票系統</title>
</head>
 
<body>
<form action="ex7-vote.php" method="post">
<table width="100%" border="1">
  <tr>
    <td colspan="2">請問您對伏冒藥的看法</td>
  </tr>
  <tr>
    <td colspan="2">EMail:<input name="ST-EMail" type="text" /></td>
  </tr>
  <tr>
    <td>&nbsp;<input name="ST-Approval" type="radio" value="yes" />支持反伏冒</td>
    <td>&nbsp;<input name="ST-Approval" type="radio" value="no" />志願喝伏冒</td>
  </tr>
  <tr>
    <td colspan="2">理由:<input name="ST-Message" type="text" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;<input name="Submit" type="submit" value="送出投票單" /></td>
  </tr>
</table>
</form>
</body>
</html>

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark