40
ENGR201 – important information 1 เเเเเเเ เเเเเเเเเเเเเเเเเเเเเ http://www.ee.eng.cmu.ac.th/~kasin/ http ://mango.cpe.eng.cmu.ac.th/course/view.p hp?id=22 (Log in as a guest) เเเเเเ Midterm: - Final: 3 เเเเเเเ 2559 8.00-11.00 เ. เเเเเเเเเ Attendance: 5% Midterm: 47.5% Final: 47.5%

Week 1 Introduction to Computer Programming and IDE

  • Upload
    kiara

  • View
    47

  • Download
    8

Embed Size (px)

DESCRIPTION

Week 1 Introduction to Computer Programming and IDE. Objectives. แนะนำเกี่ยวกับภาษาคอมพิวเตอร์ ชนิดของภาษาคอมพิวเตอร์ เครื่องมือในการพัฒนาโปรแกรมคอมพิวเตอร์ IDE เริ่มต้นเขียน C++ โปรแกรมด้วย VS Express 2013 การแปลงภาษา และการสร้างโปรแกรม (compile and build) การสั่งโปรแกรมทำงาน (run) - PowerPoint PPT Presentation

Citation preview

Page 1: Week 1 Introduction to  Computer Programming and IDE

ENGR201 – important information

1

• เวปไซต์� และเอกสารประกอบการสอน• http://www.ee.eng.cmu.ac.th/~kasin/• http://mango.cpe.eng.cmu.ac.th/course/view.php?id=22

• (Log in as a guest)•ว�นสอบ

• Midterm: -• Final: 3 พฤษภาคม 2559 8.00-11.00 น.

•ประเม�นผล• Attendance: 5%• Midterm: 47.5%• Final: 47.5%

Page 2: Week 1 Introduction to  Computer Programming and IDE

ENGR201 Website• How to access ENGR201 (259201) Course Website• http://mango.cpe.eng.cmu.ac.th • Go to Miscellaneous section (Misc.)

2

Page 3: Week 1 Introduction to  Computer Programming and IDE

ENGR201 Website• How to access ENGR201 (259201) Course Website• http://mango.cpe.eng.cmu.ac.th/course/index.php?categoryid=1

• Go to ENGR201

3

Page 4: Week 1 Introduction to  Computer Programming and IDE

ENGR201 Website• How to access ENGR201 (259201) Course Website• Log in as a guest

4

Page 5: Week 1 Introduction to  Computer Programming and IDE

ENGR201 Website• How to access ENGR201 (259201) Course Website

5

Page 6: Week 1 Introduction to  Computer Programming and IDE

Week 1Introduction to Computer

Programming and IDE

Page 7: Week 1 Introduction to  Computer Programming and IDE

Objectives•แนะน�าเก��ยวก�บภาษาคอมพ�วเต์อร�

• ชน�ดของภาษาคอมพ�วเต์อร�• เคร$�องม$อในการพ�ฒนาโปรแกรมคอมพ�วเต์อร�• IDE

• เร��มต์(นเข�ยน C++ โปรแกรมด(วย Dev-C++• การแปลงภาษา และการสร(างโปรแกรม (compile and build)

• การส��งโปรแกรมทำ�างาน (run)• ต์�วอย*างข(อผ�ดพลาดในการเข�ยนโปรแกรม• การต์รวจสอบการทำ�างาน (debugging)

7

Page 8: Week 1 Introduction to  Computer Programming and IDE

Programming Language Overview• เราใช(ภาษาคอมพ�วเต์อร� (Programming languages) ในการอธิ�บายส��งทำ��ต์(องการให้(เคร$�องคอมพ�วเต์อร�ทำ�างาน

• เราสามารถแบ*งประเภทำของภาษาคอมพ�วเต์อร� ออกกว(างๆได(เป0น• ภาษาระด�บส1ง (High Level)

• เป0นภาษาทำ��มน2ษย�ทำ�าความเข(าใจได(ไม*ยาก • ต์(องผ*านการแปลงภาษา (compile) ให้(เป0นภาษาทำ��เคร$�องคอมพ�วเต์อร�เข(าใจ จ3งจะทำ�างานได(

• ได(แก* C, C++, Java, Python • ภาษาระด�บล*าง (Low Level)

• ไม*จ�าเป0นต์(องผ*านการแปลงภาษาก4สามารถทำ�างานได(• ได(แก* assembly และภาษาเคร$�อง (machine code) ซ3�งคอมพ�วเต์อร�เข(าใจได(ทำ�นทำ�

• เป0นภาษาทำ��ทำ�างานได(บนระบบคอมพ�วเต์อร�ทำ��ค*อนข(างเฉพาะเจาะจง• ผ1กก�บชน�ดของ CPU และ Hardware อ$�นๆทำ��ประกอบก�นเป0นเคร$�อง

คอมพ�วเต์อร�8

Page 9: Week 1 Introduction to  Computer Programming and IDE

Programming Language Overview• เคร$�องม$อในการพ�ฒนาโปรแกรมคอมพ�วเต์อร�

• Text editor• โปรแกรมทำ��ใช(ในการสร(างไฟล�อ�กขระ (text file) เพ$�อใช(เข�ยนโค(ด

ในภาษาคอมพ�วเต์อร�ต์*างๆ แล(วบ�นทำ3กเป0นไฟล�นามสก2ลส�าห้ร�บโค(ดในภาษาทำ��ต์(องการ (.c, .cpp, .java, .py, …)

• Notepad (windows), TextEdit (mac), gedit (linux)• Compiler และ Interpreter

• โปรแกรมส�าห้ร�บแปลงโค(ดภาษาคอมพ�วเต์อร� (high level) ทำ��ต์(องการ ให้(กลายเป0นโปรแกรมในร1ปแบบของภาษาเคร$�อง (machine code) ทำ��พร(อมทำ�างาน

• cl (VC++), gcc (GNU C), g++ (GNU C++), java (Oracle, OpenJDK), Python

• Debugger• โปรแกรมทำ��ใช(ในการทำดสอบห้ร$อต์รวจสอบการทำ�างานของโปรแกรม

ทำ��ผ*านการแปลงภาษาแล(ว สามารถก�าห้นดให้(โปรแกรทำทำ�างานทำ�ละค�าส��งเพ$�อต์รวจด1ความถ1กต์(องของผลในแต์*ละข�7นต์อนได(

9

Page 10: Week 1 Introduction to  Computer Programming and IDE

Programming Language Overview• Integrated Development Environment (IDE)• รวบรวมเคร$�องม$อต์*างๆทำ��จ�าเป0นในการพ�ฒนาโปรแกรมคอมพ�วเต์อร�

• Text Editor, Compiler/Interpreter, Debugger, …• ม�การก�าห้นดค*าต์*างๆทำ��เห้มาะสมในการพ�ฒนาโปรแกรมให้(อ�ต์โนม�ต์�• ม�ส*วนต์�ดต์*อผ1(ใช(ทำ��แบบกราฟ8ก (GUI) สะดวกในการใช(งาน• IDE บางต์�วสามารถใช(พ�ฒนาโปรแกรมคอมพ�วเต์อร�ได(ห้ลายภาษา• ม�ทำ�7งแบบเส�ย (commercial) และไม*เส�ยค*าใช(จ*าย (freeware, open source)

• Eclipse IDE• ไม*เส�ยค*าใช(จ*าย ใช(พ�ฒนาโปรแกรมภาษา Java, C++, … (อ�กมากมาย)

• ใช(ได(บน Windows, Linux, Mac OS X• Code::Blocks

• ไม*เส�ยค*าใช(จ*าย ใช(พ�ฒนาโปรแกรมภาษา C, C++ และ Fortran• ใช(ได(บน Windows, Linux, Mac OS X

10

Page 11: Week 1 Introduction to  Computer Programming and IDE

Example of IDE• Eclipse

http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/lunar

11

Page 12: Week 1 Introduction to  Computer Programming and IDE

Example of IDE• Code::Blocks

http://www.codeblocks.org12

Page 13: Week 1 Introduction to  Computer Programming and IDE

13

Example of IDE• Visual Studio

Page 14: Week 1 Introduction to  Computer Programming and IDE

Example IDE• Dev-C++

14

Page 15: Week 1 Introduction to  Computer Programming and IDE

Dev-C++• เคร$�องม$อห้ร$อสภาพแวดล(อม (IDE) ทำ��ใช(อ(างอ�งส�าห้ร�บการเร�ยนการสอน

• Integrated Development Environment (IDE)

• สามารถใช(ในการพ�ฒนาโปรแกรม ด(วยภาษาคอมพ�วเต์อร�ได(ห้ลายภาษา• C++, C#, Visual Basic

• ไม*เส�ยค*าใช(จ*ายในการใช(งาน15

Page 16: Week 1 Introduction to  Computer Programming and IDE

ห้น(าต์*างห้ล�กของ Dev-C++ ทำ��ไม*ม�การแสดงข(อม1ล Start Page16

Dev-C++

Page 17: Week 1 Introduction to  Computer Programming and IDE

Start Programming C++• จาก File เมน1 เล$อก New >> Source File ห้ร$อ Crtl+N

17

Page 18: Week 1 Introduction to  Computer Programming and IDE

Start Programming C++• เร��มต์(นเข�ยน code

18

Page 19: Week 1 Introduction to  Computer Programming and IDE

Start Programming C++• บ�นทำ3ก code ทำ��เข�ยนในไฟล�ด(วยป29ม ห้ร$อ Crtl+S

19

Page 20: Week 1 Introduction to  Computer Programming and IDE

Compiling a C++ Program• การแปลโค(ดให้(เป0นภาษาคอมพ�วเต์อร� สามารถทำ�าได(โดยการเล$อก Execute >> Compile ห้ร$อ F9 ห้ร$อเล$อกป29ม

20

Page 21: Week 1 Introduction to  Computer Programming and IDE

Compiling a C++ program• ห้น(าจอด(านล*างแสดงสถานะของการ compile และบ*งช�7ความผ�ดพลาดของโค(ด ห้ากไม*ม�ข(อผ�ดพลาดจ�านวน Errors และ Warnings จะเป0นศู1นย�

21

จ�านวนความผ�ดพลาด (Errors)

จ�านวนการแจ(งเต์$อน(Warnings)

เวลาทำ��ใช((Compilation time)

Page 22: Week 1 Introduction to  Computer Programming and IDE

Compiling a C++ Program• ห้ากเก�ดความผ�ดพลาดใน code (เช*น ห้ากลบค�าส��ง cout ต์�วแรกออก) ต์�าแห้น*งทำ��เก�ดความผ�ดพลาดจะถ1ก highlight และห้น(าต์*างส*วนล*างจะเสนอแนวทำางแก(ไข code ให้(ถ1กต์(อง

22

1

2

Page 23: Week 1 Introduction to  Computer Programming and IDE

Structure of a C++ Program

23

Preprocessor

Main function declarationMain function definition

Comments

Page 24: Week 1 Introduction to  Computer Programming and IDE

What could go wrong?• CASE #1

24

พ�มพ�ต์กเคร$�องห้มาย semicolon ‘;’ ในบรรทำ�ดทำ�� 12

ห้น(าต์*างระบ2ความผ�ดพลาดทำ��เก�ดและแนวทำางแก(ไข

Page 25: Week 1 Introduction to  Computer Programming and IDE

What could go wrong?• CASE #2

25

พ�มพ�ค�าส��ง cout ทำ(ายเป0น count ในบรรทำ�ดทำ�� 8

ห้น(าต์*างระบ2ความผ�ดพลาดทำ��เก�ดและแนวทำางแก(ไข

Page 26: Week 1 Introduction to  Computer Programming and IDE

What could go wrong?• CASE #3

26

พ�มพ�ช$�อต์�วแปร (variable) จาก age เป0น aeg ในบรรทำ�ดทำ�� 9

ห้น(าต์*างระบ2ความผ�ดพลาดทำ��เก�ดและแนวทำางแก(ไข

Page 27: Week 1 Introduction to  Computer Programming and IDE

ห้น(าต์*างระบ2ความผ�ดพลาดทำ��เก�ดและแนวทำางแก(ไข

What could go wrong?• CASE #4

27

พ�มพ�ช$�อ Header ไฟล� จาก iostream เป0น iostrem ในบรรทำ�ดทำ�� 1

Page 28: Week 1 Introduction to  Computer Programming and IDE

What could go wrong?• CASE #5

28

ขาดเคร$�องห้มาย วงเล4บป;กกาเป8ด ‘ { ‘ ในบรรทำ�ดทำ�� 5

Page 29: Week 1 Introduction to  Computer Programming and IDE

Run a Program• Run a Program

• ส��งให้(โปรแกรมทำ��ผ*านการ Build ทำ�างานโดยเล$อก Execute >> Run ห้ร$อ F10 ห้ร$อเล$อกป29ม

• ผลล�พทำ�ทำ��ได(จากค�าส��งจะแสดงห้น(าจอการทำ�างานของโปรแกรม

29

Page 30: Week 1 Introduction to  Computer Programming and IDE

Run a Program• โปรแกรมสามารถร�บ input จากผ1(ใช(และแสดงผล output ออกมาทำางห้น(าจอ• เม$�อพ�มพ� 20 แล(วกด enter จะได( output ด�งภาพประกอบด�งน�7

ห้มายเห้ต์2 ส�าห้ร�บ Dev-C++ version ต์��ากว*า 5.8.3 ห้น(าจอการแสดงผล output จะถ1กป8ดอ�ต์โนม�ต์�ทำ�นทำ�ห้ล�งการทำ�างาน

30

Page 31: Week 1 Introduction to  Computer Programming and IDE

Start a Program• Start Debugging

• เป0นการส��งให้(โปรแกรมทำ�างานแบบแสดงข�7นต์อนการทำ�างานทำ�ละข�7น

• ก*อนเร��มการทำ�างานในโห้มดน�7 เราต์(องระบ2บรรทำ�ดทำ��ต์(องการให้(โปรแกรมห้ย2ดการทำ�างานช��วคราว โดยการคล�กเล$อกห้น(าบรรทำ�ดทำ��ต์(องการให้(โปรแกรมห้ย2ดรอ• สามารถคล�กเล$อกได(มากกว*า 1 บรรทำ�ด

31

Page 32: Week 1 Introduction to  Computer Programming and IDE

Start a Program• Start Debugging (cont.)

• เร��มทำ�างานในโห้มด debug ด(วยค�าส��ง Execute >> Debug ห้ร$อ F5 ห้ร$อกดเล$อกป29ม เม$�อเร��มการทำ�างานในโห้มดน�7 โปรแกรมจะเร��มทำ�างานแล(วไปห้ย2ดทำ��บรรทำ�ดแรกทำ��เราเล$อกให้(ห้ย2ดการทำ�างานช��วคราว

32

Page 33: Week 1 Introduction to  Computer Programming and IDE

Start a Program• Start Debugging (cont.)

• การส��งให้(โปรแกรมทำ�างานต์*อไป จะสามาถทำ�าได(โดยอาศู�ยป29มต์*างๆบนDebug Toolbar ส*วนล*างของห้น(าจอ

33

Page 34: Week 1 Introduction to  Computer Programming and IDE

• Start Debugging (cont.)• การส��งให้(โปรแกรมทำ�างานต์*อไป จะสามาถทำ�าได(โดยอาศู�ยป29มต์*างๆบนDebug Toolbar ส*วนล*างของห้น(าจอ เช*น

ใช(เม$�อต์(องการให้(ทำ�างาน (execute) ค�าส��งในบรรทำ�ดป<จจ2บ�น และไปห้ย2ดรอ

ทำ��บรรทำ�ดถ�ดไป ใช(เม$�อต์(องการให้(ทำ�างานค�าส��งป<จจ2บ�น แล(วไปห้ย2ดรอ ณ บรรทำ�ดต์*อไปทำ��เรา

เล$อกให้(ห้ย2ด ใช(เม$�อต์(องการห้ย2ดการทำ�างานโปรแกรมในโห้มดการทำ�างานน�7

Start Program

34

Page 35: Week 1 Introduction to  Computer Programming and IDE

• Start Debugging (cont.)• เม$�อม�การใช( Next line ในบรรทำ�ดทำ�� 7 จะทำ�าให้(โปรแกรมทำ�างานบรรทำ�ดด�งกล*าว แล(วจ3งห้ย2ดรออย1*ทำ��ต์(นบรรทำ�ดทำ�� 8

Start Program

35

Page 36: Week 1 Introduction to  Computer Programming and IDE

Summary• ว�ชาน�7ใช(ภาษา C++ ในการเร�ยนการสอน• ว�ชาน�7ใช( Dev-C++ (version 5.8.3) เป0นเคร$�องม$อ (IDE) ในการเข�ยนโปรแกรม

• ข�7นต์อนการเข�ยน code ทำ�าได(ด�งต์*อไปน�7 • สร(าง source file ให้ม*จาก File >> New… >> Source file ห้ร$อ Ctrl+N

• แปลโค(ดให้(เป0นภาษาคอมพ�วเต์อร�ด(วยการ Compile โดยการเล$อก Execute >> Compile ห้ร$อ F9 ห้ร$อเล$อกป29ม

• ส*งให้(โปรแกรมทำ�างานแบบปกต์�ด(วยค�าส��ง Run โดยการเล$อก Execute >> Run ห้ร$อ F10 ห้ร$อเล$อกป29ม

• ส*งให้(โปรแกรมทำ�างานทำ�ละข�7นต์อนด(วยค�าส��ง Debug โดยการเล$อก Execute >> Run ห้ร$อ F10 ห้ร$อเล$อกป29ม เพ$�อประโยชน�ในการต์รวจสอบความถ1กต์(องของโปรแกรมทำ�ละส*วน

36

Page 37: Week 1 Introduction to  Computer Programming and IDE

Summary• C++ Commands:

• cout - ใช(แสดงข(อความ และค*าต์*างๆออกทำางห้น(าจอ• ข(อความต์(องอย1*ภายใต์(เคร$�องห้มายค�าพ1ด “ ” เสมอ• ใช(ประกอบก�บเคร$�องห้มาย ‘<<‘ ซ3�งสามารถใช(เพ$�อน�าค*า ห้ร$อ

ข(อความอ$�นๆมาเช$�อมต์*อก�น (concatenation)• เม$�อแทำรก ‘\n’ เข(าไปในข(อความ จะทำ�าให้(ม�การข37นบรรทำ�ดให้ม* (newline)

• เม$�อแทำรก ‘\t’ เข(าไปในข(อความ จะทำ�าให้(ม�การแทำ4บ (tab)• เม$�อใช(ค�าส��ง endl ร*วมด(วย จะทำ�าให้(ม�การข37นบรรทำ�ดให้ม* (newline)

• cin - ใช(ในการน�าข(อม1ลทำ��ผ1(ใช(ป>อนผ*านค�ย�บอร�ด เข(ามาเก4บไว(ทำ��ต์�วแปร• ใช(ประกอบก�บเคร$�องห้มาย ‘>>’ เพ$�อช�7ไปย�งต์�วแปรทำ��ต์(องการ

ใช(เก4บค*า เช*น37

int age, budget;cin >> age; //store input from kyb. to ‘age’cin >> budget; //store (another) input from kyb. to ‘budget’

Page 38: Week 1 Introduction to  Computer Programming and IDE

Useful Links• C++ Tutorial

• อ(างอ�งการเก��ยวก�บโครงสร(างภาษา และการใช(งานค�าส��งต์*างๆ

• ม�ประโยชน�ต์*อการทำ�าแบบฝึ@กห้�ด การทำดลอง และการเต์ร�ยมสอบ

• http://www.cplusplus.com/doc/tutorial/

• Dev-C++• ส�าห้ร�บดาวน�โห้ลด • http://sourceforge.net/projects/orwelldevcpp/

38

Page 39: Week 1 Introduction to  Computer Programming and IDE

Labs• Lab 1.1 - ให้(นศู.ทำดลอง Debug โปรแกรมต์�วอย*างโดย• ระบ2ให้(โปรแกรมห้ย2ดการทำ�างานรอ ณ บรรทำ�ดทำ�� 5, 7, 11, 13

• ให้( Next line ไปจนกว*าโปรแกรมจะจบการทำ�างาน• พ�จารณาการทำ�างาน และการแสดงผลการทำ�างาน

• Lab 1.2 - ให้(นศู.ทำดลอง Debug โปรแกรมต์�วอย*างโดย• ระบ2ให้(โปรแกรมห้ย2ดการทำ�างานรอ ณ บรรทำ�ดทำ�� 7, 11, 13

• ให้( Continue ไปจนกว*าโปรแกรมจะจบการทำ�างาน• พ�จารณาการทำ�างาน และการแสดงผลการทำ�างาน

39

Page 40: Week 1 Introduction to  Computer Programming and IDE

Labs• Lab 1.3 - ให้(นศู. เข�ยนโปรแกรมเพ$�อให้(โปรแกรมแสดงผลล�พธิ�ด�งทำ��ก�าห้นดด(านล*าง

• นศู. สามารถเข�ยนโปรแกรมให้ม*น�7โดยการแก(ไขห้ร$อเข�ยนทำ�บไฟล�ต์�วอย*าง ห้ร$อสามารถสร(างโซล1ช�นให้ม*เพ$�อแก(ป<ญห้าน�7

40