การเขียนโปรแกรมภาษา C++

Download Report

Transcript การเขียนโปรแกรมภาษา C++

โปรแกรมภาษา C++
วิทยากร : เนตรนภา โสดาดี
E-mail : [email protected]
++
การเขียนโปรแกรมภาษา C
C++ เป็ นภาษาระดับสู ง ใช้ สาหรับการ
เขียนโปรแกรม ผู้สร้ าง C++ คือ บจาร์ น สโตรสตรัป
(Bjarne Stroustrp)
++
++
C พัฒนามาจากภาษา C โดย C
ประกอบด้ วยทุกสิ่ งทีม่ ใี นภาษา C และส่ วนที่เพิม่ เติม
เข้ ากับภาษา C ดังนั้นเราจึงเขียนโปรแกรมภาษา C
++
ใน C ได้
++
ความสั มพันธ์ ของ C และ C
ส่ วนประกอบอืน่
OOP
C++
ส่ วนประกอบที่
ใช้ ร่วมกันใน
C++ และ C
C
ส่ วนประกอบที่
ใช้ เฉพาะใน C
การใช้
C:\>cd tc
C:\TC >
C:\TC > tc
++
Turbo C
หรื อเรี ยกใช้ผา่ นทาง
โปรแกรม Windows
ขั้นตอนการเขียนโปรแกรม
การ Compile
การคอมไพล์ ซอร์ สโค้ ด เพือ่
ตรวจสอบไวยากรณ์ ของภาษาโดยไม่ มี
การลิงก์
• เมนู Compile
• เลือก Compile
หรือ <Alt+F9>
การ Run
หมายถึ ง การคอมไพล์ ซ อร์ ส โค้ ด และ
ไฟล์ อื่ น ที่ ใ ช้ ร่ วมกั น ทุ ก ไฟล์ และเมื่ อ
คอมไพล์ เสร็ จแล้ ว จะมีการลิงก์ อัต โนมัติ
และรันโปรแกรมนั้นทันที
• เมนู Run
• เลือก Run
หรือ <Ctrl+F9>
โครงสร้ างภาษา
#include <header file>
identify
function()
main()
{
Statement;
}
ชนิดของข้ อมูล
Type
unsigned char
char
unsigned int
int
unsigned long
long
float
double
Length(bits)
8
8
16
16
32
32
32
64
Range
0 to 255
-128 to 127
0 to 65,535
-32768 to 32767
0 to 4,294,967,295
-2,147,483,648 to
3.4 10-38 to 3.4 1038
1.7 10-308 to 1.7 10308
กฏการตั้งชื่อ
• ขึ้นต้นด้วยอักขระ A - Z หรื อ a - z
• ตัวถัดไปอาจเป็ นอักขระ (A - Z , a - z) หรื อ ตัวเลข 0 9 หรื อเครื่ องหมาย อันเดอร์บาร์ ( _ )
• ห้ามเว้นวรรค
• ตัวอักษรพิมพ์เล็กและ ตัวอักษรพิมพ์ใหญ่ ถือว่าต่างกัน
• ห้ามใช้คาสงวนในการตั้งชื่อ (reserved word)
การประกาศตัวแปร
DataType VariableName;
Ex
int a;
float x,y;
ค่ าคงที่ (Constant)
const datatype identify = value;
Ex
const float Pi = 3.1415;
ตัวอย่ าง
#include <header file>
const float Pi = 3.14;
ภายนอก >> int a;
main()
{
int num1,num2 = 5;
ภายใน >> char f_name;
}
ตัวดาเนินการ (Operator)
• Operator ทางคณิตศาสตร์
• Operator ทางการเปรียบเทียบ
• Operator ทางตรรกะ
Operator ทางคณิตศาสตร์
•
•
•
•
•
บวก (+)
ลบ (-)
คูณ (*)
หาร (/)
หารเอาเศษ (%)
เช่ น 5 / 2=2 or 2.5
เช่ น 5%2=1
การคานวณ
• 5%2 +6/3-4 = ?
• (4-2*1+6)/5%3 = ?
• 5+(6/3*2)-4 = ?
ลาดับการคานวณ
• ()
•%
• * , / เจอก่อนทาก่อน
• + , - เจอก่อนทาก่อน
Operator ทางคณิตศาสตร์
การเพิม่ ค่ า (++)
การลดค่ า (--)
Ex
a=a+1
a=a-1
b=b+2
เช่ น a++
เช่ น a- a++
 a- b+=2
Operator ทางการเปรียบเทียบ
เท่ากับ
 ==
มากกว่า
>
มากกว่าหรื อเท่ากับ  >=
น้อยกว่า
<
น้อยกว่าหรื อเท่ากับ  <=
ไม่เท่ากับ
 !=
Operator ทางตรรกะ
and
or
not



&&
||
!
การเปรียบเทียบ
•
•
•
•
•
&&
T && T = T
T && F = F
F && T = F
F && F = F
•
•
•
•
•
||
T || T = T
T || F = T
F || T = T
F || F = F
การเปรียบเทียบ
• 3 + 4 * 2 >= 4 + 2 * 3 เป็ น
• 3 + 2 <= 2 * 3 && 4 != 5 % 1 เป็ น
• 6 < 3 * 2 && 1+ 4 >= 5 || 1+2= =3 เป็ น
ลาดับการเปรียบเทียบ
•
•
•
•
•
!
> , >= , <= , <
= = ,!=
&&
||
คาสั่ งในการแสดงข้ อมูล
ใช้ iostream.h
cout << variable or “String”;
#include " iostream.h "
void main( )
{
cout << " Welcome to Siam Computer "
<<endl < ขึน้ บรรทัดใหม่
<< " Bye… ";
}
#include " iostream.h "
void main( )
{ int n;
cout<<“Input Data :”<<endl;
n = 5*3;
cout<< “ Value= ”<< n;
}
#include " iostream.h "
void main( )
{ int a,b;
a = 3;
b = 7;
<กาหนดตัวแปรดังนี้
ผลการทางานที่ต้องการ
Input Value1: 3
Input Value2: 7
Sum = 10
Minus = -4
cout<<"Input Value1 :“<<a;
cout<<"Input Value2 :“<<b;
cout<<“Sum = "<<a+b<<endl;
cout<<“Minus= "<<a-b<<endl;
}
ผลการทางานที่ต้องการ
Input Value1: 3
Input Value2: 7
Sum = 10
Minus = -4
(3*7)-(3+7) = 11
คาสั่ งในการรับข้ อมูล
ใช้ iostream.h
cin >> variable ;
Ex
int a , b ;
float c ;
cin >> c;
cin >> a >> b ;
ผลการทางานที่ต้องการ
Input Value1: 5
Input Value2: 9
Sum = 14
[enter]
[enter]
#include "iostream.h"
void main ( )
{ int num1,num2;
cout<<"Input Value1 :";
cin>>num1;
cout<<"Input Value2 :";
cin>>num2;
cout<<"sum = "<<num1+num2<<endl;
}
การ Clear จอภาพ
ใช้ conio.h
clrscr ( );
การรับอักขระ 1 อักขระโดยไม่ แสดงทีจ่ อภาพ
variable = getch ( ) ;
Or
getch ( ) ;
ผลการทางานที่ต้องการ
Input two number : 5 2
5+2 =7
5–2 =3
5 * 2 = 10
5 /2 =2
5%2 =1
[enter]
#include"iostream.h"
#include"conio.h"
void main( )
{ int num1, num2;
clrscr( ); cout << "Input two number: ";
cin >> num1 >> num2;
cout<<num1 << " + "<< num2 << " = " << num1 + num2 <<endl
<<num1 << " - " << num2 << " = " << num1 - num2 <<endl
<<num1 << " * " << num2 << " = " << num1 * num2 <<endl
<<num1 << " / " << num2 << " = " << num1 / num2 <<endl
<<num1 << " % "<< num2 << " = " << num1 % num2;
getch( ); }
การกาหนดคาอธิบาย (Comment)
// ข้อความที่จะใช้เป็ นคาอธิบาย ( 1 บรรทัด)
/* กลุ่มข้อความที่ใช้เป็ นคาอธิบาย ( มากกว่า
1 บรรทัด ) */
ผลการทางานที่ต้องการ
Input cost of goods = 433
Input amount = 1000
**************************
small change is 567
Number of 500 Baht = 1
Number of 100 Baht = 0
Number of 50 Baht = 1
Number of 20 Baht = 0
Number of 10 Baht = 1
Number of 5 Baht = 1
Number of 1 Baht = 2
[enter]
[enter]
#include<iostream.h>
#include<conio.h>
void main ( )
{ int change,five_hundred,one_hundred,fifty,twenty,ten,five,one;
float cost,amount;
clrscr( );
cout << "Input cost of goods = ";
cin >> cost;
cout << "Input amount = ";
cin >> amount;
cout <<"**************************"<<endl;
cout << "small change is " << amount-cost << endl;
change=(amount- cost);
five_hundred=change/500;
change=change -(five_hundred*500);
one_hundred=change/100;
change=change -(one_hundred*100);
fifty=change/50;
change=change -(fifty*50);
twenty=change/20;
change=change -(twenty*20);
ten=change/10;
change=change -(ten*10);
five=change/5;
one=change -(five*5);
cout <<"Number of 500 Baht = "<<five_hundred<<endl
<<"Number of 100 Baht = "<<one_hundred<<endl
<<"Number of 50 Baht = "<<fifty<<endl
<<"Number of 20 Baht = "<<twenty<<endl
<<"Number of 10 Baht = "<<ten<<endl
<<"Number of 5 Baht = "<<five<<endl
<<"Number of 1 Baht = "<<one<<endl;
getch ( ) ;
}
Control Statement
โดยปกติการทางานของ คอมพิวเตอร์ จะทางาน
เรี ยงล าดั บ ค าสั่ ง ลงมาตั้ งแต่ ต้ น โปรแกรมจนจบ
โปรแกรม แต่ถา้ เราต้องการเปลี่ ยนแปลงขั้นตอนการ
ทางานของคาสั่ง เช่น โดดข้ามไปทาคาสั่งใดคาสั่งหนึ่ ง
หรื อให้วนกลับมาทาคาสั่งที่เคยทาไปแล้วอี ก ลักษณะ
การสั่งงานแบบนี้ จะต้องใช้คาสั่งควบคุ ม ดังนั้นคาสั่ง
ควบคุมจึงเป็ นคาสั่งที่ใช้เปลี่ยนแปลงลาดับขั้นตอนการ
ทางานของ โปรแกรม
คาสั่ งควบคุม(Control Statement)
• คาสัง่ ควบคุมแบบไม่มีเงื่อนไข เช่น goto
• คาสัง่ ควบคุมแบบมีเงื่อนไข เช่น if , switch
• คาสัง่ ควบคุมให้โปรแกรมทางานซ้ า เช่น for
, while
If Statement แบบที่ 1
if ( condition )
{ statement ; }
Flow-Chart
False
condition
True
Statement ;
ตัวอย่ าง
int n;
void main() {clrscr();
cout<<“Enter number :”; cin>>n;
if(n>=10)
{cout<<n<<“ >=10”<<endl;}
cout<<“Stop Program”;
getch(); }
If Statement แบบที่ 2
if ( condition )
{ statement ; }
else
{ statement ; }
Flow-Chart
False
Statement ;
condition
True
Statement ;
ตัวอย่ าง
int x;
void main() {
cout<<"Enter a integer number:";
cin>>x;
if(x>0)
cout<<"Greater than zero\n";
else
cout<<"Equal to zero\n"; }
ผลการทางานที่ต้องการ
Enter a whole number : 9 [enter]
Your number is odd.
That’s all!
#include"iostream.h"
#include"conio.h"
void main( )
{ int your_number;
clrscr( );
cout << "Enter a whole number: ";
cin >> your_number;
if (your_number % 2 == 0)
cout << "Your number is even\n";
else
cout << "Your number is odd.\n";
cout << "That's all!\n";
getch( ); }
If Statement แบบที่ 3
if ( condition )
{ statement ; }
else if ( condition )
{ statement ; }
else
{ statement ; }
Flow-Chart
condition
true
Statement;
false
false
condition
true
false
Statement; condition
true
Statement; Statement;
ตัวอย่ าง
int sc;
void main() {
cout<<"Enter score :"; cin>>sc;
if(sc<50)
cout<<“Score :”<<sc<<“ Grade : F”;
else if(sc>=50 && sc<=79)
cout<<“Score :”<<sc<<“ Grade : P";
else cout<<“Score :”<<sc<<“ Grade : G";}
ผลการทางานที่ต้องการ
Input your mark: 78
Your grade is B
[enter]
#include"iostream.h"
#include"conio.h"
void main( )
{ int mark;
char grade;
clrscr ( );
cout << "Input your mark: ";
cin >> mark;
if (mark>=80) grade='A';
else if (mark>=70) grade='B';
else if (mark>=60) grade='C';
else if (mark>=50) grade='D';
else grade='F';
cout<<"Your grade is "<<grade;
getch();
}
ผลการทางานที่ต้องการ
Enter Number(0-9): 13 [enter]
No Number
Enter Number(0-9): 7 [enter]
Number is Seven
Switch Statement
switch ( variable )
{ case value1: statement ; break;
case value2 : statement ; break;
default: statement ;
}
Flow-Chart
ค่ าคงที่
true
Statement;
false
ค่ าคงที่
false
ค่ าคงที่
false
Statement;
true
true
Statement;
Statement;
ผลการทางานที่ต้องการ
Enter Number(0-9): 13 [enter]
No Number
Enter Number(0-9): 7 [enter]
Number is Seven
#include"iostream.h"
#include"conio.h"
void main( )
{ int num, result;
clrscr( );
cout<<" Enter Number(0-9): ";
cin>> num;
switch (num)
{ case 0: cout<< " Number is Zero”; break ;
case 1: cout<< " Number is One”; break ;
case 2: cout<< " Number is Two”; break ;
………
case 9: cout<< " Number is Nine”; break ;
default: cout<< " No Number ”;
}
getch( );
}
ผลการทางานที่ต้องการ
Input number1 (+ | - | * | / ) number2 : 5+2 [enter]
Result = 7
#include"iostream.h"
#include"conio.h"
void main( )
{ int num1, num2, result ;
char symbol;
clrscr( );
cout<<"Input number1 (+ | - | * | /) number2 : ";
cin>> num1 >> symbol >> num2 ;
switch (symbol)
{ case '+' : result = num1 + num2; break ;
case '-' : result = num1 - num2; break ;
case '*' : result = num1 * num2; break ;
case '/' : result = num1 / num2; break ;
}
cout<< "Result = “ << result;
getch( );
}
ผลการทางานที่ต้องการ
Input birth year : 2519
You are Drakon year
[enter]
#include"iostream.h"
#include"conio.h"
void main( )
{
int birth_year;
clrscr( );
cout<<"Input birth year: "; cin>>birth_year;
switch (birth_year % 12)
{
case 0 :cout << "You are Snake year" ;
case 1 :cout << "You are Horse year" ;
break;
break;
case 2 : cout<<"You are Sheep year";
case 3 : cout<<"You are Monkey year";
case 4 : cout<<"You are Hen year";
case 5 : cout<<"You are Dog year";
case 6 : cout<<"You are Pork year";
case 7 : cout<<"You are Rat year";
case 8 : cout<<"You are Cow year";
case 9 : cout<<"You are Tiger year";
case 10: cout<<"You are Rabbit year";
case 11: cout<<"You are Drakon year";
} getch( ); }
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
For Statement
for (start_value ; condition ; step )
{
statement ;
}
Flow-Chart For Statement
Start_value
Condition
true
Step
Statement
False
ออกจากFor
ผลการทางานที่ต้องการ
Show Number 1-20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Show Character A-Z
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Show Character z-a
zyxwvutsrqponmlkjihgfedcba
#include "iostream.h"
#include "conio.h"
void main ( )
{ int i;
char c;
clrscr ( );
cout << "Show Number 1-20\n" ;
for ( i=1 ; i<=20 ; i++)
cout<< i <<" ";
cout<<"\n Show Character A-Z \n" ;
for (c='A'; c<='Z'; c++)
cout << c << " ";
cout << "\n Show Character z-a \n" ;
for (c='z’ ; c >='a’; c--)
cout << c << " ";
getch( );
}
ผลการทางานที่ต้องการ
แสดงผลรวมตั้งแต่เลข 1 จนถึงค่าที่ผใู ้ ช้งานป้ อน 1+2+3+...+n ?
Input n umber : 100
1+2+3+…+100 = 5050
[enter]
#include"iostream.h"
#include"conio.h"
void main( )
{ int i,n,sum;
clrscr ( );
cout<< "Input number :" ; cin>>n;
sum=0;
for(i=1; i<=n; i++)
sum= sum + i;
cout << "1+2+3+...+" << n <<" = "<<sum;
getch( ); }
ผลการทางานที่ต้องการ
Input number : 5 [enter]
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
#include"iostream.h"
#include"conio.h"
void main( )
{ int i,n,fac;
clrscr( );
cout << "Input number: " ;
cin >> n ;
fac=1;
for(i=1; i<=n; i++)
{ fac=fac * i;
cout<< i <<"! = "<< fac <<endl;
}
getch( );
}
ตัวอย่ าง
*เขียนโปรแกรมรับเริ่มต้ นและสิ้นสุ ดพร้ อมหาผลรวมและจานวนรอบ
ผลการทางานที่ต้องการ แสดงค่า Fibonacci 20 ค่า
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6756
#include "iostream.h"
#include "conio.h"
void main( )
{
int a, b, c, i;
clrscr( );
a=0; b=0; c=1 ;
for (i=1; i<=20; i++)
{
cout << c <<" ";
a=b;
b=c;
c = a+b ;
}
getch( );
}
การใช้ For ซ้ อนกัน
for(Start_value;condition;step)
{
for(Start_value;condition;step)
{
Statement;
}
}
ตัวอย่ าง
int x,y;
void main()
{ clrscr();
for(x=1;x<=3;x++)
{ cout<<endl<<x<<“ ==>”;
for(y=1;y<=5;y++)
{ cout<<“Siam ”; }
} getch(); }
ตัวอย่ าง
*เขียนโปรแกรมสร้ างตารางสู ตรคูณ
While Statement
while ( condition )
{
statement ;
…
}
Flow-Chart
False
condition
True
Statement ;
Statement ;
.
.
.
.
end
ตัวอย่ าง
char ch;
void main()
{ clrscr();
while(ch!=‘*’)
{ cout<<“Enter (Stop=*) :”;
cin>>ch;
}
}
ผลการทางานที่ต้องการ
Input data end by –999
5
[enter]
3
[enter]
1
[enter]
-999
[enter]
Sum = 9
#include"iostream.h"
#include"conio.h"
void main( )
{ int sum, data ;
clrscr ( );
sum = 0;
cout << "Input data end by -999\n" ;
cin >> data ;
while (data != -999)
{ sum = sum + data ;
cin >> data ;
}
cout<< "Sum = " << sum ;
getch( ) ;
}
ผลการทางานที่ต้องการ
Input data end by –999
7
[enter]
2
[enter]
4
[enter]
6
[enter]
-999 [enter]
Sum = 19
Max = 7
Min = 2
Average = 4.75
#include"iostream.h"
#include"conio.h"
void main( )
{ int sum, data, max, min ;
float count;
clrscr ( );
sum=0 ; count=0 ;
cout << "Input data end by -999\n" ;
cin >> data;
max = min = data;
while (data != -999)
{ sum = sum + data ;
count = count + 1 ;
if (data > max) max = data ;
else if (data < min) min = data ;
cin>>data;
}
cout << "Sum = " << sum << endl
<< "Max = " << max << endl
<< "Min = " << min << endl
<< "Average = " << sum/count << endl ;
getch( );
}
Do-While Statement
do
{
statement ;
…
} while ( condition ) ;
Flow-Chart
Statement ;
.
.
False
condition
True
ตัวอย่ าง
char ch;
void main(){
clrscr();
do{
cout<<"Continue Y/N ? :";
cin>>ch;
} while((ch!='n')&&(ch!='N'));
}
ผลการทางานที่ต้องการ
Input number : 9
[enter]
1+2+3+…+9 = 45
Continue Y/N : n
[enter]
#include "iostream.h"
#include "conio.h"
void main( )
{ int i,n,sum;
char check;
do
{ clrscr( );
cout<< "Input number : " ;
cin >> n ;
sum = 0 ;
for ( i = 1; i <= n; i++)
sum = sum + i ;
cout<< "1+2+3+...+" << n << " = " << sum <<endl
<< "Continue Y/N : ";
cin >> check;
} while ((check != 'n') && (check != 'N'));
}
เขียนโปรแกรมการเลือกOperatorคานวณ
ผลการทางานที่ต้องการ
Select Menu
*********************
1. Align Left
2. Align Right
3. Center
4. Exit
*********************
Select Menu No. : 3 [enter]
Input number of line : 5 [enter]
*
***
*****
*******
*********
*
**
***
****
*****
*
**
***
****
*****
#include"iostream.h"
#include"conio.h"
void main( )
{ int row, col, menu_no, line, blank;
char ch;
do
{ clrscr( );
cout <<" Select Menu "<<endl
<<"*********************"<<endl
<<" 1. Align Left "<<endl
<<" 2. Align Right "<<endl
<<" 3. Center
"<<endl
<<" 4. Exit
"<<endl
<<"*********************"<<endl
<<"Select Menu No. : ";
cin >> menu_no ;
clrscr ( ) ;
switch (menu_no)
{ case 1 :cout<<"Input number of line : ";
cin>>line;
for(row = 1; row <= line; row++)
{ for(col = 1; col <= row; col++)
cout << "*" ;
cout << end l;
}
getch( );
break;
case 2 :cout << "Input number of line : ";
cin >> line;
for (row = 1; row <= line; row++)
{ for(blank = 1; blank <= line - row; blank++)
cout << " " ;
for(col = 1; col <= row; col++)
cout << "*" ;
cout << endl ;
}
getch( ) ; break ;
case 3 :cout<<"Input number of line : ";
cin>>line;
for(row = 1; row <= line; row++)
{ for(blank = 1; blank <= line - row; blank++)
cout<<" ";
for(col = 1; col <= row * 2 - 1; col++)
cout<<"*";
cout<<endl;
}
getch( ); break;
} // end switch
}while (menu_no != 4);
} // end program
goto Statement
goto label ;
…
…
label :
statement;
…
label :
statement;
…
goto label ;
…
ตัวอย่ าง
main() { int num;
cout << " enter from (0-9): "; cin >> num;
if (num = = 5)
{ cout<< "Correct“<<endl;
goto bye; }
cout<<" ***** Game Over ***** ";
goto exit;
bye: cout<<"***** You Win ***** ";
exit: getch( ); }
การส่ ุ มค่ าตัวเลข
ใช้ stdlib.h
random(n) ;
สุ่ มค่า 0 ถึง (n-1)
randomize( ) ;
กาหนดให้มีการสุ่ มค่าทุกครั้ง
การส่ ุ มค่ าตัวเลข
#include<stdlib.h>
main() { int num;
cout<<endl;
num = random(10) ;
cout<<num;
getch(); }
ผลการทางานที่ต้องการ
โปรแกรมในการสุ่ มตัวเลขจาก 0-9 โดยให้ทายว่าโปรแกรมสุ่ มค่าใด
ขึ้นมา การทายสามารถทายได้ 3 ครั้ง ถ้าครบ 3 ครั้งแล้วยังทายไม่ถูก
โปรแกรมจะทาการเฉลยให้
enter from (0-9) : 7
less than 7 enter from (0-9) : 4
more than 4 enter from (0-9) : 5
Correct
*****You Win*****
[enter]
[enter]
[enter]
#include"iostream.h"
#include"conio.h"
#include"stdlib.h"
void main( )
{ int random_num,num,i;
clrscr( );
randomize( );
random_num = random(10);
for(i=1; i<=3; i++)
{
cout << " enter from (0-9): ";
cin >> num;
if (num = = random_num)
{ cout<< "\n Correct";
goto bye;
}
else if (num>random_num)
cout<<"\n less than "<<num;
else
cout<<"\n more than "<<num;
} //end for
cout<<"\n Sorry! the answer is "
<< random_num;
cout<<"\n\n *****Game Over***** ";
goto exit;
bye:
cout<<"\n\n *****You Win***** ";
exit:
getch( );
}
การใช้ คาสั่ งควบคุมอืน่ ๆ
คาสั่ ง
-break
-continue
Break statement
คาสั่ ง break
ใช้ ควบคุมให้ โปรแกรมออกจาก Loop คาสั่ งทีก่ าหนด
จะใช้ ร่วมกับคาสั่ ง switch ,while,for,do..while
ตัวอย่ าง
int x;
void main() {
for(x=1;x<=10;x++) {
cout<<x<<endl;
if(x= =5)
break; }
getch(); }
ผลการทางานที่ต้องการ
แสดงความน่ าจะเป็ นในการสุ่ มตัวเลข 0-9
โอกาสที่พบเลข 5 จะเป็ นลาดับ ที่เท่ าไหร่
627996091685
Show number 5 in sequence : 12
#include"iostream.h"
#include"conio.h"
#include"stdlib.h"
void main( )
{
int random_num,i;
clrscr( );
randomize( );
for (i=1; i<=100; i++)
{
random_num=random(10);
cout<< random_num << " ";
if (random_num==5)
break;
}
cout <<"Show number 5 in
sequence : " <<i;
getch( );
}
continue statement
การทางานแบบวนลูปขณะที่ทางาน
คาสัง่ ต่างๆ อยู่ ถ้าท่านต้องการให้กลับไป
เริ่ ม ทางานที่ตน้ ลูปอีกครั้ง สามารถใช้
continue statement ช่วยได้
ตัวอย่ าง
int x;
void main() {
for(x=1;x<=10;x++) {
if(x==5)
continue;
cout<<x<<endl; }}
ตัวอย่ าง
*เขียนโปรแกรมแสดงตัวเลข1-10
แสดงเฉพาะตัวคู่ 0 2 4 6 8 10
int x;
void main() {
for(x=0;x<=10;x++) {
if(x%2==1)
continue;
cout<<x<<“
”; } getch();}
ผลการทางานที่ต้องการ
โปรแกรมในการทอนเงินให้ลูกค้าที่มาซื้อสิ นค้า โดยทาการตรวจสอบการป้ อน
จานวนเงินที่ตอ้ งจ่าย ต้องมากกว่าราคาสิ นค้า ไม่เช่นนั้นต้องทาการป้ อนใหม่
Input cost of goods = 200 [enter]
Input amount
= 50 [enter]
*****Not enough amount*****
Input cost of goods = 200 [enter]
Input amount
= 500 [enter]
Small change is 300
Again?(Y/N) : n
[enter]
#include"iostream.h"
#include"conio.h"
void main ( )
{ float cost, amount ;
char ch;
clrscr( );
do
{
cout << "Input cost of goods = ";
cin >> cost;
cout<<"Input amount = ";
cin>>amount;
if(amount<cost)
{ cout<<"*****Not enough amount*****\n\n";
continue;
}
cout<<"small change is "<<(amount - cost)<<endl;
cout<<"Again ? (Y/N):"; cin>>ch;
cout<<"==========================\n\n";
} while((ch!='n')&&(ch!='N'));
}
Array (ตัวแปรชุด )
array หมายถึง ชุดของข้อมูลที่มีจานวน
แน่นอนและเป็ นข้อมูลชนิดเดียวกัน ข้อมูล
แต่ละรายการเรี ยกว่า สมาชิกของอาร์เรย์
(element of array) แต่ละสมาชิกของอาร์เรย์มี
อินเด็กซ์ (index) สาหรับใช้เพื่อการอ้างถึง
One-dimension array
การประกาศตัวแปร array
datatype variable [number of array];
Ex
int mark[30] ;
การกาหนดค่ าตัวแปร array
• กาหนดค่ าคงที่ให้ กบั Array
Variable[index]=ค่ าตัวแปร;
เช่ น int Mark[5];
Mark[1]=50;
การกาหนดค่ าตัวแปร array
• กาหนดค่ าคงที่ให้ กบั สมาชิก Array ทุกรายการ
for(int x=0;x<=ค่ าสิ้นสุ ด;x++)
Variable[x]=ค่ าตัวแปร;
เช่ น int Mark[5];
for(int x=0;x<5;x++)
Mark[x]=50;
การกาหนดค่ าตัวแปร array
• กาหนดค่ าเริ่มต้ นให้ กบั สมาชิก Array
int Variable[จานวน]={ค่ าตัวแปร,..,..};
เช่ น int Mark[5]={10,50,30,40,60};
Mark[1] มีค่าเท่ ากับ 50;
การรับข้ อมูลเข้ าใน Array
รู ปแบบ
cin>>variable[index];
เช่ น
เช่ น
int
x[5];
int x[5];
for(int n=0;n<5;n++)
cin>>x[0];
cin>>x[n];
การแสดงผลข้ อมูล Array
รูปแบบ
cout<<variable[index];
เช่ น
เช่ น
cout<<x[1]; for(int n=0;n<5;n++)
cout<<x[n];
ตัวอย่ าง
สร้ าง Array รับค่ ายอดขายของสมาชิก 5 คน
พร้ อมหาผลรวม และค่ าเฉลีย่
ตัวอย่ าง
เขียนโปรแกรมรับยอดขาย ของพนักงาน 5 คน
เพือ่ คานวณและแสดงค่ าเป็ นร้ อยละของยอดขายรวม
ผลการทางานที่ต้องการ *****Random Number*****
แสดงความถี่ของ
เลข 0-9 ในการสุ่ ม
ตัวเลข 20 ครั้ง
97990648543243054978
*******Frequency *******
0: 2
1: 0
2: 1
3: 2
4: 4
5: 2
6: 1
7: 2
8: 2
9: 4
#include"iostream.h"
#include"conio.h"
#include"stdlib.h"
void main( )
{ int random_num, i, count[10] ;
clrscr( ) ;
for(i=0; i<=9; i++)
count[i] = 0 ;
cout<<"*****Random Number*****"<<endl;
randomize( );
for (i=1; i<=20; i++)
{ random_num = random(10);
cout << random_num << " ";
count[random_num] = count[random_num]+1;
}
cout << endl;
cout <<"*******Frequency*******"<<endl;
for(i=0; i<=9; i++)
cout << i << " : "<< count[i] <<endl;
getch( ); }
ผลการทางานที่ต้องการ
หาค่ากึ่งกลางข้อมูล (median)
จากตัวเลขทั้งหมดที่ผใู้ ช้งาน
ป้ อน โดยการรับค่า จะสิ้ นสุ ด
เมื่อป้ อนค่า -999
Enter number end by –999
5
[enter]
3
[enter]
7
[enter]
1
[enter]
4
[enter]
-999 [enter]
*****Data Before Sort*****
53714
*****Sort Data*****
13457
Median = 4
#include"iostream.h"
#include"conio.h"
void main( )
{ int data[50], n, i, j, temp;
float median;
clrscr( );
n = 1;
cout << "Enter number end by -999\n";
cin >> data[n];
while(data[n] !=-999)
{ n = n+1;
cin >> data[n];
}
cout<<"\n*****Data Before Sort*****\n";
for(i=1; i<n; i++)
cout << data[i] <<" ";
for(i=1; i<n-1; i++)
for( j=i+1; j<n; j++)
if(data[i] > data[ j])
{ temp = data[i];
data[i] = data[ j];
data[ j] = temp;
}
cout<<"\n*****Sort Data*****\n";
for(i=1; i<n; i++)
cout << data[i] << " ";
if ((n-1)%2==0)
median=(data[(n-1)/2]+data[(n-1)/2+1])/2.0;
else
median = (data[(n-1)/2+1]);
cout << "\n\nMedian = "<< median;
getch( ) ;
}
Two-dimension array
การประกาศตัวแปร array
DataType variable[Size1][Size2];
เช่น int data[4][3] ;
Two-dimension array
การประกาศตัวแปร array
datatype variable [number1][number2];
Ex
int data[2][3];
0
0,0
data
0,1
0,2
1,0
1,1
0
1,2
or
1
1
2
การอ้ างอิงตัวแปร array
ตัวแปรarray[index1][index2]
Ex
int data[2][3];
0,0
data
10
0,1
0,2
50 30
1,0
1,1
40 15
1,2
60
data[0][0]  10
data[1][0]  40
การกาหนดค่ าให้ กบั Array 2 มิติ
ตัวแปรarray[index1][index2]= ค่ า;
Ex
int data[2][3];
data[0][0]=100;
data[0][1]=50;
กาหนดค่ าให้ กบั Array 2 มิตทิ ุกรายการ
for(int x=0;x<number1;x++)
for(int y=0;y<number2;y++)
ตัวแปรarray[x][y]= ค่ า;
กาหนดค่ าเริ่มต้ นให้ สมาชิกทุกคน
0123 0123
int data[2][4]={{1,2,3,4},{5,6,7,8}};
0
1
การรับค่ า Array 2 มิติ
cin>>ตัวแปร[index1][index2];
หรือรับค่ าให้ กบั สมาชิกทุกรายการ
for(int x=0;x<number1;x++)
for(int y=0;y<number2;y++)
cin>>ตัวแปรarray[x][y];
การแสดงค่ า Array 2 มิติ
cout<<ตัวแปร[index1][index2];
หรือรับค่ าให้ กบั สมาชิกทุกรายการ
for(int x=0;x<number1;x++)
for(int y=0;y<number2;y++)
cout<<ตัวแปรarray[x][y];
ตัวอย่ าง
เขียนโปรแกรมรับค่ าตัวเลข Array 2 มิติ
6 รายการ พร้ อมหาผลรวม แสดงดังนี้
รับค่ าอาร์ เรย์ แบบสองมิติ แล้ วแสดงผลในลักษณะตาราง
#include"iostream.h"
#include"conio.h"
void main( )
{ int data[3][2], i, j;
clrscr( );
//รับข้อมูลโดยแสดงดัชนี
for (i= 0; i< 3 ; i++)
for ( j= 0; j< 2; j++)
{ cout<<"Input number "<<i<<","<<j<<" : ";
cin >> data[i][ j];
}
// แสดงข้อมูลในแบบ 3 แถว 2 คอลัมน์
cout <<"\n*****Matrix*****\n";
for (i =0; i < 3; i++)
{
for ( j= 0; j< 2; j++)
cout << data[i][ j] << " ";
cout << endl; //ขึ้นบรรทัดใหม่เมื่อจะเริ่ มแถวใหม่
}
getch( );
}
ผลการทางานที่ต้องการ
แสดงผลเป็ น Matrix และ
Transpose Matrix
*****Matrix*****
1 2
3 4
5 6
*Transpose Matrix*
1 3 5
2 4 6
หลักการ
#include"iostream.h"
#include"conio.h"
void main( )
{ int i, j ;
int data[3][2] = {1,2,3,4,5,6};
clrscr( );
cout << "\n*****Matrix*****\n";
for (i= 0; i< 3; i++)
{ for ( j= 0; j< 2; j++)
cout << data[i][ j] << " ";
cout << endl;
}
cout<<"\n*Transpose Matrix*\n";
for (i= 0; i< 2; i++)
{
for ( j= 0; j< 3; j++)
cout << data[ j][i] << " ";
cout << endl;
}
getch( );
}
การกาหนดตาแหน่ ง Cursor
gotoxy (col,row) ;
โดย
col=80
จอภาพ
row=25
ผลการทางานที่ต้องการ
โปรแกรมในการรับข้อมูลการขายของหนังสื อแต่ละวิชา ในแต่
ละเดือน แล้วหาผลรวมของหนังสื อแต่ละเล่มในไตรมาส และผลรวม
ในแต่ละเดือน
#include"iostream.h"
#include"conio.h"
void main( )
{ int data[4][3], i, j, x, y, sum, total;
clrscr( );
gotoxy (30,2); cout<<"***** Books Store *****";
gotoxy (20,4); cout << "January";
gotoxy (35,4); cout << "February";
gotoxy (50,4); cout << "March";
gotoxy (5,6); cout << "C++";
gotoxy (5,8); cout << "Pascal";
gotoxy (5,10); cout << "Visual C++";
gotoxy (5,12); cout << "Delphi";
y=6;
for (i= 0; i< 4; i++)
{ x = 20 ;
for ( j= 0; j< 3; j++)
{ gotoxy (x, y) ;
cin >> data[i][ j] ;
x = x+15 ;
}
y = y+2 ;
}
gotoxy (65,4); cout << "Total";
x = 65; y = 6;
for (i= 0; i< 4; i++)
{
sum = 0;
for ( j= 0; j< 3; j++)
sum = sum+data[i][ j];
gotoxy (x, y); cout<<sum;
y = y+2;
}
gotoxy (5,14); cout<<"Total";
total = 0; y = 14; x = 20;
for (i= 0; i< 3; i++)
{ sum=0;
for ( j= 0; j< 4; j++)
sum = sum+data[ j][i];
total = total+sum;
gotoxy (x, y); cout<<sum;
x = x+15;
}
gotoxy (65,14); cout<<total;
getch( ); }
อาร์ เรย์ หลายมิติ
การประกาศตัวแปรอาร์ เรย์ หลายมิติ
DataType variable[size1][size2]…[sizen] ;
เช่น เก็บข้อมูลการขายสิ นค้า 2 ชนิด ภายใน 3 เดือน ของพนักงานขาย 5 คน
int sale[5][3][2] ;
การอ้ างอิงตัวแปรอาร์ เรย์ หลายมิติ
variable [index1][index2]…[index n]
for ( i = 0; i < 5; i++)
for( j = 0; j < 3; j++)
for( k=0; k < 2 ; k++)
cin >> sale[i][ j][k] ;
ผลการทางานที่ต้องการ
การรับยอดการขายสิ นค้า 2 ชนิดจานวน 3 เดือนของพนักงาน 2 คน
จากนั้นแสดง ผลว่าพนักงานแต่ละคน ขายสิ นค้าแต่ละชนิดได้คนละเท่าใด
person 1
month 1
goods1 : 52 [enter]
goods2 : 20 [enter]
month 2
goods1 : 66 [enter]
goods2 : 29 [enter]
month 3
goods1 : 54 [enter]
goods2 : 35 [enter]
+++++++++++++++++
[วนรับค่ าของ person 2]
person 1
sum of goods1 : 172
sum of goods2 : 84
person 2
sum of goods1 : 180
sum of goods2 : 71
#include"iostream.h"
#include"conio.h"
void main( )
{ int sale[2][3][2], i, j, k, sum;
for (i= 0; i< 2; i++)
{ clrscr( );
cout<<"person "<<i+1<<endl;
for ( j= 0; j< 3; j++)
{ cout<<" month "<<j+1<<endl;
for (k=0; k<2; k++)
{ cout<<" goods"<<k+1<<" : ";
cin>>sale[i][ j][k];
}
}
cout<<"+++++++++++++++"<<endl;
}
for (i= 0; i< 2; i++)
{ cout << "person "<< i+1 <<endl;
for (k= 0; k< 2; k++)
{ sum = 0;
for(j= 0;j< 3;j++)
sum = sum+sale[i][ j][k];
cout << " sum of goods"<< k+1
<< " : "<< sum << endl;
}}
getch( ); }
คาสั่ งด้ านเสี ยง
ใช้ dos.h
sound(frequency_Hz) ;
ใช้ dos.h
nosound() ;
การกาหนดความหน่ วง
ใช้ dos.h
delay(time_milliseconds) ;
#include"dos.h"
#include"conio.h"
void main()
{ int n;
char ch;
n=100;
do
{ n=n+50;
sound(n);
delay(100);
nosound ( );
} while(n!=1000);
}
ผลการทางานที่ต้องการ
Push 1,2,3... end by 9
ความถีเ่ สี ยง
‘1’ = 262
‘2’ = 294
‘3’ = 330
‘4’ = 350
‘5’ =392
‘6’ = 440
‘7’ = 490
‘8’ = 520
‘9’ = end sound
#include"iostream.h"
#include"conio.h"
#include"dos.h"
void main( )
{ int f;
char ch;
clrscr( );
cout<<"Push 1,2,3... end by 9";
do{
ch=getch();
switch(ch)
{ case '1': f = 262; break;
case '2': f = 294; break;
case '3': f = 330; break;
case '4': f = 350; break;
case '5': f = 392; break;
case '6': f = 440; break;
case '7' : f = 490; break;
default : f = 520;
} //end switch
sound (f) ;
delay (300) ;
nosound( ) ;
}while (ch!='9') ;
}
สตริง (String)
สตริ ง หมายถึง ชุดของอักษรที่เรี ยง
ต่อกัน หรื อ อาจจะกล่าวว่า สตริ งเป็ น
อาร์เรย์ของ char โดยสตริ งเป็ นได้ท้ งั
คอนสแตนต์ และ แวเรี ยเบิล
การประกาศตัวแปร String
datatype variable[จานวน + 1];
การประกาศค่ าคงที่ String
datatype variable[]=“String”;
การรับข้ อมูลทีเ่ ป็ น String
cin >> variable ;
or
cin.get(variable , จานวน) ;
Ex
char name[20];
cin.get(name,20);
#include"iostream.h"
#include"conio.h"
void main( )
{char name[20];
clrscr( );
cout << "Input your name:";
cin >> name;
cout << "Your name is:"<<name;
getch(); }
ผลการทางาน
Input your name: Natenapa Sodadee
Your name is: Natenapa
#include"iostream.h"
#include"conio.h"
void main( )
{char name[20];
clrscr( );
cout <<"Input your name:";
cin.get(name, 20);
cout << "Your name is:"<< name;
getch(); }
ผลการทางาน
Input your name: Natenapa Sodadee
Your name is: Natenapa Sodadee
Function strcpy
เป็ นการคัดลอกสตริ ง
ใช้ string.h
strcpy(string_dest, string_src);
#include"iostream.h"
#include"conio.h"
#include"string.h"
void main()
{char copy[20],name[20];
clrscr( );
cout << "Input your name:";
cin.get (name, 20);
cout << "Your name is:"<<name;
strcpy(copy,name);
cout << "\nData:"<<copy;
getch( );
}
Function strlen
นับความยาวของสตริ ง
strlen(string);
Function strupr
เปลี่ยนข้อความเป็ นตัวพิมพ์ใหญ่
strupr(string);
Function strlwr
เปลี่ยนข้อความเป็ นตัวพิมพ์เล็ก
strlwr(string);
ข้ อมูลชนิดโครงสร้ าง (Structure)
structure เป็ นกลุ่มของข้อมูลซึ่ง
ประกอบด้วยข้อมูลชนิดเดียวกัน หรื อ
หลายชนิด ข้อมูลแต่ละรายการในสตรัก
เจอร์เรี ยกว่า สมาชิกของสตรักเจอร์
(members of the structure)
การกาหนดข้ อมูลแบบ Structure
struct struct_name
{
datatype variable;
...
};
struct struc_name struc_variable ;
การอ้ างอิงถึงสมาชิกของสตรักเจอร์
struc_variable.variable = value ;
Ex
struct person
{
char name[20];
int age;
} per ;
Or person per;
cin >> per.name ;
#include"iostream.h"
#include"conio.h"
void main( )
{
struct person
{ char name[20];
int age;
float salary;
} emp;
clrscr ( ) ;
cout << "Enter name : "; cin >> emp.name;
cout << "Enter age : "; cin >> emp.age;
cout << "Enter salary : "; cin >> emp.salary;
cout << endl<<"Your Information"<<endl;
cout << emp.name <<" is "<<emp.age
<<" years old and salary is "
<< emp.salary <<" Baht.";
getch ( );
}
ผลการทางานที่ต้องการ
โปรแกรมแสดงการรับและแสดงผลข้อมูล
ของพนักงาน 5 คนโดยนาสตรักเจอร์ และ
อาร์เรย์ มาใช้งานร่ วมกัน
#include"iostream.h"
#include"conio.h"
void main( )
{ int i;
struct person
{ char name[20];
int age;
float salary;
} per[5] ;
for (i=0; i<5; i++)
{ clrscr( );
gotoxy (15, 2);
gotoxy (5, 3);
gotoxy (5, 9);
gotoxy (8, 4);
gotoxy (10, 5);
gotoxy (10, 6);
gotoxy (10,7);
}
cout<<"Enter Data"<<endl;
cout<<"=======================";
cout<<"=======================";
cout<< "Person"<< i+1 <<endl ;
cout<< "Name: "; cin>>per[i].name;
cout<< "Age: "; cin>>per[i].age;
cout<< " Salary: "; cin>>per[i].salary;
clrscr( );
gotoxy (15, 2);
gotoxy (5, 3);
gotoxy (5, 11);
gotoxy (7, 4);
gotoxy (12, 4);
gotoxy (23, 4);
gotoxy (28, 4);
cout<<"Show Data"<<endl;
cout<<"=======================";
cout<<"=======================";
cout<<"No.";
cout<<"Name";
cout<<"Age";
cout<<"Salary";
for (i=0; i<5; i++)
{ gotoxy (7 , i+5);
gotoxy (12, i+5);
gotoxy (23, i+5);
gotoxy (28, i+5);
}
getch( );
}
cout<<i+1;
cout<<per[i].name;
cout<<per[i].age;
cout<<per[i].salary;
Pointer
เป็ นตัวแปรชนิดหนึ่งที่ทาหน้าที่เก็บ
ตาแหน่งที่อยูข่ องตัวแปรที่อยูใ่ น
หน่วยความจา
กรณีที่ต้องการทราบที่อยู่ของตัวแปร จะใช้
สั ญลักษณ์ & (Ampersand) วางไว้ ข้างหน้ า
ตัวแปรทีต่ ้ องการทราบทีอ่ ยู่ในหน่ วยความจา
การประกาศตัวแปร pointer
datatype *variable_name;
Ex
char *prt;
int
*a,*b;
การกาหนดให้ ตัวแปรพอยน์ เตอร์ ชี้ไปยังตัวแปรอืน่
pointer_variable = &variable_name ;
การอ่ านค่ าข้ อมูลทางอ้ อม
* pointer_variable
#include "iostream.h"
#include "conio.h"
void main( )
{
int *mypoint;
int data = 50 ;
clrscr( );
mypoint = &data ;
cout<<"data : Address = "<< &data
<<" , Value = "<< data << endl;
cout<<"mypoint: Address = "<< &mypoint
<<" , Value = "<<mypoint << endl;
cout<<"Value of Pointer = "<< *mypoint;
getch( );
}
ฟังก์ ชัน (Function)
ฟั ง ก์ชัน หมายถึ ง สเตตเมนต์ชุ ด
หนึ่ งซึ่ งมี ชื่ อ เฉพาะ ส่ ว นอื่ น ๆของ
โปรแกรมสามารถเรี ยกใช้สเตตเมนต์
ชุดนี้ได้
ประโยชน์ ของฟังก์ ชัน
• ช่วยให้ไม่ตอ้ งเขียนสเตตเมนต์เดิมซ้ ากัน
หลายครั้งในโปรแกรมเดียวกัน
• ช่วยให้สามารถค้นหาส่ วนที่ผดิ หรื อส่ วนที่
ต้องปรับปรุ งได้รวดเร็ ว
• ทาให้โปรแกรมมีขนาดกระทัดรัด ทาความ
เข้าใจได้ง่ายและรวดเร็ ว
โครงสร้ างของฟังก์ ชัน
type function_name (parameter)
{
statement ;
…
return value ;
}
การเรียกใช้ ฟังก์ ชัน
• function ทีไ่ ม่ มีการคืนค่ า(ไม่ มีโปรโตไทป์ )
function_name();
function_name(argument);
• function ทีม่ ีการคืนค่ า
variable = function_name(argument);
การวางตาแหน่ งฟังก์ ชัน
#include " header file "
dataType function_name (parameter)
{ statements ;
...
return value ;
}
void main ( )
{ statements... ;
}
#include " header file “
dataType function_name(parameter) ; // declaration function
void main ( )
{
statements ;
...
}
dataType function_name (parameter)
{
statements ;
...
return value ;
}
#include"iostream.h"
#include"conio.h"
void line( )
{ int i;
for ( i=1; i<30; i++)
cout<<“ * ";
cout<<endl;
}
void main ( )
{
clrscr( );
line( );
cout<<"Siam Computer"<<endl
<<"Test Function"<<endl;
line( );
getch( );
}
แบบมีพารามิเตอร์
#include"iostream.h"
#include"conio.h"
void line (int n) ;
void main( )
{ clrscr( );
line(20);
cout<<"Siam Computer"<<endl
<<"Test Function"<<endl ;
line(15);
getch( );
}
void line (int n)
{
int i;
for ( i=1; i<n; i++)
cout<<“ * ";
cout<<endl;
}
แบบสองพารามิเตอร์
#include"iostream.h"
#include"conio.h"
void line (int n, char c)
{ int i;
for ( i=1; i<n; i++)
cout<< c;
cout<<endl;
}
void main( )
{ int num; char ch;
clrscr( );
cout<<"Input number of line:"; cin>>num;
cout<<"Input character:"; cin>>ch;
line (num, ch);
cout<<"Siam Computer"<<endl
<<"Test Function"<<endl;
line (num, ch);
getch( );
}
ผลการทางานที่ต้องการ
#include <iostream.h>
#include <conio.h>
void border (int x, int y, int w, int h)
{ int i, j;
//+++++ Horizontal Top Line +++++
gotoxy (x, y);
for ( i=1; i<= w; i++)
cout<<"#";
//+++++ Horizontal Bottom Line +++++
gotoxy (x, y+(h-1));
for ( i=1; i<=w; i++)
cout<<"#";
//+++++ Vertical Left Line +++++
for ( i=1; i <= h-2; i++)
{ gotoxy (x, y+i);
cout<<"#";
}
//+++++ Vertical Right Line +++++
for ( i=1; i <= h-2; i++)
{ gotoxy (x+(w-1), y+i);
cout<<"#";
}
} // end function
void main( )
{ int x, y, w, h;
clrscr( );
cout<<"Input x: ";
cout<<"Input y: ";
cout<<"Input w: ";
cout<<"Input h: ";
clrscr ( );
border(x, y, w, h);
getch( );
}
cin>> x;
cin>> y;
cin>> w;
cin>> h;
ผลการทางานที่ต้องการ การสร้ างฟังก์ชันหาค่าของ Factorial
Input number : 5 [ enter]
5 ! = 120
#include"iostream.h"
#include"conio.h"
double fac(int n)
{ double f ;
if (n==0 || n ==1)
f=1;
else
f = n*fac(n-1) ;
return f ;
}
void main ( )
{ int num ;
clrscr( );
cout << "Input number :" ; cin>>num ;
cout << num << "! = " << fac(num) ;
getch ( );
}
การเขียนโปรแกรด้ านกราฟฟิ ก
ใช้ graphics.h
การเริ่มใช้ งานระบบกราฟฟิ ก
initgraph(&GrDriver, &GrMode, directory) ;
GrDriver = DETECT หรื อ 0
GrMode = 0
directory = “”
การปิ ดกราฟฟิ กโหมด
closegraph( ) ;
การวาดเส้ น
line (x1,y1,x2,y2) ;
x1,y1
x2,y2
ค่ าพิกเซลสูงสุดของจอภาพ
getmaxx( ) ; // ให้ ค่าพิกเซลสูงสุดของแกน x
getmaxy( ) ; // ให้ ค่าพิกเซลสูงสุดของแกน y
การกาหนดสี วตั ถุ
โดย
setcolor ( n ) ;
n=1,2,…15
การกาหนดสี พนื้
setbkcolor ( n ) ;
โดย
n=1,2,…15
การสร้ างตารางขนาด 3 แถว 3 คอลัมน์ โดยจุดเริ่มต้ นของตาราง
เป็ น (50,50) และแต่ ละเซลในตารางมีขนาด 50 * 50 พิกเซล
#include"graphics.h"
#include"conio.h"
void main( )
{ int n,m,i;
n=m=0;
initgraph (&n,&m,"") ;
setcolor (RED) ;
setbkcolor (15) ;
for (i=1; i<=4; i++)
{
line(50, 50 * i ,200, 50 * i);
line(50 * i ,50, 50 * i,200);
}
getch( );
closegraph( );
}
ผลการทางานที่ต้องการ
การสร้ างตารางตามขนาดแถวและคอลัมน์ ทีผ่ ้ ูใช้ งานกาหนด
โดยจุดเริ่มต้ นของ ตารางเป็ น (50,50) และแต่ ละเซลในตารางมี
ขนาด 50 * 50 พิกเซล
#include"iostream.h"
#include"conio.h"
#include"graphics.h"
void main( )
{ int n,m,i,row,col;
clrscr( );
cout<<"Enter row:"; cin>>row;
cout<<"Enter column:"; cin>>col;
n=m=0;
initgraph(&n,&m,"");
for(i=1; i<=row+1; i++)
line(50, 50*i , (col+1)*50 , 50*i);
for(i=1; i<=col+1; i++)
line(50*i ,50 , 50*i , (row+1)*50);
getch( );
closegraph( );
}
การวาดวงกลม
circle (x,y,radius) ;
การวาดส่ วนโค้ ง
arc (x,y,st_angle,end_angle,radius);
การวาดวงรี
ellipse (x,y,st_angle,end_angle,radiusx,radiusy);
การวาดสี่ เหลีย่ ม
rectangle(left,top,right,bottom);
การวาดภาพหลายเหลีย่ ม
drawpoly ( numpoints, polypoints ) ;
เป็ น Array
ผลการทางานที่ต้องการ
การวาดภาพสามเหลีย่ มโดยใช้ คาสั่ ง drawpoly
#include"graphics.h"
#include"conio.h"
void main( )
{ int n,m;
int polypoint[8]={100,200,200,200,150,100,100,200};
n=m=0;
initgraph(&n,&m,"");
drawpoly(4,polypoint);
getch( );
closegraph( );
}
ผลการทางานที่ต้องการ
การวาดวงกลมซ้าๆ โดยวงกลมทุกวงจะมีจุดศูนย์ กลางเดียวกัน ส่ วน
รัศมีจะลดลงที่ ละ1 พิกเซล ซึ่งโปรแกรมจะหยุดทางานเมื่อกดคีย์ใดๆ
#include"graphics.h"
#include"conio.h"
#include"stdlib.h"
#include"dos.h"
void main( )
{ int n, m, i, x, y ;
n=m=0;
initgraph (&n, &m, "");
x = getmaxx( ) / 2 ;
y = getmaxy( ) / 2 ;
do
{ for( i = 200; i >= 0 ; i--)
{ setcolor(random(15));
circle(x, y, i);
delay(10);
}
}while (kbhit( ) = = 0);
closegraph( );
}
การวาดภาพบาร์
bar (left,top,right,bottom);
การวาดภาพวงรีทึบ
fillellipse(x,y,radiusx,radiusy);
การวาดภาพบาร์ 3 มิติ
bar3d(left,top,right,bottom,depth,topflag);
การกาหนดรูปแบบของเส้ น
setlinestyle ( linestyle, upattern,thickness
)linesstyle
;
upattern
thickness
การกาหนดลวดลายในวัตถุ
setfillstyle(pattern,color);
pattern = 0-12
color = 0-15
การระบายสี วตั ถุ
floodfill ( x, y, bordercolor ) ;
ในกรณีถ้าค่ าของโคออร์ ดเิ นตอยู่ภายในวัตถุ
จะเป็ นการระบายสี ในวัตถุ แต่
ถ้ าค่ าของโคออร์ ดเิ นตอยู่ภายนอกวัตถุจะเป็ น
การระบายสี พนื้ ด้ านนอก
ผลการทางานที่ต้องการ
#include"graphics.h"
#include"conio.h"
void main( )
{ int n,m;
n=m=0;
initgraph (&n,&m,"");
setcolor (RED);
circle (100,100,25);
}
setcolor (BLUE);
circle (150,100,50);
setfillstyle (5,11);
floodfill (100,100,RED);
getch( );
closegraph( );
ผลการทางานที่ต้องการ
แสดงลวดลายแบบต่ างในคาสั่ งบาร์ โดยสร้ างบาร์ ขนาด 50 * 50 พิก
เซล 2 แถว แถวละ 6 ภาพ และระยะระหว่ างแต่ ละบาร์ 30 พิกเซล
#include"graphics.h"
#include"conio.h"
void main( )
{ int i,j,x,y,n,m,pattern;
n=m=0;
initgraph(&n,&m,"");
setbkcolor(15);
y=50;
pattern=0;
for(i=1;i<=2;i++) // กาหนดให้มีการสร้างบาร์ 2 แถว
{ x=50;
// กาหนดค่าเริ่ มต้นของแกน x
for (j=1;j<=6;j++) // กาหนดให้แต่ละแถวมีบาร์ 6 ภาพ
{ setfillstyle(pattern+=1,1); // ให้ลวดลายเปลี่ยนค่าทีละ 1
bar(x,y,x+50,y+50); // สร้างบาร์ขนาด 50 * 50
x=x+80; // กาหนดให้ระยะห่างของแต่ละภาพ ห่าง 30
}
y=y+80; // เพิ่มค่าแกน y เมื่อจะสร้างบาร์แถวใหม่
}
getch( );
closegraph( );
}
ผลการทางานที่ต้องการ
Enter Data1 : 100 [enter]
Enter Data2 : 150 [enter]
...
Enter Data10 : 300 [enter]
#include"iostream.h"
#include"conio.h"
#include"graphics.h"
void main( )
{ const number=10; // กาหนดค่าคงที่ของจานวนข้อมูล
int n,m,i,data[number];
//+++++++++Text Mode+++++++++++++++
clrscr( );
for(i=0; i<=number-1; i++) // วนรับข้อมูลทั้งหมด
{ cout<<"Enter Data"<<i+1<<" : ";
cin>>data[i];
}
n=m=0;
//+++++++++Graphic Mode++++++++++++
initgraph(&n,&m,"");
setbkcolor (15);
setcolor (1);
line (50, 50, 50, 450);
// กาหนดเส้นแกน y
line (50, 450,600,450);
// กาหนดเส้นแกน x
// วนสร้างเส้นบอกขนาดตามแกน y
for( i= 400; i > = 50; i- = 50 )
line (40, i, 60, i);
setfillstyle (2, 2);
// วนสร้างกราฟตามจานวนข้อมูล
for (i=0; i <= number-1; i++)
bar3d (50*(i+2)-20, 450, 50*(i+2),
450-data[i], 10, 1);
getch( );
closegraph( );
}
การแสดงข้ อความในกราฟฟิ ก
outtext(“string”);
or
outtextxy(x,y,”string”);
การกาหนดรูปแบบอักษร
settextstyle(font,direction,charsize);
โดย
font= 0-10
direction= 0 or 1
OOP
OOP เป็ นวิธีการเขียนโปรแกรม
ซึ่งจัดให้โปรแกรมดาเนินการกับ
กลุ่มของ ออบเจ็กต์ (object) ที่มี
อยูใ่ นโปรแกรม
วิธีการสร้ างและใช้ ออบเจ็กต์
class class_name
{
private: or public: or protected:
datamember;
private: or public: or protected:
memberfunction;
};
การประกาศ datamember
datatype variable ;
การประกาศ memberfunction
datatype function_name(parameter);
Member function
datatype class_name::function_name(parameter)
{
statement ;
…
}
การประกาศตัวแปร class
class_name variable_calss;
การอ้ างอิง data member
variable_class.datamember=value;
การอ้ างอิง member function
variable= variable_class.memberfunction(argument);
Constructor(คอนสตักเตอร์ )
เป็ นเมมเบอร์ฟังก์ชนั ที่ทางานโดย
อัตโนมัติทนั ทีที่ออบเจ็กต์ถูกสร้างขึ้น
class_name(parameter)
{
statement;
}
Destructor(ดิสตรักเตอร์ )
เป็ นเมมเบอร์ฟังชันสาหรับยกเลิกกาใช้
ออบเจ็กต์แบบอัตโนมัติ
~class_name()
{
statement;
}
การสื บทอด
class ชื่อดีไรฟ์ คราส : private or public or
protected ชื่อเบสคลาส
{ private or public:
datamember;
private or public :
memberfunction;
}
#include"iostream.h"
#include"conio.h"
class Date
{ public: // private:
int Year;
int Month;
int Day;
public:
Date(){Year=1999;Month=9;Day=19;}
~Date(){};
void SetDate(int Y,int M,int D);
void Display();
void GetDate();
};
void Date::SetDate(int Y,int M,int D)
{
Year=Y;
Month=M;
Day=D;
}
void Date::GetDate()
{
cout << "Enter Year : ";cin >> Year;
cout << "Enter Month : ",cin >> Month;
cout << "Enter Day : ",cin >> Day;
}
void Date::Display()
{
cout << "Year : " << Year <<endl
<< "Month : "<<Month<<endl
<<"Day : "<<Day<<endl;
}
class test: public Date
{ public:
int x,y;
};
void main( )
{ clrscr ( );
Date a;
a.Display ( );
a.GetDate ( );
a.Display ( );
a.Day = 5;
getch ( );
}
test data;
data.Display( );
คลาสด้ านการคานวณ
#include"conio.h"
#include"iostream.h"
class cal
{ public :
int x,y;
public:
int add(int x,int y);
int mul(int x,int y);
int minus(int x,int y);
float div(float x1,float y1);
};
int cal::add(int x,int y)
{
return x+y;
}
int cal::mul(int x,int y)
{
return x*y;
}
int cal::minus(int x,int y)
{
return x-y;
}
float cal::div(float x1,float y1)
{
return x1/y1;
}
void main()
{ int m,n;
clrscr();
cal a;
cout<<"Enter Number1:";cin>>m;
cout<<"Enter Number2:";cin>>n;
cout<<"Add="<<a.add(m,n)<<endl
<<"Mul="<<a.mul(m,n)<<endl
<<"Minus="<<a.minus(m,n)<<endl
<<"Div="<<a.div(m,n)<<endl;
getch();
}
File
ใช้ fstream.h
การเปิ ดไฟล์ เพือ่ เขียน
ofstream WriteTextFile(“filename”);
การเขียน string ลงไฟล์
WriteTextFile<<“ String ” ;
การเขียน อักขระลงไฟล์
WriteTextFile.put(char);
ใส่ ข้อมูลลงในไฟล์
#include"fstream.h"
void main( )
{
ofstream WriteTextFile("test1.txt");
WriteTextFile<<"Test File"<<endl;
WriteTextFile<<"Bye"<<endl;
}
การเปิ ดไฟล์ เพือ่ อ่ าน
ifstream ReadTextFile(“filename”);
การอ่ าน string จากไฟล์
ReadTextFile.getline(variable,size);
การอ่ านไฟล์ อกั ขระ
ReadTextFile.get(variable);
อ่ านข้ อมูลจากไฟล์
#include"fstream.h"
#include"conio.h"
void main()
{
char data[50];
clrscr();
ifstream ReadTextFile("test1.txt");
while(ReadTextFile)
{
ReadTextFile.getline(data,50);
cout<<data<<endl;
}
getch();
}
ป้ อนข้ อมูลลงในไฟล์
#include"fstream.h"
void main( )
{
char data[50];
ofstream WriteTextFile("test2.txt");
cout<<"Input Data:";cin.get(data,50);
WriteTextFile<<data;
}
อ่ านข้ อมูลจากไฟล์
#include"conio.h"
#include"fstream.h"
void main( )
{
char ch;
clrscr( );
ifstream ReadTextFile("test2.txt");
while(ReadTextFile)
{
ReadTextFile.get(ch);
cout<<ch;
}
getch();
}