Principle of computer problem solving

Download Report

Transcript Principle of computer problem solving

สัญญลักษณ์ที่ใช้แทนขั้นตอนวิธี
• ภาษาธรรมชาติ (Natural languge)
เป็ นภาษาที่มนุษย์ใช้พดู และเขียนในชีวิตประจาวัน ข้อเสี ย ไม่ชดั เจน
ตีความหมายแตกต่างกัน
• รหัสเทียม (Pseudocode)
เป็ นการเขียนคาสั่งคล้ายในภาษาโปรแกรมแต่ไม่ตอ้ งมีไวยากรณ์ หรื อวิธีการ
สร้างประโยคที่เข้มงวด ผูเ้ ขียนแต่ละคนอาจมีวธิ ี การเขียนที่แตกต่างกัน
• ผังงาน (Flowchart)
เป็ นสัญลักษณ์ที่มีรูปแบบชัดเจน
เริ่ มต้น
ผังงาน (Flowchart)
• พัฒนาโดย American National
Standards Institute (ANSI)
• แสดงการแก้ปัญหาด้วยภาพ
• ใช้สัญลักษณ์แทนการกระทา
• ใช้ลูกศรเพื่อแสดงลาดับของการกระทา
• สัญลักษณ์ที่ใช้มีหลายรู ปแบบเพื่อแสดงถึง
การกระทาที่แตกต่างกัน
จ่าหน้าซอง
พับจดหมาย
นาจดหมายใส่ ซอง
มีแสตมป์ ?
มีแล้ว
ติดแสตมป์
นาจดหมายใส่ ซอง
จบ
ยังไม่มี
ซื้ อแสตมป์
รหัสเทียม (Pseudocode)
• คล้ายภาษาอังกฤษ
• บางทีเรี ยกว่า ภาษาอังกฤษแบบโครงสร้าง
• ใช้เพื่อเขียนขั้นตอนวิธีให้เป็ นระบบระเบียบดีกว่าภาษาอังกฤษที่
ใช้กนั ทัว่ ไป แต่มีกฎเกณฑ์นอ้ ยกว่าภาษาโปรแกรมคอมพิวเตอร์
• ช่วยทาให้สามารถมุ่งความสนใจไปที่ตรรกของขั้นตอนวิธีได้
เต็มที่ โดยไม่ตอ้ งกังวลในกฎเกณฑ์ของภาษา
• สามารถแปลงเป็ นภาษาโปรแกรมได้ง่าย
• ไม่สามารถกระทาการด้วยคอมพิวเตอร์
ขั้นตอนวิธี
ประกอบด้วยการดาเนินการ 2 ชนิด
1. การดาเนินการลาดับ (Sequential operations)
2. การดาเนินการควบคุม (Control Operation
2.1 การดาเนินการเงื่อนไข (Conditional operations)
2.2 การดาเนินการซ้ า (Iterative operations)
Sequential Operation
ขั้นตอนวิธีการโทรศัพท์
1. ค้นหาหมายเลข
2. กดหมายเลข และปุ่ มโทร
3. ฟังเสี ยงสัญญาณ
Algorithm
4. กล่าวคาทักทาย
1. Find phone number
5. สนทนา
2. Enter phone number
6 กล่าวลา
and press call button
7. วางหูโทรศัพท์
3. Listen to phone signal
4. Say “hello”
5. Make conversation
6. Say “Good bye”
7. Hang up a call
Start
Find phone number
Enter phone number
and Press the call button
Listen to signal
Say ‘hello’
Make conversation
Say “good bye”
Hang up a call
Stop
การคานวณ (Computation)
Set “variable” to “arithmetic expression”
ประมวลผลนิพจน์เลขคณิ ต (arithmetic expression)
แล้วนาผลที่ได้เก็บไว้ในตัวแปร (variable)
Set a to 100
Set b to 200
Set Result to a+b
100
a
200
300
b
Result
กาหนดให้ราคาสิ นค้าต่อชิ้น 27.75 บาท
กาหนดให้จานวนสิ นค้ามี 15 ชิ้น
ราคาสิ นค้า = ราคาสิ นค้าต่อชิ้น x จานวนสิ นค้า
แสดง ราคาสิ นค้า
Set priceperitem to 27.75
Set quantity to 15
Set price to priceperitem x quantity
Print price
คานวณราคาสิ นค้า
Start
priceperitem = 27.75
quantity = 15
price = priceperitem x quantity
Print price
Stop
27.75
15
priceperitem quantity
price
คานวณราคาสิ นค้า
Start
vat = 0.07
priceperitem = 27.75
quantity = 15
price = priceperitem x quantity
tax = price x vat
priceincludetax = price + tax
Print priceincludetax
Set vat to 0.07
Set priceperitem to 27.75
Set quantity to 15
Set price to priceperitem x quantity
Set tax to price x vat
Set priceincludetax to price + tax
Print priceincludetax
0.07
vat
27.75
15
priceperitem quantity
Stop
price
tax
priceincludetax
รหัสเทียมสาหรับการนาเข้าและส่ งออก
• Input :
Get “variable1”, variable2, …
Get the radius of the circle
Get radius
• Output
Print “variable1”, variable2, …
Print area
รับราคาสิ นค้าต่อชิ้นจากแป้ นพิมพ์
รับจานวนสิ นค้าจากแป้ นพิมพ์
คานวณราคาสิ นค้า = ราคาสิ นค้าต่อชิ้น x จานวนสิ นค้า
แสดง ราคาสิ นค้า
Get priceperitem
Get quantity
Set price to priceperitem x quantity
Print price
คานวณราคาสิ นค้า
Start
input
priceperitem, quantity
price = priceperitem x quantity
Print price
Stop
priceperitem quantity
price
กาหนดให้อตั ราภาษี 7 %
รับราคาสิ นค้าต่อชิ้น
รับจานวนสิ นค้า
ราคาสิ นค้า = ราคาสิ นค้าต่อชิ้น x จานวนสิ นค้า
ภาษี = ราคาสิ นค้า x อัตราภาษี
ราคาสิ นค้ารวมภาษี = ราคาสิ นค้า + ภาษี
แสดงราคาสิ นค้ารวมภาษี
Set vat to 0.07
Get priceperitem
Get quantity
Set price to priceperitem x quantity
Set tax to price x vat
Set priceincludetax to price + tax
Print priceincludetax
คานวณราคาสิ นค้า
Start
vat = 0.07
input
priceperitem, quantity
price = priceperitem x quantity
tax = price x vat
priceincludetax = price + tax
Print priceincludetax
Stop
อ่านจานวนเต็ม 2 จานวน เก็บไว้ในตัวแปร a และ b
แล้วให้สลับค่ากัน
Control Operation
เป็ นการดาเนินการที่เปลี่ยนการทางานแบบตามลาดับ ให้ไปทางานที่จุดอื่น
ประกอบด้วยการดาเนินการ 2 ชนิด
• Conditional Operation เป็ นการดาเนินการแบบ ถาม
คาถาม เพื่อเลือกการดาเนินการถัดไปที่จะกระทา
• Iterative Operation เป็ นการดาเนินการแบบวนซ้ า หรื อ
กระทาคาสัง่ เดิมหลายครั้ง
Conditional statement
If “condition” then
set of operation
ประมวลผล “condition” ถ้าเป็ นจริ ง ให้ทา set of operation
เงื่อนไข
true
T1
T2
false
กดเอทีเอ็ม
Start
ค้นหาบัตรเอทีเอ็ม
พบ?
true
สอดบัตร
กดถอนเงิน
กลับบ้าน
Stop
false
ค้นหาบัตรเอทีเอ็ม
ถ้าพบบัตร
สอดบัตร
กดถอนเงิน
กลับบ้าน
Search for the ATM card
If find the ATM card
insert the ATM card
press withdrawal button
Return home
Start
Grade = ‘U’
get score
Score >= 50
true
Grade = ‘S’
Print Grade
Stop
false
Set grade to ‘U’
Get score
If (score >= 50) then
Set grade to ‘S’
Print grade
Conditional statement
If “condition” is true then
first set of operations
Else
second set of operations
1.
2.
3.
4.
ประมวลผล “condition”
ถ้าเป็ นจริ ง ให้ทา first set of operations
ถ้าเป็ นเท็จ ให้ทา second set of operations
ดาเนินการ operation ถัดจาก conditional statement
if/then/else statement
true
เงื่อนไข
false
S1
T1
S2
T2
S3
T3
V
อ่านจานวนเต็ม 3 จานวน
แล้วหาค่าที่มากที่สุด
Start
read a,b,c
false
a>b
true
a>c
false
true
true
big = a
print big
Stop
b>c
false
big = c
big = b
big = c
Start
get score
Score >= 50
false
true
Grade = ‘S’
Print Grade
Stop
Grade = ‘U’
Get score
If (score >= 50) then
Set grade to ‘s’
Else
Set grade to ‘u’
Print grade
Start
get score
score >= 80
true
false
score >= 50
false
true
grade = ‘G’
print grade
Stop
grade = ‘P’
grade = ‘F’
Get score
if (score >= 80)
set grade to ‘G’
Else
if (score >= 50)
set grade to ‘P’
else
set grade to ‘F’
print grade
Get WorkHour,MaxHour, OTRate
IF WorkHour > MaxHour THEN
OverHour = MaxHour - WorkHour
ELSE
OverHour = 0
ENDIF
OverTime = OverHour * OTRate
Print OverTime
Start
Receive
WorkHour, MaxHour, OTRate
WorkHour > MaxHour
false
true
OverHour = MaxHour - WorkHour
OverTime = OverHour * OTRate
Print OverTime
Stop
OverHour = 0
กดเอทีเอ็ม
Start
ค้นหาบัตรเอทีเอ็ม
พบ?
false
true
สอดบัตรและใส่ รหัส
รหัสถูกต้อง?
true
กดถอนเงิน
กลับบ้าน
Stop
กดยกเลิกการถอนเงิน
ค้นหาบัตรเอทีเอ็ม
ถ้าพบบัตร
สอดบัตรและใส่ รหัส
ถ้ารหัสถูกต้อง
กดถอนเงิน
มิฉะนั้น
กดยกเลิกการถอนเงิน
กลับบ้าน
Loop
the repetition of a block of instructions
• ความสามารถที่แท้จริ งของคอมพิวเตอร์
อยูท่ ี่การทางานซ้ าหลายๆครั้ง
• ถ้าต้องการทางานนี้เพียงครั้งเดียว คงไม่
จาเป็ นต้องเขียนโปรแกรม แต่ถา้ ต้องทา
เป็ นหมื่นครั้ง แสนครั้ง ก็คุม้ ที่จะเขียน
โปรแกรม
• ถ้าเครื่ องคิดเลขใช้เวลา 5 วินาที
หนึ่งล้านครั้งใช้เวลา
5000000/(60x60x24)= 58 วัน
• ใช้คอมพิวเตอร์ ใช้เวลาไม่ถึง 1 วินาที
Looping : while statement
While (‘condition’) do
operation
operation
….
operation
เมื่อเงื่อนไข(a true/false condition)เป็ นจริ ง loop body จะถูกกระทา
แล้วกลับไปตรวจสอบเงื่อนไขอีกครั้ง
ถ้าเงื่อนไขเป็ นเท็จจะข้ามไปกระทาการ operation ที่อยูถ่ ดั จาก loop
true
S1
S2
S3
Sn
แบบจาลองการกระทาการ while loop
เงื่อนไข
false
Start
count = 1
count <= 100
true
Print count
Add 1 to count
Stop
false
count = 1
while count <= 100 do
print count
add 1 to count
Start
count = 1
count <= 100
true
Print count
Add 2 to count
Stop
false
count = 1
while count <= 100 do
print count
add 2 to count
Start
total = 0, count = 1,
count <= 100
true
add 1 to count
add count to total
Print count
Stop
false
count = 1
while count <= 100 do
add 1 to count
add count to total
print count
ให้หาพื้นที่สี่เหลี่ยมจัตุรัส
และแสดงผลดังนี้
Start
count = 1
count <= 100
true
square =count x count
false
1
1
2
4
3
9
4
16
5
25
6
36
…
100 10000
print count, square
add 1 to count
Stop
count = 1
while count <= 100 do
set square to count x count
print count, square
add 1 to count
Start
Set vat to 0.07
get priceperitem
while priceperitem <> 0 do
get quantity
vat = 0.07
Input priceperitem
priceperitem <> 0
true
input quantity
price = priceperitem x quantity
tax = price x vat
priceincludetax = price + tax
print priceincludetax
Input priceperitem
Stop
false
set price to priceperitem x quantity
set tax to price x vat
set priceincludetax to price + tax
print priceincludetax
get priceperitem