วันจันทร์ที่ 31 มกราคม พ.ศ. 2554

ค่าสี

       ค่าสี

 ใช้ในการกำหนดสีให้กับตัวอักษร  และพื้นหลังของตัวอักษร  จะใช้ร่วมกับฟังก์ชั่น cprintf    textcolor( )    textbackground( ) 

       ตัวอย่างการใช้ค่าสี

 textcolor(14)            หรือ   textcolor(YELLOW)



 textbackground(4)   หรือ    textbackground(RED)     



          

        ค่าสี                                                               สี


   0 (BLACK)                                                            สีดำ
   1 (BLUE)                                                                สีน้ำเงิน
   2 (GREEN)                                                             สีเขียว
   3 (CYAN)                                                               สีฟ้า
   4 (RED)                                                                   สีแดง
   5 (MAGENTA)                                                       สีม่วง
   6 (BROWN)                                                            สีน้ำตาล
   7 (LIGHTGRAY)                                                     สีเทาสว่าง
   8 (DARKGRAY)                                                     สีเทาดำ
   9 (LIGHTBLUE)                                                      สีน้ำเงินสว่าง
 10 (LIGHTGREEN)                                                   สีเขียวสว่าง
 11 (LIGHTCYAN)                                                     สีฟ้าสว่าง
 12 (LIGHTRED)                                                         สีแดงสว่าง
 13 (LIGHTMAGENTA)                                             สีม่วงสว่าง
 14 (YELLOW)                                                           สีเหลือง
 15 (WHILE)                                                               สีขาว


                       **********************************************

ตัวอย่างการนำฟังก์ชั่น switch-case มาเขียนโปรแกรมเมนูเลือกการทำงาน

ตัวอย่างการประยุกต์ใช้ฟังก์ชั่น switch-case

ในการเขียนโปรแกรม เมนูเลือกการทำงาน   มักจะใช้ฟังก์ชั่น switch-case มาประยุกต์

#include<stdio.h>
void  main( )
{   int      sele;
     clrscr( );
     printf(“             *** Menu ***     \n”);
     printf(“            1    Add   data       \n”);
     printf(“            2    Edit   data       \n”);
     printf(“            3    Delete data      \n”);
     printf(“            4    Exit                  \n”);
     printf(“       Please  select choice 1-4  :    “);
     scanf(“%d”,&sele);
     switch (sele)
     {   case   1   :  printf(“Add  data\n”);
                            break;
           case  2  :  printf(“Edit  data\n”);
                                 break;
           case   3  : printf(“Delete data      \n”);
                            break;
           case  4   : printf(“Thank  you \n”);
                            break;
          default  :  printf(“You select wrong number !!!   \n”);
     }
    getch( );
}

วันอังคารที่ 25 มกราคม พ.ศ. 2554

ฟังก์ชั่นตัดสินใจ

ฟังก์ชั่นตัดสินใจ
 ฟังก์ชั่นตัดสินใจ หรือ ฟังก์ชั่นเงื่อนไข  หรือฟังก์ชั่นทางเลือก  ในภาษา C มี 4 แบบ คือ
       1   ฟังก์ชั่นทางเลือกทางเดียว       คือ ฟังก์ชั่น    if
       2   ฟังก์ชั่นทางเลือก 2 ทางเดียว  คือ ฟังก์ชั่น    if-else
       3   ฟังก์ชั่นทางเลือกหลายทาง      คือ ฟังก์ชั่น  switch-case

              ***************************************

ฟังก์ชั่น    if    (ฟังก์ชั่น 1 ทางเลือก)
       รูปแบบ
                  if ( เงื่อนไข )
                              { คำสั่งที่ทำเมื่อเงื่อนไขเป็นจริง; }
         ตัวอย่าง
                     if (age <= 18)
                                 {  printf(“You  are   young  girl \n“);
      
       ตัวอย่างโปรแกรม
           #include<stdio.h>
           void  main()
            {       int    age=0;
                     clrscr();
                     printf(“enter  your  age  :  “);   
                     scanf(“%d”,&age);
                     if (age <=17)
                              {  printf(“you  are  young  man !!! \n“);  }
                     printf(“ end  of  program ! “);
                     getch();
                }
     
          ******************************************

      ฟังก์ชั่น    if - else   (ฟังก์ชั่น 2 ทางเลือก)
    รูปแบบ
                  if ( เงื่อนไข )
                              { คำสั่งที่ทำเมื่อเงื่อนไขเป็นจริง; }
                  else
                              { คำสั่งที่ทำเมื่อเงื่อนไขเป็นเท็จ;}
         ตัวอย่าง
                     if (age <= 18)
                                   printf (“You  are   young  man \n“);
                     else
                                   printf (“You are  man \n”);
          ตัวอย่างโปรแกรม
            #include<stdio.h>
            void  main( )
            {       char    sex;
                     clrscr( );
                     printf(“enter  your  sex  :  “);   
                     scanf(“%c”,&sex);
                     if (sex = “m”)
                                  printf(“you  are    male !!! \n“); 
                     else
                                  printf (“you  are female !!!\n”);               
                     printf(“ end  of  program ! “);
                     getch( );
                }
                          **************************************

   ฟังก์ชั่น  switch…case    (ฟังก์ชั่นหลายทางเลือก)
   รูปแบบ
     switch (ตัวแปร)
    {
         case  ค่าคงที่1 :   คำสั่ง  ;
                                     break;
         case  ค่าคงที่2  :  คำสั่ง;
                                     break;
                       …………..
                       …………..
        case  ค่าคงที่ n  :  คำสั่ง;
                                     break;
       default  :   คำสั่ง;
    }
ตัวอย่าง
  switch (num)
{   case   1  :  printf(“one\n”);
                       break;
    case   2  :  printf(“two\n”);
                       break;
    case   3   :  printf(“three\n”);
                       break;
    case   4   :  printf(“four\n”);
                       break;
     case   5   :  printf(“five\n”);
                       break;
     default  :  printf(“number > 5 \n”);
}
ตัวอย่างโปรแกรม
#include<stdio.h>
void   main( )
{      char     ans;
        clrscr( );
       printf(“ Enter character : “);
       scanf(“%c”,&ans);
       switch (ans)
       {   case   “a”   :  printf(“A\n”);
                                  break;
            case   “b”  :  printf(“B\n”);
                                  break;
            case   “c”  :   printf(“C\n”);
                                  break;
            case   “d” :   printf(“D\n”);
                                 break;
          default  :  printf(“character < > “A” “B” “C” “D”   \n”);
    }

                 ************************************************ 
แบบฝึกหัด
จงเขียนโปรแกรมรับตัวอักษร 1 ตัว จาก keyboard   ถ้าตัวอักษรที่รับมาเป็น
                      r  ให้พิมพ์  red
                      y ให้พิมพ์  yellow
                      g ให้พิมพ์  green
                      b ให้พิมพ์  black
                      w ให้พิมพ์  while
แต่ถ้าตัวอักษรที่รับมาเป็นตัวอักษรอื่น  ให้แสดงข้อความ
                           “error  character  !!!”
                ***********************************

วันจันทร์ที่ 24 มกราคม พ.ศ. 2554

การใช้คำสั่งทำซ้ำ (Loop)

คำสั่งทำซ้ำ   คำสั่งวนรอบ   คำสั่ง loop  หรือ loopping  ก็คือคำสั่งลักษณะเดียวกัน
แล้วแต่ใครถนัดจะเรียกอย่างไร 

ฟังก์ชั่นทำซ้ำในภาษา c  มี 3 ฟังก์ชั่นเท่านั้นคือ    (ภาษา c เรียกคำสั่งว่า ฟังก์ชั่น)
1       ฟังก์ชั่น  for
2       ฟังก์ชั่น while
3       ฟังก์ชั่น do while

           *************************************************
ฟังก์ชั่น  for      
   รูปแบบ       for (ตัวแปร := ค่าเริ่มต้น ; เงื่อนไข;  เพิ่มหรือลดค่าตัวนับ)
                                 {  
                                                 คำสั่งที่จะทำซ้ำ ;
                                           }
ตัวอย่าง                                  
        for (k = 1; k <= 100; k++)
                                    {  
                                        printf(“ pongsawadi !! “);
                                               }
                                                                                                                                
      ตัวอย่างโปรแกรม
       #include<stdio.h>
            void   main()
            {     int    i;
                  clrscr();
                  for   (i  = 1;  i <=30; i++)
                        { 
                           gotoxy(30,13);

                           printf(“number = %d “,i);
                           delay(200);
                          }
                    getch();
                 }
  ข้อสังเกตุ  
        ฟังก์ชั่น for  จะทำคำสั่งที่จะทำซ้ำก็เมื่อเงื่อนไขเป็นจริง 
               จากตัวอย่างเงื่อนไข คือ i <=30  
                         ***********************************************************************

ฟังก์ชั่น  while 
        รูปแบบ             while(เงื่อนไข)
                                           
{    
                                          คำสั่งที่จะทำซ้ำ;
                                        }

        ตัวอย่าง            while(count <= 20)
                                        { 
                                            printf(“pongsawadi”);
                                            count++; 
                                          }
     
 ตัวอย่างโปรแกรม  
#include<stdio.h>
void  main()
{      int    i = 0;
       clrscr();
       while(i<=30)
                   {   
                       i++;
                       gotoxy(30,13);
                       printf(“number = %d”,i  “);
                       delay(200);
                   }
        getch();
  }

ข้อสังเกตุ
ฟังก์ชั่น while  จะทำคำสั่งที่จะทำซ้ำก็เมื่อเงื่อนไขเป็นจริง 
      จากตัวอย่างเงื่อนไข คือ i <=30

***********************************************************************
ฟังก์ชั่น  do-while
            รูปแบบ        do {
                                       คำสั่งที่จะทำซ้ำ ;
                                   } while(เงื่อนไข);

            ตัวอย่าง
                            do{    
                                       j++;
                                       printf(“pongsawadi\n”);
                                   }while ( j<=15);
    
      ตัวอย่างโปรแกรม                       
      #include<stdio.h>
      void  main()
            {   int     count=0;
                 clrscr();
                 do {
                         count++;
                          gotoxy(30,13);
                          printf(“number = %d”,count);
                          delay(200);
                        } while(count <=30);
            getch();
           }  
ข้อสังเกตุ

ฟังก์ชั่น do-while  จะทำคำสั่งที่จะทำซ้ำก็เมื่อเงื่อนไขเป็นจริง 
จากตัวอย่างเงื่อนไข คือ count <=30


***********************************************************************
  แบบฝึกหัด
  จงเขียนโปรแกรมแสดงตัวเลข 1 ถึง 10 กลางจอภาพ
   ใช้คำสั่ง  for      1 โปรแกรม
   ใช้คำสั่ง while   1 โปรแกรม
   ใช้คำสั่ง do while   1 โปรแกรม

  ผลลัพธ์

                                                     number =    1

                                                     number =    2
            
                                                     number =    3

                                                     number =    4
                                                    
                                                     number =    5

                                                     number =    6

                                                     number =    7

                                                     number =    8
                       
                                                     number =    9

                                                      number =    10



***********************************************************************