This is default featured slide 1 title
Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.
This is default featured slide 2 title
Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.
This is default featured slide 3 title
Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.
This is default featured slide 4 title
Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.
This is default featured slide 5 title
Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.
Tuesday, June 21, 2016
Monday, April 18, 2016
មេរៀនទី១: អំពីភាសា C/C++
មេរៀនទី១: អំពីភាសា C/C++
C ឬ C++ ជា Programming Language ដែលសរសេរ សំរាប់បង្កើត កម្មវិធីដំណើរការលើកុំព្រូទ័រដ៏មានប្រសិទ្ធិភាព ដែលអនុញ្ញាតអោយ កុំព្រូទ័រ ធ្វើមួយជាក់លាក់និងត្រឹមត្រូវ ។
1. ប្រវត្តិសង្ខេប
-1960 គេបានបង្កើតភាសាសរសេរកម្មវិធីមួយឈ្មោះ ALGOLដែលជា មូលដ្ឋានគ្រឹះនៃរចនាសម្ព័ន្ធរបស់កម្មវិធី។-
-1967 លោក Martin Rechards បានបង្កើតភាសាសរសេរកម្មវិធី CPL បន្ទាប់មកលោក Martin Rechart បានបង្កើតភាសាមួយទៀតឈ្មោះ BCPL ដែលជា Version ថ្មីរបស់ CPL ។
-1970 លោក Ken Tomson ជាបុគ្គលិកម្នាក់នៅមន្ទីរពិសោធន៍ Bell បានរកវិធីវឌ្ឍន៍ពីភាសា BCPL មកបង្កើតភាសា B ដែលជា Version ថ្មីរបស់ BCPL ដែលមានលក្ខណះច្បាស់លាស់ដើម្បីប្រើប្រាស់ក្នុង System Programming
-1972 មានបុគ្គលិករួមការងារម្នាក់របស់លោក Ken Tomson ឈ្មោះ Dennis Ritchie បានយកការងារជាក់ស្តែងជាច្រើនក្នុងការវិវឌ្ឍន៍ភាសា BCPL ទៅ B មកបង្កើតភាសាមួយ ដែលយើងបានស្គាល់មកដល់បច្ចុន្បន្ន គឺភាសា C ។
-1983 វិទ្យាស្ថាន ANSI បានបង្កើតគណះកម្មាធិការមួយ ដើម្បីកំណត់ ភាសា C អោយមានលក្ខណះស្តង់ដា រួចកំណត់ឈ្មោះថា ANSI C ។
C / C++ ជាភាសាសរសេរកម្មវិធីមួយ ដែលមានភាពល្បី ល្បាញ ដូចដែលយើងបានដឹង ក៏ព្រោះតែប្រពន្ធ័ដំណើរការ UNIX គឺបង្កើត ឡើងដោយភាសា Assembly ហើយស្ទើរតែទាំងអស់ ត្រូវបាន សរសេរ ឡើងដោយភាសា C / C++ ។ ក្នុងអំឡុងចុងទសវត្ស 1970 នៅប្រទេសជឿនលឿនមួយចំនួន ភាសា C / C++ ត្រូវបានគេ ផ្សព្វ ផ្សាយនិងបញ្ចូោក្នុងកម្មវិធីសិក្សានៅតាមអនុវិទ្យាល័យ វិទ្យាល័យ និង មហាវិទ្យាល័យ ព្រោះវាជាភាសាមួយដែលនៅកៀក UNIX និងមាន C Compiler ដែលអាចអោយយើង Compile កូដដែលយើងបានសរសេរ ។
2. ការបង្កើតកម្មវិធីនៃភាសា C/C++
*Editing
ក្នុងដំណាក់កាលនេះគឺជាការបង្កើត Source Code របស់ C/C++ និងការដាក់ឈ្មោះអោយ Source Code ដែលបានសរសេររួច ។ ជាទូទៅឈ្មោះរបស់ Source Code ដែលបានដាក់ឈ្មោះអោយ
មានលក្ខណះសំគាល់ ( xxxxxxx.c ) សំរាប់ C និង ( xxxxxx.cpp ) សំរាប់ C++ ។
ក្នុងការសរសេរតំរូវអោយមានកម្មសរសេរកូដដូចជា Turbo C++/C , Code Block , Dev C++ …..
*Compiling
ក្នុងដំណាក់កាលនេះ Compiling បំលែង Source Code ទៅជា Machine Code ដែលកុំព្យូទ័រអាចយល់បាន ព្រមទាំងស្វែងរក រួចបង្ហាញនូវភាព Error (ប្រសិនបើមាន) ។ ប្រសិបបើគ្មានភាព Error កើតមានទេ នោះវានឹងបង្កើតបាន Object Code ដែលត្រូវផ្ទុកក្នុង File មួយផ្សេងទៀតគឺ Object File ដែលមានសំគាល់ ( xxxxxx.obj ) ។
*Linking
ក្នុងដំណាក់កាលនេះ Linker បានផ្សំបញ្ចូលទៅ Object Code ជាមួយនិង Module Code ដែលអានមកពី Program Library (ផ្នែកគាំ ទ្ររបស់ CPL) រួចបង្កើតជា Execut_able File ដែលមានលក្ខណះសំគាល់ ( xxxxxx.exe ) ។ ម្យ៉ាងវិញទៀត Linker ក៏អាចស្វែងរក និង បង្ហាញនូវភាព Error ផងដែរ ។
*Execution
ក្នុងដំណាក់កាលនេះ គឺជាដំណាក់កាលចុងក្រោយ នៃការ ដំណើរការកម្មវិធី ដែលដំណាក់កាលទាំងបីខាងលើ ដំណើរកាល បាន ដោយជោគជ័យ ។ ក៏ប៉ុន្តែក្នុងដំណាក់កាលនេះ ក៏មានភាព Error កើតមាន ផងដែរ ប្រសិនបើមានការខុសពីលក្ខខ័ណ្ឌកំណត់ណាមួយ ដូចជាការអោយតំលៃខុងពីលក្ខខ័ណ្ឌរបស់វា នេះគេហៅថា Logic Error ។
3. ទំរង់ទូទៅនៃភាសា C/C++
ឧទាហរណ៍
#include<stdio.h> ជា Library Function សំរាប់ភាសា C
#include<iostream> ជា Library Function សំរាប់ភាសា C++
using namespace std;
int x = 10; ជាការប្រកាសអញ្ញាតិប្រភេទ Global Declarations
int y = 15;
int main(){
int a = 50; ជាការប្រកាសអញ្ញាតិប្រភេទ Local Declarations
int b = 80;
printf(“value of x = %d\n”,x);
cout<<“value of x =”<<x<<endl;
……….
return 0;
}
4.Data Type
គឺជា Standard Type ដែលត្រូវបានគេប្រើសំរាប់ សំណត់ដែន តំលៃរបស់អថេរ ប្ញ ចំនួនថេរនោះនៅពេលដែលយើងប្រកាស់ ដើម្បី ប្រើប្រាស់ ។ Standard Type សំខាន់ៗនៅក្នុងភាសា C++ មានដូចជា
5.Variable
អថេរគឺជាទំហំដែលអាចប្រែប្រួលតំលៃ អថេររបស់កម្មវិធីគឺជាឈ្មោះនៃតំបន់ Memory រក្សារទិន្នន័យ។បណ្តាលអថេរត្រូវបានប្រកាស់ដោយរបៀបដាក់ឈ្មោះអថេរនៅផ្នែក Variable Declarition ។
ឈ្មោះអថេរ / Identifiers(Variable Name) : អថេរដែលត្រូវបាន ប្រកាស់សំរាប់ប្រើប្រាស់ក្នុងភាសាសរសេរកម្មវិធី ។ យើងអាចកំណត់ ឈ្មោះអោយអថេរ បានតាមសេចក្តីត្រូវការហើយឈ្មោះដែលប្រកាស់រួចត្រូវបានរក្សាទុកក្នុងMemory ដែលមានទំហំ អាស្រ័យទៅ តាមប្រភេទ ទិន្នន័យរបស់អថេរនោះ។
Identifiers ឈ្មោះដែលប្រើសំរាប់តាងអោយអញ្ញាតិណាមួយ ហើយការតាងឈ្មោះនេះមានលក្ខខ័ណ្ឌដូចតទៅ
-A,B,C….a,b,c…ជាតួអក្សរអង់គ្លេស រឺ underscore ” _ ” នាំមុខជានិច្ច
-ហាមដកឃ្លា
-1,2,3,4,…9 អាចមានតួលេខតែមិនអាចនៅខាងមុខគេទេ
-ហាមប្រើឈ្មោះជាន់នឹង Keyword
-ហាមប្រើ Symbol as & % + – / ^ . \ | ……
-អាចប្រើ underscore ” _ ” ចំនួសការដកឃ្លា
ឧទាហរណ៍ int score_st1; ត្រឹមត្រូវ
char 1s; មិនត្រឹមត្រូវ
float love-ss; មិនត្រឹមត្រូវ
int ss ee; មិនត្រឹមត្រូវ
Keyword គឺជាពាក្យគន្លឹះដែលគេប្រើប្រាស់ញឹកញាប់ក្នុងការសរសេរកូដ ហើយមានអត្ថន័យ នៃការប្រើប្រាស់ខុសប្លែកពីគ្នា ម្យ៉ាងទៀត Keyword ដែលមាននៅក្នុងភាសា C / C++ សុទ្ធសឹងតែជាអក្សរតូចទាំងអស់ ដូចជា asm , auto , bool ,break , case , catch , char , class , const , const_cast , continue , default , delete , do , double , dynamic_cast , else , enum , explicit , export , extern , false , float , for , friend , goto , reinterpret_cast , return , short , signed , sizeof , static , static_cast , struct , switch , template , this , throw , true , try , typedef , typeid , typename , union , unsigned , using , virtual , void , volatile , wchar_t , while …..
Declare Variable : DataType Name_Variable ;
ឧទាហរណ៍
int score ; // ការប្រកាសអថេរដោយមិនកំណត់តំលៃតំបូង
int score = 0; // កំណត់តំលៃតំបូងរបស់អថេរ ជាតួលេខចំនួនគត់ int
char charater;
char charater = ‘A'; // កំណត់តួអក្សរ១តួតំបូង ដោយប្រើ ”
string name;
string name = “Jack”; // កំណត់អក្សរតំបូង ដោយប្រើ “”
int score1,score2; // ការប្រកាសអថេរលើសពីរក្នុងពេលតែមួយ
Variable Declaration គឺជាការប្រកាសអអថេរ
ការប្រកាសអថេរមួយត្រូវមាន DataType Identifier ដែលបព្ចាប់ដោយ ; ។
អថេរមាន 2 ប្រភេទធំៗ គឹ Global variable និង Local variable .
Global Variable :
គឺជាអថេរដែលគេប្រកាសនៅក្រៅ Function សំរាប់អោយ Functions នានាយកទៅប្រើ ។
Local Variable :
គឺជាអថេរដែលគេប្រកាសនៅក្នុង Function សំរាប់អោយបានតែ Function
នោះយកទៅប្រើប៉ុណ្ណោះ ។
Initialization គឺជាការប្រកាសអថេរដោយផ្តល់តំលៃមួយទៅអោយអថេរដោយផ្ទាល់ នៅពេលគេប្រកាស់អថេរនោះ ។
ឧ.int x = 100;
6.Operator
Operator ក្នុងភាសា C / C++ គេបែងជាច្រើនផ្នែកដូចជា :
Arithmetic Operators ពិជណិត
Compound Assignment :
Increase and decrease (++ , –)
Relational and Equality Operators (==,!=,>,<,>=,<=)
ប្រើក្នុងលក្ខខ័ណ្ឌពិចារណា conditional
Logical Operators (!,&&,||)
Conditional Operator (?)
បើcondition នេះ true នោះវានឹងមានតំលៃ result1 តែបើ false នោះវានឹងមានតំលៃ result2
Comma Operator (,)
ការតំណើរការនៃកម្មវិធីគឺកំណត់ b = 3 ហើយក៏ធ្វើការបូកបន្ថែម2ទៅលើ b បន្ទាប់មកទៀតបញ្ចូលតំលៃចុងក្រោយទៅក្នុង a លទ្ធផលគឺ a = 5
7.Comment
8.Input / Output
+ printf ( ) : ជា Function សំរាប់ Print ទិន្នន័យដែលនៅក្នុងសញ្ញា “ … ” មកលើ Screen output ។ ពិនិត្យឧទាហរណ៍ខាងក្រោម ៖
# include < stdio.h >
# include < conio.h >
main ( )
{
int n; float x; char ch;
n = 5; x = 8.2; ch = ‘A’ ;
Ex1: int n,p;
scanf (“%d%d”, &n, &p);
Ex2: char c;
int n;
scanf (“%c%d”,&c,&n);
Ex4: char c;
int n;
scanf (“ %d %c”, &n, &c);
* Macro getchar( ); សំរាប់ Read character មួយពី Keyboard ។
# include <conio.h>
main ( )
{ clrscr ();
int n,p;
printf (“ Enter n = ”); scanf (“ %d”, &n) ;
while (getchar ( )! = ‘\n’);
printf (“ thank you for your Enter Value %d \n”, n) ;
printf (“ Enter P =”); scanf (“ %d”, &p) ;
while (getchar ( ) ! = ‘\n’) ;
printf (“thank you for your Enter Value %d \n”, p) ;
Note: ចំពោះ textbackground តំលៃ n គិតពី ( 0Æ 8 ) ។
Function lowvideo ( ) និង normvideo( ) : បញ្ជាពន្លឺ Text, lowvideo( ) ធ្វើអោយពន្លឺរបស់ text ខ្សោយៗទៅ normvideo( ) ធ្វើអោយពន្លឺរបស់ text ធមμតាឡើងវិញ ។
Ex: សរសេរពាក្យ WELCOME ( នោះអក្សរ W E មានពន្លឺ ភ្លឺ )
# include < stdio.h>
# include <conio.h>
main( );
{ clrscr () ;
highvideo( ); cprintf (“W”); lowvideo( ); cprintf (“ ELCOM”); highvideo( ); cprintf (“E”);
getch( );
return(0);
}
wherex និង wherey : បញ្ជាក់ប្រាប់ កូអរដោនេរបស់ Current cursor ។
Ex: # include < stdio.h>
# include <conio.h>
# include <stdio.h>
# include <conio.h>
main ( )
{
int n = 345;
int p = 5;
float x = 34.5678 ;
printf (“A :%d %f \n”, n,x ); printf (“B :%4d %10f \n”, n,x ); printf (“C :%2d %3f \n”, n,x );
printf (“D :%10.3f %10.3ef \n”, x,x );
printf (“E :%*d \n”, p,n ); printf (“F :%*.*f \n”, 12,5,x ); getch ( );
return (0);
}
2. ចូរប្រាប់ពីតំលៃនៃ Variable ប្រភេទ int គឺ n និង p ក្រោយពីអនុវត្ដន៏ Statement ខាងក្រោមៈ
scanf (“%4d %2d”, &n, &p ); ជាមួយទិន្នន័យដែលវាយបញ្ចូលដូចខាងក្រោម ៖
(សញ្ញា ^ ជាសញ្ញាដកឃ្លា )
a) 12^ 45 b) 123456
c) 123456^7 d) 1^458
e) ^^^4567^^891
3. ចូរសរសេរ screen output នៃកមμវិធីខាងក្រោម ៖
# include <stdio.h>
# include <conio.h>
main ( )
{
char firstname [ ]= “ Chan ” ; char lastname [ ] = “ Dara” ; float money = 5000 ;
clrscr ( ) ; gotoxy(10,10);
printf (“ %s %s, you will get $ %6.2f \n ”, firstname, lastname, money) ;
getch ( ) ;
return (0 ) ;
}
4. ចូរសរសេរ screen output នៃកមμវិធីខាងក្រោម ៖
# include <stdio.h>
# include <conio.h>
main ( )
{ int letter ;
/* print Value of code ASCII of characters */
letter = ‘e’ ;
printf (“Code ASCII of %c is %d \n” , letter , letter );
getch ( );
return (0);
}
5. ហេតុអ្វីគេនិយមប្រើ Function gets ( ) ជាង Function scanf ( )?
6. ចូររកកន្លែងខុសនៃ កមμវិធីខាងក្រោម ៖
main ( )
{
char str [ ] = {‘H’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ };
puts (str);
getch ( );
return (0);
C ឬ C++ ជា Programming Language ដែលសរសេរ សំរាប់បង្កើត កម្មវិធីដំណើរការលើកុំព្រូទ័រដ៏មានប្រសិទ្ធិភាព ដែលអនុញ្ញាតអោយ កុំព្រូទ័រ ធ្វើមួយជាក់លាក់និងត្រឹមត្រូវ ។
1. ប្រវត្តិសង្ខេប
-1960 គេបានបង្កើតភាសាសរសេរកម្មវិធីមួយឈ្មោះ ALGOLដែលជា មូលដ្ឋានគ្រឹះនៃរចនាសម្ព័ន្ធរបស់កម្មវិធី។-
-1967 លោក Martin Rechards បានបង្កើតភាសាសរសេរកម្មវិធី CPL បន្ទាប់មកលោក Martin Rechart បានបង្កើតភាសាមួយទៀតឈ្មោះ BCPL ដែលជា Version ថ្មីរបស់ CPL ។
-1970 លោក Ken Tomson ជាបុគ្គលិកម្នាក់នៅមន្ទីរពិសោធន៍ Bell បានរកវិធីវឌ្ឍន៍ពីភាសា BCPL មកបង្កើតភាសា B ដែលជា Version ថ្មីរបស់ BCPL ដែលមានលក្ខណះច្បាស់លាស់ដើម្បីប្រើប្រាស់ក្នុង System Programming
-1972 មានបុគ្គលិករួមការងារម្នាក់របស់លោក Ken Tomson ឈ្មោះ Dennis Ritchie បានយកការងារជាក់ស្តែងជាច្រើនក្នុងការវិវឌ្ឍន៍ភាសា BCPL ទៅ B មកបង្កើតភាសាមួយ ដែលយើងបានស្គាល់មកដល់បច្ចុន្បន្ន គឺភាសា C ។
-1983 វិទ្យាស្ថាន ANSI បានបង្កើតគណះកម្មាធិការមួយ ដើម្បីកំណត់ ភាសា C អោយមានលក្ខណះស្តង់ដា រួចកំណត់ឈ្មោះថា ANSI C ។
C / C++ ជាភាសាសរសេរកម្មវិធីមួយ ដែលមានភាពល្បី ល្បាញ ដូចដែលយើងបានដឹង ក៏ព្រោះតែប្រពន្ធ័ដំណើរការ UNIX គឺបង្កើត ឡើងដោយភាសា Assembly ហើយស្ទើរតែទាំងអស់ ត្រូវបាន សរសេរ ឡើងដោយភាសា C / C++ ។ ក្នុងអំឡុងចុងទសវត្ស 1970 នៅប្រទេសជឿនលឿនមួយចំនួន ភាសា C / C++ ត្រូវបានគេ ផ្សព្វ ផ្សាយនិងបញ្ចូោក្នុងកម្មវិធីសិក្សានៅតាមអនុវិទ្យាល័យ វិទ្យាល័យ និង មហាវិទ្យាល័យ ព្រោះវាជាភាសាមួយដែលនៅកៀក UNIX និងមាន C Compiler ដែលអាចអោយយើង Compile កូដដែលយើងបានសរសេរ ។
2. ការបង្កើតកម្មវិធីនៃភាសា C/C++
*Editing
ក្នុងដំណាក់កាលនេះគឺជាការបង្កើត Source Code របស់ C/C++ និងការដាក់ឈ្មោះអោយ Source Code ដែលបានសរសេររួច ។ ជាទូទៅឈ្មោះរបស់ Source Code ដែលបានដាក់ឈ្មោះអោយ
មានលក្ខណះសំគាល់ ( xxxxxxx.c ) សំរាប់ C និង ( xxxxxx.cpp ) សំរាប់ C++ ។
ក្នុងការសរសេរតំរូវអោយមានកម្មសរសេរកូដដូចជា Turbo C++/C , Code Block , Dev C++ …..
*Compiling
ក្នុងដំណាក់កាលនេះ Compiling បំលែង Source Code ទៅជា Machine Code ដែលកុំព្យូទ័រអាចយល់បាន ព្រមទាំងស្វែងរក រួចបង្ហាញនូវភាព Error (ប្រសិនបើមាន) ។ ប្រសិបបើគ្មានភាព Error កើតមានទេ នោះវានឹងបង្កើតបាន Object Code ដែលត្រូវផ្ទុកក្នុង File មួយផ្សេងទៀតគឺ Object File ដែលមានសំគាល់ ( xxxxxx.obj ) ។
*Linking
ក្នុងដំណាក់កាលនេះ Linker បានផ្សំបញ្ចូលទៅ Object Code ជាមួយនិង Module Code ដែលអានមកពី Program Library (ផ្នែកគាំ ទ្ររបស់ CPL) រួចបង្កើតជា Execut_able File ដែលមានលក្ខណះសំគាល់ ( xxxxxx.exe ) ។ ម្យ៉ាងវិញទៀត Linker ក៏អាចស្វែងរក និង បង្ហាញនូវភាព Error ផងដែរ ។
*Execution
ក្នុងដំណាក់កាលនេះ គឺជាដំណាក់កាលចុងក្រោយ នៃការ ដំណើរការកម្មវិធី ដែលដំណាក់កាលទាំងបីខាងលើ ដំណើរកាល បាន ដោយជោគជ័យ ។ ក៏ប៉ុន្តែក្នុងដំណាក់កាលនេះ ក៏មានភាព Error កើតមាន ផងដែរ ប្រសិនបើមានការខុសពីលក្ខខ័ណ្ឌកំណត់ណាមួយ ដូចជាការអោយតំលៃខុងពីលក្ខខ័ណ្ឌរបស់វា នេះគេហៅថា Logic Error ។
3. ទំរង់ទូទៅនៃភាសា C/C++
ឧទាហរណ៍
#include<stdio.h> ជា Library Function សំរាប់ភាសា C
#include<iostream> ជា Library Function សំរាប់ភាសា C++
using namespace std;
int x = 10; ជាការប្រកាសអញ្ញាតិប្រភេទ Global Declarations
int y = 15;
int main(){
int a = 50; ជាការប្រកាសអញ្ញាតិប្រភេទ Local Declarations
int b = 80;
printf(“value of x = %d\n”,x);
cout<<“value of x =”<<x<<endl;
……….
return 0;
}
4.Data Type
គឺជា Standard Type ដែលត្រូវបានគេប្រើសំរាប់ សំណត់ដែន តំលៃរបស់អថេរ ប្ញ ចំនួនថេរនោះនៅពេលដែលយើងប្រកាស់ ដើម្បី ប្រើប្រាស់ ។ Standard Type សំខាន់ៗនៅក្នុងភាសា C++ មានដូចជា
ប្រភេទ
|
ការប្រើប្រាស់
|
ទំហំ
|
ដែនកំណត់
|
char |
តាងអោយតួអក្សរ1តួអក្សរ
|
1byte
|
(signed)-128 to 127 |
short int |
តាងអោយចំនួនគត់
|
2byte
|
(signed) -32768 to 32767 (unsigned) 0 to 65535 |
int |
តាងអោយចំនួនគត់
|
4byte
|
(signed)-2147483648 to 2147483647 (unsigned) 0 to 4294967295 |
long int |
តាងអោយចំនួនគត់
|
4byte
|
(signed)-2147483648 to 2147483647 (unsigned) 0 to 4294967295 |
bool |
តាងអោយតំលៃពី គឺ
True/False |
1byte
|
true or false
|
float | តាងអោយចំនួសទស្សភាគ |
4byte
|
+/-3.4e +/-38(~7 digits) |
double | តាងអោយចំនួសទស្សភាគ |
8byte
|
+/-1.7e +/-308(~15 digits) |
long double | តាងអោយចំនួសទស្សភាគ |
8byte
|
+/-1.7e +/-308(~15 digits) |
wchar_t | តាងអោយតួរអក្សរច្រើនតួ |
2 or 4byte
|
1 wild charater |
អថេរគឺជាទំហំដែលអាចប្រែប្រួលតំលៃ អថេររបស់កម្មវិធីគឺជាឈ្មោះនៃតំបន់ Memory រក្សារទិន្នន័យ។បណ្តាលអថេរត្រូវបានប្រកាស់ដោយរបៀបដាក់ឈ្មោះអថេរនៅផ្នែក Variable Declarition ។
ឈ្មោះអថេរ / Identifiers(Variable Name) : អថេរដែលត្រូវបាន ប្រកាស់សំរាប់ប្រើប្រាស់ក្នុងភាសាសរសេរកម្មវិធី ។ យើងអាចកំណត់ ឈ្មោះអោយអថេរ បានតាមសេចក្តីត្រូវការហើយឈ្មោះដែលប្រកាស់រួចត្រូវបានរក្សាទុកក្នុងMemory ដែលមានទំហំ អាស្រ័យទៅ តាមប្រភេទ ទិន្នន័យរបស់អថេរនោះ។
Identifiers ឈ្មោះដែលប្រើសំរាប់តាងអោយអញ្ញាតិណាមួយ ហើយការតាងឈ្មោះនេះមានលក្ខខ័ណ្ឌដូចតទៅ
-A,B,C….a,b,c…ជាតួអក្សរអង់គ្លេស រឺ underscore ” _ ” នាំមុខជានិច្ច
-ហាមដកឃ្លា
-1,2,3,4,…9 អាចមានតួលេខតែមិនអាចនៅខាងមុខគេទេ
-ហាមប្រើឈ្មោះជាន់នឹង Keyword
-ហាមប្រើ Symbol as & % + – / ^ . \ | ……
-អាចប្រើ underscore ” _ ” ចំនួសការដកឃ្លា
ឧទាហរណ៍ int score_st1; ត្រឹមត្រូវ
char 1s; មិនត្រឹមត្រូវ
float love-ss; មិនត្រឹមត្រូវ
int ss ee; មិនត្រឹមត្រូវ
Keyword គឺជាពាក្យគន្លឹះដែលគេប្រើប្រាស់ញឹកញាប់ក្នុងការសរសេរកូដ ហើយមានអត្ថន័យ នៃការប្រើប្រាស់ខុសប្លែកពីគ្នា ម្យ៉ាងទៀត Keyword ដែលមាននៅក្នុងភាសា C / C++ សុទ្ធសឹងតែជាអក្សរតូចទាំងអស់ ដូចជា asm , auto , bool ,break , case , catch , char , class , const , const_cast , continue , default , delete , do , double , dynamic_cast , else , enum , explicit , export , extern , false , float , for , friend , goto , reinterpret_cast , return , short , signed , sizeof , static , static_cast , struct , switch , template , this , throw , true , try , typedef , typeid , typename , union , unsigned , using , virtual , void , volatile , wchar_t , while …..
Declare Variable : DataType Name_Variable ;
ឧទាហរណ៍
int score ; // ការប្រកាសអថេរដោយមិនកំណត់តំលៃតំបូង
int score = 0; // កំណត់តំលៃតំបូងរបស់អថេរ ជាតួលេខចំនួនគត់ int
char charater;
char charater = ‘A'; // កំណត់តួអក្សរ១តួតំបូង ដោយប្រើ ”
string name;
string name = “Jack”; // កំណត់អក្សរតំបូង ដោយប្រើ “”
int score1,score2; // ការប្រកាសអថេរលើសពីរក្នុងពេលតែមួយ
Variable Declaration គឺជាការប្រកាសអអថេរ
ការប្រកាសអថេរមួយត្រូវមាន DataType Identifier ដែលបព្ចាប់ដោយ ; ។
អថេរមាន 2 ប្រភេទធំៗ គឹ Global variable និង Local variable .
Global Variable :
គឺជាអថេរដែលគេប្រកាសនៅក្រៅ Function សំរាប់អោយ Functions នានាយកទៅប្រើ ។
Local Variable :
គឺជាអថេរដែលគេប្រកាសនៅក្នុង Function សំរាប់អោយបានតែ Function
នោះយកទៅប្រើប៉ុណ្ណោះ ។
Initialization គឺជាការប្រកាសអថេរដោយផ្តល់តំលៃមួយទៅអោយអថេរដោយផ្ទាល់ នៅពេលគេប្រកាស់អថេរនោះ ។
ឧ.int x = 100;
6.Operator
Operator ក្នុងភាសា C / C++ គេបែងជាច្រើនផ្នែកដូចជា :
Arithmetic Operators ពិជណិត
សញ្ញា
|
អត្តន័យ
|
ឧទាហរណ៍
|
+
|
បូក |
c = a + b ;
|
-
|
ដក |
d = e – d ;
|
*
|
គុណ |
a = b * c ;
|
/
|
ចែក |
a = b / c ;
|
%
|
សំណល់នៃការចែក |
k = m % n ;
|
សញ្ញា
|
ឧទាហរណ៍
|
អត្តន័យ
|
+=
|
c += b; |
c = c + b ;
|
-=
|
d -= e; |
d = d -e ;
|
*=
|
a *= c; |
a = a * c ;
|
/=
|
a /= c; |
a = a / c ;
|
%=
|
k %= n; |
k = k % n ;
|
ឧទាហរណ៍
|
អត្តន័យ
|
n++; | ដំណើរការនូវតំលៃ n រួចហើយទើបបានបូក 1 បន្ថែមទៅលើតំលៃ n |
++n; | បូក 1 បន្ថែមទៅលើតំលៃ n រួចហើយទើបដំណើរការនូវតំលៃ n |
n–; | ដំណើរការនូវតំលៃ n រួចហើយទើបបានដក 1 ពីលើតំលៃ n |
–n; | ដក 1 ពីលើតំលៃ nរួចហើយទើបដំណើរការនូវតំលៃ n |
ប្រើក្នុងលក្ខខ័ណ្ឌពិចារណា conditional
សញ្ញា
|
អត្ថន័យ
|
ឧទាហរណ៍
|
== | ស្មើ | if(a==5).. |
!= | មិនស្មើ | if(a!=5).. |
> | ធំជាង | if(a>5).. |
< | តូចជាង | if(a<5).. |
>= | ធំជាងរឺស្មើ | if(a>=5).. |
<= | តូចជាងរឺស្មើ | if(a<=5).. |
a
|
b
|
a&&b
|
a||b
|
!a
|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
ឧទាហរណ៍ |
!(5==5) // evaluates to false because the expression at its right (5==5) is true. !(6<=4) // evaluates to true because (6<=4) is false. !true // evaluates to false. !false // evaluates to true. |
បើcondition នេះ true នោះវានឹងមានតំលៃ result1 តែបើ false នោះវានឹងមានតំលៃ result2
Example
|
a = (7==5?4:3); // a = 3 cause 7==5 is false |
Code
|
លទ្ធផល
|
//conditional operator#include <iostream>using namespace std;
int main(){ int a,b,c; a=2; b=7; c=(a>b)?a:b; cout<<c; return 0; } |
7 |
Example
|
a = ( b = 3 , b+2 ); |
Example
|
int number; // declare variable number as integer char name; /* declare variable name as charactor */ |
//…………. ប្រើបានតែ១បន្ទាប់/*………… ប្រើបានច្រើនបន្ទាប់……………. ………*/ |
For C++
|
||
Key word | Meaning | Example |
cin | បញ្ចូលទិន្នន័យ | cin>>n; |
cout | បង្ហាញទិន្នន័យ | cout<<n; |
For C
|
||
Key word | Meaning | Example |
scanf | បញ្ចូលទិន្នន័យ | scanf(“%d”,n); |
printf | បង្ហាញទិន្នន័យ | printf(“%d”,n); |
មេរៀនទី៣: បញ្ចូល និង បញ្ចេញទិន្នន័យ
មេរៀនទី៣: បញ្ចូល និង បញ្ចេញទិន្នន័យ DATA Input & Output
I. អនុគមន៍ printf ( ) and putchar ( ) :+ printf ( ) : ជា Function សំរាប់ Print ទិន្នន័យដែលនៅក្នុងសញ្ញា “ … ” មកលើ Screen output ។ ពិនិត្យឧទាហរណ៍ខាងក្រោម ៖
# include < stdio.h >
# include < conio.h >
main ( )
{
int n; float x; char ch;
n = 5; x = 8.2; ch = ‘A’ ;
Note : + %d ដែល % ជា Format code និង d ជា Conversion code ។
+ បើចង់ print Expression (Variable ) នោះយើងត្រូវកំណត់ Format អោយតំលៃនៃកន្សោមនោះ ។
តារាង Format Code :
+ បើចង់ print Expression (Variable ) នោះយើងត្រូវកំណត់ Format អោយតំលៃនៃកន្សោមនោះ ។
តារាង Format Code :
Format Code |
អត្ថន័យ
|
%c | Print បណ្ដាចំនួនជា characters short, int |
%d | Print បណ្ដាចំនួន int , short |
%u | Print បណ្ដាចំនួន គμានសឭាា, unsigned int, |
%ld | Print បណ្ដាចំនួន long |
%X, %x | Print បណ្ដាចំនួនគត់ខាងក្រោមរាង hexa |
%o | Print បណ្ដាចំនួនគត់ខាងក្រោមរាង Octab ( គោល 8) |
%f %lf | Print float, double |
%s | Print string |
* បើចង់ print % ត្រូវសរសេរ
printf (“ 50 %%” ); Æ 50%
+ print សំលេង Beep :
printf (“ \a\a Enter Value :”);
+putchar ( c) : ជា Function សំរាប់ print character មួយមកលើ screen ។
ch = ‘a’; putchar (ch); Æ a
+ puts : សំរាប់ Print string មកលើ screen, ហើយ Cursor និងចុះបន្ទាត់,
puts (“this is a character”) ;
fprintf (stdprn, “ Print Data on printer!”); និង print
ទិន្នន័យទៅក្នុង printer ។ stdprn :សំរាប់បញ្ជាកុំព្យូទ័រអោយ print ទៅ
printer ។printf (“ 50 %%” ); Æ 50%
+ print សំលេង Beep :
printf (“ \a\a Enter Value :”);
+putchar ( c) : ជា Function សំរាប់ print character មួយមកលើ screen ។
ch = ‘a’; putchar (ch); Æ a
+ puts : សំរាប់ Print string មកលើ screen, ហើយ Cursor និងចុះបន្ទាត់,
puts (“this is a character”) ;
II. Print Data into printer :
III. scanf( ) function :
ជា Function សំរាប់ Read data from keyboardEx1: int n,p;
scanf (“%d%d”, &n, &p);
Ex2: char c;
int n;
scanf (“%c%d”,&c,&n);
បើ ៖ | Enter a25 |
Result
c = ‘a’ , n = 25
|
|
a^^25 | c = ‘a’ , n = 25 | ||
តែបើ ៖ | Ex3: char | c; | |
int n;scanf (“ %d %c”, &n, &c); | |||
Enter | Result | ||
12^a | Æ | n = 12; c = ‘^’; (មិនមែន = ‘a’ ទេ ) |
int n;
scanf (“ %d %c”, &n, &c);
Enter | Result | |
12^a | Æ | n = 12; c = ‘a’; |
12^^a | Æ | n = 12; c = ‘a’; |
Ex: c = getchar ( );
Ex: # include <stdio.h># include <conio.h>
main ( )
{ clrscr ();
int n,p;
printf (“ Enter n = ”); scanf (“ %d”, &n) ;
while (getchar ( )! = ‘\n’);
printf (“ thank you for your Enter Value %d \n”, n) ;
printf (“ Enter P =”); scanf (“ %d”, &p) ;
while (getchar ( ) ! = ‘\n’) ;
getch(); }
Statement : while (getchar ( ) != ‘n\’); មានន័យថា ៖ Read character រហូតដល់ Enter ទើបឈប់
Note : scanf (“%d”,&n); Readln (n) ក្នុង Pascal ។
យើងអាចជំនួស while (getchar ( ) ! = ‘\n’); ដោយ gets (ss);
scanf (“%d”, &n);
gets (ss);
ដើម្បី chear stdin យើងប្រើ Statemenut fflush(stdin);
* Control តើមាន character Read បញ្ចូល ឬអត់ ៖ kbhit ( ):
Function នេះនឹងត្រួតពិនិត្យមើល,មាន Read character ណាពី Keyboard ឬអត់, ហើយដែល Character កំពុងស្ថិតនៅក្នុង memory ។ kbhit ( ) សមមូល និង keypressed ក្នុង Pascal ។ វាផ្ទុកក្នុង <conio.h>
Statement : while (getchar ( ) != ‘n\’); មានន័យថា ៖ Read character រហូតដល់ Enter ទើបឈប់
Note : scanf (“%d”,&n); Readln (n) ក្នុង Pascal ។
<=>
while (getchar( ) ! = ‘\n’); (Read តំលៃហើយចុះបន្ទាត់តែម្ដង )យើងអាចជំនួស while (getchar ( ) ! = ‘\n’); ដោយ gets (ss);
scanf (“%d”, &n);
gets (ss);
ដើម្បី chear stdin យើងប្រើ Statemenut fflush(stdin);
* Control តើមាន character Read បញ្ចូល ឬអត់ ៖ kbhit ( ):
Function នេះនឹងត្រួតពិនិត្យមើល,មាន Read character ណាពី Keyboard ឬអត់, ហើយដែល Character កំពុងស្ថិតនៅក្នុង memory ។ kbhit ( ) សមមូល និង keypressed ក្នុង Pascal ។ វាផ្ទុកក្នុង <conio.h>
Ex:
Note :
# include <conio.h>
# include <stdio.h>
int main (void )
{ clrscr () ;
cprintf (“\r\npress any key to continue :”); while(!kbhit ( ) ); /* No action */ cprintf (“\r\n you have press a key ! \r\n”);
getch ( );
return (0);
}
-cprintf() :ជា Function <=> printf, ក្នុង cprintf =>
អត់ស្គាល់ \n ជាសញ្ញាចុះដើមបន្ទាត់ទេ, ត្រូវប្រើ\r\n ទើបស្គាល់ថា
ចុះដើមបន្ទាត់ ។# include <conio.h>
# include <stdio.h>
int main (void )
{ clrscr () ;
cprintf (“\r\npress any key to continue :”); while(!kbhit ( ) ); /* No action */ cprintf (“\r\n you have press a key ! \r\n”);
getch ( );
return (0);
}
* getch ( ) Function (សមមូលនឹង Readkey ក្នុង Pascal )
getche ( ) Function ប្រភេទ Macro Read character នីមួយៗពី keyboard ។ character
Read ពី keyboard ត្រូវបានសំដែងមកពី screen ។
ចំណែក Function getch ( ) ក៏ជាប្រភេទ Macro Read character ពី keyboard ដែរ តែ ,Character
ដែល Read ពី keyboard នោះមិនសំដែងមកលើ screen ទេ ។ ( សមមូល និង Readkey ក្នុង Pascal )
។ ដូច្នេះយើងអនុវត្ដន៏ Function getch( ) ក្នុងកមμវិធី Password ។
ម្យ៉ាងទៀតយើងប្រើ Functionនេះនៅ ចុងនៃគ្រប់កមμវិធីទាំង អស់ដើម្បីទប់
screen output ។getche ( ) Function ប្រភេទ Macro Read character នីមួយៗពី keyboard ។ character
Read ពី keyboard ត្រូវបានសំដែងមកពី screen ។
ចំណែក Function getch ( ) ក៏ជាប្រភេទ Macro Read character ពី keyboard ដែរ តែ ,Character
ដែល Read ពី keyboard នោះមិនសំដែងមកលើ screen ទេ ។ ( សមមូល និង Readkey ក្នុង Pascal )
+ Function getche( ) និង getch( ) ដូចគ្នាត្រង់ Read មួយ character ពី keyboard ដោយមិនចាំ
Enter, តែខុសគ្នាត្រង់ Function getche ( )និងសំដែង character ដែល Read ពី keyboard មកលើ screen ។
+ Function getche( ) និង getche( ) : Read ត្រង់ពី keyboard តែម្ដង, ដោយមិនឆ្លងកាត់ stdin ទេ ។
ដើម្បី Read password, សូមពិនិត្យ Example ខាងក្រោមដែលមានប្រើ Function get pass( ):
Ex: Read password :
# include < stdio.h>
# include <conio.h>
int main (void)
{
char *password; clrscr ();
password = getpass (“Enter password :”);
cprintf (“ password is : %s \r\n”, password);
getch( );
return (0);
}
* gets ( ) Function : ( សមមូល Readln(st) ក្នុង Pascal )
gets ( ) សំរាប់ Read string ពី keyboard រហូតដល់ Enter ទើបឈប់ ។ បន្ទាប់ពី Read រួចវាថែម
សញ្ញា បញ្ចប់ បន្ទាត់ (\0) នៅខាងចុង String ។
Ex:
# include < stdio.h>
# include < conio.h>
main ( )
{ clrscr ():
char name[80];
printf (“\n Enter your name :”);
gets (name);
printf (“\n your name is %s”, name );
getch ( );
return (0);
}
Enter, តែខុសគ្នាត្រង់ Function getche ( )និងសំដែង character ដែល Read ពី keyboard មកលើ screen ។
+ Function getche( ) និង getche( ) : Read ត្រង់ពី keyboard តែម្ដង, ដោយមិនឆ្លងកាត់ stdin ទេ ។
ដើម្បី Read password, សូមពិនិត្យ Example ខាងក្រោមដែលមានប្រើ Function get pass( ):
Ex: Read password :
# include < stdio.h>
# include <conio.h>
int main (void)
{
char *password; clrscr ();
password = getpass (“Enter password :”);
cprintf (“ password is : %s \r\n”, password);
getch( );
return (0);
}
* gets ( ) Function : ( សមមូល Readln(st) ក្នុង Pascal )
gets ( ) សំរាប់ Read string ពី keyboard រហូតដល់ Enter ទើបឈប់ ។ បន្ទាប់ពី Read រួចវាថែម
សញ្ញា បញ្ចប់ បន្ទាត់ (\0) នៅខាងចុង String ។
Ex:
# include < stdio.h>
# include < conio.h>
main ( )
{ clrscr ():
char name[80];
printf (“\n Enter your name :”);
gets (name);
printf (“\n your name is %s”, name );
getch ( );
return (0);
}
Note : Programmer បើចង់ភ្ជាប់ការងារក្នុងការ Readnumber និង String គប្បីប្រើប្រាស់
ដំណាក់ ការ ខាងក្រោម ៖
+ Read ចំនួនគត់ ៖ បន្ទាប់ពី Enter;
gets (str );
i = atoi (str); /* a to int */
+ Read ចំនួនពិត ៖ បន្ទាប់ពីចុច Enter;
j = atof (str); /* a to float */
Total Function input / output Data ក្នុង language :
+ getchar ( )and putchar( ): (macro) Read និង write character មួយពី keyboard ។
+ gets ( ) and puts ( ): Read និង write string ( គិតទាំងសញ្ញា space ) ។
+ scanf ( ): Read តាម format Code ( %), តាម Conversion code (d)
+ printf ( ): write Data តាម format code, Conversion code ។
+ getche() និង getch( ): Read character ត្រង់ពី Keyboard ដោយមិនចាំ Enter, មិនឆ្លងកាត់ stdin ។
* gotoxy (int x, int y );
នាំ Cursor ទៅទីតាំង x, y នៃ screen output ។ x = 1 ដល់ 80 , y = 1 ដល់ 25 ។
Ex: # include < stdio.h>
# include <conio.h>
int main (void)
{
clrsrcr( )
gotoxy (35,12); cprintf (“Hello world !!!”);
getch ( );
return (0);
}
clrscr ( ); (clear screen )
ជា Function សំរាប់លុបសំអាត screen output, បន្ទាប់មកវានាំ Cursor ទៅទីតាំង (1, 1) ។
clreol ( ): (Clear end of line )
ជា Function លុបបណ្ដា character ពីចំនុច Current cursor ទៅចុងបន្ទាត់ ។
textcolor (n) និង textbackground (n): ដាក់ពណ៏ text និងពណ៏ Background
តារាង Colors :ដំណាក់ ការ ខាងក្រោម ៖
+ Read ចំនួនគត់ ៖ បន្ទាប់ពី Enter;
gets (str );
i = atoi (str); /* a to int */
+ Read ចំនួនពិត ៖ បន្ទាប់ពីចុច Enter;
j = atof (str); /* a to float */
Total Function input / output Data ក្នុង language :
+ getchar ( )and putchar( ): (macro) Read និង write character មួយពី keyboard ។
+ gets ( ) and puts ( ): Read និង write string ( គិតទាំងសញ្ញា space ) ។
+ scanf ( ): Read តាម format Code ( %), តាម Conversion code (d)
+ printf ( ): write Data តាម format code, Conversion code ។
+ getche() និង getch( ): Read character ត្រង់ពី Keyboard ដោយមិនចាំ Enter, មិនឆ្លងកាត់ stdin ។
* gotoxy (int x, int y );
នាំ Cursor ទៅទីតាំង x, y នៃ screen output ។ x = 1 ដល់ 80 , y = 1 ដល់ 25 ។
Ex: # include < stdio.h>
# include <conio.h>
int main (void)
{
clrsrcr( )
gotoxy (35,12); cprintf (“Hello world !!!”);
getch ( );
return (0);
}
clrscr ( ); (clear screen )
ជា Function សំរាប់លុបសំអាត screen output, បន្ទាប់មកវានាំ Cursor ទៅទីតាំង (1, 1) ។
clreol ( ): (Clear end of line )
ជា Function លុបបណ្ដា character ពីចំនុច Current cursor ទៅចុងបន្ទាត់ ។
textcolor (n) និង textbackground (n): ដាក់ពណ៏ text និងពណ៏ Background
Const |
Value
|
BLACK |
0
|
BLUE |
1
|
GREEN |
2
|
CYAN |
3
|
RED |
4
|
MAGENTA |
5
|
BROWN |
6
|
LIGHT GRAY |
7
|
DARKGRAY |
8
|
LIGHT BLUE |
9
|
LIGHT GREEN |
10
|
LIGHT CYAN |
11
|
LIGHT RED |
12
|
LIGHT MAGENTA |
13
|
YELLOW |
14
|
WHITE |
15
|
BLINK |
128
|
Function lowvideo ( ) និង normvideo( ) : បញ្ជាពន្លឺ Text, lowvideo( ) ធ្វើអោយពន្លឺរបស់ text ខ្សោយៗទៅ normvideo( ) ធ្វើអោយពន្លឺរបស់ text ធមμតាឡើងវិញ ។
Ex: សរសេរពាក្យ WELCOME ( នោះអក្សរ W E មានពន្លឺ ភ្លឺ )
# include < stdio.h>
# include <conio.h>
main( );
{ clrscr () ;
highvideo( ); cprintf (“W”); lowvideo( ); cprintf (“ ELCOM”); highvideo( ); cprintf (“E”);
getch( );
return(0);
}
wherex និង wherey : បញ្ជាក់ប្រាប់ កូអរដោនេរបស់ Current cursor ។
Ex: # include < stdio.h>
# include <conio.h>
int main (void)
{
clrscr ( );
gotoxy (10,10);
printf (“coordinate x :%d y :%d \n”, wherex, wherey);
getch( ); return(0);
}
int main(void)
{
window (10, 10, 40, 11); textcolor (YELLOW);
textbackground (BLUE); clrscr ();
cprintf(“This is a test \n”);
getch( );
return(0);
}
{
clrscr ( );
gotoxy (10,10);
printf (“coordinate x :%d y :%d \n”, wherex, wherey);
getch( ); return(0);
}
window (int x1, int y1, int x2, int y2 );
ជា Function បង្កើត text window ដែលមានកូអរដោនេ មុំកែងខាង
លើផ្នែកខាងឆ្វេង ជាចំនុច (x1,y1) និង មុំកែងខាងក្រោមផ្នែកខាងស្ដាំ (x2, y2)
ហើយ statement print data on screen និង print data មកលើ Text window
ដែលទើបនឹងបង្កើតនេះ ។
Ex: # include <stdio.h>
# include <conio.h>int main(void)
{
window (10, 10, 40, 11); textcolor (YELLOW);
textbackground (BLUE); clrscr ();
cprintf(“This is a test \n”);
getch( );
return(0);
}
លំហាត់អនុវត្ដន៍
1. ចូរសរសេរ Screen output នៃ program ខាងក្រោម ៖# include <stdio.h>
# include <conio.h>
main ( )
{
int n = 345;
int p = 5;
float x = 34.5678 ;
printf (“A :%d %f \n”, n,x ); printf (“B :%4d %10f \n”, n,x ); printf (“C :%2d %3f \n”, n,x );
printf (“D :%10.3f %10.3ef \n”, x,x );
printf (“E :%*d \n”, p,n ); printf (“F :%*.*f \n”, 12,5,x ); getch ( );
return (0);
}
2. ចូរប្រាប់ពីតំលៃនៃ Variable ប្រភេទ int គឺ n និង p ក្រោយពីអនុវត្ដន៏ Statement ខាងក្រោមៈ
scanf (“%4d %2d”, &n, &p ); ជាមួយទិន្នន័យដែលវាយបញ្ចូលដូចខាងក្រោម ៖
(សញ្ញា ^ ជាសញ្ញាដកឃ្លា )
a) 12^ 45 b) 123456
c) 123456^7 d) 1^458
e) ^^^4567^^891
3. ចូរសរសេរ screen output នៃកមμវិធីខាងក្រោម ៖
# include <stdio.h>
# include <conio.h>
main ( )
{
char firstname [ ]= “ Chan ” ; char lastname [ ] = “ Dara” ; float money = 5000 ;
clrscr ( ) ; gotoxy(10,10);
printf (“ %s %s, you will get $ %6.2f \n ”, firstname, lastname, money) ;
getch ( ) ;
}
4. ចូរសរសេរ screen output នៃកមμវិធីខាងក្រោម ៖
# include <stdio.h>
# include <conio.h>
main ( )
{ int letter ;
/* print Value of code ASCII of characters */
letter = ‘e’ ;
printf (“Code ASCII of %c is %d \n” , letter , letter );
getch ( );
return (0);
}
5. ហេតុអ្វីគេនិយមប្រើ Function gets ( ) ជាង Function scanf ( )?
6. ចូររកកន្លែងខុសនៃ កមμវិធីខាងក្រោម ៖
main ( )
{
char str [ ] = {‘H’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ };
puts (str);
getch ( );
return (0);
Monday, April 11, 2016
មេរៀនទី៤: Control Structure
មេរៀនទី៤: Control Structure
បង្រៀនដោយលោកគ្រូ: ទាវ ឈុនណន
ព្រឹទ្ធបរុស មហាវិទ្យាល័យ វិទ្យាសាស្រ្ត និង បច្ចេកវិទ្យា
នៃ សាកលវិទ្យាល័យកម្ពុជា
Tel: 077778647-070778647-0977778647
Yahoo, Skype, google: chhunnan
4.1 និយមន័យ Control
structure គឺគេត្រូវបានកំណត់ថា ជាការ ត្រួត
ពិនិត្យដែលមានរចនាសម្ពន្ធ័លក្ខខណ្ឌ ដែលមានលក្ខខ័ណ្ធ ធ្វើការដ៏ដែលៗ
បើករណីពិតទើបអនុវត្តន៍ Statement បើវាមិនទិត វាមិនអនុវត្តន៍ Statement
នោះទេ គឺកម្មវិធីនឹងចាកចេញក្រៅ។ សម្រាប់ក្នុងភាសា VB.NET វិញ Control
structure មានចំនួនដែល សំខាន់។ព្រឹទ្ធបរុស មហាវិទ្យាល័យ វិទ្យាសាស្រ្ត និង បច្ចេកវិទ្យា
នៃ សាកលវិទ្យាល័យកម្ពុជា
Tel: 077778647-070778647-0977778647
Yahoo, Skype, google: chhunnan
១. if Statement
២. if else Statement
៣. if else_if Statement
៤. Select Case Statement
4.1.1 ការប្រើ IF STATEMENT បើសិន Expression ពិត (Expression មានតម្លៃមិនស្មើសូន្យ) statement ត្រូវបានអនុវត្តន៍ តែផ្ទុយ ទៅវិញ បើ expression មិនពិត (Expression មានតម្លៃ ស្មើសូន្យ) វានឹងអនុវត្តន៍ statement 2។ ដូច្នេះ IF statement មាន Syntax ដូចខាងក្រោម:
ឧទាហរណ៍១: ចូរសរសេរកម្មវិធី រកចំនួន ធំជាង នៃពីរចំនួន
ឧទាហរណ៍២: ចូរសរសេរកម្មវិធី រកចំនួនចែក ទៅលើលក្ខ ខណ្ឌ តួចែកស្មើរ ០
4.1.2 ការប្រើ IF ESLE STATEMENT សម្រាប់ IF else statement វិញវាមិនខុសពី IF statement ប៉ុន្មានទេ គ្រាន់តែវាមាន Else ថែម បើសិន Expression1 ពិតជាត្រូវការអនុវត្តន៍ statement1 ទី២ អត់ តែប់សិន expression1 ខុសត្រូវចូល statement2។ ដូច្នេះ IF statement មាន syntax ដូចខាងក្រោម:
ឧទាហរណ៍១: ចូរសរសេរកម្មវិធីរកចំនួនចែក ទៅលើលក្ខខ័ណ្ខ តួចែកស្មើរ 0
ឧទាហរណ៍២: ចូរសេសរកម្មវិធី រកចំនួន ធំជាង នៃបីចំនួន ដោយប្រើ IF Else Statement
ឧទាហរណ៍៣: ចូរសរសរកម្មវិធី រកចំនួនបង់ថ្លៃអគ្គិសនី
4.1.3 ការប្រើ IF ESLE-IF STATEMENT
if – else_if Statement : ត្រូវបានប្រើប្រាស់ដូច if-else statement គេត្រូវបានគេប្រើប្រាស់វា សម្រាប់ ដាក់ លក្ខខ័ណ្ឌច្រើន (លើសពី២) ។ ទម្រង់ទូទៅរបស់ if-else-if Statement ដូចខាងក្រោម
ឧទាហរណ៍១:
ឧទាហរណ៍១: ចូរសរសេរកម្មវិធី បូនសរុប ប្រាក់ខែ ជាប្រចាំសប្តាហ៍ របស់បុគ្គលិកម្នាក់ ដោយមានការ បន្ថែមម៉ោងស្មើរ ២ដងនៃម៉ោង ការងារ។
កូដ
ឧទាហរណ៍២: ចូររកនិន្ទេសសិស្ស
4.1.4 ការប្រើ SELECT CASE STATEMENT ចំពោះ Select Case វិញមាន ក៍មានដំណើរការ មិនខុសពី If else , if else if Statement ផងដែរ។ ខាងក្រោមនេះជារូបមន្តនៃការបង្កើត Select Case:
ឧទាហរណ៍១:
ឧទាហរណ៍៣: ចូររកនិន្ទេសិស្ស ដោយប្រើ Select Case ធ្វើការវាយបញ្ចូល ដោយក្នុង ចន្លោះ
១. ពិន្ទុក ០-៥៩ បង្ហាញលទ្ធផលជា Need to work harder
២. ពិន្ទុក ៥០-៥៩ បង្ហាញលទ្ធផលជា Average
៣. ពិន្ទុក ៦០-៦៩ បង្ហាញលទ្ធផលជា Above Average
៤. ពិន្ទុក ៧០-៨៤ បង្ហាញលទ្ធផលជា Good
៥. ពិន្ទុក ៨៤-១០០ បង្ហាញលទ្ធផលជា Excellence
4.1.5 ការប្រើ Text Box ដើម្បីបង្កើត calculator ដែលបន្ថែម បូកពីរចំនួនដោយប្រើ TextBox control។ ក្នុងកម្មវិធីនេះ លោក អ្នកបញ្ចួលប្រអប់ text box ពីរ និង label បី និងប៊ូតុង១។ ដែល បង្ហាញកូដដូចខាងក្រោម:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2, product As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
product = num1 + num2
Label1.Text = product
End Sub
មេរៀនទី៣: ប្រភេទទិន្នន័យនិងអថេរ
មេរៀនទី៣: ប្រភេទទិន្នន័យ និងអថេរ
បង្រៀនដោយលោកគ្រូ: ទាវ ឈុនណន
ព្រឹទ្ធបរុស មហាវិទ្យាល័យ វិទ្យាសាស្រ្ត និង បច្ចេកវិទ្យា
នៃ សាកលវិទ្យាល័យកម្ពុជា
Tel: 077778647-070778647-0977778647
Yahoo, Skype, google: chhunnan
3.1 អថេរ យើងដឹងថាការសរសេរកម្មវិធី
និមួយៗ មិនថាភាសា HTML, Java ទេ VB.NET ក៍ដូចគ្នាដែរ។ សម្រាប់ការ
ប្រកាសអថេរ វាមានសារ:សំខាន់ ក្នុងការរក្សាទុកតម្លៃជាបណ្តោះអាសន្ន
ពេលកម្មវិធី ដំណើរការ។ ដូចនេះខាងក្រោមនេះ ជារូបមន្តនៃការប្រកាសអថេរ។ព្រឹទ្ធបរុស មហាវិទ្យាល័យ វិទ្យាសាស្រ្ត និង បច្ចេកវិទ្យា
នៃ សាកលវិទ្យាល័យកម្ពុជា
Tel: 077778647-070778647-0977778647
Yahoo, Skype, google: chhunnan
ចំណាំ
-Keyword : សម្រាប់កំណត់ពី របៀបប្រើ អោយអថេរមាន ដូចជា (Public , Static , Dim , Private )
-Viable Name : ឈ្មោះអថេរ
-As : កំណត់ប្រភេទ Data_type ទៅអោយអថេរ
-Data_Type : ជាប្រភេទទិន្នន័យ របស់អថេរ
Ex.
-Public a as integer
-Dim a as string
-private k as long
កំណត់ចំណាំ ៖ សម្រាប់អថេរ ដែលប្រកាសក្នុង General Declaration គឺជា Static Variable
3.1.1 ដែនកំណត់របស់អថេរ ( Scope Of Variable )
ដើម្បីប្រកាសអថេរអោយ បានត្រឹមត្រូវ យើងត្រូវកំណត់ដែនកំណត់ របស់អេថេរអោយ បានត្រឹមត្រូវ (Life time Variable ) តើអថេរវាមានដែន រក្សាតម្លៃបានត្រឹមណា។ ដូចដែលបានចែកជា ៥ របៀបនៃការប្រកាស អថេរគឺ:
-Global Variable
-Global Friend Variable
-Module Level Variable
-Dynamic local Variable
-Static Local Variable
ខាងក្រោមនេះជារបៀបនៃការប្រកាសអថេរនីមួយៗ
3.1.1.1 ការប្រកាសអថេរជា Global Variable ៖
-គេប្រកាសវានៅ Standard Module ( Code )
– Keyword ជា Public
– Scope របស់វាគឺប្រើបានគ្រប់ Form ក្នុង Project
Life Time : នៅពេកបញ្ចប់ Application
-Ex. Public a as integer
3.1.1.2 ការប្រកាសអថេរជា Global Friend Variable
អត្ថន័យនៃការប្រកាសអថេរជា Global Friend Variable ៖
– គេប្រកាសវានៅ Standard Module ( Code )
– Keyword ជា Friend
– Scope របស់វាគឺ ប្រើបានគ្រប់ Form ក្នុង Project
– Life Time : នៅពេលបញ្ចប់ Application
-Ex. Friend ak as String
3.1.1.3 ការប្រកាសអថេរជា Module Level Variable
អត្ថន័យនៃការប្រកាសអថេរជា Module Level Variable ៖
-គេប្រកាសវានៅ Standard Module , Form Module ,Class Module
– Keyword ជា Private / Dim
– Scope របស់វាគឺ ប្រើបានតែក្នុង Module ដែលប្រកាស
– Life Time : នៅពេលបញ្ចប់ Application
-Ex. Dim/Private c as String
3.1.1.4 ការប្រកាសអថេរជា Dynamic Local Variable
អត្ថន័យនៃការប្រកាសអថេរជា Dynamic Local Variable
-គេប្រកាសវានៅ Procedure ( Sub Procedure , Function Procedure)\
– Keyword ជា Dim
– Scope របស់វាគឺ ប្រើក្នុង Procedure ដែលប្រកាស
– Life Time : នៅពលបញ្ចប់ Application
-Ex. Dim c as String
3.1.1.5 ការប្រកាសអថេរជា Static Local Variable
អត្ថន័យនៃការប្រកាសអថេរជា Static Local Variable
-គេប្រកាសវានៅ Procedure ( Sub Procedure , Function Procedure)
– Keyword ជា Static
– Scope របស់វាគឺ ប្រើបានតែក្នុង Procedure ដែលប្រកាស
– Life Time : នៅពេលបញ្ចប់ Application
-Ex. Static c as String
3.1.2 លក្ខណ:នៃការប្រកាសអថេរ
ការប្រកាសអថេរមួយ មិនមែន ជាការប្រកាសលេង លេងនោះទេ។ វាមានលក្ខណ:ដូចជា:
– មិនត្រូវប្រកាសអថេរអោយដូច Keyword ( dim , Private , Static ,…)
– មិនត្រូវប្រកាសអថេរជាលេខ (223)
– មិនត្រូវប្រកាសអថេរ ដោយដកឃ្លា បើចងដកឃ្លាត្រូវដាក់ Under Score ( a ad ជា a_ad )
– មិនត្រូវប្រកាសអថេរជាសញ្ញា (‘’&($#)
– ការប្រកាសត្រូវដាក់ Keyword មុនបន្ទាប់ម ឈ្មោះអថេរ បន្ទាប់មក As បន្ទាប់មក Data_type
3.2 ប្រភេទទិន្នន័យ Data_type
3.2.1 និយមន័យ ប្រភេទទិន្នន័យ គឺចាសំណុំទិន្នន័យ ដែលគេប្រើ សម្រាប់កំណត់លក្ខណ:អថេរមួយ ឬច្រើន។ ការប្រកាសអថេរគឺត្រូវធ្វើឡើង ដោយភ្ជាប់ជាមួយ ប្រភេទទិន្នន័យសម្រាប់កំណត់លក្ខណ: អថេរនោះ។
3.2.2 ប្រភេទទិន្នន័យ ប្រភេទទិន្នន័យ មានចំនួន ៨ ដែលគេប្រើញឹកញាប់ជាងគេ ក្នុងគ្រប់ភាសាដូចជា VB.NET, HTML….។
ខាងក្រោមនេះជាប្រភេទទិន្នន័យ និងទំហំរបស់វា។
ខាងក្រោមនេះ ជាឧទាហរណ៍ នៃការប្រកាសអថេរ ជាប្រភេទទិន្នន័យនិមួយៗ។
១. ជា Byte , Integer , Long
២. Single, Double
ឧទារហណ៍ ១
ឧទារហណ៍ ២ គេដឹងថាក្រុមហ៊ុន មួយមានគោលការណ៍ធ្វើការចំនួន ៨ម៉ោងក្នុងមួយថ្ងៃ ដោយ ប្រាក់ឈ្នួល ក្នុងមួយម៉ោង ២០$។ ប្រសិនបើបុគ្គលិកណាធ្វើការលើសពី ៨ម៉ោងក្នុងមួយថ្ងស គេគិតប្រាក់ បន្ថែមចំនួន ១ម៉ោង ២០$ និងការថែម ២០% ដោយគិតប្រាក់ម៉ោងបន្ថែមអ្វីទាំងអស់។
ឧទារហណ៍ ៣ ចូរសរសេរកម្មវិធី បូក ដក គុណ ចែក នៃពីរចំនួនខាងក្រោម
កូដ
ឧទារហណ៍ ៤ ចូរសរសរកម្មវិធី បូកសរុប ពិន្ទុក និងមធ្បមភាគសិស្សតាម មុខវិជ្ជា
កូដ
ឧទារហណ៍ ៥ ចូរសរសេរកម្មវិធីរកចំនួន សន្លឹកលុយដុល្លា
កូដ
ឧទារហណ៍ ៦ ចូរសរសេរកម្មវិធី រកចំនួន គិតម៉ោងខាងក្រោម
មេរៀនទី២: លក្ខណ:កម្មវិធី VB.NET
មេរៀនទី២: លក្ខណ:របស់កម្មវិធី VB.NET
បង្រៀនដោយលោកគ្រូ: ទាវ ឈុនណន
ព្រឹទ្ធបរុស មហាវិទ្យាល័យ វិទ្យាសាស្រ្ត និង បច្ចេកវិទ្យា
នៃ សាកលវិទ្យាល័យកម្ពុជា
Tel: 077778647-070778647-0977778647
Yahoo, Skype, google: chhunnan
2.1 ឧបករណ៍ Toolboxព្រឹទ្ធបរុស មហាវិទ្យាល័យ វិទ្យាសាស្រ្ត និង បច្ចេកវិទ្យា
នៃ សាកលវិទ្យាល័យកម្ពុជា
Tel: 077778647-070778647-0977778647
Yahoo, Skype, google: chhunnan
នៅក្នុងកម្មវិធីណាក៍សុទ្ធតែមាន Toolbar មកជាមួយពីព្រោះវាមានសារ:ប្រយោជន៍ ក្នុងការបង្កើត ឧបករណ៍ អ្វីមួយក្នុងកម្មវិធីនោះ។ ដូច្នេះកម្មវិធី Visual Studio 2008 នេះវិញក៍មាន toolbar ដែរ។ ដែលមានដូចជា
-textbox –button
-Label -Combo box
-Checkbox -មានផ្សេងទៀត
ខាងក្រោមនេះជាតួនាទីរបស់ Toolbox និមួយៗ
ឧបករណ៍
2.2 Solution Explorer Solution Explorer គឺជាកន្លែងមួយ សម្រាប់បង្ហា ញពី Project ឬ Form ឬ Module ទាំងអស់ដែលមាន នៅក្នុង Project របស់យើង ។ ខាងក្រោមនេះជា Project មួយដែលមាន Form ជាច្រើន។
2.3 Property Object Property គឺជា Object ដែលប្រើសម្រាប់កែរប្រែទៅលើ Toolbox ណាមួយ ។ ខាងក្រោមនេះ ជាឧបករណ៍ នៅលើ button។ បញ្ជាក់ property របស់ toolbox នីមួយមានលក្ខណ: ខុសគ្នា។ ខាក្រោមជាឧទាហរណ៍ property របស់ textbox មួយ:
Property ដែលសំខាន់ៗសម្រាប់ប្រើ
-Name : សម្រាប់ដាក់ឈ្មោះអោយ Textbox
-BackColor : សម្រាប់ដាក់ពណ៌អោយ Textbox
-ForeColor : ពណ៍ អក្សរក្នុង Textbox
-Font : សម្រាប់ប្តូរស្ទាយអក្សរ ទំហំ ពណ៌ ក្នុង Textbox