ស្វែងរក

3. Variables និង Arithmetic Expression

កម្មវិធីបន្ទាប់ប្រើប្រាស់រូបមន្ត Celsius=5*Fahrenheit-32/9 ដើម្បីធ្វើការបង្ហាញតារាងបំលែង Celsius ទៅ Fahrenheit ដោយ Loop ម្ដង 20 degree និងធំបំផុតត្រឹម 300 degree។

Console:

#include <stdio.h>/*Include standard library*/
/*Fahrenheit-Celsius Table*/
int main(void)/*Define function main*/
{/*start function main*/
 int celsius, fahr;/*variable celsius and fahr as integer (identifier)*/
 int lower=0, upper=300, step=20;/*variable lower, upper, step as integer*/
 printf("Fahrenheit\tCelsius\n");/*print header of table*/
 fahr = lower;/*first value of fahr is 0*/
 while (fahr<=upper)/*do loop until fahr=upper*/
 {/*start while loop*/
  celsius = 5*(fahr - 32) / 9;/*value of celsius*/
  printf("%d\t\t%d\n",fahr,celsius);/*print %d (decimal) by order fahr, celsius*/
  fahr = fahr + step;/*increase fahr by step(20)*/
 }/*end while loop*/
 return 0;/*exited function main*/
}

Output:
Fahrenheit      Celsius
0               -17
20              -6
40              4
60              15
80              26
100             37
120             48
140             60
160             71
180             82
200             93
220             104
240             115
260             126
280             137
300             148
Console:
#include <stdio.h>/*Include standard library*/
/*Fahrenheit-Celsius Table*/
int main(void)/*Define function main*/
{/*start function main*/
 float celsius, fahr;/*variable celsius and fahr as float (identifier)*/
 int lower = 0, upper = 300, step = 20;/*variable lower, upper, step as integer*/
 printf("Fahrenheit\tCelsius\n");/*print header of table*/
 fahr = lower;/*first value of fahr is 0*/
 while (fahr <= upper)/*do loop until fahr=upper*/
 {/*start while loop*/
  celsius = 5 * (fahr - 32) / 9;/*value of celsius*/
  printf("%.0f\t\t%.2f\n", fahr, celsius);/*print %f (float) by order fahr, celsius .0 or .2 or 0.2 show number after (.) */
  fahr = fahr + step;/*increase fahr by step(20)*/
 }/*end while loop*/
 return 0;/*exited function main*/
}
Output:
Fahrenheit      Celsius
0               -17.78
20              -6.67
40              4.44
60              15.56
80              26.67
100             37.78
120             48.89
140             60.00
160             71.11
180             82.22
200             93.33
220             104.44
240             115.56
260             126.67
280             137.78
300             148.89
យើងមានមួយរបៀបទៀត៖

#include <stdio.h>
int main(void)
{
 int fahr;
 printf("Fahrenheit\tCelsius\n");
 for (fahr = 0; fahr <= 300; fahr=fahr + 20)
 {
  printf("%3d\t\t%6.1f\n",fahr,(5.0/9.0)*(fahr-32));
 }
 return 0;
}

សេចក្ដីពន្យល់

int ហៅថា identifier មានប្រភេទទិន្នន័យជា Integer។
celsius, fahr, lower, upper, step ហៅថា Variable។ បានសេចក្ដីថាអ្វីដែលនៅក្រោយ identifier គឺជា variable។
lower=0, upper=300, step=20 ហៅថា initial value ឬតម្លៃដំបូង។
while(){} គឺប្រភេទ while loop មានតួនាទីធ្វើការម្ដងហើយម្ដងទៀត ដោយមានចំណុចចាប់ផ្ដើម និងបញ្ចប់ណាមួយ។
%d គឺសំដៅទៅលើការទទួលយកតម្លៃជាប្រភេទចំនួនគត់ integer។
+, -, *, /,% (remainder)... ហៅថា arithmetic expression។

ឧទាហរណ៍២៖ 
ការគណនារកផ្ទៃក្រឡា។

Console:
#include<stdio.h>/*include standard library*/
int main(void)/*define function main()*/
{/*start function main()*/
 float base, height, length, topbase, bottombase, area;/*variables declare as float number*/
 int shape;/*user's choice*/
 printf("Welcome! Area calculation!\n"); 
 printf("1. Square\n");
 printf("2. Rectangle\n");
 printf("3. Triangle\n");
 printf("4. Trapezium\n");
 printf("5. Paralllelogram\n");
 printf("6. Rhombus\n");
 printf("7. Kite\n");
 printf("Please, chose one of these (put number only):\n");
 scanf_s("%d",&shape);/*& get the address of variable shape in memory where it located*/
 
 if (shape == 1||shape==2||shape==5)/*If user input 1, 2, 5 are the same formula. || mean "or"*/
 {  
  printf("Base=");
  scanf_s("%f",&base);
  printf("Height=");
  scanf_s("%f",&height);
  area= (base * height);
  printf("Area=%f", area);
 }
 else if (shape==3)
 {
  printf("Base=");
  scanf_s("%f",&base);
  printf("Height=");
  scanf_s("%f", &height);
  area = (base * height) / 2;
  printf("Area=%f", area);
 }
 else if (shape==4)
 {
  printf("Top Base=");
  scanf_s("%f", &topbase);
  printf("Bottom Base=");
  scanf_s("%f",&bottombase);
  printf("Height=");
  scanf_s("%f",&height);
  area = ((topbase + bottombase) * height) / 2;
  printf("Area=%f", area);
 }
 else if (shape==6||shape==7)
 {
  printf("Lenght=");
  scanf_s("%f",&length);
  printf("Height=");
  scanf_s("%f", &height);
  area = (length * height) / 2;
  printf("Area=%f",area);
 }
 else
 {
  printf("Sorry! Try again!\n");/*if user input non of choice*/
 }
 return 0;
}

Output:
Welcome! Area calculation!
1. Square
2. Rectangle
3. Triangle
4. Trapezium
5. Paralllelogram
6. Rhombus
7. Kite
Please, chose one of these (put number only):
1
Base=4
Height=6
Area=24.000000

ឧទាហរណ៍៣៖ 
ការគណនាលុយខ្មែរទៅជាលុយដុល្លារសហរដ្ឋអាមេរិក និងពីដុល្លារសហរដ្ឋអាមេរិកទៅជាលុយខ្មែរវិញ។
Console:
#include <stdio.h>
int main(void)
{
 int khmer_currency, us_currency, rate,choice;
 printf("Currency Conversion\n");
 printf("--------------------\n");
 printf("1. Khmer Riel-->US Dollar\n2. US Dollar-->Khmer Riel\n");
 printf("--------------------\n");
 printf("Current Rate 1$=");
 scanf_s("%d", &rate);
 printf("Please, chose one of these:");
 scanf_s("%d",&choice);
 if (choice==1) 
 {
  printf("Khmer Riel Amount=");
  scanf_s("%d",&khmer_currency);
  us_currency = khmer_currency / rate;
  printf("US Dollar Amount=$%d",us_currency);
 }
 else if (choice==2)
 {
  printf("US Dollar Amount:");
  scanf_s("%d",&us_currency);
  khmer_currency = us_currency * rate;
  printf("Khmer Riel Amount=%dR",khmer_currency);
 }
 else
 {
  printf("Sorry! Out of choice!\n");
 }
 return 0;
}

Output:
Currency Conversion
--------------------
1. Khmer Riel-->US Dollar
2. US Dollar-->Khmer Riel
--------------------
Current Rate 1$=4000
Please, chose one of these:2
US Dollar Amount:100
Khmer Riel Amount=400000R


ឧទាហរណ៍៣៖ 
Global Variable និង Static Variable

Console:
#include <stdio.h>
int globalvar;/*Global Variable*/
int main(void)
{
 static int staticva;/*Static variable*/
 int sum;
 printf("Input Global Variable:");
 scanf_s("%d",&globalvar);
 printf("Input Static Variable:");
 scanf_s("%d",&staticva);
 sum = globalvar + staticva;
 printf("Sum is %d",sum);
}

Output:
Input Global Variable:34
Input Static Variable:21
Sum is 55 

1 comment:

  1. CasinoDaddy.com is an independent platform providing evaluations of online casinos, games, on line casino bonuses, slot games, and something gambling associated. With us, find a way to|you possibly can} at all times find out the distinctive on line casino bonuses and free spins for casinos on the world market. List 점보카지노 and compare the most effective online on line casino sites in South Korea in our up-to-date, streamlined listing.

    ReplyDelete

សូមមេត្តាបញ្ចេញមតិយោបល់របស់លោកអ្នកដោយមានសុជីវធម៌ និងទំនួលខុសត្រូវ