1 / 23

การแสดงผล และการรับข้อมูล

การแสดงผล และการรับข้อมูล. คำสั่งที่ใช้ในการแสดงผล และการรับข้อมูล. printf(). รูปแบบ printf (“ control “,value):. control : ส่วนที่ใช้ควบคุมการแสดงผล ซึ่งมีอยู่ 3 แบบด้วยกัน คือ ข้อความ ธรรมดา รหัสควบคุมรูปแบบ ( เช่น %d, %f) และอักขระควบคุมการแสดงผล

munin
Download Presentation

การแสดงผล และการรับข้อมูล

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. การแสดงผล และการรับข้อมูล

  2. คำสั่งที่ใช้ในการแสดงผล และการรับข้อมูล printf() รูปแบบ printf (“ control “,value): control : ส่วนที่ใช้ควบคุมการแสดงผล ซึ่งมีอยู่ 3 แบบด้วยกัน คือ ข้อความ ธรรมดา รหัสควบคุมรูปแบบ (เช่น %d, %f) และอักขระควบคุมการแสดงผล (เช่น %n) โดยส่วนที่ใช้ควบคุมการแสดงผลเหล่านี้จะต้องเขียนไว้ภายใน “ “ value: คือ ค่าของเครื่องหมาย นิพจน์ หรือมาโครที่ต้องการแสดงผล โดย ถ้ามีมากกว่าหนึ่งตัวให้ใช้เครื่องหมาย , (comma) คั่นระหว่างแต่ละตัว

  3. ตัวอย่าง /*Ex1 */ #include <stdio.h> main() { printf (“Nice to meet you!”); } OUTPUT Nice to meet you! /*Ex2 */ #include <stdio.h> int num = 32; main() { printf (“%d” ,num); } 32 OUTPUT

  4. รหัสรูปแบบ (Format Code) • %d สำหรับการแสดงผลตัวเลขจำนวนเต็ม • %u สำหรับการแสดงผลตัวเลขจำนวนเต็มบวก • %o สำหรับการแสดงผลออกมาในรูปแบบของเลขฐานแปด • %x สำหรับการแสดงผลออกมาในรูปแบบของเลขฐานสิบหก • %f สำหรับการแสดงผลตัวเลขทศนิยม • %e สำหรับการแสดงผลตัวเลขทศนิยมออกมาในรูปแบบ E • %c สำหรับแสดงผลอักขระ 1 ตัว • %s สำหรับแสดงผลข้อความ • %p สำหรับการแสดงผลตัวชี้ตำแหน่ง

  5. /* EX 3 */ #include<stdio.h> main() { int x1=43,x2=0x77,x3=0573; float y1=-764.512,y2=1.25e02; char z='A'; char name[10]="Southeast"; clrscr(); printf("\n%d\n",x1); printf("%x\n%o\n",x2,x3); printf("%f\n%e\n",y1,y2); printf("%c\n%s\n",z,name); } ตัวอย่าง OUTPUT

  6. อักขระควบคุมการแสดงผลอักขระควบคุมการแสดงผล • \n ขึ้นบรรทัดใหม่ • \t เว้นช่องว่างเป็นระยะ 1 Tab (6 ตัวอักษร) • \r กำหนดให้ Cursor ไปอยู่ต้นบรรทัด • \f เว้นช่องว่างเป็นระยะ 1 หน้าจอ • \b ลบอักขระตัวท้ายสุดออก 1 ตัว

  7. ตัวอย่าง #include <stdio.n> #define PUBLISH “infopress” int x1 =14, x2 = 5; main() { printf (“This is the book from %s\n”, PUBLISH); printf(“Sum of %d + %d =\t%d\n”,x1, x2, x1+x2); } This is the book from infopress Sum of 14 + 5 = 19 OUTPUT

  8. แสดงผลทีละอักขระด้วย putchar() รูปแบบ putchar(char); char : เป็นตัวแปรชนิด char หรืออักขระที่เขียนภายในเครื่องหมาย ‘ ‘

  9. ตัวอย่าง การใช้คำสั่ง putchar( ) #include <stdio.h> char first = ‘0’; main() { clrscr(); putchar(first); putchar(‘k’); } เรียกใช้ Function Clrscr แสดงข้อความในตัวแปร First แสดงตัวอักษร k OUTPUT OK

  10. แสดงผลเป็นข้อความด้วย puts() รูปแบบ puts(str); str : เป็นตัวแปรชนิดข้อความหรือข้อความที่เขียนภายในเครื่องหมาย “ “

  11. ตัวอย่าง การใช้คำสั่ง puts( ) #include <stdio.h> char message[ ] = “C Language”; main() { clrscr(); puts(message); puts(“Easy & fun”); } เรียกใช้ Function Clrscr แสดงข้อความในตัวแปร แสดงข้อความ easy & fun C Language Easy & fun OUTPUT

  12. รับข้อมูลจาก Keyboard ด้วย scanf() Scanf ( ) รูปแบบ scanf (“ format “ , &variable); format : เป็นการกำหนดรูปแบบ ของข้อมูลที่จะรับเข้ามา โดยจะใช้รหัส รูปแบบเหมือนกับ printf () variable :ตัวแปรที่ใช้สำหรับรับข้อมูลเข้ามา โดยจะต้องเขียนนำหน้า ด้วยเครื่องหมาย & ยกเว้นตัวแปรที่จะเก็บข้อความเท่านั้น

  13. ตัวอย่าง /* EX 4 */ #include <stdio.h> int age; main () { clrscr( ); printf (“How Old are you?”); scanf (“%d”,&age); printf (“You are %d years old. \n”,age); } How Old are you? 25 You are 25 years old. OUTPUT

  14. รับข้อมูลทีละอักขระด้วย getchar() getchar ( ) รูปแบบ variable = getchar(); variable : ชื่อของตัวแปรชนิดอักขระ ซึ่งจะใช้เก็บค่าที่รับเข้ามา จาก Keyboard

  15. ตัวอย่างของการใช้ getchar() #include <stdio.h> char x; main() { clrscr(); printf (“Enter you favorite letter : “); x = getchar(); printf (“You insert : %c”,x); } เรียกใช้ Function Clrscr แสดงข้อความ รับข้อมูลมาเก็บไว้ที่ตัวแปร x แสดงข้อความ และค่าที่เก็บ ไว้ที่ตัวแปร x C Language Easy & fun OUTPUT

  16. รับข้อมูลทีละอักขระด้วย getch() getch ( ) รูปแบบ variable = getch(); variable : ชื่อของตัวแปรชนิดอักขระ ซึ่งจะใช้เก็บค่าที่รับเข้ามา จาก Keyboard

  17. ตัวอย่างของการใช้ getch() #include <stdio.h> char x; main() { clrscr(); printf (“Enter you favorite letter : “); x = getch(); printf (“You insert : %c”,x); } เรียกใช้ Function Clrscr แสดงข้อความ รับข้อมูลมาเก็บไว้ที่ตัวแปร x แสดงข้อความ และค่าที่เก็บ ไว้ที่ตัวแปร x Enter you favorite letter : You insert :x OUTPUT

  18. รับข้อมูลเป็นข้อความด้วย gets() gets ( ) รูปแบบ gets(str); Str : ตัวแปรที่จะใช้เก็บข้อความ ซึ่งเราต้องสร้างเตรียมไว้ก่อนที่จะเรียกใช้ Function gets()

  19. ตัวอย่างของการใช้ gets() #include <stdio.h> char message[30]; main() { clrscr(); printf (“Enter your message:”); gets (message); printf (“Your message is %s”,message); } สร้างตัวแปรที่เก็บข้อความ รับข้อความจาก Keyboard แสดงข้อความที่รับเข้ามา Enter your message: Good Night Your message is Good Night OUTPUT

  20. แบบฝึกหัด 1. จงเขียนโปรแกรมเพื่อรับตัวอักษรจากแป้น Keyboard เข้ามา 1 ตัว แล้วบอกว่า ตัวอักษรที่รับเข้ามานั้นเป็นตัวอักษรใด เช่น Confirm your answer [y/n] : y you choose y

  21. แบบฝึกหัด • จงเขียนโปรแกรมเครื่องคิดเงินทอนของร้านค้า โดยให้ผู้ใช้กรอกชื่อสินค้า ราคาสินค้า และจำนวนเงินที่ลูกค้าจ่ายเงิน จากนั้นโปรแกรมจะทำการคำนวณเพื่อหาจำนวนเงินทอน พร้อมกับแสดงผลออกที่หน้าจอ

  22. เฉลยข้อ 1 #include<stdio.h> main() { char a; clrscr(); printf("\nConfirm yours answer[y/n]:\n"); a=getch(); printf("\nYou Choose %c\n",a); getch(); }

  23. #include<stdio.h> main() { char nproduct; int price,pay,total; clrscr(); printf("\t\t********Program Change********"); printf("\t\t\nInsert Product Name: ");scanf("%s",&nproduct); printf("\t\t\nInsert Price : ");scanf("%d",&price); printf("\t\t\nInsert Money to pay: ");scanf("%d",&pay); total=pay-price; printf("\t\t\nYou Money give back: %d Baht\n",total); getch(); } เฉลยข้อ 2

More Related