The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Araya Pothisorn Florence, 2020-04-26 12:12:39

Compro63

ฟังก์ชั่นถัดมาคอ func2(list) รับ list เป็นพารามิเตอร์ในลักษณะเดียวกันกับ func1 ในการท างาน

ของฟังก์ชั่น จะท าการพิมพ์สิ่งที่อยู่ใน list แล้วเพิ่มวัตถุ 25 และ 12 เข้าไปต่อท้ายลิสต์ หลังจากนั้นจะทาการ

พิมพ์สิ่งที่อยู่ในลิสต์ออกมาอกครั้ง

def func2(list):
print(list)
list += [25, 12]
print(list)

เมื่อเรียกใช้งานในส่วนของฟงก์ชั่นหลักด้วย func2(fib) ในส่วนของการ print(list) จึงเป็นการส่งค่า

แบบ pass by reference เสมือนสั่งให้ print(fib) นั่นเอง แต่เมื่อมีการสั่ง list += [25, 12] ซึ่ง
เทียบได้กับการ append วัตถุ 25 และ 12 เข้าไปในลิสต์ด้วยค าสั่ง list.append(25) และ list.append(12)
ไม่ใช่การก าหนดค่าให้กับชื่อที่ส่งเข้ามาทางพารามิเตอร์ แต่เป็นการเปลี่ยนแปลงสิ่งที่อยู่ใน list จึงท าให้เป็น

การ pass by object reference เช่นเดิม เสมือนกับการเอา fib มา append ด้วย 25 และ 12 ตามล าดับ
ท าให้เมื่อท าการ print(list) จึงได้ผลเป็น [1, 2, 3, 4, 5, 25, 12] ซึ่งเป็นการกระท ากับลิสต์ที่ชื่อว่า fib
ฟังก์ชั่นต่อมาคือ func3(list) รับ list เป็นพารามิเตอร์ ในส่วนของฟงก์ชั่น จะท าการสร้างลิสต์ว่างตัว

ใหม่ขึ้นมาชื่อว่า list2 หลังจากนั้นท าการสั่ง reverse ลิสต์ที่ถูกส่งเข้ามา (list) แล้วท าการส าเนาลิสต์ดังกล่าว

ให้กับ list2 และส่ง list2 กลับออกไปที่ฟังก์ชั่นหลัก
def func3(list):
list2 = []
list.reverse()
list2 = list.copy()
return list2
ในส่วนของ Listing 7.2 บรรทัดที่ 19 เป็นการสร้างลิสต์ว่างชื่อว่า list3 แล้วท าการเรียกใช้งาน
func3 ด้วยส าเนาของ fib เนื่องจาก func3 จะส่งลิสต์กลับออกมา ท าให้การเรียกใช้งานฟังก์ชั่นต้องมีชื่อของ
ลิสต์มารองรับลิสต์ที่ func3 จะส่งกลับออกมา ท าให้ต้องสั่ง list3 = func3(fib[:]) เมื่อเรียกใช้งาน
func3(fib[:]) เป็นการส่งส าเนาของ fib เข้าไปในฟังก์ชั่น การส่งค่าแบบนี้จะเป็นการส่งแบบ pass by value
ดังนั้นการเปลี่ยนแปลงของลิสต์ที่ชื่อว่า fib จะไม่เกิดขึ้นในฟังก์ชั่นหลัก สังเกตได้จากผลตอบสนองในรูปที่ 7.2

7.4 การค้นข้อมูลในลิสต์ (Searching lists)

ไพธอนมีเมธอดให้เรียกใช้ในการค้นหาวัตถุในลิสต์ด้วยค าสั่ง
isinstance(a, list)
เมื่อ a คือวัตถุที่ต้องการค้นหา


7.5 การเรียงข้อมูลในลิสต์ (Sorting lists)


การเรียงข้อมูลในลิสต์สามารถใช้เมธอดในการเรียงด้วยค าสั่ง
sorted(a)


7.6 การประกาศอาร์เรย์สองมิติ (Declaring 2D Arrays)


ในไพธอน มี package ที่ชื่อ numpy ที่มี module ต่างๆให้เรียกใช้งาน รวมถึงแมตริกซด้วย แม
ตริกซ์แตกต่างจากลิสต์สองมิติคือ ชนิดข้อมูลในแมตริกซ์เป็นชนิดเดียวกัน แต่ในลิสต์สองมิติอาจเป็นต่างชนิด
กันได้ อาร์เรย์สองมิติอย่างง่ายก็คือแมทริกซ์ทางคณิตศาสตร์นั่นเอง หากมองอาร์เรย์หนึ่งมิติเป็นเวกเตอร์

143 | P a g e





Araya's Creation


อาร์เรย์สองมิติกคือชุดของเวกเตอร์นั่นเอง อาร์เรย์สองมิติมีแถวและหลัก โดย subscript แรกเป็นแถว และ
subscript ตัวต่อมาเป็นหลัก


ลิสต์สองมิติ

เมื่อเป็นลิสต์ ขนาดของแต่ละแถว ไม่จ าเป็นต้องมีจ านวนหลักเท่ากัน และข้อมูลในลิสต์ ไม่จ าเป็นต้อง

เป็นชนิดเดียวกัน รูปแบบทั่วไปในการประกาศลิสต์คือ

สร้างลิสต์ว่าง


list_name = [ ]

เพิ่มลิสต์ว่างเข้าไปให้ลิสต์ว่างดังกล่าวสองอัน จะท าให้ได้ลิสต์ที่ซ้อนอยู่ในลิสต์


list_name.append([ ]) #index ที 0

list_name.append([ ]) #index ที 1
เพิ่มอิลิเมนต์ให้ลิสต์ที่ index 0

list_name[0].append(2) #ตัวแรก

list_name[0].append(4) #ตัวทีสอง
จะท าให้ได้ข้อมูลในแถวแรก หรือที่ลิสต์ที่ index 0 เป็น [2, 4]


เพิ่มอิลิเมนต์ให้ลิสต์ที่ index 0

list_name[1].append(5) #ตัวแรก
list_name[1].append(6) #ตัวทีสอง


list_name[1].append(7) #ตัวทีสาม
จะท าให้ได้ข้อมูลในแถวที่สอง หรือที่ลิสต์ที่ index 1 เป็น [5, 6, 7]

ซึ่งจะเห็นได้ว่าลิสต์แต่ละอันที่ index แต่ละอัน สามารถมีจ านวนสมาชิกที่ไม่เท่ากันได้ ในไพธอนจะ

เห็นลิสต์ในลักษณะที่เป็น nested อยู่ ดังแสดงในรูปที่ 7.3 ซึ่งได้มาจากชุดค าสั่งดังนี้

table1 = [ ]
table1.append([20, 25, 30, 35, 40 ])
table1.append([68.0, 77.0, 86.0, 95.0, 104.0])
























144 | P a g e





Araya's Creation

รูปที่ 7.3 แสดงความสัมพันธ์ของสมาชิกใน nested list

อาร์เรย์ใน numpy


การเขียนโปรแกรมเชิงวิศวกรรมส่วนมากจบลงที่แมตริกซ์ ในไพธอนมี array ให้เรียกใช้งาน โดยที่
จะต้อง import numpy ก่อน และใน numpy ก็มีแมตริกซ์ ซึ่งเป็น subclass ของอาร์เรย์ให้เรียกใช้งานได้
เลย matrix เป็น subclass ของ ndarray ใน numpy เมื่อประกาศใช้ matrix นั่นหมายความว่าเป็นอาร์เรย์

สองมิติ คือมีแถวและหลัก แต่หากประกาศ ndarray จะสามารถประกาศได้มากกว่าสองมิติ ทั้งนี้ในการเขียน
โปรแกรมเกี่ยวกับ matrice ในไพธอนมีเมธอดให้เรียกใช้ซึ่งสามารถกระท าการทางคณิตศาสตร์ที่เกี่ยวข้อง
กับแมตริกซ์ได้ รวมไปถึง dot product, transpose, inverse ได้ด้วย


import numpy as np

การสร้างอาร์เรย์หนึ่งมิติ

x = np.array([1, 2])

จะได้ = [1 2]

การสร้างอาร์เรย์สองมิติ


x = np.array([[1, 2], [3, 4]])

1 2
จะได้ = [ ]
3 4
การสร้างแมตริกซ ์


x = np.matrix([[1, 2], [3, 4]])

แปลงลิสต์ให้เป็นอาร์เรย์
145 | P a g e





Araya's Creation

list = [1, 2]
x = np.array(list)
แปลงลิสต์ให้เป็นแมตริกซ ์


x = np.matrix(list)

รายการเมธอดบางอย่างที่ใช้บ่อยกับอาร์เรย์อยู่ในตารางที่ 7.2

ี่
ตารางท 7.2 ชุดค าสั่งที่ใช้บ่อยเกี่ยวกับอาร์เรย์

ชุดค าสั่ง ค าอธิบาย
array_a[np.arange(n), เลือก n อิลิเมนต์จากแต่ละแถวของ array_a ด้วย
array_b]
index ที่อยู่ใน array_b
type บอกชนิดของอาร์เรย์
shape บอกขนาดของอาร์เรย์
a = np.zeros((2,2)) สร้างอาร์เรย์ขนาด 2x2 ที่สมาชิกทุกตัวเป็น 0

a = np.ones((2,2)) สร้างอาร์เรย์ขนาด 2x2 ที่สมาชิกทุกตัวเป็น 1
d = np.eye(2) สร้าง identity matrix ขนาด 2x2

e = np.random.random((2,2)) สร้างอาร์เรย์ขนาด 2x2 ที่มีคาสุ่มเป็นสมาชิก
f = np.full((2,2),7) สร้างอาร์เรย์ขนาด 2x2 ที่มีสมาชิกทุกตัวเป็น 7.0



นอกจากนี้แล้วยังสามารถกระท าการทางคณตศาสตร์กับทั้งอาร์เรย์ได้ เช่น สั่ง arc_a = math.sin(a)

เพื่อหาค่า sin ของแต่ละอิลิเมนต์ของอาร์เรย์ a


รายละเอียดต่างๆเกี่ยวกับ numpy มีอยู่มากมายในเวป ตัวอย่างเช่น http://cs231n.github.io/python-
numpy-tutorial/


7.8 การจัดการอาร์เรย์สองมติ (Processing 2D Arrays)

การจัดการอาร์เรย์สองมิติสามารถท าได้ตามตัวอย่างต่อไปใน ภายใต้สมมุติฐานที่มีการกาหนดอาร์เรย์

ดังนี้

x = np.zeros((2,2))
1. การก าหนดค่าให้กับอาร์เรย์โดยรับค่าเข้ามาจากผู้ใช้
for i in range(len(x)):
for j in range(len(x[0])):
x[i][j]=int(input("Enter a number: "))
2. การก าหนดค่าด้วยค่าสุ่ม โดยค่าที่สุ่มเป็นค่าทอยู่ระหว่าง 0 ถึง 99
ี่
for i in range(len(x)):
for j in range(len(x[0])):
x[i][j]=random.random(0,99)
3. แสดงค่าที่เก็บอยู่ในอาร์เรย์ให้อยู่ในรูปแบบแถวและคอลัมน์
print(x)
4. รวมค่าของทุกสมาชิกในแมทริกซ์

146 | P a g e





Araya's Creation

np.sum(x)
5. รวมค่าในแต่ละหลัก
np.sum(x, axis = 0)
6. รวมค่าในแต่ละแถว
np.sum(x, axis = 1)
7. หาค่ามากที่สุดในแต่ละแถว
np.amax(x, axis = 1)

7.9 การส่งอาร์เรย์สองมิติเข้าไปในฟังก์ชั่น (Passing 2D Arrays to Functions)



การส่งอาร์เรย์ไปเป็น argument ในฟังก์ชั่นก็มีหลักการเดียวกันกับที่ใช้ในอาร์เรย์หนึ่งมิติ ไม่มความ
แตกต่างแต่อย่างใด






























































147 | P a g e





Araya's Creation

This page intentionally left blank.

















































148 | P a g e





Araya's Creation

1309 200 Computer Programming Laboratory


ปฏิบัติการที่ 7.1: Arrays and C-String



วัตถุประสงค์



1. ท าความเขาใจและสามารถอธิบายลักษณะของอาเรย์แบบหนึ่งมิติได้
2. สามารถประกาศตัวแปรอารย์แบบหนึ่งมิติได้

3. สามารถก าหนดข้อมูลให้กับตัวแปรอาเรย์แบบหนึ่งมิติได้
4. สามารถวนลูปเพื่อเขียนและอานข้อมูลทุกตัวในอาเรย์แบบหนึ่งมิติได้


5. สามารถเขียนและใช้งานฟังกชั่นที่รับพารามิเตอร์เป็นอาร์เรย์ได้
ทฤษฏีที่เกี่ยวข้อง


อาเรย์ขนาด 1 มิติ (One Dimension Arrays)


อาเรย์ขนาด 1 มิติ จะใช้เก็บขอมูลเดียวกันได้หลายๆ จ านวนในหนึ่งมิติ โดยในแถวหนึ่งสามารถเกบ


ข้อมูลได้หลายๆ คอลัมน์หรือเปรียบเทียบได้กับการกล่องหลายๆใบมาเรียงต่อกันเป็นแถว โดยเป็นชนิดข้อมูล
เดียวกัน หลักๆแล้วการใช้งานอาร์เรย์ขึ้นอยู่กับความสามารถในการวิเคราะห์ประเด็นปัญหาว่าเหมาะสมที่จะ

ใช้งานอาร์เรย์หรือไม่ โดยมีหลักการดังนี้


1. ข้อมูลหลายๆจ านวนนี้เป็นชนิดเดียวกัน


2. มีความจ าเป็นต้องเก็บขอมูลนั้นไว้เพื่อน ามาวิเคราะห์ร่วมกน
ปฏิบัติการส่วนที่ 1 การใช้งานอาร์เรย์หนึ่งมิติ


Listing 7.1 ในบทนี้เป็นการรับข้อมูลเข้ามาจากผู้ใช้ 100 ค่า ด้วยการวนไปในอาร์เรย์ ระหว่างวนไปในอาร์เรย์
เพื่อรับข้อมูลจากผู้ใช้ก็ท าการเก็บสะสมหาผลรวมของทุกสมาชิกในอาร์เรย์ หลังจากนั้นท าการหาค่าเฉลี่ยของ

อาร์เรย์ แล้วท าการวนลูปอีกหนึ่งครั้งเพอนับว่ามีข้อมูลที่มากกว่าค่าเฉลี่ยอยู่กี่ขอมูล

ื่
ค าสั่ง จาก Listing 7.1 จงท าการปรับปรุงโปรแกรมให้


1. สุ่มให้ค่าที่เก็บในอาร์เรย์เป็นค่าระหว่าง 0.0 – 99.9

2. พิมพ์ค่าทมากกว่าค่าเฉลี่ยออกไปเก็บทไฟล์ที่ชื่อ data.txt
ี่
ี่

3. มาร์คต าแหน่งของค่าที่มากกว่าค่าเฉลี่ยไว้ในอาร์เรย์อกตัวหนึ่งชื่อ int above_avg_pos[100];

โดยก าหนดค่า 0 ให้กับต าแหน่งที่ค่าน้อยกว่าหรือเท่ากบค่าเฉลี่ย และให้ก าหนดค่า 1 ให้กับ
ต าแหน่งที่ค่าสูงกว่าค่าเฉลี่ย


ปฏิบัติการส่วนที่ 2 การใช้งานอาร์เรย์หนึ่งมิติในฟังก์ชั่น



149 | P a g e





Araya's Creation

ค าสั่ง จากโปรแกรมที่ได้ในปฏิบัติการส่วนที่ 1 ให้ท าการปรับโค้ดให้อยู่ในรูปของฟังก์ชั่น โดยอย่างน้อยให้มี



ฟังก์ชั่นในการพมพอาร์เรย์ออกที่หน้าจอ โดยก าหนดให้พิมพ์บรรทัดละ 10 ค่า
ปฏิบัติการส่วนที่ 3 การใช้งาน C-String

ค าสั่ง จงเขียนโปรแกรมให้รับข้อความเข้ามาจากผู้ใช้เป็นประโยคโดยก าหนดให้ความยาวของข้อความไม่

เกิน 100 อักขระ แล้วท าการ


1. นับว่ามีสระอยู่กี่ตัวในข้อความดังกล่าว
2. หาว่ามีค าว่า is, am, are อยู่กี่ครั้งในข้อความดังกล่าว

































































150 | P a g e





Araya's Creation

แบบฝึกหัด



ค าสั่ง ให้นักศกษาน ารหัสนักศึกษาของตนเอง modulus ด้วย 4 หากได้เศษ 0 ให้ท าข้อ 4 เศษ 1 ท าข้อ 1
ื่
เศษ 2 ท าข้อ 2 และเศษ 3 ท าข้อ 3 โดยท าการเขียนโค้ดในโปรเจ็คใหม่ เพอหาผลลัพท์ของการท า modulo

รหัสนักศึกษาของตนเองกอนท าการเขียนโค้ด ตามโจทย์ของข้อที่ได้ต่อไปนี้

1. (Practice) a. Write a program to input the following values in an array named volts:
11.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, and 17.59. After the data has been
entered, have your program display the values.

b. Repeat Exercise 6a, but after the data has been entered, have your program display it
in the following form:
11.95 16.32 12.15

8.22 15.98 26.22
13.54 6.45 17.59


2. (Data Processing) a. Write a program to input 10 integer numbers in an array named
fmax and determine the maximum value entered. Your program should contain only one
loop, and the maximum should be determined as array element values are being input.
(Hint: Set the maximum equal to the first array element, which should be input before

the loop used to input the remaining array values.)
b. Repeat Exercise 8a, keeping track of both the maximum element in the array and the
index number for the maximum. After displaying the numbers, print these two messages

(replacing the underlines with the correct values):
The maximum value is: ___
This is element number ___ in the list of numbers
c. Repeat Exercise 8b, but have your program locate the minimum of the data entered.


3. (Electrical Eng.) Write a program that specifies three one-dimensional arrays named
current, resistance, and volts. Each array should be capable of holding 10 elements. Using a

for loop, input values for the current and resistance arrays. The entries in the volts array
should be the product of the corresponding values in the current and resistance arrays (so
volts[i] = current [i] * resistance[i]). After all the data has been entered, display the following
output, with the appropriate value under each column heading:

Current Resistance Volts

4. (Practice) The following declaration was used to create the volts array:
int volts[500];
Write two different function headers for a function named sortArray() that accepts
the volts array as a parameter named inArray.



151 | P a g e





Araya's Creation

This page intentionally left blank.

















































152 | P a g e





Araya's Creation

1309 200 Computer Programming Laboratory


ปฏิบัติการที่ 7.2: 2D Arrays



วัตถุประสงค์



1. ท าความเขาใจและสามารถอธิบายลักษณะของอาเรย์แบบสองมิติได้
2. สามารถประกาศตัวแปรอารย์แบบสองมิติได้

3. สามารถก าหนดข้อมูลให้กับตัวแปรอาเรย์แบบสองมิติได้
4. สามารถวนลูปเพื่อเขียนและอานข้อมูลทุกตัวในอาเรย์แบบสองมิติได้


5. สามารถเขียนและใช้งานฟังกชั่นที่รับพารามิเตอร์เป็นอาร์เรย์สองมิติได้
ทฤษฏีที่เกี่ยวข้อง


อาเรย์ขนาด 2 มิติ (Two Dimension Arrays)


ี่
อาเรย์ขนาด 2 มิติ ในการค านวณเชิงวิศวกรรมคือแมทริกซ์ การกระท าทท าได้กับแมทริกซ์ได้แก ่

1. การบวกแมทริกซ ์
2. การคูณแมทริกซ ์

3. การท าแมทริกซ์ทรานสโพส

4. การหาค่าดีเทอร์มิแนนซ์ของแมทริกซ์


ปฏิบัติการส่วนที่ 1 การบวกแมทริกซ์

ค าสั่ง จงเขียนโปรแกรมให้รับแมทริกซ์ขนาด 3 x 4 เข้ามาจากผู้ใช้ โดยโปรแกรมจะต้องมีฟังก์ชั่นต่างๆดังนี้


inputMat34 (array34)


showMat34 (array34)


addTwoMats (arrayA, arrayB)


ปฏิบัติการส่วนที่ 2 การคูณแมทริกซ์


ค าสั่ง เขียนโปรแกรมค านวณหาผลลัพธ์ของเมตริกซ์ ขนาด 2x2 โดยรับค่าอินพุทของเมตริกซ์ คือ A และ
B หาผลลัพธ์ต่อไปนี้ = × ก าหนดให้


1 8 2 1
= [ ] , = [ ]
2 5 1 2




153 | P a g e





Araya's Creation


เมื่อ = [ 1,1 1,2 ] , = [ 1,1 1,2 ] และ = [ 1,1 1,2 ]





2,2
2,1
2,2
2,1
2,2
2,1
และ , = + + ⋯ + = ∑
, ,
,2 2,
, ,
,1 1,
=1
ปฏิบัติการส่วนที่ 3 การหาค่าเดทและการทรานสโพสแมทริกซ์
−2 2 3
ค าสั่ง เขียนโปรแกรมค านวณหาดิเทอร์มิแนนของเมตริกซ์ A ก าหนดให้ = [−1 1 3 ]
2 0 −1












1 2 3
และจงท าการ Transpose แมททริกซ์ต่อไปนี้ = [4 5 6] โดยให้ท าการรับค่าอินพุททางแป้นพิมพ์
7 8 9
แสดงผลของค่าอินพท และค่าของเมตริกซที่ได้จากการ Transposes ดังรูป








































154 | P a g e





Araya's Creation

แบบฝึกหัด



ค าสั่ง ให้นักศกษาน ารหัสนักศึกษาของตนเอง modulus ด้วย 2 หากได้เศษ 0 ให้ท าข้อ 2 เศษ 1 ท าข้อ 1
ื่

โดยท าการเขียนโค้ดในโปรเจ็คใหม่ เพอหาผลลัพท์ของการท า modulo รหัสนักศกษาของตนเองกอนท าการ

เขียนโค้ด ตามโจทย์ของข้อที่ได้ต่อไปนี้
1. (Computational) A magic square is a square of numbers with N rows and N columns,
in which each integer value from 1 to (N * N) appears exactly once, and the sum of each
column, each row, and each diagonal is the same value. For example, Figure 7.17 shows

a magic square in which N = 3, and the sum of the rows, columns, and diagonal is 15.
Write a program that constructs and displays a magic square for any given odd number
N. This is the algorithm:

Insert the value 1 in the middle of the first row (element [0][N%2]).
After a value, x, has been placed, move up one row and to the right one column.
Place the next number, x + 1, there, unless:
(1) You move off the top (row = -1) in any column. Then move to the

bottom row and place the next number, x + 1, in the bottom row of that
column.
(2) You move off the right end (column = N) of a row. Then place the
next number, x + 1, in the first column of that row.

(3) You move to a position that is already filled or out of the upper-right
corner. Then place the next number, x + 1, immediately below x.
Stop when you have placed as many elements as there are in the array.














2. (Data Processing) Your professor has asked you to write a C++ program that determines
grades at the end of the semester. For each student, identified by an integer number

between 1 and 60, four exam grades must be kept, and two final grade averages must be
computed. The first grade average is simply the average of all four grades. The second grade
average is computed by weighting the four grades as follows: The first grade gets a weight
of 0.2, the second grade gets a weight of 0.3, the third grade gets a weight of 0.3, and the

fourth grade gets a weight of 0.2. That is, the final grade is computed as follows:
0.2 * grade1 + 0.3 * grade2 + 0.3 * grade3 + 0.2 * grade4
Using this information, construct a 60-by-7 two-dimensional array, in which the first

155 | P a g e





Araya's Creation

column is used for the student number, the next four columns for the grades, and the last
two columns for the computed final grades. The program’s output should be a display of

the data in the completed array. For testing purposes, the professor has provided the
following data:








































































156 | P a g e





Araya's Creation

บทที่ 8



การใช้งานไฟล์ (Working with files)


โปรแกรมที่ท ำงำนบนระบบปฏิบัติกำรใดๆนั้น เมื่อผู้ใช้ท ำกำรติดตั้ง ก็จะติดตั้งโปรแกรมลงไป
ที่หน่วยควำมจ ำของหลักคอ Hard disk แต่เวลำที่โปรแกรมถูกเรียกให้ท ำงำน โปรแกรมจะถูกโหลด

เข้ำมำเก็บไว้ในหน่วยควำมจ ำส ำรอง โดยระบบปฏิบัติกำรจะเป็นผู้จัดสรรพื้นที่เอำไว้ให้ส ำหรับ
โปรแกรมต่ำงๆ และตัวแปรต่ำงๆ ที่อยู่ในโปรแกรมนั้น

8.1 ไฟล์ในคอมพิวเตอร์
ประเภทของไฟล์ก็ยังแบ่งออกเป็น 2 ประเภท ดังนี้
1.ไฟล์แบบตัวอักษร หรือ ASCII File (หรือ เรียกว่ำ Text File)

2.ไฟล์แบบเลขฐำนสอง หรือ Binary File

นำมสกุลของ Text File มีดังนี้
.txt ไฟล์ข้อควำม Text
.bak ไฟล์ส ำรอง Back Up
.log ไฟล์ log
.ini ไฟล์เก็บค่ำ Profile
.c ไฟล์ซอร์สโค้ดโปรแกรมภำษำ C
.php ไฟล์ซอร์สโค้ดโปรแกรมภำษำ PHP
.java ไฟล์ซอร์สโค้ดโปรแกรมภำษำ Java


ตัวอย่ำงไฟล์ที่เก็บขอมูลแบบ Binary
ไฟล์รูปภาพ เช่น bmp, jpg, gif
ไฟล์เอกสาร ของโปรแกรมต่ำงๆ .doc, .xls, ,ppt, .rdw
ไฟล์มัลติมีเดีย เช่น .wav, .mp3, .ogg, .wma, .wmv
ไฟล์โปรแกรม เช่น .exe, .com, .dll

8.2 ฟังก์ชั่นจัดการไฟล์ในไพธอน
ในไพธอน มีวัตถุชนิด file ไว้ให้เรียกใช้งำน และจะมีเมธอดที่เกี่ยวข้องกับวัตถุชนิดนี้อยู่ ที่
ส ำคัญและจ ำเป็นกับกำรใช้งำนในรำยวิชำนี้แสดงในตำรำงที่ 8.1





157 | Page













Araya's Creation

ี่
ตารางท 8.1 แสดงเมธอดที่เกี่ยวกับวัตถุไฟล์
เมธอด ค าอธิบาย

file.close() ปิดไฟล์
file.flush() flush internal buffer
file.next() อ่ำนบรรทัดถัดไป
file.read(size) อ่ำนตำมขนำด size ที่ก ำหนด

file.readline() อ่ำนทีละบรรทัด
file.readlines() อ่ำนจน eof ด้วย readline()
file.tell() หำต ำแหน่งปัจจุบันของตัวชี้ไฟล์ที่ก ำลังเปิดอยู่

file.write(str) เขียน str string ลงไปในไฟล์

เมื่อเรำต้องกำรจะเขียนโปรแกรม ให้อ่ำนไฟล์ หรือเขียนไฟล์ จะมีขั้นตอนกำรเขียนอยู่ 3 ขั้น
ดังนี้

เปิดไฟล์ อ่ำนหรือเขียนไฟล์ ปิดไฟล์

ควรระลึกไว้เสมอว่ำ ก่อนท ำกำรใดๆกับไฟล์ ต้องท ำกำรเปิดไฟล์ก่อน หลังจำกนั้นจึงจะสำมำรถท ำ
กำรอ่ำนหรือเขียนไฟล์ได้ เมื่อเสร็จสิ้นกำรด ำเนินกำรแล้วจะต้องปิดไฟล์เสมอ

8.3 การเปิดไฟล์แบบข้อความเพื่ออ่านและเขียน
กำรอ่ำนจำกไฟล์ จะต้องมีกำรเตรียมไฟล์ไว้ในโปรเจ็คของ Eclipse เสียก่อนที่จะท ำกำรเปิดไฟล์ ส่วน

กำรเขียนลงไฟล์ หำกไม่มีไฟล์ที่อ้ำงถึงในโค้ดอยู่ในโปรเจ็ค จะมีกำรสร้ำงไฟล์ขึ้นให้ใหม่ วิธีกำร เตรียม

ไฟล์ข้อมูลเพื่อทดสอบ สำมำรถท ำได้ดังนี้

1. ไฮไลท์ที่ชื่อโปรเจ็ค

2. คลิกขวำแล้วเลือก New >> File ดังแสดงในรูปที่ 8.1
3. จะปรำกฏไดอะล็อกดังแสดงในรูป 8.2 ให้ตั้งชื่อไฟล์แล้วกด Finish
4. ท ำกำรป้อนข้อมูลลงในไฟล์ให้มีลักษณะดังรูป 8.3 แล้วกด Save
















158 | Page








Araya's Creation

ิ่
รูปที่ 8.1 แสดงวิธีกำรเพมไฟล์ให้โปรเจ็ค


































รูปที่ 8.2 แสดงกำรตั้งชื่อไฟล์





159 | Page













Araya's Creation


รูปที่ 8.3 แสดงกำรเตรียมขอมูลในไฟล์
ื่
สำมำรถท ำกำรเขียนโปรแกรมเพอท ำกำรเรียกไฟล์ออกมำใช้งำนได้ดังนี้

fo =
open("C:\\Users\\Scoob\\py_workspace\\SampleFile\\data.tx
t", "r+")
str = fo.read(10);
print ("Read String is : ", str)
# Close opend file
fo.close()
เมื่อท ำกำรรันโปรแกรม จะได้ผลดังรูปที่ 8.4














รูปที่ 8.4 ผลกำรรันโปรแกรม

โปรแกรมได้เรียกใช้วัตถุ file ที่ชื่อ fo ในกำรอ่ำนข้อมูลออกมำจำกไฟล์ และท ำกำรปิดไฟล์

หลังจำกใช้งำน


160 | Page








Araya's Creation

fo =
open("C:\\Users\\Scoob\\py_workspace\\SampleFile\\data.tx
t", "r+")

จำกโค้ดที่มีกำรใส่เครื่องหมำย \ สองอันในกำรบอกต ำแหน่งที่อยู่ของไฟล์ หรือที่เรียกว่ำ
Path เอำไว้ในเครื่องหมำยค ำพูด”...” เนื่องจำก เครื่องหมำยอันที่หนึ่งเป็นกำรบอกว่ำ สิ่งที่ตำมมำ

เป็นสัญลักษณพิเศษ ซึ่งในที่นี้ก็คือเครื่องหมำยแบคสแลชนั่นเอง กล่ำวคือที่อยู่ของไฟล์ที่ต้องกำรคือ
C:\\Users\\Scoob\\py_workspace\\SampleFile\\data.txt

โหมดในกำรเปิดไฟล์ มีดังนี้

Modes Description
r Opens a file for reading only. The file pointer is placed at the
beginning of the file. This is the default mode.

rb Opens a file for reading only in binary format. The file pointer is
placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed
at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file

pointer placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If
the file does not exist, creates a new file for writing.

wb Opens a file for writing only in binary format. Overwrites the file if
the file exists. If the file does not exist, creates a new file for
writing.
w+ Opens a file for both writing and reading. Overwrites the existing

file if the file exists. If the file does not exist, creates a new file for
reading and writing.
wb+ Opens a file for both writing and reading in binary format.
Overwrites the existing file if the file exists. If the file does not

exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file
if the file exists. That is, the file is in the append mode. If the file
does not exist, it creates a new file for writing.

ab Opens a file for appending in binary format. The file pointer is at
the end of the file if the file exists. That is, the file is in the

161 | Page













Araya's Creation

append mode. If the file does not exist, it creates a new file for
writing.

a+ Opens a file for both appending and reading. The file pointer is at
the end of the file if the file exists. The file opens in the append
mode. If the file does not exist, it creates a new file for reading
and writing.

ab+ Opens a file for both appending and reading in binary format. The
file pointer is at the end of the file if the file exists. The file opens
in the append mode. If the file does not exist, it creates a new file
for reading and writing.


จำกตัวอย่ำงในข้ำงต้น โหมดในกำรเปิดไฟล์ถูกก ำหนดให้เป็น r+ เพื่ออ่ำนและเขียนไฟล์
ดังนั้นเมื่อต้องกำรเขียนข้อควำม “Hello” ลงไปในไฟล์ ก็สำมำรถสั่ง
fo.write("Hello")

ื่
หำกไฟล์ที่เปิดนั้นไม่มอยู่ และต้องกำรให้สร้ำงไฟล์ใหม่เพอเขียน ให้เลือกเปิดด้วยโหมด w+


















































162 | Page








Araya's Creation


Click to View FlipBook Version