關於我自己

我的相片
Welcome to discuss about : Chinese Traditional Medicine and Acupuncture Please send me the email: tccnchsu@gmail.com Chih-Yu Hsu

最新消息

總網頁瀏覽量

2008年9月27日 星期六

繳教作業的方法

繳交作業的方法
1.先將您的作業變成goole文件
2.再用分享方式將超連結放在回應中
(並註明學校、學號與暱稱)

朝陽課程-數值分析

ED304330 - General Variable Skill, Computer Programming and Mathematics.
http://eric.ed.gov/ERICWebPortal/custom/portlets/recordDetails/detailmini.jsp?_nfpb=true&_&ERICExtSearch_SearchValue_0=ED304330&ERICExtSearch_SearchType_0=no&accno=ED304330
relation mathematics computer programming
http://www.google.com.tw/search?hl=zh-TW&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=relation+mathematics+computer+programming&spell=1
-------------------------------------------------------------
如何將數值分析這門課
讓數學基礎不穩固的研究生能夠聽的懂?
這是很重要的課題

先從兩個未知數的聯立方程組的產生說起
以父子問題和雞兔同籠問題來了解出題的思維
再以"加減消去法"及"代入法"來解父子問題和雞兔同籠問題
ax+by=c
dx+ey=f

x=(ce-bf)/(ae-bd);y=(af-dc)/(ae-bd) (公式)

設計Matab程式,利用公式法來求解兩個未知數的聯立方程組:
x+y=2
x-y=0
的答案 。

%利用公式法來求解兩個未知數的聯立方程組
%x+y=2
%x-y=0
%的答案
%----------------------------------------------------------------------------

% Input
a=1;
b=1;
c=2;
d=1;
e=-1;
f=0;
% Process
x=(c*e-b*f)/(a*e-b*d);
y=(a*f-d*c)/(a*e-b*d) ;
% Output
x
y
%----------------------------------------------------------------------------

作業
利用高斯消去法求三個未知數的聯立方程組的解法
並設計設計Matab程式。
已交作業名單
9630614
9627636
9730619
9730601
9730626
9730608
9727622
9730620
期中報告
歐龍謙
王朝棨
高富斌


參考網站
http://www.dyu.edu.tw/~lhuang/class/numerical/
roundoff error
http://www.google.com.tw/search?complete=1&hl=zh-TW&q=roundoff+error+%E4%BD%8D%E6%95%B8%E5%B0%87%E8%A2%AB%E6%8D%A8%E6%A3%84&meta=&aq=f&oq=
truncation error
http://www.google.com.tw/search?hl=zh-TW&q=truncation+error&meta=&aq=1&oq=truncation+
-------------------------------------------------------------------------------------
y=f(x)
y是x的函數
例 y=sin(x)
Matlab程式設計
-------------------------------------------------------------------------------------
angles=[0:0.01*pi:2*pi]
y=sin(angles)
plot(angles,y)
-------------------------------------------------------------------------------------

2008年9月18日 星期四

逢甲課程-物件導向設計-VC++(#)

971-物件導向設計-VC++(#)OBJECT-ORIENTED DESIGN(應數三合)[選課代號:3013]
-----------------------------------------------------------------------------

第十六週 2008/12/19 網路程式
-----------------------------------------------------------------------------
Wireshark 教學
http://blog.shaolin.tw/2008/03/wireshark.html
防毒軟體失效,您要靠誰? 木馬後門入侵,誰能救您? 網路通訊的關鍵,看封包! 網路攻擊的關鍵,看封包! 病毒蠕蟲感染分析.


----------------------------------------------------------------------------------
網路通訊是靠"封包"廣播的方式,因此才可以看見自己和別人的封包!

安裝Wireshark:

官方網頁來下載安裝程式,位置是http://www.wireshark.org/


操作步驟
步驟1: Capture → Interfaces 選擇一個 start

步驟2: Capture → stop

步驟3:Expression,找到TELNET字樣如下,直接按OK。
步驟4:Apply
步驟5:Capture → start


實例測試

步驟1:測試
開始->執行cmd
在命令提示字元視窗

1. ipconfig 本機之IP
2. 連線 telnet bbs.rumor.tw
3. Wireshark 看封包, bbs.rumor.tw 之IP 為 140.136.148.18
Source(來源) 發送封包, Destination (目的地) 接收封包
4.輸入 guest
5.Wireshark 看封包, telnet 可看到 g-u-e-s-t
6. Follow TCP Stream

練習:
觀察HTTP Protocol的封包
目的:藉由HTTP Protocol的觀察,練習篩選所要的資訊,並能夠清楚的知道TCP/IP實際的運作模式


-------------------------------------------------------------------------------
參考資料


Instructor Resources Computer Networking with Internet Protocols and Technology
http://williamstallings.com/CNIP/CNIP1e-inst.html

Ethereal 抓網路封包+封包內容分析+看明碼連線教學內容

http://chris701203.pixnet.net/blog/post/21891886
http://www.google.com.tw/search?hl=zh-TW&q=%E7%9C%8B%E5%B0%81%E5%8C%85++%E5%BE%8C%E9%96%80&meta=&aq=f&oq=


-----------------------------------------------------------------------------
匯入 vb 之winsock 元件
網路程式設計CH7
http://www.csie.thit.edu.tw/ELearn/Chapter07A.ppt
http://www.csie.thit.edu.tw/ELearn/Chapter03A.ppt

Download a Copy of the MSCOM32.OCX
http://www.yes-tele.com/mscomm.html
下载mswinsck.ocx与VB6Controls.reg:
http://zzzevazzz.bokee.com/inc/vb6controls.rar
參考
http://hi.baidu.com/zzzevazzz/blog/item/8790a501de37ced7267fb52e.html



1. 將mswinsck.ocx放入C:\WINDOWS\system32 之中

2.
開始->執行
regsvr32 mswinsck.ocx

Someone else says this worked install the license for MSCOMM32.OCX with VB.NET
I executed VB6Controls.reg from the VB.Net installation CD and registered the .ocx and it worked.

Full Download Results For "VB6Controls.reg"


http://www.yes-tele.com/mscomm.html

http://support.microsoft.com/kb/318597/zh-tw

3.
開始->執行
regedit.exe
檔案-匯入
選 vb6controls
4. 執行microsoft visual studio 2003
建立專案
其他語言 Visual Basic
windows 應用程式
確定
5. 工具箱-資料-右鍵-選擇項目-COM元件
Microsoft WinSock Control.version 6.0
打勾,確定








------------------------------------------------------------------------------
第十五週 2008/12/12 網路程式設計


------------------------------------------------------------------------------
1.問 http://tw.yahoo.com/ 的主機 IP為何?
using System.Net;
-------
IPAddress[]myaddress = Dns.GetHostAddresses("tw.yahoo.com");
Console.WriteLine(myaddress[0]);
-------------------------------------------------------------------------------
2.問 IP 119.160.246.241 的主機名稱為何?
IPHostEntry hostaddress;
hostaddress = Dns.GetHostEntry("119.160.246.241");
Console.WriteLine(hostaddress.HostName);
-------------------------------------------------------------------------------
3.
How to read XML from a file by using Visual C# .NET
http://programsolution.blogspot.com/2008/01/how-to-read-xml-from-file-by-using.html

XML Programming in C# and .NET
http://www.c-sharpcorner.com/Articles/ArticleListing.aspx?SectionID=1&SubSectionID=79
---------------------------------------
使用 Visual C# .NET 從 URL 讀取 XML 資料
http://support.microsoft.com/kb/307643
-------------
using System.Xml;
---
String URLString = "http://www.galaxquery.com/demo/docs/book1.xml";
XmlTextReader reader = new XmlTextReader(URLString);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;

case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;

case XmlNodeType.EndElement: //Display the end of the element.
Console.Write(" Console.WriteLine(">");
break;
}
}

-------------------------------------------------------------------------------------
-----------------------------------------------------------------------------




------------------------------------------------------------------------------
第十四週 2008/12/12 集合(Queue,Stack)


Queue 類別(FIFO 先進先出 First In First Out)
------------------------------------------------------------------------------
Enqueue()方法加入元素到佇列最尾端,Dequeue 方法從佇列最前端拿出元素


using System.Collections;

object x;
Queue a = new Queue();
Console.WriteLine("長度" + a.Count);
a.Enqueue("1");
Console.WriteLine("加入1後長度" + a.Count);
a.Enqueue("2");
Console.WriteLine("加入2後長度"+a.Count);
x=a.Dequeue();
Console.WriteLine("取出資料" + x);
Console.WriteLine("長度" + a.Count);
x=a.Dequeue();
Console.WriteLine("取出資料" + x);
Console.WriteLine("長度" + a.Count);


------------------------------------------------------------------------------
peek() 方法永遠只看最前端,並不會將它取出

object x;
Queue a = new Queue();
Console.WriteLine("長度" + a.Count);
a.Enqueue("1");
Console.WriteLine("加入1後長度" + a.Count);
a.Enqueue("2");
Console.WriteLine("加入2後長度"+a.Count);
x=a.Peek();
Console.WriteLine("取出資料" + x);
Console.WriteLine("長度" + a.Count);
x=a.Peek();
Console.WriteLine("取出資料" + x);
Console.WriteLine("長度" + a.Count);


------------------------------------------------------------------------------
ToArray()方法 將佇列轉換成陣列

object[] x;
Queue a = new Queue();
Console.WriteLine("長度" + a.Count);
a.Enqueue("1");
Console.WriteLine("加入1後長度" + a.Count);
a.Enqueue("2");
Console.WriteLine("加入2後長度"+a.Count);
x=a.ToArray();
Console.WriteLine("取出資料" + x[0]);
Console.WriteLine("長度" + a.Count);
Console.WriteLine("取出資料" + x[1]);
Console.WriteLine("長度" + a.Count);
------------------------------------------------------------------------------
課堂練習:
設計程式模擬
一台網路型印表機(標籤),有兩台電腦(兩個按鈕)透過網路送出列印文件,顯示等待列印的數目.
設計(一個按鈕)可以印完一個文件並將它除去.
在類別下宣告
Queue a=new Queue(32);
object[] x;
int b = 0;
------------------------
按鈕程式
string s="";
b = b + 1;
a.Enqueue(b);
x=a.ToArray();
for (int i = 0; i < a.Count; i++)
{
s = s + x[i];
label1.Text = "" + s;
}



------------------------------------------------------------------------------
Stack 類別(LIFO Last In First Out 後進先出)

object[] x;
Stack a = new Stack();
Console.WriteLine("長度" + a.Count);
a.Push("1");
Console.WriteLine("加入1後長度" + a.Count);
a.Push("2");
Console.WriteLine("加入2後長度"+a.Count);
Console.WriteLine("長度" + a.Count);
Console.WriteLine("取出資料" + a.Pop());
Console.WriteLine("長度" + a.Count);
Console.WriteLine("取出資料" + a.Pop());
Console.WriteLine("長度" + a.Count);

------------------------------------------------------------------------------
課堂練習:
設計程式模擬
漢諾塔問題
http://www.google.com.tw/search?hl=zh-TW&q=%E8%8B%B1%E6%96%87+%E6%BC%A2%E8%AB%BE%E5%A1%94%E5%95%8F%E9%A1%8C&meta=&aq=f&oq=
http://www.google.com.tw/search?hl=zh-TW&q=Hanoi+tower&btnG=%E6%90%9C%E5%B0%8B&meta=&aq=f&oq=




http://www.google.com.tw/images?q=tbn:3ssGiIp86l4J::mmdays.files.wordpress.com/2007/05/tower_of_hanoi.jpg

1.實作三個stack a,b,c
2.第一個按鈕 "開始",將a 放入 3,2,1, 並在標籤labale中顯現(利用方法toArray產生陣列再用for結合陣例放入字串),將b,c stack中的內容清掉.
3. 另七個按鈕
按鈕"第一步": (a之1->b)將a 的最上元素pop出,再push到b,即b.push(a.pop())
按鈕"第二步": (a之2->c) 將a 的最上元素pop出,再push到c,即c.push(a.pop())
按鈕"第三步": (b之1->c) 將b 的最上元素pop出,再push到c,即c.push(b.pop())
按鈕"第四步": (a之3->b) 將a 的最上元素pop出,再push到b,即b.push(a.pop())
按鈕"第五步": (c之1->a) 將c 的最上元素pop出,再push到a,即a.push(c.pop())
按鈕"第六步": (c之2->b) 將c 的最上元素pop出,再push到b,即b.push(c.pop())
按鈕"第七步": (a之1->b) 將a 的最上元素pop出,再push到b,即b.push(a.pop())



------------------------------------------------------------------------------

第十三週 2008/12/05 集合(ArrayList)




------------------------------------------------------------------------------
複習
範例 10-3a


int c,i;
int[] a ={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
string s="";


Random r = new Random();
for (i = 0; i <= 3;i++ )
{
c = r.Next(10-i) + i;
s = s + a[c];
label2.Text = s;
a[c] = a[i];%挑過的數用前面的數覆蓋,因為前面的數還未被挑到,被挑過的不能再挑,因此覆蓋掉
}
----------------------------------------

------------------------------------------------------------------------------
1. 增加命名空間
using System.Collections;
2. 實作ArrayList類別之物件b
ArrayList a = new ArrayList();
3. 印出物件b所放資料的容量
Console.WriteLine(b.Capacity);
4. 加入數字5和2到b中
b.Add(5);
b.Add(2);
5. 印出物件b能放資料的容量,是否為2
Console.WriteLine(b.Capacity);
ans:4 加倍

課堂練習:建構子給定值0,2,3
ArrayList a = new ArrayList(0);

7. 印出物件b已放資料的容量,是否為2
Console.WriteLine(b.Count);
ans: Yes
8. 把b的元素取出
Console.WriteLine(b[1]);
Console.WriteLine(b[2]);
--------------------------
Console.WriteLine(b[0]);
Console.WriteLine(b[1]);
正確
-----------------------------------------------------------------------------
ArrayList b = new ArrayList(3);
Console.WriteLine(b.Capacity);
b.Add(5);
b.Add(2);
Console.WriteLine(b.Capacity);
Console.WriteLine(b.Count);
Console.WriteLine(b[0]);
Console.WriteLine(b[1]);
------------------------------------------------------------------------------
9. 排序方法 sort (複習 6-9~ 6-15)
ArrayList b = new ArrayList(9);
Console.WriteLine(b.Capacity);
b.Add(5);
b.Add(2);
b.Add(3);
b.Add(9);
b.Add(6);
b.Add(4);
Console.WriteLine(b.Capacity);
Console.WriteLine(b.Count);
Console.WriteLine("排序前");
Console.WriteLine(b[0]);
Console.WriteLine(b[1]);
Console.WriteLine(b[2]);
Console.WriteLine(b[3]);
Console.WriteLine(b[4]);
Console.WriteLine(b[5]);
b.Sort();
Console.WriteLine("排序後");
for (int i=0;i<=5;i++)
{
Console.WriteLine(b[i]);
}
----------------------------------------------------------------------------------
10. 清除ArrayList 的所有內容
Console.WriteLine("清除前");
Console.WriteLine(b.Count);
b.Clear();
Console.WriteLine("清除後");
Console.WriteLine(b.Count);
-----------------------------------------------------------------------------
搜尋給訂數字的編號
int n=b.BinarySearch(4);
Console.WriteLine(n);

Console.WriteLine(b.IndexOf(4));
------------------------------------------------------------------------------
插入:在給定的號插入數值
b.Insert(2, 7);
Console.WriteLine("插入後");

for (int i = 0; i <= 5; i++)
{
Console.WriteLine(b[i]);
}

-------------------------------------------------------------------------------
從0~9 中挑四個數字,但不可重複
另一種寫法
int c;
string s="";
ArrayList b = new ArrayList(9);
for (int i = 0; i <= 9; i++)
{
b.Add(i);
Console.WriteLine(b[i]);
}

Random r = new Random();
for (int i=0;i<=3;i++)
{
c = r.Next(b.Count);
s = s + b[c];
Console.WriteLine("移除前"+b.Count);
b.RemoveAt(c);
Console.WriteLine( "移除後"+ b.Count);
Console.WriteLine( s);
}


------------------------------------------------------------------------------
課後練習
利用ArrayList製作猜數字遊戲


------------------------------------------------------------------------------
第十二週 2008/11/28 字串
------------------------------------------------------------------------------
String 字串類別
windows application
------------------------------------------------------------------------------
1. 在表單內寫程式(滑鼠在表單上按左鍵兩下)

2. 在表單上放一個textbox,並檢視屬性視窗中name的屬性值
放一個標籤修改name的屬性值為lname,修改text屬性值為"長度"
放一個標籤修改name的屬性值為llength,修改text屬性值為" ",並修改borderstyle


3. 程式
1. 宣告 並實作str1物件
char[] a ={ 'a', 'b', 'c' };
String strlen;
int length;
String str1 = new string(a);

2. 在文字方塊放入字串str
textBox1=str;


char[] a ={ 'a', 'b', 'c' };
String strlen;
int length;
String str1 = new string(a);
textBox1.Text = str1;
4. llength.Text = strlen;

char[] a ={ 'a', 'b', 'c' };
String strlen;
String str1 = new string(a);
textBox1.Text = str1;
string length ="長度"+str1.Length;
strlen = length;
llength.Text = strlen;

------------------------------------------------------------------------------
範例:猜數字遊戲(四位數阿拉伯數字不可重複)
1. 使用人出題,電腦猜數字
2. 電腦出題,使用人猜數字

按鈕的程式
private void button1_Click(object sender, EventArgs e)
{
Ngame a = new Ngame();
llength.Text =""+a.no1+a.no2+a.no3+a.no4;
}

----------------
發牌類別
public class Ngame
{
public int no1,no2,no3,no4;
public Ngame()
{
Random s = new Random();
no1 = s.Next(1, 5);
no2 = s.Next(1, 5);
no3 = s.Next(1, 5);
no4 = s.Next(1, 5);

}

}


課堂練習:
猜撲克牌遊戲
1. 使用人出題(花色和數字),電腦猜(花色和數字)
2. 電腦出題(花色和數字),使用人猜(花色和數字)
-----------------------------------------------------------------------------
第十一週 2008/11/21 數值
------------------------------------------------------------------------------
複習_類別


在主類別MyMain中main方法之中實作產生的rect物件的屬性,預設值為0.
class Mymain
{

static void Main(string[] args)
{
CRectangle rect = new CRectangle();
//實作名稱為rect的物件(OBJECT)具有CRectangle 類別的屬性width和height及方法compute_area()
Console.WriteLine("rect.width" + rect.width);
Console.WriteLine("rect.height" + rect.height);
}
}
public class CRectangle
{
public int width, height;
public int compute_area()
{
int area;
area = width * height;
return area;
}

}
-----------------------------------------------------------------------------
rect屬性質更改後產生永久改變,
但是新誕生新的物件rect1其width與height也是預設之值0.


static void Main(string[] args)
{
CRectangle rect = new CRectangle();
Console.WriteLine("rect.width" + rect.width);
Console.WriteLine("rect.height" + rect.height);
rect.width = 99;
rect.height = 100;
Console.WriteLine("rect.width" + rect.width);
Console.WriteLine("rect.height" + rect.height);
CRectangle rect1 = new CRectangle();
Console.WriteLine("rect1.width" + rect1.width);
Console.WriteLine("rect1.height" + rect1.height);
}
----------------------------------------------------------------------------
利用建構子(constructor)來解決預設值不希望是0的情況
public CRectangle()
{
width = 99;
height = 100;
}
-----------------------------------------------------------------------------
仿方形類別設計圓形類別,分別計算半徑為5,10,15的三個圓形面積與周長
1. 改類別名稱CCricle
public class CCricle
{
public int width, height;
public int compute_area()
{
int area;
area = width * height;
return area;
}

}
2.將屬性改成只有半徑
public class CCricle
{
public int radius;
public int compute_area()
{
int area;
area = width * height;
return area;
}

}

3.修改方法compute_area()的內容,圓面積公式


int area;
area = radius * radius*ˇ;
return area;

4. 主程式實作產生物件 circ
CCircle circ = new CCircle;


5.簡單的發牌程式
Random r = new Random();
int no=r.Next(1,14);
int color = r.Next(1, 5);
switch (color)
{
case 1:
Console.WriteLine("黑桃");
break;
case 2:
Console.WriteLine("紅心");
break;
case 3:
Console.WriteLine("方塊");
break;
case 4:
Console.WriteLine("梅花");
break;
}

Console.WriteLine(no);

---------------------------------------------------------------------------





-----------------------------------------------------------------------------
第十週 2008/11/14
類別(Class)

what:幾何形狀可以分為許多類,例如圓形,...,三角形,矩形.
圓形和矩形是很重要的類別(類別 Class)
幾何形狀中的矩形類別有四個直角,其大小由長(width)與寬(height)做決定.
矩形的長與寬的值給定以後,我們叫這個具有特定長與寬的矩形為一個實體物件(東西),是由(抽象)矩形類別實作而成的東西(物件).
同理幾何形狀中圓形類別是由到一個點(圓心)相等距離(半徑)之所有點所乘的集合.給定特殊半徑值的圓形物件(東西)),是由(抽象)圓形類別實作而成的東西(物件).

why:寫程式為何要有類別的觀念?

以簡馭繁

為自己的類別取名
例:
-----------------------------------------------------------------------------
class Mymain
{
static void Main(string[] args)
{

}
}

-----------------------------------------------------------------------------
宣告變數
class Mymain
{
static void Main(string[] args)
{
int width, height, area;

}
}
-----------------------------------------------------------------------------
給定舉行之width與height定起始值並計算矩形面積(area)
class Mymain
{
static void Main(string[] args)
{
int width, height, area;
width = 10;
height = 5;
area = width * height;
Console.WriteLine("area="+area);

}
}
-----------------------------------------------------------------------------
如果要計算三個不同長寬的矩形面積,則程式要複製三份,使程式看起來繁瑣,又佔篇幅
class Mymain
{
static void Main(string[] args)
{
int width, height, area;
width = 10;
height = 5;
area = width * height;
Console.WriteLine("area="+area);
width = 20;
height = 10;
area = width * height;
Console.WriteLine("area=" + area);
width = 30;
height = 15;
area = width * height;
Console.WriteLine("area=" + area);
}
-----------------------------------------------------------------------------
建立矩形類別
class Mymain
{
static void Main(string[] args)
{
int width, height, area;
width = 10;
height = 5;
area = width * height;
Console.WriteLine("area="+area);
width = 20;
height = 10;
area = width * height;
Console.WriteLine("area=" + area);
width = 30;
height = 15;
area = width * height;
Console.WriteLine("area=" + area);
}
}
class CRectangle
{
int width, height;
int compute_area()
{
int area;
area=width*height;
return area;
}

}
-----------------------------------------------------------------------------
在主類別中新生()一個給定名稱(rect)的矩形物件
class Mymain
{
static void Main(string[] args)
{
CRectangle rect = new CRectangle();
}
}
class CRectangle
{
int width, height;
int compute_area()
{
int area;
area=width*height;
return area;
}

}
-----------------------------------------------------------------------------
給定rect矩形物件之特定的長寬並修改屬性width及height之範圍為public
class Mymain
{
static void Main(string[] args)
{
CRectangle rect = new CRectangle();
rect.width = 10;
rect.height = 5;
}
}
public class CRectangle
{
public int width;
public int height;
int compute_area()
{
int area;
area=width*height;
return area;
}

}

-----------------------------------------------------------------------------
修改矩形rect物件之計算面積方法compute_area()之範圍為public並利用其計算矩形面積的數值指定給變數result,宣告int整數型態給result.
54
class Mymain
{
static void Main(string[] args)
{
int result;
CRectangle rect = new CRectangle();
rect.width = 10;
rect.height = 5;
result=rect.compute_area();
}
}
public class CRectangle
{
public int width;
public int height;
public int compute_area()
{
int area;
area=width*height;
return area;
}

}
-----------------------------------------------------------------------------
利用終端機consle顯示result之數值結果
class Mymain
{
static void Main(string[] args)
{
int result;
CRectangle rect = new CRectangle();
rect.width = 10;
rect.height = 5;
result=rect.compute_area();
Console.WriteLine("result=" + result);

}
}
public class CRectangle
{
public int width;
public int height;
public int compute_area()
{
int area;
area=width*height;
return area;
}

}

-----------------------------------------------------------------------------
在CRectangle類別中增加計算周長的計算方法perimeter()
public class CRectangle
{
public int width;
public int height;
public int compute_area()
{
int area;
area=width*height;
return area;
}
public int compute_parimeter()
{
int parimeter;
parimeter = 2 * (width + height);
return parimeter;
}

}
----------------------------------------------------------------------------
在主類別MyMain中使用矩形rect物件之計算面積方法compute_perimeter()並利用其計算矩形周長的數值指定給變數result_perimeter,宣告int整數型態給result.

static void Main(string[] args)
{
int result, result_perimeter;
CRectangle rect = new CRectangle();
rect.width = 10;
rect.height = 5;
result=rect.compute_area();
Console.WriteLine("result=" + result);
result_perimeter = rect.compute_parimeter();
Console.WriteLine("result_perimeter=" + result_perimeter);
}
-----------------------------------------------------------------------------
在主類別MyMain中之main方法增加計算width = 20;height = 10及width = 30;height = 15;兩矩形之面積與周長

static void Main(string[] args)
{
int result, result_perimeter;
CRectangle rect = new CRectangle();
rect.width = 10;
rect.height = 5;
result=rect.compute_area();
Console.WriteLine("result=" + result);
result_perimeter = rect.compute_parimeter();
Console.WriteLine("result_perimeter=" + result_perimeter);
rect.width = 20;
rect.height = 10;
result = rect.compute_area();
Console.WriteLine("result=" + result);
result_perimeter = rect.compute_parimeter();
Console.WriteLine("result_perimeter=" + result_perimeter);
rect.width = 30;
rect.height = 15;
result = rect.compute_area();
Console.WriteLine("result=" + result);
result_perimeter = rect.compute_parimeter();
Console.WriteLine("result_perimeter=" + result_perimeter);
}


-----------------------------------------------------------------------------
課堂練習
設計圓形類別,分別計算半徑為5,10,15的三個圓形面積與周長
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------

第九週 2008/11/07
主題:方法(Method)
static void Main(string[] args)
{
int d = 0, e = 0,f=0;
d = 1;
e = 2;
f = work(d, e);

Console.Write(f);
}

static int work(int a, int b)
{
int c;
c=a+b;
return (c);
}
---------------------------------------------------
static void Main(string[] args)
{
int[,] a ={ { 1, 2 }, { 4, 3 } };
int[] b ={0,0,0,0};
b=work(a);
for (int i = 0; i <= 3;i++ ) Console.Write(b[i]); } static int[] work(int[,] a) { int[] b ={ 0, 0, 0, 0 }; b[0] = a[0, 0]; b[1] = a[0, 1]; b[2] = a[1, 1]; b[3] = a[1, 0]; return (b); } ----------------------------------------------------------------------------- static int[] work(int[,] a) { int[] b ={ 0, 0, 0, 0 }; int k = 0; for (int i = 0; i <= 1; i++) { if (i == 0) { //第一列 for (int j = 0; j <= 1; j++) { b[k] = a[i, j]; k++; } } if (i == 1) { //第二列 for (int j = 1; j >= 0; j--)
{
b[k] = a[i, j];
k++;
}
}

}

return (b);

}
----------------------------------四列的情形----------------------------------
static int[] work(int[,] a)
{
int[] b ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int k = 0;
for (int i = 0; i <= 3; i++) { if (i == 0) { //第一列 for (int j = 0; j <= 3; j++) { b[k] = a[i, j]; k++; } } if (i == 1) { //第二列 for (int j = 3; j >= 0; j--)
{
b[k] = a[i, j];
k++;
}
}
if (i == 2)
{
//第三列
for (int j = 0; j <= 3; j++) { b[k] = a[i, j]; k++; } } if (i == 3) { //第四列 for (int j = 3; j >= 0; j--)
{
b[k] = a[i, j];
k++;
}
}
}

return (b);
------------------------------比較簡潔的寫法------------------------------------
static int[] work(int[,] a)
{
int[] b ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int k = 0;
for (int i = 0; i <= 3; i++) { if (i%2 == 0) { //偶數列 for (int j = 0; j <= 3; j++) { b[k] = a[i, j]; k++; } } if (i%2 == 1) { //奇數列 for (int j = 3; j >= 0; j--)
{
b[k] = a[i, j];
k++;
}
}

}

return (b);
------------------------------switch的寫法-------------------------------------
{
int[] b ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int k = 0;
for (int i = 0; i <= 3; i++) { switch (i%2 == 0) { case 0: //偶數列 for (int j = 0; j <= 3; j++) { b[k] = a[i, j]; k++; } break; case 1: { //奇數列 for (int j = 3; j >= 0; j--)
{
b[k] = a[i, j];
k++;
}
}
break;
}


}

return (b);
------------------------------------------------------------------------------

第八週 2008/10/31
期中考
-----------------------------------------------------------------------------
第七週 2008/10/24

static void Main(string[] args)
{
int[] a ={0,2};
Console.WriteLine(a[1]);
}
----------------------------------------
static void Main(string[] args)
{
int[] a ={0,2,4,6,8,10};
int i = 0;
for (i = 0; i <= 5;i++ ) Console.WriteLine(a[i]); } ----------------------------------------- static void Main(string[] args) { int[] a ={0,2,4,6,8,10}; int i = 0; int tmp = 0; tmp = a[0]; a[0] = a[1]; a[1] = tmp; for (i = 0; i <= 5;i++ ) Console.WriteLine(a[i]); } ---------------換位置------------------------ static void Main(string[] args) { int[] a ={6,4,2,0}; int i = 0; int tmp = 0; if (a[0] > a[1])
{

tmp = a[0];
a[0] = a[1];
a[1] = tmp;
}
for (i = 0; i <= 5;i++ ) Console.WriteLine(a[i]); } ------------------------------------------- static void Main(string[] args) { int[] a ={6,4,2,0}; int i = 0; int tmp = 0; if (a[0] > a[1])
{
i = 2;
tmp = a[1];
a[i] = a[i+1];
a[i+1] = tmp;
}
for (i = 0; i <= 3;i++ ) Console.WriteLine(a[i]); } -------------------------------------------- static void Main(string[] args) { int[] a ={6,4,2,0}; int i = 0; int tmp = 0; for (i = 0; i <= 2; i++) { if (a[i] > a[i + 1])
{
tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
for (i = 0; i <= 3;i++ ) Console.WriteLine(a[i]); } ------------------------------------------ static void Main(string[] args) { int[] a ={6,4,2,0}; int i = 0; int tmp = 0; int k = 0; for (k = 1; k <= 3; k++) { for (i = 0; i <= 2; i++) { if (a[i] > a[i + 1])
{
tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
}
for (i = 0; i <= 3;i++ ) Console.WriteLine(a[i]); } ---------------------------------------------------------- 讓程式更有效率,減少不必要的運算。(下列數字由小排序到大) static void Main(string[] args) { int[] a ={5,14,12,6,8,4,1,18,11,9,3,2,7,15}; int i = 0; int tmp = 0; int k = 0; for (k = 0; k <= 12; k++) { for (i = 0; i <= 12-k; i++) { if (a[i] > a[i + 1])
{
tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
}
for (i = 0; i <= 13;i++ ) Console.WriteLine(a[i]); } ------------------------------------------------------------ static void Main(string[] args) { int [,] a=new int[,]{{0,1,2},{3,4,5}}; int [] b={0,0,0,0,0,0}; int i = 0,j=0,k=0,tmp= 0; int count = 0; while (count<=14) { count++; tmp = a[0, 0]; b[0] = tmp; Console.WriteLine(count); } -------------------------------------------------------------- ----------------------------------------------------------------------------- 第六週 2008/9/12 範例目標: 1.數一數 for (int i = 1; i < i =" i">
i=i+1; i+=1; i++;
------------------------------------------------------------------------------
int i = 0;
i = i + 1;
Console.WriteLine(i);
i += 1;
Console.WriteLine(i);
i++;
Console.WriteLine(i);
------------------------------------------------------------------------------
3.求和
sum=sum+x
------------------------------------------------------------------------------
int i = 0;
int sum = 0;
for (i = 0; i <= 10; i++) { Console.WriteLine("i={0}",i); sum = sum + i; Console.WriteLine("sum={0}",sum); } --------------------------- -------------------------------------------------- int i = 0; int s = 0; for (i = 1; i <= 10; i++) { s += i; Console.WriteLine("s={0}",s); } ---------------------------------------------------------------------------- int i = 0; int s = 0; int ai = 0; for (i = 1; i <= 5; i++) { ai = 4 + (i - 1) * 3; s = s + ai; Console.WriteLine("s={0}",s); } ----------------------------------------------------------------------------- 九九乘法表 int i, j; for (i = 1; i <= 9; i++) { for( j = 1; j <= 9; j++) { Console.WriteLine("i={0},j={1},i*j={2}", i,j,i*j); } } ----------------------------------------------------------------------------- P5-14第15題 int i, j; int s = 0; for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { s+=(i+j); } } Console.WriteLine("s={0}", s); --------------------------------------------------------- 4. Game over --> while
---------------------------------------------------------
int i = 1000;
while (i > 0)
{
i--;
}
Console.WriteLine(i);
---------------------------------------------------------
int i = 0;
int sum = 0;
while (i <= 9) { i++; sum += i; Console.WriteLine("i={0},sum={1}",i,sum); } ------------------------------------------------------ P5-36 求積分 int i, n = 100; double s = 0; double ai=0; double a = 1; double b = 2; for (i = 1; i <= n; i++) { ai = (a + i * (b - a) / n) * ((b - a) / n); s = s + ai; Console.WriteLine(s); } ----------------------------------------------------------------------------- 第五週 2008/10/10 雙十節 第一週 2008/9/12 ---------------------------------------------------------------------------- 第二週 2008/9/19 http://www.powercam.cc/show.php?id=409
範例目標:
1.在記憶體中保留之空間給對應位置的變數.
2.利用終端機將一字串顯示出來
3.利用終端機將變數對應記憶體位置中的數字顯示出來
範例一
static void Main(string[] args)
{
//declare 在記憶體保留空間給變數a和b
int a,b;
a = 2; //將數字2放在記憶體中 a對應位置所保留之空間
b = 1;//將數字1放在記憶體中 b對應位置所保留之空間
Console.Write("a=");//將字串"a="輸出到終端機
Console.WriteLine(a);//將記憶體中 a對應位置所保留之空間內的數字輸出到終端機
Console.Write("b=");//將字串"b="輸出到終端機
Console.Write(b);//將記憶體中 b對應位置所保留之空間內的數字輸出到終端機
}
範例目標:
1.在記憶體中保留之空間給對應位置的變數.
2.利用終端機將鍵盤輸入到電腦的一字串顯示出來
3.利用終端機將變數對應記憶體位置中的數字顯示出來
範例二:
//declare 在記憶體保留空間給變數a和b
String a, b;
Console.Write("請輸入a");//提示語
a = Console.ReadLine();//將鍵盤的字元放入變數a所對應記憶體的空間內 Console.WriteLine("a={0:c}", a);//將字串"a="輸出到終端機及記憶體中 a對應位置所保留之空間內的數字輸出到終端機
Console.Write("請輸入b");//提示語
b = Console.ReadLine();//將鍵盤的字元放入變數b所對應記憶體的空間內
Console.Write("b={0:c}", b);//將字串"b="輸出到終端機及記憶體中 b對應位置所保留之空間內的數字輸出到終端機
----------------------------------------------------------------------------------------
課堂作業
輸入三角形三邊長a,b,c
計算三角形面積
輸出三角形面積結果
----------------------------------------------------------------------------

第三週 2008/9/26
http://www.powercam.cc/show.php?id=459

範例目標:
1.視窗程式顯示表單
2.提示文字所使用的物件 "Lable"標籤
3.輸入文字的物件"Textbox"文字方塊
4.製作物件"Button"按鈕並將指令程式放入其中
4.1.輸出文字到標籤
4.2.輸出文字到物件"MessageBox"對話框

MessageBox.Show("請按確定按鈕","標題");

課堂作業
1.利用表單及標籤和文字方塊
輸入三角形三邊長a,b,c

2.製作物件"Button"按鈕
並將"計算三角形面積"指令程式放入其中

3.輸出三角形面積結果 到標籤和對話框
-----------------------------------------------------------------------------
第四週 2008/10/03

範例目標:
一 了解電腦程式語言的決策指令 if
二 如果條件 (運算式)為真就執行敘述
if (運算式)
{
敘述區塊;
}
範例1
利用多個if 條件 (運算式)製作出可以依據鍵盤輸入判斷輸入值是否大於設定值:
例如
設定值為60
測試結果
1.若輸入1就顯示"不及格"
2.若輸入59就顯示"不及格"
3.若輸入60就顯示"及格"
4.若輸入61就顯示"及格"

範例2
利用多個if製作出可以依據鍵盤輸入不同而執行不同的敘述:
例如
輸入1就顯示"一"
輸入2就顯示"二"
輸入3就顯示"三"

三. 如果條件 (運算式)為真就執行 { 敘述區塊1; } 否則就執行 { 敘述區塊2; }
if (運算式)
{
敘述區塊1;
}
else
{
敘述區塊2;
}
四 將三中的敘述區塊2換成if (運算式) { 敘述區塊2; } else {敘述區塊3; }
if (運算式) { 敘述區塊1; }
else {

if (運算式)
{ 敘述區塊2; }
else {敘述區塊3; }

}
範例4 利用巢狀if else 製作出判斷成
績等級A(90-100),B(80-89),C(70-79),D(<70)


五. 利用切換開關的方式,依據運算式所算出的數值再選取不同 case 的敘述區塊來執行;
switch (運算式)
{
case 數值1
{
敘述區塊1;
break;
}
case 數值2
{
敘述區塊1;
break;
}

...
...

default:
{
敘述區塊;
break;
}

}

附註:
運算式若為字串型態的變數則可利用 case "字串" 選擇不同 情況來執行不同敘述區塊;
i=i+1

範例3

利用switch...case 製作出輪流明暗之紅綠燈

4. 例外處理
try
{
}
catch
{
}
catch
{
}
...
...
finally
{
}


課堂作業
1.利用表單及標籤和兩個文字方塊將輸入轉成實數a,b(雙精倍數)
2.製作物件"Button"按鈕四個來進行+,-,*,/四種運算,其運算元是a和b
3.利用例外處理處理除數為零的情況
4.輸出結果到標籤和對話框