1 / 13

บทที่ 5 รหัสควบคุมและการคำนวณ

C-Programming. บทที่ 5 รหัสควบคุมและการคำนวณ. C Programming. C-Programming. มีอะไรบ้างในบทนี้. C Programming. 2.1 รหัสควบคุมในภาษา C 2.2 ใส่คำอธิบาย (Comment) ลงในโปรแกรม 2.3 การคำนวณในภาษา C 2.4 นิพจน์การคำนวณ 2.5 การคำนวณทศนิยม 2.6 สรุป. C-Programming. 2.1 รหัสควบคุมในภาษา C.

Download Presentation

บทที่ 5 รหัสควบคุมและการคำนวณ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C-Programming บทที่ 5 รหัสควบคุมและการคำนวณ C Programming

  2. C-Programming มีอะไรบ้างในบทนี้ C Programming • 2.1 รหัสควบคุมในภาษา C • 2.2 ใส่คำอธิบาย(Comment) ลงในโปรแกรม • 2.3 การคำนวณในภาษา C • 2.4 นิพจน์การคำนวณ • 2.5 การคำนวณทศนิยม • 2.6 สรุป จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  3. C-Programming 2.1 รหัสควบคุมในภาษา C C Programming • \a ส่งเสียง Beep • \n ขึ้นบรรทัดใหม่ • \t แท็บในแนวนอน • \b ย้อนกลับไป 1 ตัวอักษร • \v แท็บในแนวตั้ง • \f ขึ้นหน้าใหม่ • \r รหัส Return • \’ แทนตัวอักษร Single Quote(’) • \’’ แทนตัวอักษร Double Quote(’’) • \\ แทนตัวอักษร Backslash(\) • \000 แทนตัวอักษรที่มีค่า ASCII เท่ากับ 000 ในระบบเลขฐานแปด • \xhh แทนตัวอักษรที่มีค่า ASCII เท่ากับ hh ในระบบเลขฐานสิบหก จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  4. C-Programming 2.1 รหัสควบคุมในภาษา C C Programming #include<stdio.h> Void main() { printf(“== Welcome == \n\n”); printf(“Alert\a\n”); print(“1 2 \b3 4\n”); printf(“backslash \\ \n”); printf(“show \” \n”); printf(“show \ ‘hello\’ \n”); printf(“ascii \123 \n”); printf(“ascii \x2e \n”); } จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  5. C-Programming 2.2 ใส่คำอธิบาย(comment)ลงในโปรแกรม C Programming // ใช้ในการใส่คำอธิบายแบบบรรทัดเดียว โดยจะมีผลให้ข้อความใดๆ หลังจากเครื่องหมาย // ไปจนสุดบรรทัดนั้นๆ เป็นคำอธิบายทั้งหมด /*..*/ ใช้ในการใส่คำอธิบายแบบหลายบรรทัด โดยจะมีผลให้ข้อความใดๆ ที่อยู่ระหว่าง /* และ */ กลายเป็นคำอธิบาย(อาจจะเป็น 1 บรรทัดหรือมากกว่าก็ได้) เช่น /* Program by Sasalak Thongkhao sasalak@riska.ac.th */ //include stdio.h for printf command #include<stdio.h> จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  6. C-Programming 2.3 การคำนวณในภาษาซี C Programming เครื่องหมายหรือโอเปอเรเตอร์(Operator) มีดังนี้ + เครื่องหมายบวก(Addition) - เครื่องหมายลบ(Subtraction) * เครื่องหมายคูณ(Multiplication) / เครื่องหมายหาร(Division) % เครื่องหมายหารแบบเอาเศษเป็นคำตอบ(Mod) จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  7. C-Programming 2.3 การคำนวณในภาษาซี C Programming ตัวอย่าง math1.c #include<stdio.h> void main() { Printf(“%d\n”,250+43); } %d เป็นการกำหนดรูปแบบของผลลัพธ์เป็นจำนวนเต็มฐานสิบ และแทนที่ลงตรงตำแหน่ง%d 293 จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  8. C-Programming 2.3 การคำนวณในภาษาซี C Programming ตัวอย่าง math1update.c #include<stdio.h> void main() { printf(“Answer is %d.\n”,250+43); printf(“%d %d\n”,5-3,10-2); printf(“%d \n”,5*5); printf(“%d \n”,7/3); printf(“%d \n”,7%3); } Answer is 293 2 -22 25 2 1 จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  9. C-Programming 2.4 นิพจน์การคำนวณ C Programming ลำดับการคำนวณนิพจน์ทางคณิตศาสตร์ • เครื่องหมายที่อยู่หน้าตัวเลข เช่น -2 • (...) วงเล็บ • *,/ เครื่องหมายคูณและหาร • +,- เครื่องหมายบวกและลบ จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  10. C-Programming 2.4 นิพจน์การคำนวณ C Programming ตัวอย่าง math2.c #include<stdio.h> void main() { printf(“A = %d\n”,(10-5)*3+(2+10)/4); } A = 18 จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  11. C-Programming 2.4 การคำนวณทศนิยม C Programming ใช้ %f (f ย่อมาจาก float) #include<stdio.h> void main() { printf(“Area = %f”,0.43*3*4); } Area = 5.160000 จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  12. C-Programming 2.4 การคำนวณทศนิยม C Programming ตัวอย่าง math4.c #include<stdio.h> void main() { printf(“Average = %f\n”,(65.5+15.4+22.0)/3); } Average = 34.300000 จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

  13. C-Programming 2.5 สรุป C Programming ในการคำนวณนั้น ไม่ว่าจะเป็นเลขจำนวนเต็มหรือเลขทศนิยมก็ตาม เราสามารถใส่นิพจน์ให้กับการคำนวณได้ เช่น การใส่วงเล็บเพื่อให้ลำดับการคำนวณเป็นไปตามที่ต้องการ และถ้าคาดว่าผลลัพธ์ที่ได้จากการคำนวณจะออกมาเป็นเลขทศนิยมเราจะต้องแสดงค่าโดยใช้ %f เพื่อให้ได้ค่าที่ถูกต้อง จันทร์ดารา สุขสาม @Rmuti Surin Campus : 2555

More Related