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

advertisement
โปรแกรมภาษา C++
วิทยากร : เนตรนภา โสดาดี
E-mail : natenapa@siamcom.co.th
การเขียนโปรแกรม
++
ภาษา
C
++
C เป็ นภาษาระดับสู ง ใช้
สาหร ับการเขียนโปรแกรม ผู ส
้ ร ้าง
++
C คือ บจาร ์น สโตรสตร ัป
(Bjarne Stroustrp)
++
C พัฒนามาจากภาษา
C
++
่ มี
่ ใน
โดย C ประกอบด้วยทุกสิงที
่ มเติ
่
ภาษา C และส่วนทีเพิ
มเข้า
้
ความสัมพันธ ์ของ
++
C และ C
ส่วนประกอบอ
OOP
C++
ส่วนประก
่
อบทีใช้
ร่วมกันใน
C++ และ
C
C
ส่วนประก
่
อบทีใช้
เฉพาะใน
การใช้ Turbo
C:\>cd
tc
C:\TC >
C:\TC >
++
C
หรือเรียกใชผ่้ านทาง
โปรแกรม Windows
้
ขันตอนการเขียน
โปรแกรม
การ Compile
การคอมไพล ์ซอร ์สโค้ด
่
เพือตรวจสอบไวยากรณ์
ของภาษาโดยไม่มก
ี าร
ลิงก ์
• เมนู Compile
• เลือก Compile
การ Run
หมายถึ ง การคอมไพล ์
่ ใช้
่
ซอร ์สโค้ดและ ไฟล ์อืนที
่
ร่ ว ม กัน ทุ กไ ฟ ล ์ แล ะ เ มื อ
คอมไพล ์เสร็จแล้ว จะมีการ
ลิ ง ก ์ อ ั ตโ น มั ติ แ ล ะ ร ั น
้หรือน<Ctrl+F9>
โปรแกรมนันทั
ที
• เมนู Run
โครงสร ้างภาษา
#include
<header file>
identify
function()
main()
{
Statement;
ชนิ ดของข้อมู ล
Type
Range
unsigned char
Length(bits)
8
0 to
255
char
8
-128 to
127
unsigned int
16
0 to
65,535
int
16
-32768 to
32767
unsigned long
32
0 to
้
่
กฏการตงชื
ั อ
• ขึน
้ ต ้นด ้วยอักขระ A - Z หรือ a - z
• ตัวถัดไปอาจเป็ นอักขระ (A - Z , a z) หรือ ตัวเลข 0 - 9 หรือ
เครือ
่ งหมาย อันเดอร์บาร์ ( _ )
• ห ้ามเว ้นวรรค
• ตัวอักษรพิมพ์เล็กและ ตัวอักษรพิมพ์
ใหญ่ ถือว่าต่างกัน
การประกาศต ัวแปร
Ex
DataType
VariableName;
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 ทาง
คณิ ตศาสตร ์
• บวก (+)
• ลบ (-)
• คู ณ (*)
• หาร (/)
เช่น 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 ทาง
คณิ
ต
ศาสตร
์
่ า (++) เช่น a++
การเพิมค่
การลดค่า (--)
Ex
a=a+1
a=a-1
b=b+2
เช่น
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
ลาดับการเปรียบเทียบ
•!
• > , >= , <= , <
• = = ,!=
• &&
• ||
่
คาสังในการแสดง
ข้อมู ล
ใช ้ 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=
่ องการ
ผลการทางานทีต้
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
[enter]
Input Value2: 9
[enter]
Sum = 14
#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 [enter]
5+2 =7
5–2 =3
5 * 2 = 10
5 /2 =2
#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 << " = "
การกาหนดคาอธิบาย
(Comment)
้ น
// ข ้อความทีจ
่ ะใชเป็
คาอธิบาย ( 1 บรรทัด)
้ น
/* กลุม
่ ข ้อความทีใ่ ชเป็
คาอธิบาย ( มากกว่า 1
บรรทัด ) */
่ องการ
ผลการทางานทีต้
Input cost of goods = 433
[enter]
Input amount = 1000
[enter]
**************************
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
#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;
change=(amountcost);
five_hundred=change/5
00;
change=change (five_hundred*500);
one_hundred=change/1
twenty=change/20;
change=change (twenty*20);
ten=change/10;
change=change (ten*10);
five=change/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
Control Statement
โ ด ย ป ก ติ ก า ร ท า ง า น ข อ ง
คอมพิว เตอร์ จะท างานเรี ย งล าดั บ
คาสั่งลงมาตัง้ แต่ต ้นโปรแกรมจนจบ
โ ป ร แ ก ร ม แ ต่ ถ ้า เ ร า ต ้อ ง ก า ร
เปลีย
่ นแปลงขัน
้ ตอนการทางานของ
ค าสั่ ง เช ่น โดดข ้ามไปท าค าสั่ ง ใด
คาสงั่ หนึง่ หรือให ้วนกลับมาทาคาสงั่
ที่ เ ค ย ท าไ ปแ ล ว้ อี ก ลั กษ ณะ กา ร
ั่
้
ั่
่
คาสังควบคุ
ม(Control
Statement)
• คาสงั่ ควบคุมแบบไม่ม ี
่ goto
เงือ
่ นไข เชน
• คาสงั่ ควบคุมแบบมีเงือ
่ นไข
่ if , switch
เชน
• คาสงั่ ควบคุมให ้โปรแกรม
่
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
่
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
[enter]
Your grade is B
#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
่ องการ
ผลการทางานทีต้
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;
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 ;
่ องการ
ผลการทางานทีต้
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 ;
่ องการ
ผลการทางานทีต้
Input birth year :
2519
[enter]
You are Drakon
year
#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" ; break;
case 2 : cout<<"You are Sheep
year"; break;
case 3 : cout<<"You are
Monkey year"; break;
case 4 : cout<<"You are Hen
year";
break;
case 5 : cout<<"You are Dog
year";
break;
case 6 : cout<<"You are Pork
year";
break;
case 7 : cout<<"You are Rat
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
A B C D E F G H I J K L M N O P Q
R S T U V W X Y Z
Show Character z-a
z y x w v u t s r q p o n m l k j i h
#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<<"\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 << " ";
่ องการ
ผลการทางานทีต้
้ เลข 1 จนถึงค่าทีผู
่ ใ้ ช ้งานป้ อน 1+
แสดงผลรวมตังแต่
Input n umber : 100
[enter]
1+2+3+…+100 = 5050
#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+...+" <<
่ องการ
ผลการทางานทีต้
Input number : 5
[enter]
1! = 1
2! = 2
3! = 6
4! = 24
#include"iostream.
h"
#include"conio.h"
void main( )
{ int i,n,fac;
clrscr( );
cout << "Input
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
#include
"iostream.h"
#include
"conio.h"
void main( )
{
int a, b, c, i;
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
#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" ;
while (data != -999)
{ sum = sum + data ;
count = count + 1 ;
if (data > max) max
= data ;
else if (data < min)
min = data ;
cout << "Sum = " << sum <<
endl
<< "Max = " << max <<
endl
<< "Min = " << min <<
endl
<< "Average = " <<
sum/count << endl ;
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 : " ;
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
*****************
****
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
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;
}
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++)
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;
}
goto Statement
goto label
;
…
…
label :
statemen
label :
statemen
t;
…
goto label
ต ัวอย่าง
main() { int num;
cout << " enter from (0-9):
"; cin >> num;
if (num = = 5)
{ cout<<
"Correct“<<endl;
goto bye; }
cout<<" ***** Game Over
***** ";
การสุ่มค่าตัวเลข
ใช ้ 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
[enter]
less than 7 enter from (0-9) :
4
[enter]
more than 4 enter from (0-9)
:5
[enter]
#include"iostream.h"
#include"conio.h"
#include"stdlib.h"
void main( )
{ int
random_num,num,i;
clrscr( );
randomize( );
for(i=1; i<=3; i++)
{
cout << " enter
from (0-9): ";
cin >> num;
if (num = =
random_num)
{ cout<< "\n
else if
(num>random_num)
cout<<"\n less
than "<<num;
else
cout<<"\n more
than "<<num;
} //end for
cout<<"\n Sorry! the
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 จะเป็ นลาดับ ทีเท่
62799609168
5
Show number 5 in
sequence : 12
#include"iostre
am.h"
#include"conio.
h"
#include"stdlib.
h"
void main( )
{
for (i=1; i<=100;
i++)
{
random_num=rand
om(10);
cout<<
random_num << "
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]
=
#include"iostream.h"
#include"conio.h"
void main ( )
{ float cost, amount ;
char ch;
clrscr( );
do
{
cout << "Input cost
of goods = ";
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;
Array (ตัวแปรชุด )
array หมายถึง ชุดของ
ข ้อมูลทีม
่ จ
ี านวนแน่นอนและ
เป็ นข ้อมูลชนิดเดียวกัน ข ้อมูล
ิ
แต่ละรายการเรียกว่า สมาชก
ของอาร์เรย์ (element of
ิ ของ
array) แต่ละสมาชก
อาร์เรย์ม ี อินเด็กซ ์ (index)
One-dimension
array
Ex
การประกาศต ัวแปร
array
datatype variable
[number of array];
int mark[30] ;
การกาหนดค่าตัวแปร
array
่
• กาหนดค่าคงที
ให้กบ
ั Array
Variable[index]=
ค่าตัวแปร;
เช่น int Mark[5];
การกาหนดค่าตัวแปร
array
่
• กาหนดค่าคงทีให้กบ
ั สมาชิก
Array ทุกรายการ
for(int x=0;x<=ค่า
้ ด;x++)
สินสุ
Variable[x]=ค่าตัว
แปร;
เช่น int Mark[5];
การกาหนดค่าตัวแปร
array
่ นให้ก ับ
• กาหนดค่าเริมต้
สมาชิก Array
int Variable[
จานวน]={ค่าตัวแปร
,..,..};
เช่น int
การร ับข้อมู ลเข้าใน
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 ร ับค่ายอดขายของสมาช
่
พร ้อมหาผลรวม และค่าเฉลีย
ตัวอย่าง
เขียนโปรแกรมร ับยอดขาย ของพนักง
่ านวณและแสดงค่าเป็ นร ้อยละขอ
เพือค
่ *****Random
ผลการทางานทีต้
องการ
แสดงความถี่
ของเลข 0-9
ในการสุม
่
ตัวเลข 20
ครง้ั
Number*****
979906485432
43054978
*******Frequency
*******
0: 2
1: 0
2: 1
3: 2
4: 4
5: 2
6: 1
7: 2
#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
for (i=1; i<=20; i++)
{ random_num =
random(10);
cout << random_num << "
";
count[random_num] =
count[random_num]+1;
}
cout << endl;
cout
่ อEnter
ผลการทางานทีต้
งการnumber end
่
หาค่ากึงกลางข
้อมูล
(median) จาก
้
่
ตัวเลขทังหมดที
ผู ้ใช ้งานป้ อน โดย
้ ด
การร ับค่า จะสินสุ
่ อนค่า -999
เมือป้
by –999
5 [enter]
3 [enter]
7 [enter]
1 [enter]
4 [enter]
-999
[enter]
*****Data Before
Sort*****
5 3 7 1 4
*****Sort
Data*****
#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";
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;
}
if ((n-1)%2==0)
median=(data[(n1)/2]+data[(n-1)/2+1])/2.0;
else
median = (data[(n1)/2+1]);
cout << "\n\nMedian =
"<< median;
getch( ) ;
Two-dimension
array
การประกาศต ัวแปร
array
DataType
variable[Size1][Size
่ int data[4][3]
เชน
;
2];
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
ตัวแปร
Ex
int data[2][3];
array[index1][in
dex2]
data
0,0
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 มิต ิ
ตัวแปร
Exarray[index1][index2]=
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++)
ตัวแปร
่ นให้
กาหนดค่าเริมต้
สมาชิกทุกคน
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++)
การแสดงค่า Array 2
มิต ิ
cout<<ตัวแปร
[index1][index2];
หรือร ับค่าให้กบ
ั สมาชิกทุก
รายการ
for(int
x=0;x<number1;x++)
ตัวอย่าง
เขียนโปรแกรมร ับค่าตัวเลข Array
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; //ขึนบรรทั
ด
่
่
ใหม่เมือจะเริ
มแถวใหม่
}
หลักการ
่ องการ
ผลการทางานทีต้
แสดงผลเป็ น
Matrix และ
Transpose
*****Matrix*
Matrix
****
1
3
5
2
4
6
*Transpose
Matrix*
1 3 5
#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<<"\n*Transpose
Matrix*\n";
for (i= 0; i< 2; i++)
{
for ( j= 0; j< 3; j++)
cout << data[ j][i]
<< " ";
cout << endl;
}
การกาหนดตาแหน่ ง
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 <<
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 ;
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;
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 =
อาร ์เรย ์หลายมิต ิ
การประกาศต ัวแปร
อาร ์เรย ์หลายมิต ิ
DataType
variable[size1][size2]…[
่ เก็บข ้อมูลการขายสน
ิ ค ้า 2 ชนิด ภายใน 3
เชน
sizen]
เดือน ของพนักงานขาย
5 คน ;
int sale[5][3][2] ;
การอ้างอิงตัวแปร
อาร ์เรย ์หลายมิต ิ
variable [index1][index2]…[in
for ( i = 0; i < 5; i++)
for( j = 0; j < 3;
j++)
for( k=0; k <
2 ; k++)
่ องการ
ผลการทางานทีต้
การร ับยอดการขายสินค ้า 2 ชนิ ดจานวน 3 เดือน
ของพนักงาน 2 คน จากนั้นแสดง ผลว่าพนักงานแต่ละ
คน ขายสิ
นค ้าแต่1ละชนิ ดได ้คนละเท่าใด
person
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
#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];
}
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
<< " : "<<
่ านเสียง
คาสังด้
ใช ้ dos.h
sound(frequency
ใช ้ dos.h
_Hz)
;
nosound() ;
การกาหนดความหน่ วง
ใช ้ dos.h
delay(time_millis
econds) ;
#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
‘6’ = 440
‘2’ = 294
‘3’ = 330
‘4’ = 350
sound
‘7’ = 490
‘8’ = 520
‘9’ = end
#include"iostream.h
"
#include"conio.h"
#include"dos.h"
void main( )
{ int f;
char ch;
clrscr( );
do{
ch=getch();
switch(ch)
{ case '1': f = 262;
break;
case '2': f = 294;
break;
case '3': f = 330;
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;
ผลการทางาน
Input your name: Naten
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;
ผลการทางาน
Input your name: Naten
Your name is: Natenapa
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
struct struct_name
{
datatype
variable;
...
}
;
struct struc_name struc_variable ;
การอ้างอิงถึงสมาชิก
ของสตร ักเจอร ์
struc_variable.varia
ble = 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 "
โปรแกรมแสดงการร ับและ
่
ผลการทางานทีต้แสดงผลข
องการ้อมูลของพนักงาน 5
คนโดยนาสตร ักเจอร ์ และ
อาร ์เรย ์ มาใช ้งานร่วมกัน
#include"iostrea
m.h"
#include"conio.
h"
void main( )
{ int i;
struct person
{ char
name[20];
for (i=0; i<5; i++)
{ clrscr( );
gotoxy (15, 2); cout<<"Enter
Data"<<endl;
gotoxy (5, 3);
cout<<"==================
=====";
gotoxy (5, 9);
cout<<"==================
=====";
gotoxy (8, 4); cout<<
"Person"<< i+1 <<endl ;
clrscr( );
gotoxy (15, 2); cout<<"Show
Data"<<endl;
gotoxy (5, 3);
cout<<"==================
=====";
gotoxy (5, 11);
cout<<"==================
=====";
gotoxy (7, 4);
cout<<"No.";
gotoxy (12, 4); cout<<"Name";
for (i=0; i<5; i++)
{ gotoxy (7 , i+5);
cout<<i+1;
gotoxy (12, i+5);
cout<<per[i].name;
gotoxy (23, i+5);
cout<<per[i].age;
gotoxy (28, i+5);
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;
mypoint = &data ;
cout<<"data : Address =
"<< &data
<<" , Value = "<<
data << endl;
cout<<"mypoint: Address
= "<< &mypoint
<<" , Value =
"<<mypoint << endl;
ฟั งก ์ช ัน (Function)
ฟั ง ก์ ชั น ห ม า ย ถึ ง
สเตตเมนต์ช ุด หนึ่ง ซ งึ่ มี
ื่ เฉพาะ ส่ว นอืน
ชอ
่ ๆของ
โปรแกรมสามารถ
้
เรีย กใช สเตตเมนต์
ชด
ุ นี้
ประโยชน์ของฟั งก ์ช ัน
• ชว่ ยให ้ไม่ต ้องเขียนสเตต
เมนต์เดิมซ้ากันหลายครัง้ ใน
โปรแกรมเดียวกัน
• ชว่ ยให ้สามารถค ้นหาสว่ นที่
ผิดหรือสว่ นทีต
่ ้องปรับปรุงได ้
รวดเร็ว
• ทาให ้โปรแกรมมีขนาด
โครงสร ้างของฟั งก ์ช ัน
type function_name
(parameter)
{
statement ;
…
return value ;
}
การเรียกใช้ฟังก ์ช ัน
่ มก
• function ทีไม่
ี ารคืนค่า
(ไม่มโี ปรโตไทป์ )
function_name();
function_name(argume
nt);
่
การวางตาแหน่ ง
ฟั
งก
์ช
ัน
#include " header file "
dataType
function_name
(parameter)
{ statements ;
...
return value ;
}
void main ( )
#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( );
แบบมีพารามิเตอร ์
#include"iostream.h"
#include"conio.h"
void line (int n) ;
void main( )
{ clrscr( );
line(20);
cout<<"Siam
Computer"<<endl
<<"Test
Function"<<endl ;
line(15);
void line (int n)
{
int i;
for ( i=1; i<n; i++)
cout<<“ * ";
cout<<endl;
แบบสองพารามิเตอร ์
#include"iostrea
m.h"
#include"conio.
h"
void line (int n,
char c)
{ int i;
for ( i=1; i<n;
i++)
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;
่ องการ
ผลการทางานทีต้
#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<<"#";
}
void main( )
{ int x, y, w, h;
clrscr( );
cout<<"Input x: ";
cin>> x;
cout<<"Input y: ";
cin>> y;
cout<<"Input w: ";
cin>> w;
cout<<"Input h: ";
cin>> h;
clrscr ( );
่ การสร
้างฟั งก ์ช ันหาค่าของ
ผลการทางานทีต้
องการ
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( ) ; // ให้คา่ พิกเซลสูงสุด
getmaxy( ) ; // ให้คา่ พิกเซลสูงสุด
การกาหนดสีว ัตถุ
setcolor ( n )
;
โดย
n=1,2,…15
การกาหนดสีพน
ื้
setbkcolor ( n )
;
โดย
n=1,2,…15
การสร ้างตารางขนาด 3 แถว 3 คอลัมน์
่ นของตารางเป็ น (50,50) และแต่
โดยจุดเริมต้
ละเซลในตารางมี
ขนาด 50 * 50 พิกเซล
#include"graphic
s.h"
#include"conio.h
"
void main( )
{ int n,m,i;
n=m=0;
initgraph
for (i=1; i<=4; i++)
{
line(50, 50 * i
,200, 50 * i);
line(50 * i ,50, 50
* i,200);
}
getch( );
่ องการ
ผลการทางานทีต้
การสร ้างตารางตามขนาดแถวและ
่ ใ้ ช้งานกาหนด โดยจุดเริมต้
่ น
คอลัมน์ ทีผู
ของ ตารางเป็ น (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;
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( );
การวาดวงกลม
circle
(x,y,radius) ;
การวาดส่วนโค้ง
arc (x,y,st_angle,end_angle,radius);
การวาดวงรี
ellipse
(x,y,st_angle,end_angle,radiu
sx,radiusy);
่
่
การวาดสีเหลี
ยม
rectangle(left,top,right,bottom);
การวาดภาพหลาย
่
เหลียม
poly ( numpoints, polypoi
เป็ น
Array
่ องการ
ผลการทางานทีต้
่
การวาดภาพสามเหลียมโดยใช้
คาสัง่ draw
#include"graphics.h"
#include"conio.h"
void main( )
{ int n,m;
int
polypoint[8]={100,200,200,200,1
50,100,100,200};
n=m=0;
initgraph(&n,&m,"");
drawpoly(4,polypoint);
getch( );
่ องการ
ผลการทางานทีต้
้ โดยวงกลมทุกวงจะมีจด
การวาดวงกลมซาๆ
ุ
ศู นย ์กลางเดียวกัน ส่วนร ัศมีจะลดลงที่ ละ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, "");
do
{ for( i = 200; i
>= 0 ; i--)
{
setcolor(random
(15));
circle(x, y,
i);
delay(10);
การวาดภาพบาร ์
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(patter
n,color);
pattern = 0-12
color = 0-15
การระบายสีว ัตถุ
floodfill ( x, y,
bordercolor
)
;
ในกรณี ถา้ ค่าของ
โคออร ์ดิเนตอยู ่ภายในวัตถุจะ
เป็ นการระบายสีในวัตถุ แต่
ถ้าค่าของโคออร ์ดิเนตอยู ่
ภายนอกวัตถุจะเป็ นการระบาย
่ องการ
ผลการทางานทีต้
#include"grap
hics.h"
#include"conio
.h"
void main( )
{ int n,m;
n=m=0;
initgraph
setcolor
(BLUE);
circle
(150,100,50);
setfillstyle
(5,11);
floodfill
่ องการ
ผลการทางานทีต้
่
แสดงลวดลายแบบต่างในคาสังบาร
์ โดย
สร ้างบาร ์ขนาด 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,
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
}
่ องการ
ผลการทางานทีต้
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++) //
้
วนร ับข ้อมูลทังหมด
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- =
// วนสร ้างกราฟตามจานวนข ้อมูล
for (i=0; i <= number-1;
i++)
bar3d (50*(i+2)-20,
450, 50*(i+2),
450-data[i],
10, 1);
getch( );
การแสดงข้อความใน
กราฟฟิ ก
outtext(“string”);
or
outtextxy(x,y,”str
ing”);
การกาหนดรู ปแบบ
อ ักษร
settextstyle(font,direc
tion,charsize);
โดย
font= 0-10
direction= 0 or 1
OOP
OOP เป็ นวิธก
ี าร
เขียนโปรแกรมซงึ่ จัด
ให ้โปรแกรม
ดาเนินการกับกลุม
่
ของ ออบเจ็กต์
วิธก
ี ารสร ้างและใช้
ออบเจ็กต ์
class class_name
{
private: or public: or
protected:
datamember;
private: or public: or
protected:
การประกาศ
datamember
datatype
variable
;
การประกาศ
memberfunction
datatype function_name(parameter);
Member function
datatype
class_name::function_nam
e(parameter)
{
statement ;
…
}
การประกาศต ัวแปร
class
class_name
variable_calss;
การอ้างอิง data
member
variable_class.datam
ember=value;
การอ้างอิง member
function
variable=
variable_class.memberfunctio
n(argument);
Constructor(คอนสตัก
เตอร ์)
ั ที่
เป็ นเมมเบอร์ฟังก์ชน
ทางานโดยอัตโนมัตท
ิ ันที
ทีอ
่ อบเจ็กต์ถก
ู สร ้างขึน
้
class_name(parameter)
{
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;Mont
h=9;Day=19;}
~Date(){};
void SetDate(int Y,int
M,int D);
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 :
void Date::Display()
{
cout << "Year : " <<
Year <<endl
<< "Month :
"<<Month<<endl
<<"Day :
class test:
public Date
{ public:
int x,y;
};
void main( )
{ clrscr ( );
Date a;
a.Display (
);
a.GetDate
( );
a.Display (
);
test data;
data.Disp
lay( );
คลาสด้านการคานวณ
#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(“filena
me”);
การเขียน string ลง
ไฟล ์
WriteTextFile<<“
String ” ;
การเขียน อ ักขระลงไฟล ์
WriteTextFile.put(
char);
ใส่ขอ
้ มู ลลงในไฟล ์
#include"fstream.h"
void main( )
{
ofstream WriteTextFile("test1.txt");
WriteTextFile<<"Test File"<<endl;
WriteTextFile<<"Bye"<<endl;
}
่ าน
การเปิ ดไฟล ์เพืออ่
ifstream
ReadTextFile(“filena
me”);
การอ่าน string จาก
ไฟล ์
ReadTextFile.getline(
variable,size);
การอ่านไฟล ์อ ักขระ
ReadTextFile.get(v
ariable);
อ่านข้อมู ลจากไฟล ์
#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();
}
Download