មេរៀនទី១៣: សរសេរកម្មវិធី C
មេរៀនទី១៣: សរសេរកម្មវិធី C
I. Introduction to C++ Language:C/C++ Programming Language ជាភាសាកម វμ ិធីទូទៅមួយដែលស្ថិតនៅក្នុង ភាសាកមវμ ិធីកំរិតខ្ពស់ ( High Level Programming Language) ។ គេបង្កើត C ដើម្បីសរសេរ System program គេថា C ជាភាសា ដើម កំណើតរបស់ UNIX Operating System។ C/C++ ត្រូវបានគេចាត់ទុកជា ភាសាមួយ មាន ប្រជាប្រិយក្នុងការសរសេរភា្ជប់ Application ជាមួយនិង System ដោយភាសា C/C++ ជាភាសាកំរិតខ្ពស់ (High Level Language) និង មាន Compiler បកប្រែពាក្យ បញ្ជាអោយទៅជាភាសា ម៉ាស៊ីន រួច មកជាភាសាដែលអាចអានបានដោយភាសារបស់មនុស្ស។ C មិន អាស្រ័យ តែ នៅលើ O.S(Operating System) រឺ ម៉ាស៊ីនណាមួយ ដោយឡែកឡើយ មាននយ័ថា C ត្រូវបានគេយកទៅប្រើប្រាស់ ជាមួយ O.S និង ម៉ាស៊ីនផ្សេងៗតាម Object របស់វា។
ឆ្នាំ 1970 លោក Ken Thompson បានរៀបចំភាសា B Language ដែលមានឥទ្ធិពលនៅលើ Basic Computer Programming Language ដែលបង្កើតដោយ លោក Martin Richard។
ឆ្នាំ 1972 លោក Dennis Ritchie បានធ្វើការកែប្រែ B Programming Language អោយទៅជា C Programming Language នៅឯ Bell Telephone Laboratories។
ឆ្នាំ 1978 លោក Brain W Kernighen និង Dennis Ritchie បានបោះពុម្ពសៀវភៅ The C Programming Language (First edition) ។ ឆ្នាំ 1982 ទើបគេបង្កើត ANSI(American National Standard Institute) ដោយផ្ដើម សរសេរ នៅ អនុគមន៍ដែល ជា Standard Library បញ្ចូលទៅក្នុង Compiler ដើម្បីប្រើប្រាស់ជា ទូទៅរហូតមកដល់សព្វថ្ងៃនេះ។ នៅឆ្នាំ 1988 គេចាប់ផ្ដើមបង្កើត OOP( Object Oreinted Programming)
ដោយលោក Brjare Stron Stup នៅឯ Bell Laboratories ដែលក្នុងគោលបំណងដើម្បីបង្កើតនូវ Application Wizard និង System Programming។
II. ទំរង់ទូទៅនៃC++:
#include<iostream.h>//Header file/Preprocessor/Library
#include<iostream.h>//Header file/Preprocessor/Library
void main(){//ចំនុចចាប់ផ្ដើមនៃកមμវិធី
statement1;
statement2; Block of statement
………….;
}// ចំនុចបញ្ចប់នៃកមμវិធី
-បើកកមμវិធី C/C++:
- C:\> TC\Bin\Tc.exe
- Copy: Ctrl+Insert
- Past: Shift+Insert#include<iostream.h>//Header file/Preprocessor/Library
int main(){//ចំនុចចាប់ផ្ដើមនៃកមμវិធី
statement1;
statement2; Block of statement
………….;
return 0;//បញ្ជាក់ថាដំណើរការដល់ទីបញ្ចប់
}// ចំនុចបញ្ចប់នៃកមμវិធីstatement1;
statement2; Block of statement
………….;
return 0;//បញ្ជាក់ថាដំណើរការដល់ទីបញ្ចប់
#include<iostream.h>//Header file/Preprocessor/Library
void main(){//ចំនុចចាប់ផ្ដើមនៃកមμវិធី
statement1;
statement2; Block of statement
………….;
}// ចំនុចបញ្ចប់នៃកមμវិធី
-បើកកមμវិធី C/C++:
- C:\> TC\Bin\Tc.exe
- Compiler: Alt +F9
- Run (Make EXE): Ctrl +F9- Copy: Ctrl+Insert
- Cut: Shift+Delete

Ex:
cout<< “Hello world!”<<endl;
cout>>x;
IV. អនុគមន៍getch( ):
ប្រើសំរាប់ទប់ Screen output ដើម្បីអោយយើងអាចពិនិត្យលទ្ធផល (Result) នៃកមμវិធីនៅពេលដែលយើង ដំណើរការកមμវិធី ។ អនុគមន៍ getch( ) ស្ថិត នៅ library conio.h (Console Input/Output library) ។
V. Input and Output function:
- cout<< “String”/Variables; ប្រើសំរាប់បង្ហាញ string ឬ តំលៃមកលើ Screen ។
Ex: int x=10;
cout<< “x=”<<x;
- cin>>Var1,var2,var3, …; ប្រើសំរាប់បញ្ចូលទិន្ននយ័តាមរយះ Keyboard
ទៅអោយ var1,var2,…
Ex: int x;
cin>>x;
VI. Data Type:cout<< “Hello world!”<<endl;
cout>>x;
IV. អនុគមន៍getch( ):
ប្រើសំរាប់ទប់ Screen output ដើម្បីអោយយើងអាចពិនិត្យលទ្ធផល (Result) នៃកមμវិធីនៅពេលដែលយើង ដំណើរការកមμវិធី ។ អនុគមន៍ getch( ) ស្ថិត នៅ library conio.h (Console Input/Output library) ។
V. Input and Output function:
- cout<< “String”/Variables; ប្រើសំរាប់បង្ហាញ string ឬ តំលៃមកលើ Screen ។
Ex: int x=10;
cout<< “x=”<<x;
- cin>>Var1,var2,var3, …; ប្រើសំរាប់បញ្ចូលទិន្ននយ័តាមរយះ Keyboard
ទៅអោយ var1,var2,…
Ex: int x;
cin>>x;
VI.1. Definition of Data-Type: ជាការកំណត់លក្ខណះទៅលើអថេរ (Variables) និមួយៗ ដែលបាន ប្រកាស។
VI.2. Type of Data-Type: Data type មានច្រើនប្រភេទដូចជាៈ
- Scalars type
- Structure type
- Pointer type

Keyword | Byte | Limited |
Char | 1 | -128 ដល់ 128 |
Int | 2 | -32768 ដល់ 32768 |
Long | 4 | -2147483648 ដល់ 2147483648 |
Float | 4 | 3,4 .10-38 ដល់ 3,4 .1038 |
Double | 8 | 1,7.10-308 ដល់ 1,7.10308 |
Unsigned int | 2 | 0 ដល់ 65535 |
Note: អនុគមន៍ Sizeof()ប្រើសំរាប់ផ្ដល់ទំហំនៃប្រភេទរបស់ទិន្ននយ័ (Data type
)ផ្ដល់តំលៃជាចំនួន integer។
)ផ្ដល់តំលៃជាចំនួន integer។
Syntax:
int sizeof(Data-Type/var);
Ex:
cout<<“Size of Integer=”<< sizeof(int);
Output:
Size of Integer=2
VII. Variables:
7.1. Definition of Variable: ជាអថេរ ឬ Address ប្រើសំរាប់រក្សានូវតំលៃជាបណ្ដោះអាសន្នក្នុង Memory នៅពេលកមμវិធីដំណើរការ (Run time) ។ តំលៃនេះត្រូវបានបាត់បង់ទៅវិញនៅពេលគេឈប់ដំណើរការ កមμវិធី (Stop Run) ។
7.2. Syntax:
Ex:VII. Variables:
7.1. Definition of Variable: ជាអថេរ ឬ Address ប្រើសំរាប់រក្សានូវតំលៃជាបណ្ដោះអាសន្នក្នុង Memory នៅពេលកមμវិធីដំណើរការ (Run time) ។ តំលៃនេះត្រូវបានបាត់បង់ទៅវិញនៅពេលគេឈប់ដំណើរការ កមμវិធី (Stop Run) ។
7.2. Syntax:
int x,y;
Data-type VarName1, VarName2, …, VarName n;
7.3. Assignment Statement: ជាការផ្ទេរតំលៃទៅអោយអថេរ (Variable) ។
Ex1:
- int x;
x=20;
- int y=30;
Ex1: សរសេរ Program ដើម្បីគណនា Y=4×5 + 2×2 + 3x + 1
#include<iostream.h>
#include<math.h>
#include<conio.h> void main(){ clrscr();
float x, y;
cout<< “Please Input x=”; cin>>x; y=4*pow(x,5)+2*pow(x,2)+3*x+1;
cout<< “y=”<<y<<endl;#include<iostream.h>
#include<math.h>
#include<conio.h> void main(){ clrscr();
float x, y;
cout<< “Please Input x=”; cin>>x; y=4*pow(x,5)+2*pow(x,2)+3*x+1;
getch( );
}
VIII. Constants:
VIII.1. Definition of Constants:
ជាតំលៃដែលមិនភ្លាស់ប្ដូរក្នុងពេលកមវμ ិធីដំណើរការ (Process) ។ គេអាចផ្លាស់ ប្ដូរតំលៃវាបានតែតាមរយះ Source codeតែប៉ុណ្ណោះ។
VIII.2. Memory Constants: ត្រូវបានគេប្រកាសដោយ Keyword const ។
Ex: const int num=100;
const float y=20.00;
VIII.3. Define Constants: ជាការបង្កើត Constant តាមរយះការប្រើ # (Signal number) និង Keyword define សំរាប់កំណត់ពី Pre-Processor ក្នុងកមμវិធី។
Ex1:
#define PI 3.14
Ex2: កមវμ ិធីសំរាប់ គណនាក្រលាផ្ទៃរង្វង់ៈ
#include<iostream.h>
#include<conio.h>
#define PI 3.14 void main(){ clrscr();
float radius, circle_area;
cout<< “Please Input radius of Circle=”;
cin>>radius;
circle_area=PI * (radius * radius);
cout<< “Circle Area =”<<circle_area<<endl;
getch();
}
IX. Comments and Escape Code:
- Comments: ជារការធ្វើកំណត់សំគាល់នៅក្នុងកមμវិធី។ វាមិនមាន
ដំណើរការទេនៅពេល Run Time។
នៅក្នុង C ++ មាន Comments ពីរគឺៈ
- // One Line
- /* Multiple Line */VIII. Constants:
VIII.1. Definition of Constants:
ជាតំលៃដែលមិនភ្លាស់ប្ដូរក្នុងពេលកមវμ ិធីដំណើរការ (Process) ។ គេអាចផ្លាស់ ប្ដូរតំលៃវាបានតែតាមរយះ Source codeតែប៉ុណ្ណោះ។
VIII.2. Memory Constants: ត្រូវបានគេប្រកាសដោយ Keyword const ។
Ex: const int num=100;
const float y=20.00;
VIII.3. Define Constants: ជាការបង្កើត Constant តាមរយះការប្រើ # (Signal number) និង Keyword define សំរាប់កំណត់ពី Pre-Processor ក្នុងកមμវិធី។
Ex1:
#define PI 3.14
Ex2: កមវμ ិធីសំរាប់ គណនាក្រលាផ្ទៃរង្វង់ៈ
#include<iostream.h>
#include<conio.h>
#define PI 3.14 void main(){ clrscr();
float radius, circle_area;
cout<< “Please Input radius of Circle=”;
cin>>radius;
circle_area=PI * (radius * radius);
cout<< “Circle Area =”<<circle_area<<endl;
getch();
}
IX. Comments and Escape Code:
- Comments: ជារការធ្វើកំណត់សំគាល់នៅក្នុងកមμវិធី។ វាមិនមាន
ដំណើរការទេនៅពេល Run Time។
នៅក្នុង C ++ មាន Comments ពីរគឺៈ
- // One Line
- Escape Code:
- \n : New line
- \t : Move one tab
- \a : Beeb
- \\ : \
- \r : Move cursor to the first of line
- \” : ”
Ex:
// This program used to change dollars to riels
#include<iostream.h>
#include<conio.h> void main(){ clrscr();
const riel=4000;
float dollar, result;
cout<< “\n Enter Dollar you have=”;
cin>>dollar;
result =dollar * riel;
cout<< “After dollar to riel=”<<result<<endl;
getch();
}
X. Predefined Manipulate function: ជាបណ្ដាអនុគមន៍មួយចំនួនត្រូវបានគេប្រើសំរាប់ធ្វើការកែប្រែការ បញ្ចញ និង បញ្ចូលទិន្នន័យ។ បណ្ដាអនុគមន៍ទាំងនោះមានៈ
X.1. endl: ជា output manipulate function ប្រើសំរាប់ចុះដើមបន្ទាត់ថμី។
Ex: cout<< “Area=”<<area<<end<< “Population=”<<pop<<endl;
X.2. hex, dec and oct: ប្រើសំរាប់បំលែងប្រពន្ធ័គោលៈ
- hex : ជាប្រពន្ធ័គោល 16
- dec : ជាប្រពន្ធ័គោល 10
- oct : ជាប្រពន្ធ័គោល 8
Ex:
//Convert to hex, dec, oct;
#include<iostream.h>
#include<conio.h> void main(){ clrscr();X.1. endl: ជា output manipulate function ប្រើសំរាប់ចុះដើមបន្ទាត់ថμី។
Ex: cout<< “Area=”<<area<<end<< “Population=”<<pop<<endl;
X.2. hex, dec and oct: ប្រើសំរាប់បំលែងប្រពន្ធ័គោលៈ
- hex : ជាប្រពន្ធ័គោល 16
- dec : ជាប្រពន្ធ័គោល 10
- oct : ជាប្រពន្ធ័គោល 8
Ex:
//Convert to hex, dec, oct;
#include<iostream.h>
int num;
cout<< “Enter num=”;
cin>>num;
cout<< “Decimal=”<<dec<<num<<endl;
cout<< “Octal=”<<oct<<num<<endl;
cout<< “Hexadecimal=”<<hex <<num<<endl;
getch();
}
X.3. Setbase(base): ប្រើសំរាប់បំលែងប្រពន្ធ័គោលតាមរយះ base (8, 10, 16);
ដែលវាស្ថិតនៅក្នុង header file <iomanip.h> ។
Ex:
//Convert to hex, dec, oct;
#include<iostream.h>
#include<conio.h> void main(){ clrscr();
int num;
cout<< “Enter num=”;
cin>>num;
cout<< “Decimal=”<<setbase(10)<<num<<endl;
cout<< “Octal=”<<setbase(8)<<num<<endl;
cout<< “Hexadecimal=”<<setbase(16)<<num<<endl;
getch();
}
10.4. setw(): ប្រើសំរាប់កំណត់ទំហំបង្ហាញមកលើ Screen ។
Ex:
cout<< setw(4)<< “Web”<<setw(5)<< “Site”; Output:
9Web9Site
10.5. setfill(): ប្រើសំរាប់បំពេញកន្លែង Space នៅពេល Output
ដោយនិមិត្ដសញ្ញាណាមួយ។
Ex:
cout<<setfill(‘*’);cout<< setw(4)<< “Web”<<setw(5)<< “Site”; Output:*Web*Site
Ex:
float a;
a=7/3;
cout<< “a=”setprecision(2)<<a<<endl; Output:
a=2.33
XI. Operators: នៅក្នុង C ++ មានប្រមាណវិធីផ្សេងៗមួយចំនួនដែលគេប្រើញឹកញាប់ដូចជាៈ
XI.1. Arithmetic Operators:
float a;
a=7/3;
cout<< “a=”setprecision(2)<<a<<endl; Output:
a=2.33
XI. Operators: នៅក្នុង C ++ មានប្រមាណវិធីផ្សេងៗមួយចំនួនដែលគេប្រើញឹកញាប់ដូចជាៈ
XI.1. Arithmetic Operators:
Operators |
Meaning
|
Example |
+
|
Addition | c = a + b; |
-
|
Subtraction | c = a – b; |
*
|
Multiplication | c = a * b; |
/
|
Division | c = a / b ; |
%
|
Modulo | c = a % b; |
Ex:
int a=20, b=40, c;c= a+ b;
cout<< “c=”<<c<<endl;
XI.2. Assignment Operators:
Operators | Meaning/Example |
=
|
a = 10; |
+ =
|
a + = 10; a = a + 10; |
- =
|
a – = 10; a = a – 10; |
/ =
|
a / = 10; a = a / 10; |
* =
|
a * = 10; a = a * 10; |
% =
|
a % = 10; a = a % 10; |
>> =
|
n >> = 1; n = n >> 1; |
<<=
|
n << = 1; n = n<<1; |
Operators | Meaning |
<
|
Less than |
>
|
Greater than |
<=
|
Less than or equal to |
>=
|
Greater than or equal to |
= =
|
Equal to |
!=
|
Not equal to |
&&
|
Logical AND |
||
|
Logical OR |
!
|
Not |
- Logical AND (&&):
X
|
Y
|
X && Y |
1
|
1
|
1
|
1
|
0
|
0
|
0
|
1
|
0
|
0
|
0
|
0
|
X
|
Y
|
X || Y |
1
|
1
|
1
|
1
|
0
|
1
|
0
|
1
|
1
|
0
|
0
|
0
|
Operators | Meaning |
&
|
Bit wise AND |
|
|
Bit wise OR |
^
|
Bit wise XOR |
>>
|
Bit wise Right Shift |
<<
|
Bit wise Left Shift |
~
|
Bit wise Complement |
- Bit wise AND (&):
X
|
Y
|
X & Y |
0101
|
0110
|
0100
|
0110
|
0010
|
0010
|
1111
|
0001
|
0001
|
0001
|
0000
|
0000
|
X
|
Y
|
X | Y
|
0101
|
0010
|
0111
|
0110
|
0010
|
0110
|
1111
|
0001
|
1111
|
0001
|
0000
|
0001
|
X
|
Y
|
X^Y
|
0101
|
0010
|
0111
|
0110
|
0011
|
0101
|
1111
|
0001
|
1110
|
0001
|
0000
|
0001
|
X
|
Y
|
~X
|
~Y
|
00001011 | 11111111 | 11110100 | 00000000 |
- Unary Operator: ជា Operator ដែលត្រូវការ Operant តែមួយគត់។
Ex:
++ i;– i;
i ++;
i–;
- Ternary Operator (?): ជា Operator លក្ខខណ្ឌ័ ដែលមានបី Operants។
Syntax: Operant1 ? Operant2 : Operant3; Ex:
MAX= (a>b) ? a : b; MIN=(a>b) ? a : b;
cout<< “Max=”<<MAX<<endl;
cout<< “Min=”<<MIN;
XII. Type Conversion:
ដំបូងអថេរខ្លះត្រូវបានប្រកាសជា Integer ប៉ុន្ដែជួនកាលវាអាចមាន លទ្ធភាព ទទួលយកនូវចំនួនទសភាគ (float) បាន ដែរតាម ទំរង ខាងក្រោមៈ
Var1= (Data-type Var1) Var2; Ex1:
float y;
x=(int)y;
Ex2:
int x;
float y;
y=x;
13. អនុគមន៍ Math ដែ់លមាននៅក្នុង Preprocessor directive <math.h>:
Functions | Result Data Type | Description |
abs(i) | int | | i | |
sin(d) | double | Sin(d) |
cos(d) | double | Cos(d) |
tang(d) | double | Tang(d) |
exp(d) | double | Exp(d) |
log(d) | double | Log(d) |
pow(x1,x2) | double | Pow(d) |
flour(d) | double | Flour(d) |
ceil(d) | double | Ceil(d) |
fmod(d1,d2) | double | Fmod(d1,d2) |
sqrt(d) | double | Sqrt(d) |
random(d) | void | Random(d) |
Functions | Result DataType | Description |
toascii (ch) | int | Convert ch to Ascii code |
tolower (ch) | char | Convert ch to lowercharacter |
toupper(ch) | char | Convert ch to uppercharacter |
#include<conio.h>
#include<math.h>
main( ){
clrscr( );
float a;
int result;
printf(“Please, Input a=”);
scanf(” %f”,&a); result =abs(a); textcolor(6);
cprintf(“Result are =%d”,result);
getch( );
return 0;
Exercises:
1. Write program to calculate Sum of Three number?
2. They have output on the screen as follow: W*e*l*C*o*m*e***to***Learn***C++**Programming**Language! Write program that output above?
3. Write program to calculate area of Rectangle?
4. Write program to calculate f(x)= axn + bxn-2 + c
5. Using Ternary operator, write program to find Min and Max of two numbers?
6. Using setbase() function, write program to convert to hexadecimal and octal number?
7. Write program to change Riels to Dollars?
Post a Comment