วันพุธที่ 22 มิถุนายน พ.ศ. 2554

สรุปบทที่ 2 Review C Language

บทที่ 2 Review C Language

objective
- เข้าใจรูปแบบที่ใช้คำสั่งในภาษา C
- เข้าใจวิธีดำเนินการ
- เข้าใจวิธีการวนรอบ
- สามารถสร้างโปแกรมที่เกี่ยวข้องกับการจัดการของหน่วยความจำ
- สามารถสร้างฟังก์ชั้นและดำเนินงานเงื่อนไขต่างๆ

คำสั่งพื้นฐานต่างๆ เช่น printf,scanf

printf  เช่น printf(“abc”);

Format Code ใช้ในการแสดงผลที่นิยมใช้
 %d - decimal integer
 %c - character
 %s - string
Operator
เครื่องหมายดำเนินการ ( Operator ) แบ่งออกได้เป็น 3 ชนิด คือ
1. Arithmetic Operators ได้แก่ + , - , * , / , % , -- , ++2. Relational and Equality Operators ได้แก่ < , > , <= , >= ,== , !=3. Logical Operators ได้แก่ ! , && , ||
if statement if – else statement
if – else statement (Nested if )-
if statement
Format:
                  if (expression)
                  statement;
Example:
                  if (score >= 80)
                  printf(“Your grade is A \n”);
if – else statement
Format:
                 if (expression)
                 statement-1;
                 else
                 statement-2;

if – else statement (Cont.)
Example:
                if (score >= 60)
                printf(“Your grade is satisfied\n”);
                else
                printf(“Your grade is unsatisfied\n”);

Repetition / Loop
while statement
for statement

while statement
Format:
                while (expression) {
                statement-1;
                statement-2;
                     …
                statement-n;
                }
- do – while statement
while statement (Cont.)
Example:
int n = 0;
while (n <= 10) {
printf(“%d “, n);
n++;
}

Function
ข้อดี
- เขียน Code ครั้งเดียวแต่สามารถเรียกได้หลายครั้ง
- สามารถนำกลับไปใช้ใหม่ในโปรแกรมอื่นได้
- ทำให้โปรแกรมมีความเป็นโครงสร้าง
- สามารถแบ่งเป็นโมดูลย่อยได้
Function (Cont.)
แบ่งเป็น 2 ประเถท คือ
1. Library Function

Library Function
- เป็นฟังก์ชันที่ผู้ผลิต  C Compiler เป็นผู้เขียนขึ้น และเก็บไว้ในแฟ้มข้อมูล C Libery เรียบร้อยแล้ว ผู้ใช้สามารถเรียกได้เลย
-Format:
     #include <file-name>
-Example:
    #include <stdio.h>
    #include <conio.h>
    etc.
Library Function (Cont.)
วิธีเรียกใช้ Library Function
- เรียกชื่อของฟังก์ชันที่ต้องการใช้งาน
- เอาค่าที่จะส่งงานในฟังก์ชัน ใส่ลงไปในวงเล็บตาทมหลังชื่อฟังชั้นนั้น

Example strcpy Library Function
     #include <string.h>
     main ( ) {
           char s[20];
           strcpy(s, “SAWASDEE”);
     }
Example Library Function

strcat( ) - อยู่ในแฟ้มข้อมูล

ทำหน้าที่ : ใช้เชื่อมต่อข้อความ 2 ข้อความเข้าด้วยกัน
     Format:
                  strcat(str1, str2);

Example strcat Library Function
#include <string.h>
main ( ) {
       char s1[10], s2[10];
       strcpy(s1, “SAWAS”);
       strcpy(s2, “DEE”);
       strcat(s1, s2);
       printf(“%s”, s1);
}

  
Example Function Definition(Cont.)
*Function Prototype
*Function Definition
*Invocation

#include <stdio.h>
int main(void) {
      /* produce some output */
      printf(“Hello World\n”);
      /* print banner line */
      printf(“**********\n”);
      /* produce more output */
     printf(“Hello Sun\n”);
     /* print banner line */
     printf(“**********\n”);
     return 0;
}

Invocation
- คือการเรียกฟังก์ชัน
- ลักษณะของฟังก์ชัน
  * ฟังก์ชันที่ไม่มีการสั่งค่ากลับ
  * ฟังก์ชันที่มีการสั่งค่ากลับ
  * ฟังก์ที่มีการรับค่า argument
- ฟังก์ชันที่ไม่มีการสั่งค่ากลับ
              การเรียกใช้   ทำได้โดยอ้างถึงชื่อฟังก์ชัน
              ………
             

print_banner( );
             int main(void) {
                int k, j;
             j = prompt( );
             k = prompt( );
             printf(“j = %d and k = %d”, j, k);
}

Pass by value
- การสั่งค่าฟังก์ชันที่ถูกเรียกใช้โดยส่งค่าตัวแปรผ่านให้กับฟังก์ชันที่ถูกคัดลอกส่งไปยังฟังก์ชันและจะถูกเปลี่ยนเฉพาะภายในฟังก์ชัน โดยค่าของอาร์กิวเมนต์ในโปรแกรมที่เรียกใช้จะไม่เปลี่ยนแปลง


Example Pass by Value
- switch statement%f
- floating point numberscanf เช่น  scanf(“%d”, &x);
#include <stdio.h>
void swap(int, int);
int main(void) {
     int a = 10, b = 20;

    swap(a, b);
    printf(“A is %d B is %d\n”, a, b);

    return 0;
}
                               
Invocation (Cont.)
การเรียกใช้ทำได้โดย

User Defined Function
- คือ ฟังก์ชันที่ผู้เขี่ยนโปรแกรมสามารถเขียนฟังก์ชั้นขึ้นใช้เองโดยฟังก์ชันนี้อาจจะรวมอยู่กับโปรแกรมหลักเป็นแฟ้เดียวกัน
- การสร้างฟังก์ชันประกอบด้วย  

printf เช่น (“%s”, “abc”);
        
                   ----------------------  จบ  ---------------------- 


ไม่มีความคิดเห็น:

แสดงความคิดเห็น