2 3 using namespace std

Download Report

Transcript 2 3 using namespace std

886201
หลักการโปรแกรม 1
Lecture 1: ความรูเบื
้ งตนเกี
ย
่ วกับ
้ อ
้
การโปรแกรม
โปรแกรม
• โปรแกรม คือ รูปแบบทางตรรกะของคาสั่ งตางๆ
ทีน
่ ามา
่
รวมกันเพือ
่ แกปั
และนามาใช้สั่ งให้
้ ญหาเฉพาะบางอยาง
่
คอมพิวเตอรท
ผู้เขียนโปรแกรมจะเขียนโปรแกรมตาม
์ างาน
ความตองการของนั
กวิเคราะหระบบ
หรืออาจกลาวได
สั
้
่
้ ้ นๆ วา่
์
โปรแกรมหมายถึง ชุดคาสั่ งซึง่ ถูกเขียนขึน
้ เพือ
่ สั่ งให้
คอมพิวเตอรท
้
์ างานตามตองการ
The Electronic Numerical Integrator And Computer
(The ENIAC)
ตัวแปลภาษา
Edit-Compile-Run
กระบวนการพัฒนาโปรแกรมที่
โปรแกรมเมอร์ ทกุ คนต้ องทา
(เหมือนเป็ นวงจรชีวิต) ก็คือ
การเขียน/แก้ ไข โปรแกรม
การคอมไพล์
และการรันโปรแกรม
ภาษา C++
•
•
•
•
•
C (1972)
ANSI Standard C (1989)
C++ (1985) Bjarne Stroustrup
ANSI Standard C++ (1998)
ANSI Standard C++ [revised]
(2003)
IDE - CodeBlocks
เริม
่ ตนเขี
ยนโปรแกรม
้
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
เริม
่ ตนเขี
ยนโปรแกรม
้
ข้อผิดพลาดทีม
่ ก
ั พบบอย
่
1
2
3
4
5
6
7
8
9
Oh No!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl
return 0;
}
ข้อผิดพลาดทีม
่ ก
ั พบบอย
่
1
2
3
4
5
6
7
8
9
Oh No!
#include <iostream>
using namespace std;
int Main()
{
cout << "Hello, World!" << endl;
return 0;
}
ข้อผิดพลาดทีม
่ ก
ั พบบอย
่
1
2
3
4
5
6
7
8
9
Oh No!
#include <iostream>
using namespace std;
ใช้ ตัว ‘แอล’ ไม่ ใช่
int main()
เลขหนึ่ง
{
cout << "Hello, World!" << end1;
return 0;
}
ข้อผิดพลาดทีม
่ ก
ั พบบอย
่
1
2
3
4
5
6
7
8
9
Oh No!
#include <iostream>
using namespace std;
ใช้ เครื่ องหมาย <<
ไม่ ใช่ >>
int main()
{
cout >> "Hello, World!" << end1;
return 0;
}
การเขียนโปรแกรมให้อานง
าย
่
่
นิสิตรูสึ้ กอยางไรกั
บรูปแบบการเขียนโปรแกรม
่
แบบนี้ ?
int main(){cout<<"Hello,
World!"<<endl;return 0;}
การเขียนโปรแกรมให้อานง
าย
่
่
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
Learn by
Example
ตัวอยางโปรแกรม
1
่
1 #include <iostream>
2
3 using namespace std;
4
สั งเกตุวาไม
มี
คาสั่ ง
่
่
5 int main()
endl
6 {
7
cout << "Hello, ";
8
cout << "World!" << endl;
9
return 0;
10}
ตัวอยางโปรแกรม
2
่
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7
cout << "Hello, " << endl;
8
cout << "World!" << endl;
9
return 0;
10}
สั งเกตุวามี
่ คาสั่ ง
endl
ตัวอยางโปรแกรม
3
่
1 #include <iostream>
2
3 using namespace std;
4
ลองสั งเกตุ
5 int main()
เครือ
่ งหมายคาพูด
6 {
7
cout << "Test: ";
8
cout << "1 + 2" << endl;
9
return 0;
10}
ตัวอยางโปรแกรม
4
่
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7
cout << "Test: ";
8
cout << 1 + 2 << endl;
9
return 0;
10}
สั งเกตุวา่ ไมมี
่
เครือ
่ งหมายคาพูด
ตัวอยางโปรแกรม
5
่
1 #include <iostream>
2
3 using namespace std;
สั งเกตุวาใช
4
่
้
เครือ
่ งหมาย ‘\’ 2 อัน
5 int main()
6 {
7
cout << "
/ \\" << endl;
8
cout << "
/
\\" << endl;
9
cout << " /
\\" << endl;
10
cout << " -------" << endl;
11
return 0;
12}
แบบฝึ ก
หัด
โปรแกรมตอไปนี
้แสดงผลลัพธอะไรออก
่
์
ทางหน้าจอ
โปรแกรมตอไปนี
้แสดงผลลัพธอะไรออก
่
์
ทางหน้าจอ
โปรแกรมตอไปนี
้แสดงผลลัพธอะไรออก
่
์
ทางหน้าจอ
โปรแกรมตอไปนี
้ผด
ิ พลาดตรงตาแหน่ง
่
ใด
โปรแกรมตอไปนี
้ ผด
ิ พลาดตรงตาแหน่ง
่
ใด
ลองเขียนโปรแกรมเพือ
่ พิมพรู์ ปตอไปนี
้
่
ออกทางหน้าจอ