CT 2101 Procedural Programming - Ramkhamhaeng...
Transcript of CT 2101 Procedural Programming - Ramkhamhaeng...
![Page 1: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/1.jpg)
CT 2101 Procedural Programming
โดย ผศ.ดร.อไร ทองหวไผ
![Page 2: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/2.jpg)
เนอหารายวชา
น าความรเกยวกบการสรางอลกอรทมและการเขยนโปรแกรมมาใชงานโดยเนนการออกแบบและการสรางโปรแกรมขนาดใหญ การออกแบบโปรแกรมแบบบนลงลาง แบบลางขนบน ศกษาโครงสรางขอมลเบองตน เชนรายการโยง สแตค คว ทร และแฟม การท าขอมลเปนนามธรรม เพอสรางสวนประกอบโปรแกรมในรปสวนจ าเพาะ การเขยนโปรแกรมเรยกตวเอง การเรยงล าดบและการคนหาขอมลแบบทวไป ปญหาทางทฤษฏของวทยาการคอมพวเตอรทเกยวของกบการเขยนโปรแกรม ความเขาใจเบองตนเกยวกบวศวกรรมซอฟตแวร เชนคณสมบตของโปรแกรมทด วธการทดสอบความถกตองของโปรแกรม วธแกไขความผดพลาดในโปรแกรม วธการท างานแบบเปนทมและวธการบรหารทมงานเขยนโปรแกรมเบองตน
![Page 3: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/3.jpg)
PR.
เนอหารายวชานเชอมโยงมาจากรายวชา COS1101 การออกแบบโปรแกรม โดยผเรยนตองมพนฐานความรในการออกแบบโปรแกรมเปนอยางด สามารถออกแบบแนวคดในการแกปญหาโปรแกรมเบองตนได
![Page 4: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/4.jpg)
วตถประสงค
มงเนนใหนกศกษามความร ความเขาใจ และมความสามารถในการจดการดานขอมลเบองตนได
มความสามารถในการวเคราะหระบบงานและเขยนโปรแกรมไดอยางถกตองตามหลกไวยากรณภาษา
ผเรยนสามารถทดสอบและแกไขความผดพลาดของโปรแกรมกอนน าไปใชงานได
นอกจากนผเรยนสามารถท างานเปนทม และมวธการบรหารทมงานเขยนโปรแกรมเบองตนได
![Page 5: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/5.jpg)
เนอหา
Structure Programming
Sequence
Condition
Repetition
Array
Record
Recursive
File
![Page 6: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/6.jpg)
เนอหา(ตอ)
String
Pointer
Data structure
Stack
Queue
Linked-list
Tree
![Page 7: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/7.jpg)
คอมพวเตอรคออะไร?
อปกรณทใชส าหรบประมวลผล องคประกอบหลกทส าคญของคอมพวเตอรประกอบดวย 2 สวนคอ
ฮารตแวร และซอฟตแวร ฮารตแวรคออปกรณอเลคโทรนกสตางๆทใชในการปฏบตการ ซอฟตแวรเปนโปรแกรมหรอชดค าสงทใชในการตดตอกบฮารตแวรนนเอง โดยการตดตอระหวางกนใชภาษาโปรแกรมทเครองคอมพวเตอรเขาใจ
เรยกวาภาษาเครอง (machine language) ซงประกอบดวยเลขฐานสอง
![Page 8: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/8.jpg)
การประมวลผลโปรแกรมภาษาระดบสง
![Page 9: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/9.jpg)
การประมวลผลโปรแกรมคดคาน า
ขนตอนท1 โปรแกรมภาษาเครองในการคดคาน า และขอมลทใชในการประมวลผลคอจ านวนของยนคน าทใช จะถกเกบในหนวยความจ า
ขนตอนท2 โปรแกรมจะท าการประมวลผลโดยน าจ านวนของยนคของน ามาค านวณหาคาน าทตองจาย และเกบผลลพธไวในหนวยความจ า
ขนตอนท3 น าผลลพธทค านวณเกบไวในหนวยความจ า แสดงผลออกมาเปนบลคาน า
![Page 10: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/10.jpg)
การประมวลผลโปรแกรมคดคาน า
![Page 11: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/11.jpg)
software development method
ระบถงความตองการ (problem) วเคราะหปญหา (analysis) ออกแบบอลกอรทมเพอแกปญหา (design) สรางโปรแกรม (implementation) ทดสอบและตรวจสอบความสมบรณของโปรแกรม (testing) บ ารงรกษาและแกไขโปรแกรม (maintenance)
![Page 12: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/12.jpg)
ระบถงความตองการ (problem)
ผพฒนาสามารถสอบถามไดจากบคคลตางๆทเกยวของ ความตองการทดตองชดเจน ไมคลมเครอ มความจ าเปน สามารถทวนสอบ
หรอตรวจสอบได ตดตามได
![Page 13: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/13.jpg)
วเคราะหปญหา (analysis) ตอจากนนจะน าความตองการตางๆเหลานมาวเคราะหถงหนทางในการ
แกปญหา
โดยน าปญหาตางๆเหลานมาก าหนดเปนนยาม(abstraction)ของ ขอมลเขา ขอมลน าออก การค านวณ เงอนไขในการปฏบตการตางๆ
![Page 14: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/14.jpg)
ออกแบบอลกอรทมเพอแกปญหา (design) โดยมการแบงปญหาออกเปนปญหายอยๆเพอใหงายตอการพฒนาซงเรยกวา
top-down design หรอ divide and conquer ในแตละปญหายอยจะก าหนดเปนล าดบขนตอนในการแกปญหาโดยละเอยด
เรยกวา algorithm refinement
Tools
Flowchart
Structure Chart
Stepwise refinements (Pseudo code)
![Page 15: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/15.jpg)
สรางโปรแกรม (implementation)
เขยนเปนภาษาโปรแกรมใหถกตามไวยากรณของภาษานนๆ ภาษา C++
Syntax
Semantic
Algorithm
Goal
Context
![Page 16: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/16.jpg)
ทดสอบและตรวจสอบความสมบรณของโปรแกรม (testing) ทดสอบโดยก าหนดขอมลทดสอบหลายๆชด เพอใหแนใจวาการท างาน
ถกตอง และสามารถน าไปปฏบตการในการแกปญหาได
Test Case
Path
Statements
Valid data
Invalid data
![Page 17: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/17.jpg)
บ ารงรกษาและแกไขโปรแกรม (maintenance)
ปองกนและแกไขขอผดพลาดทอาจเกดขนในขณะท างาน
Corrective maintenance
Adaptive maintenance
Perfective manintenance
Preventive maintenance
![Page 18: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/18.jpg)
กรณศกษา : การแปลงไมลเปนกโลเมตร
ปญหา ตองการแปลงระยะทางทเปนไมลใหเปนกโลเมตร
วเคราะห ตองหาขอมลวาอะไรคอขอมลเขา อะไรคอผลลพธทตองการ และตองมการประมวลผลอยางไร ในทนขอมลเขาคอระยะทางทเปนไมล ผลลพธทตองการคอระยะทางทเปนกโลเมตร ส าหรบการประมวลผลเปนการค านวณทตองทราบวา 1 ไมลมคาเทากบ 1.609 กโลเมตร ตอจากนนนยามขอมลตางๆเหลานเพอใหสามารถเกบในหนวยความจ าของเครองคอมพวเตอรได
![Page 19: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/19.jpg)
วเคราะห
DATA REQUIREMENTS
Problem Input
miles ระยะทางเปนไมล
Problem Output
kms ระยะทางเปนกโลเมตร
Formular
1 ไมล = 1.609 กโลเมตร
![Page 20: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/20.jpg)
ออกแบบ
ALGORITHM WITH REFINEMENTS
1. รบระยะทางเปนไมล 2. แปลงระยะทางจากไมลเปนกโลเมตร 2.1 ระยะทาง 1 ไมลมคาเทากบ 1.609 กโลเมตร 3. แสดงระยะทางเปนกโลเมตร
![Page 21: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/21.jpg)
เขยนโปรแกรม
//miles.cpp
//Coverts distance in miles to kilometers.
#include <iostream>
using namespace std;
int main() //start of main function
{
const float KM_PER_MILE=1.609 //1.609 km in a mile
float miles, //input : distance in miles
kms; //output: distance in kilometers
//Get the distance in miles.
cout << “Enter the distance to miles: ”;
cin >> miles;
// Convert the distance to kilometers.
kms = KM_PER_MILE * miles;
// Display the distance to kilometers.
cout << “ The distance in kilometers is ” << kms <<endl;
return 0; }
![Page 22: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/22.jpg)
ทดสอบ
![Page 23: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/23.jpg)
คณภาพของซอฟตแวร
![Page 24: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/24.jpg)
คณภาพของซอฟตแวร
![Page 25: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/25.jpg)
How to Build Quality Programs
![Page 26: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/26.jpg)
รปแบบทวไปของโปรแกรม C++
compiler directives
using namespace std;
void main()
{
declaration statements;
executable statements;
}
![Page 27: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/27.jpg)
//miles.cpp
//Coverts distance in miles to kilometers.
-----------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
-----------------------------------------------------------------------------------------------------
int main() //start of main function
{
const float KM_PER_MILE=1.609 //1.609 km in a mile
float miles, //input : distance in miles
kms; //output: distance in kilometers
------------------------------------------------------------------------------------------------------
//Get the distance in miles.
cout << “Enter the distance to miles: ”;
cin >> miles;
// Convert the distance to kilometers.
kms = KM_PER_MILE * miles;
// Display the distance to kilometers.
cout << “ The distance in kilometers is ” << kms <<endl;
return 0; } -------------------------------------------------------------------------------------------------------
![Page 28: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/28.jpg)
![Page 29: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/29.jpg)
![Page 30: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/30.jpg)
![Page 31: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/31.jpg)
![Page 32: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/32.jpg)
ค าเฉพาะ (Reserved Words)
![Page 33: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/33.jpg)
ชอ(Identifiers)
ขนตนดวยตวอกษรภาษาองกฤษ หรอเครองหมาย underscore (ขดลาง) ภาษา C++ ถอวาอกษรภาษาองกฤษตวเลกกบตวใหญเปนคนละตวกน เชน Rate , rate , RATE เปนตน
ตวตอไปอาจเปนตวเลข ตวอกษร หรอ ขดลางกได เชน Letter1 ,
letter1 , Letter_1 หามใชค าเฉพาะ เปนชอทแทนความหมายอนใด
![Page 34: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/34.jpg)
ชนดของขอมล(Data Types)
Integers : เลขจ านวนเตม เปนเลขทไมมเศษ เชน 5 , -52 , 343222
โดยชนดขอมลทแทนเลขจ านวนเตมไดแก short , int , long
Real numbers: เลขทศนยม เชน 2.5 , –0.00034 ,
3.66666666 , 5.0 เปนตน ชนดขอมลทแทนเลขทศนยมไดแก float ,
double , long double
Boolean : เปนขอมลทมคา true หรอ false ซงเปนคาทไดจากการเปรยบเทยบดวยค าสงเงอนไข ชนดขอมลทแทนคาบลน คอ ขอมลชนด bool
Character : เปนขอมลทเกบอกขระ 1 ตวอกษรซงอาจเปนอกขระทเปนตวเลข ตวอกษรภาษาองกฤษ หรออกขระพเศษ กได ขอมลชนดนไดแก char
![Page 35: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/35.jpg)
String Class
เปนคลาสทภาษา C++ เตรยมไวใหกบโปรแกรมเมอรส าหรบการใชขอมลทเปนขอความ โดย ขอความคอการน าอกขระมาเรยงตอๆกนเปนกลมค า อทเชน “A” “1234” “Enter the distance in miles “ เปนตน
ผใชสามารถอาน บนทก เปรยบเทยบ รวม หรอคนหา ขอความตางๆในการปฏบตงานได
โดยตองมค าสง #include <string> ในโปรแกรมกอนใชงาน
![Page 36: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/36.jpg)
![Page 37: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/37.jpg)
![Page 38: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/38.jpg)
ค าสงก าหนดคา(Assignment Statement)
ใชสญลกษณเทากบ = เปนการใหแกตวแปร kms = KM_PER_MILE * miles ;
![Page 39: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/39.jpg)
![Page 40: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/40.jpg)
![Page 41: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/41.jpg)
การท างานของค าสง cin>>miles
![Page 42: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/42.jpg)
ค าสง cin >> letter1 >> letter2 >>lastName ;
ก าหนดให ตวแปร letter1 และ letter2 เปนขอมลชนด char
และ lastName เปนขอมลชนด string
![Page 43: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/43.jpg)
![Page 44: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/44.jpg)
![Page 45: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/45.jpg)
ตวอยางการพมพขอความทกทาย
//File.hello.cpp
//Display a user’s name
#include <iostream>
#include <string>
using namespace std;
int main()
{
char letter1,letter2 ; //input and output :first two initials
string lastName; //input and output :last name
//Enter letters and print message.
cout << “ Enter 2 initials and a last name : ” ;
cin >>letter1 >>letter2 >> lastName;
cout << “Hello” << letter1 << “ . ” << letter2 << “ . ”
<< lastName << “ ! ” ;
cout << “ We hope you enjoy studying C++.” << endl ;
return 0; }
![Page 46: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/46.jpg)
นพจนคณตศาสตร(Arithmetic Expression)
![Page 47: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/47.jpg)
![Page 48: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/48.jpg)
กฎการค านวณของนพจนคณตศาสตร
เมอพบวงเลบ () จะกระท าวงเลบในสดกอน และกระท าเปนล าดบจนถงวงเลบนอกสด
กรณทมตวกระท าหลายตวมล าดบของการกระท าดงน
unary + , - กระท ากอน
* , / , % กระท าตอมา + , - สดทาย กรณทมล าดบของการกระท าอยในระดบเดยวกนเชน + , - จะกระท าตว
ทพบกอน หรอกระท าจากซายไปขวา
![Page 49: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/49.jpg)
ตองการการหาคา V = (p2 – p1) / ( t2 - t1) ;
![Page 50: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/50.jpg)
ตองการการหาคาของ m = x + k / 2
![Page 51: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/51.jpg)
ขอผดพลาดในการเขยนโปรแกรม
Syntax Errors
Run-Time-Errors
Logic Errors
![Page 52: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/52.jpg)
Syntax Errors
เปนความผดพลาดทสามารถตรวจพบในขณะแปลโปรแกรม เปนความผดทางไวยากรณภาษา ซงการเขยนค าสงโปรแกรมไมสมบรณ มการพมพตกหลน หรอผดพลาด ซงตวแปลภาษาตรวจพบและแสดงขาวสารใหผเขยนโปรแกรมทราบ
[C++ Error] miles.cpp (12) : E2141 Declaration syntax error.
[C++ Error] miles.cpp (15) : E2380 Unterminated string or character constant.
[C++ Error] miles.cpp (16) : E2379 Statement missing ; .
[C++ Error] miles.cpp (19) : E2451 Undefined symbol ‘kms’.
[C++ Warning] miles.cpp (25) : W8080 ‘miles’ declared but never used.
[C++ Warning] miles.cpp (12) : W8004 ‘KM_PER_MILE’ is assigned a value that is never used.
![Page 53: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/53.jpg)
Run-Time-Errors เปนความผดพลาดทเกดขนในขณะปฏบตการ (run) โดยไมสามารถ
ปฏบตงานตามค าสงโปรแกรมได อทเชน มการหารคาดวย 0 เปนตน เมอมความผดพลาดลกษณะนเกดขน โปรแกรมจะหยดการประมวลผลทนท พรอมแสดงบรรทดทผดพลาดรวมทงขอความทอธบายถงความ ผดพลาดทเกดขนใหกบโปรแกรมเมอรไดทราบ เพอใหโปรแกรมเมอรแกไขตอไป
ความผดพลาดทเกดขนโดยปรากฎดงนคอ
“Floating point division by zero”
![Page 54: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/54.jpg)
Logic Errors
เปนความผดพลาดของผลลพธ ซงมผลมาจากอลกอรทมในการแกปญหาโปรแกรม ซงยากทจะคนพบ ตองใชวจารณญาณ ความเชยวชาญของโปรแกรมเมอรในการคนหาและแกไข
ความผดพลาดนอาจเกดจาก การเขยนนพจนทางคณตศาสตรไมถกตอง มการใชเงอนไขในการท างานผดพลาด หรอการลมก าหนดคาเรมตน ส าหรบปฏบตงานเปนตน
![Page 55: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/55.jpg)
แบบฝกหด 1
จงเขยนโปรแกรมหาพนทของสามเหลยมหนาจว สตร
พนท = ½ * ความยาวฐาน * สวนสง
![Page 56: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/56.jpg)
แบบฝกหด 2
จงเขยนโปรแกรมหาคาเฉลยของเลขจ านวนเตมใดๆ 5 จ านวน
![Page 57: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/57.jpg)
แบบฝกหด 3
จงเขยนโปรแกรมในการแปลง จ านวนหลา จ านวนฟต และจ านวนนว ใดๆ ใหเปนนวทงหมด เชน ปอน 2 หลา 2 ฟต 4 นว ผลคอ 100 นว
![Page 58: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/58.jpg)
แบบฝกหด 4
จงแปลง นว ใหเปน จ านวนหลา จ านวนฟต และจ านวนนว
![Page 59: CT 2101 Procedural Programming - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_1.pdf · ส่วนประกอบโปรแกรมในรูปส่วนจาเพาะ](https://reader035.fdocuments.us/reader035/viewer/2022070704/5e887b035f185f6473698937/html5/thumbnails/59.jpg)
การบาน
1. จงแปลง ชวโมง นาท วนาท ใหเปนวนาททงหมด 2. ปอนจ านวนเงนทเปนเศษเหรยญทางแปนพมพ โปรแกรมสามารถน าไปหา
จ านวนแบงคทนอยทสดสามารถแลกไดจากธนาคาร โดยแลกจากแบงคใหญจนกระทงถงแบงคทนอยทสด
3. จงหาคายกก าลง 2 , ยกก าลง 3 และคายกก าลง 4 ของเลขใดๆ