340 likes | 498 Views
บทที่ 5. โครงสร้างภาษาซี รับข้อมูล แสดงผล. Preprocessor directive. Global Declarations. main function. void main(void) { }. int function () { }. User define functions. Local Declarations. Local Declarations. User define functions. Statements ;. Statements ;.
E N D
บทที่ 5 โครงสร้างภาษาซี รับข้อมูล แสดงผล 30-010-104 Computer Programming
Preprocessor directive Global Declarations main function void main(void) { } int function () { } User define functions Local Declarations Local Declarations User define functions Statements ; Statements ; โครงสร้างโปรแกรมภาษาซี โครงสร้างภาษาซีประกอบด้วยหลายส่วน แต่ในการเขียนไม่จำเป็นจะต้องเขียนทุกส่วน 30-010-104 Computer Programming
หลักการตั้งชื่อ (Identifier) ไอเดนติฟายเออร์ เป็นชื่อที่ผู้ใช้กำหนดขึ้นในโปรแกรม เช่น ชื่อค่าคงที่ ชื่อตัวแปร ชื่อฟังก์ชัน เป็นต้น • ต้องขึ้นต้นด้วยตัวอักษรภาษาอังกฤษ (ตัวใหญ่หรือเล็กก็ได้) หรือขีดล่าง ‘_’ • ตามด้วยตัวอักษรภาษาอังกฤษ ตัวเลข หรือขีดล่าง (Underscore)‘_’ • ไม่มีช่องว่างหรือตัวอักษรพิเศษอื่นๆ เช่น ‘!’, ‘@’, ‘#’, ‘$’, ‘%’, ‘^’ เป็นต้น • ตัวพิมพ์ใหญ่และเล็กจะเป็นคนละตัวกันเช่น NAME, name, Name, NamE • ห้ามซ้ำกับคำสงวน Reserve Words ของภาษา C • ห้ามตั้งชื่อซ้ำกับ Function ที่อยู่ใน Library ของภาษา C 30-010-104 Computer Programming
คำสงวน Reserve Words ของภาษา C 30-010-104 Computer Programming
การใช้ Preprocessor Directive • ทุกโปรแกรมต้องมี • ใช้เรียกไฟล์ที่โปรแกรมใช้ในการทำงานร่วมกัน • ใช้กำหนดค่าคงที่ให้กับโปรแกรม • ใช้กำหนดเงื่อนใขในการคอมไพล์ให้กับโปรแกรม • เริ่มต้นด้วยเครื่องหมาย # • ที่เราจะเรียนกันในหลักสูตรมี 2 directives คือ • #include ใช้สำหรับเรียกไฟล์ที่โปรแกรมใชในการทำงาน • #define ใช้สำหรับกำหนดมาโครที่ให้กับโปรแกรม 30-010-104 Computer Programming
การใช้ #include วิธีการใช้งาน #include <ชื่อไฟล์>หรือ #include “ชื่อไฟล์” ตัวอย่าง #include <stdio.h>(เป็นการเรียกใช้ไฟล์ stdio.h เข้ามาในโปรแกรม) #include <mypro.h>(เป็นการเรียกใช้ไฟล์ mypro.h เข้ามาในโปรแกรม) < > จะเรียกไฟล์ใน directory ที่กำหนดโดยตัวคอมไพล์เลอร์“ ” จะเรียกไฟล์ใน directory ทีทำงานอยู่ในปัจจุบัน 30-010-104 Computer Programming
การใช้ #define วิธีการใช้งาน #define ชื่อ ค่าที่ต้องการ ตัวอย่าง #define START 10 (กำหนดค่า START = 10) #define A 3*5/4 (กำหนดค่า A=3*5/4) #define pi 3.14159 (กำหนดค่า pi = 3.14159) #define sum(a,b) a+b(กำหนดค่า sum(ตัวแปรที่1, ตัวแปรที่2) = ตัวแปรที่1+ตัวแปรที่2 30-010-104 Computer Programming
ส่วนประกาศ (Global Declarations) • เป็นการประกาศตัวแปรเพื่อใช้งานในโปรแกรม โดยตัวแปรนั้นสามารถใช้ได้ในทุกที่ในโปรแกรม • เป็นส่วนที่ใช้ในการประกาศ Function Prototype ของโปรแกรม • ส่วนนี้ในบางโปรแกรมอาจจะไม่มีก็ได้ ตัวอย่าง int summation(float x, float y) ; (ประกาศ functionsummation) int x,y ; (กำหนดตัวแปร x,y เป็นจำนวนเต็ม) float z=3; (กำหนดตัวแปร z เป็นจำนวนจริง) 30-010-104 Computer Programming
ส่วนประกาศ (Global Declarations) ตัวอย่าง #include <stdio.h> int feet,inches; void main() { feet = 6; inches = feet * 12; printf("Height in inches is %d",inches); } ผลการทำงาน Height in inches is 72 30-010-104 Computer Programming
ฟังก์ชันหลักของโปรแกรม (Main Function) • ส่วนนี้ทุกโปรแกรมจะต้องมี โดยโปรแกรมหลักจะเริ่มต้นด้วย main() และตามด้วยเครื่องหมายปีกกาเปิด ‘{’ และปีกกาปิด ‘}’ • ระหว่างปีกกาจะประกอบไปด้วยคำสั่ง(Statement) ต่างๆ ที่จะให้โปรแกรมทำงาน • แต่ละคำสั่งจะต้องจบด้วยเซมิโคลอน ‘;’ (Semicolon) #include <stdio.h> void main(void) { ... Statement ; } 30-010-104 Computer Programming
ฟังก์ชันหลักของโปรแกรม (Main Function) ตัวอย่าง #include <stdio.h> int feet,inches; void main() { feet = 6; inches = feet * 12; printf("Height in inches is %d",inches); } ผลการทำงาน Height in inches is 72 30-010-104 Computer Programming
การสร้างฟังก์ชันใช้งานเอง (User Define Function) • สร้างฟังก์ชันหรือคำใหม่ ขึ้นมาใช้งานตามที่เราต้องการ • ระหว่างปีกกาจะประกอบด้วยคำสั่ง(Statement) ต่างๆ ที่จะให้ฟังก์ชันทำงาน • สามารถเรียกใช้ภายในโปรแกรมได้ทุกที่ #include <stdio.h> int function() void main(void) { ... Statement ; } int function() { Statement ; ... return (int value); } 30-010-104 Computer Programming
การสร้างฟังก์ชันใช้งานเอง (User Define Function) ตัวอย่าง #include <stdio.h> int FtoI(int); int feet,inches; void main() { feet = 6; inches = FtoI(feet); printf("Height in inches is %d",inches); } int FtoI(int f) { return f*12; } ผลการทำงาน Height in inches is 72 30-010-104 Computer Programming
การใช้คำอธิบาย (Program Comments) • ใช้เขียนส่วนอธิบายโปรแกรม (คอมเมนต์) • ช่วยให้ผู้ศึกษาโปรแกรมภายหลังเข้าใจการทำงานของโปรแกรม • ส่วนของคำอธิบายจะถูกข้ามเมื่อคอมไพล์โปรแกรม การเขียนส่วนอธิบายโปรแกรม (comments)ทำได้ 2 วิธีคือ //สำหรับคำอธิบายไปจนถึงท้ายบรรทัด และ /*คำอธิบาย */ลักษณะการใช้เหมือนวงเล็บนั้นเอง 30-010-104 Computer Programming
การใช้คำอธิบาย (Program Comments) ตัวอย่าง #include <stdio.h> // Change Feet to Inches void main() // by CPE RMUTL { // Start int feet,inches; feet = 6; // feet 6 inches = feet * 12; // inches feet * 12 printf("Height in inches is %d", inches); // write inches } // Stop ผลการทำงาน Height in inches is 72 30-010-104 Computer Programming
Display Input/Output 30-010-104 Computer Programming
การแสดงผลและการรับข้อมูลการแสดงผลและการรับข้อมูล แสดงผลออกทางหน้าจอด้วย printf() การจัดรูปแบบการแสดงผล แสดงผลทีละอักขระด้วย putchar() แสดงผลเป็นข้อความด้วย puts() รับข้อมูลจากคียบอร์ด์ด้วย scanf() รับข้อมูลทีละอักขระด้วย getchar() รับข้อมูลทีละอักขระด้วย getch() รับข้อ้อมูลเป็นข้อความด้วย gets() 30-010-104 Computer Programming
แสดงผลออกทางหน้าจอด้วย printf( ); printf(“control”, value1, value2, ...); • control : ประกอบด้ว้วยข้อ้อความ, รหัสควบคุมรูปแบบ (Format Code), และอักขระควบคุมการแสดงผล (Carriage Control) • value : ค่าของตัวแปร, นิพจน์, หรือมาโครที่ต้องการแสดง #include <stdio.h> void main() { printf(“Wake Up!”); } printf() เป็นฟังก์ช์ชั่นมาตรฐานในภาษา C รวมอยู่ใน stdio.h สาหรับแสดงข้อความค่าของตัวแปรหรือผลการดำเนินการของนิพจน์ออกทางหน้าจอ 30-010-104 Computer Programming
การแสดงผลและการรับข้อมูลการแสดงผลและการรับข้อมูล Value Format Code printf("Error = %f\n", (alpha_out-alpha_l)*100/alpha_l); Carriage Control Format Code ใช้สาหรับควบคุมรูปแบบของตัวแปร นิพจน์ มาโครที่จะถูกแสดงออกทางหน้าจอ Carriage Control ใช้สาหรับจัดรูปแบบการแสดงผล เช่น \n เพื่อขึ้นบรรทัดใหม่ 30-010-104 Computer Programming
รหัสควบคุมรูปแบบ (Format Code) 30-010-104 Computer Programming
ตัวอย่างการใช้รหัสควบคุมรูปแบบที่เหมาะสมตัวอย่างการใช้รหัสควบคุมรูปแบบที่เหมาะสม • #include <stdio.h> • int x1 = 43, x2 = 0x77, x3 = 0573; • float y1 = -764.512, y2 = 1.25e02; • char z = ‘A’; • char name[11] = “Rajamagala”; • void main() • { • printf(“x1 = %d ”, x1); • printf(“x2 = %x x3 = %o ”, x2, x3); • printf(“y1 = %f y2 = %e ”, y1, y2); • printf(“z = %c name = %s ”, z, name); • } Run x1 = 43 x2 = 77 x3 = 573 y1 = -764.512024 y2 = 1.250000e+02 z = A name = Rajamagala 30-010-104 Computer Programming
ตัวอย่างการใช้รหัสควบคุมรูปแบบทีไม่เหมาะสมตัวอย่างการใช้รหัสควบคุมรูปแบบทีไม่เหมาะสม • #include <stdio.h> • int x1 = -234, x2 = 027, x3 = 0x529; • float y1 = -51.248, y2 = 1.45e03; • char z = ‘C’; • void main() • { • printf(“x2 = %d x3 = %d ”, x2, x3); • printf(“x1 = %u ”, x1); • printf(“y2 = %f ”, y2); • printf(“z = %d ”, z); • } Run x2 = 23 x3 = 1321 x1 = 65302 y2 = 1450.000000 z = 67 30-010-104 Computer Programming
การใช้รหัสควบคุมรูปแบบสำหรับเลขทศนิยมการใช้รหัสควบคุมรูปแบบสำหรับเลขทศนิยม • #include <stdio.h> • float x1 = 17.765, x2 = 1.578e+03; • void main() • { • printf(“%.4f ”, x1); • printf(“%.1f ”, x1); • printf(“%.2e ”, x2); • printf(“%.1e ”, x2); • } Run 17.7650 17.8 1.58e+03 1.6e+03 จำนวนหลักหลังจุดทศนิยมสามารถกำหนดได้โดยคำสั่ง %?fหรือ%?e โดย ?คือจำนวนหลักหลังจุดทศนิยมที่ต้องการแสดง 30-010-104 Computer Programming
การใช้รหัสควบคุมรูปแบบสำหรับข้อความการใช้รหัสควบคุมรูปแบบสำหรับข้อความ • ข้อความสามารถถูกแสดงเพียงบางส่วนเมื่อใช้คำสั่ง %m.ns โดย n คือจำนวนอักขระที่ต้องการให้แสดง และ m คือพื้นที่ที่ใช้แสดงผล • %-m.ns ทำให้ข้อความที่ถูกแสดงชิดขอบด้านซ้าย • include <stdio.h> • char message [ ] = “Engineering”; • void main () • { • printf(“%.6sX\n”, message); • printf(“%12.6sX\n”, message); • printf(“%-12.6sX\n”, message); • } EngineX EngineX Engine X 30-010-104 Computer Programming
อักระควบคุมการแสดงผล (Carriage Control) • ใช้สำหรับเคลื่อนเคอร์เซอร์ไปยังตำแหน่งที่ต้องการ 30-010-104 Computer Programming
ตัวอย่างการใช้อักขระควบคุมการแสดงผลตัวอย่างการใช้อักขระควบคุมการแสดงผล • #include <stdio.h> • void main() • { • printf(“Information\tTechnology\n”); • printf(“C Lang”); • printf(“\b\b\b\bProgramming”) • } Information Technology C Programming 30-010-104 Computer Programming
แสดงผลทีละอักขระด้วย putchar() • putchar()ใช้แสดงอักขระออกทางหน้าจอ putchar(char); char: เป็นข้อมูลหรือตัวแปรชนิด character • #include <stdio.h> • char first = ‘O’; • void main() • { • putchar(first); • putchar(‘k’); • } Ok 30-010-104 Computer Programming
แสดงผลเป็นข้อความด้วย puts() • puts() ใช้แสดงข้อความออกทางหน้าจอ puts (str); str : เป็นข้อมูลหรือตัวแปรที่เก็บข้อมูลชนิด string • #include <stdio.h> • char message[ ] = “C Language”; • void main() • { • puts(message); • puts(“is easy but useful”); • } C Language is easy but useful 30-010-104 Computer Programming
รับข้อมูลจากคีย์บอร์ดด้วย scanf() • scanf( ) รับข้อมูลจากผู้ใช้การใช้งานคล้ายกับ printf() scanf(“control”, &variable1, &variable2, ...); • Control : ประกอบด้วยข้อความและรหัสควบคุมรูปแบบ(format Code) • Variable : ตัวแปรที่ใช้รับข้อมูลจะต้องเขียนนำหน้าชื่อตัวแปรด้วย & ยกเว้นตัวแปรที่รับข้อมูลชนิดข้อความ • หากผู้ใช้ป้อนข้อมูลไม่ตรงกับชนิดของตัวแปร โปรแกรมจะทำงาน • ผิดพลาด (Runtime Error) 30-010-104 Computer Programming
45 20/12/1997 ตัวอย่างการรับข้อมูลด้วย scanf() • #include <stdio.h> • int age,day,month,year; • char name[15], surname[30]; • void main() • { • printf(“Insert age : ”); • scanf(“%d”,&age); • printf(“You are %d years old.\n”,age); • //--------------------------------------------- • clrscr(); • printf(“Insert birthdate dd/mm/yyyy : ”); • scanf(“%d/%d/%d”,&day,&month,&year); • printf(“Your birthdate is %d/%d/%d\n”,day,month,year); • //--------------------------------------------- • clrscr(); • printf(“Insert name surname : ”); • scanf(“%s %s”,name,surname); • printf(“Hi %s %s\n”,name,surname); • } Somkiat Boonroddit 30-010-104 Computer Programming
A รับข้อมูลทีละอักขระด้วย getchar() • getchar() ใช้รับข้อมูลทีละอักขระโดยต้องลงท้ายด้วย enter variable : ตัวแปรชนิด char เก็บค่าอักขระที่ถูกป้อนเข้ามา variable = getchar(); ฟังก์ชั่น getch() อยู่ ในไลบราลี conio.h • #include <stdio.h> • #include <conio.h> • char x; • void main() • { • clrscr(); • printf(“Enter your grade : ”); • x = getchar(); • printf(“Programming Language Grade %c”,x); • } 30-010-104 Computer Programming
รับข้อมูลทีละอักขระด้วย getch() • getch() ใช้รับข้อมูลทีละอักขระเช่นเดียวกับ getchar() แต่ไม่ต้องลงท้ายด้วย enter และอักขระที่ใส่เข้าไปจะไม่ปรากฎบนหน้าจอ variable = getch(); variable : ตัวแปรชนิด char เก็บค่าอักขระที่ถูกป้อนเข้ามา ฟังก์ชั่น getch() อยู่ ในไลบราลี conio.h • #include <stdio.h> • #include <conio.h> • char x; • void main() • { • clrscr(); • printf(“Enter your grade : \n”); • x = getch(); • printf(“Programming Language Grade : %c”,x); • } A 30-010-104 Computer Programming
I Miss you like crazy!! รับข้อมูลเป็นข้อความด้วย gets() • gets() ใช้รับข้อมูลชนิดข้อความโดยเฉพาะ str : ตัวแปรที่ใช้เก็บข้อมูลชนิด string gets (str); • #include <stdio.h> • char message[30]; • void main() • { • clrscr(); • printf(“Enter your message : ”); • gets(message); //scanf(“%s”,message); • printf(“%s”,message); //puts(message); • } 30-010-104 Computer Programming
แบบฝึกหัดท้ายบท 30-010-104 Computer Programming