งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Computer Programming การเขียนโปรแกรมคอมพิวเตอร์"— ใบสำเนางานนำเสนอ:

1 Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
สัปดาห์ที่ 4 การรับค่าและการแสดงผลในภาษาซี

2 objectives รู้จักและเข้าใจคำสั่งเพื่อใช้ในการรับค่าและแสดงผลในภาษาซี
สามารถรับข้อมูลจากคีย์บอร์ดและแสดงข้อความออกทางจอภาพได้ สามารถจัดรูปแบบการแสดงผลออกทางจอภาพตามรูปแบบต่างๆ ได้อย่างถูกต้อง เขียนโปรแกรมประมวลผลตัวอักษรได้

3 Outline 1 Review Assignment 2 Scanf Function 3 Printf Function 4
The Other Function 5 Assignment #3

4 C Programming Structure
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

5 Return_Type Function_name(Type variable)
Process (ทำงาน) ส่งค่ากลับ รับค่ามา Function Return_Type Function_name(Type variable)

6 Operator & Operand N - 1 5 * ( a * b – 3 ) นิพจน์

7 Type of Operators ตัวดำเนินการเลขคณิต (arithmetic operators)
ตัวดำเนินการเชิงสัมพันธ์ (relational operators) ตัวดำเนินการเชิงตรรกะ (logical operators) ตัวดำเนินการระดับบิต (bitwise operators) ตัวดำเนินการกำหนดค่า (assignment operators) ตัวดำเนินการกำหนดค่าเชิงประกอบ (compound assignment operators) ตัวดำเนินการเพิ่มและลดค่า (increment and decrement operators) ตัวดำเนินการแปลงชนิดข้อมูล (type cast operators)

8 Arithmetic Operators เครื่องหมาย การทำงาน ตัวอย่าง + บวก ans = a + b;
- ลบ ans = a - b; * คูณ ans = a * b; / หาร ans = a / b; % โมดูลัส (modulo) ans = a % b;

9 Examples of Relational Operators
การเปรียบเทียบ ผลที่ได้ 7 == 9 False 7 != 9 True 8 > 8 8 >= 8 (10+9)<7 4 <= 3 การเปรียบเทียบ ผลที่ได้ 22 == 22 True (3+5)!=8 False 9 > 7 7 >= 9 7<(10+9) 3 <= 4 ไม่ควรใช้เครื่องหมายเท่ากับ == หรือไม่เท่ากับ != สำหรับข้อมูลทศนิยม

10 Logical Operators (1) เครื่องหมาย ความหมาย ตัวอย่าง && และ (and)
x && y || หรือ (or) x || y ! ไม่ หรือ ตรงกันข้าม (not) !x

11 Logical Operators (2) การดำเนินการ ผลที่ได้ การดำเนินการ ผลที่ได้
T && T T T && F F F && T F && F การดำเนินการ ผลที่ได้ T || T T T || F F || T F || F F การดำเนินการ ผลที่ได้ !T F !F T

12 Bitwise Operators

13 Logical & Bitwise Operator
ตัวอย่าง 10 = ; 1 = Logical Bitwise 1313

14 Assignment Operators คือ ตัวดำเนินการเพื่อใช้ในการกำหนดค่าข้อมูล รูปแบบ assignment expression คือ variable = expression กำหนดค่าให้ตัวแปรหนึ่งตัว เช่น x = 2, x = y+2; รูปแบบ Multiple assignment expression คือ variable#1 = variable#2 =… = variable#k = expression กำหนดค่าให้ตัวแปรหลายตัวในคราวเดียวกัน เช่น x = z = w = y + 2

15 Composition Operator เครื่องหมาย ตัวอย่างการใช้งาน ตัวอย่างรูปแบบเต็ม += y += x; y = y + x; -= y -= x; y = y - x; *= y *= x; y = y * x; /= y /= x; y = y / x; %= y %= x; y = y % x;

16 Increment & Decrement Operator
เครื่องหมาย การทำงาน ตัวอย่าง ขั้นตอนการทำงาน ++ เพิ่มค่าทีละ 1 (Increment) x++; ++x; เพิ่มค่า x ขึ้น 1 y = ++x; กำหนดค่าให้ y y = x++; -- ลดค่าทีละ 1 (Decrement) x--; --x; ลดค่า x ลง 1 y = --x; y = x--;

17 Cast Type Operator ตัวอย่าง กำหนด char ch; int i; float f; double d;

18 Example of Casting Operator
#include<stdio.h> main( ) { float gpa = 3.9; int num = 3; printf ("gpa(f) :%-10f num(d) :%-10d\n",gpa,num); printf ("gpa(d) :%-10d num(f) :%-10f\n",gpa,num); printf ("(int)gpa(d) :%-10d", (int)gpa); printf ("(float)num(f) :%-10f", (float)num); }

19 Assignment Review: What is the wrong?
#include<stdio.h> main( ) { int X; X = * 3; printf(“4 + 2 * 3 = %d\n”, X); X = ; printf(“ = %d\n”, X); X = % 7; printf(“ % 7 = %d\n”, X); X = % 27 – 32 / 4; printf(“ % 27 – 32 / 4 = %d\n”, X); }

20 To correct this example
#include<stdio.h> main( ) { int X; X = * 3; printf(“4 + 2 * 3 = %d\n”, X); X = ; printf(“ = %d\n”, X); X = % 7; printf(“ mod 7 = %d\n”, X); X = % 27 – 32 / 4; printf(“ mod 27 – 32 / 4 = %d\n”, X); }

21 Outline 1 Review Assignment 2 Scanf Function 3 Printf Function 4
The Other Function 5 Assignment #3

22 Receiving the Information from Keyboard
โปรแกรมโดยทั่วไปต้องมีการรับค่าข้อมูลจากผู้ใช้โปรแกรม เพื่อนำมาหาผลลัพธ์ตามกระบวนการทำงานของโปรแกรม หรือตามความต้องการของผู้ใช้งาน คำสั่งที่ใช้สำหรับการรับค่าในภาษาซีมีหลายคำสั่ง แต่ที่สามารถใช้งานได้ครอบคลุมและนิยมใช้กัน คือ คำสั่งหรือฟังก์ชัน scanf()

23 Scanf Function (1) scanf() เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ stdio.h ใช้รับข้อมูลจากผู้ใช้ scanf (format-string, address-list); scanf(“format”,argument1,argument2, … ); scanf("control", &variable1,&variable2,…);

24 Scanf Function (2) Format Code Value scanf ("%f", a);

25 format-string or format or control format
เป็นรูปแบบการรับค่าที่ถูกกำหนดตามชนิดของข้อมูล (ค่าตัวแปร) แสดงอยู่ในเครื่องหมาย Double quotation mark (“_”) รูปแบบการรับค่าตัวแปร (% ตามหลังด้วยอักขระ) เมื่อรันผลต้องป้อนค่าตามรูปแบบที่กำหนดตามด้วยการกด enter หมายเหตุ หากผู้ใช้ป้อนข้อมูลไม่ตรงกับชนิดของตัวแปร โปรแกรมจะทำงานผิดพลาด (Runtime Error)

26 address-list or argument or address of variable
คือ ตำแหน่งของตัวแปรที่ต้องการเก็บข้อมูลไว้ (การใช้งานตำแหน่งของตัวแปรจะใช้เครื่องหมาย ampersand (&) นำหน้าชื่อตัวแปร ยกเว้นตัวแปรชนิดข้อความ) ตัวอย่าง &variable_name

27 Format Control of Scanf Function (1)
ส่วนแสดงชนิดข้อมูล การใช้งาน %d แสดงผลข้อมูลชนิดจำนวนเต็ม %u แสดงผลข้อมูลชนิดจำนวนเต็มบวก (ไม่คิดเครื่องหมาย) %o แสดงผลข้อมูลเป็นเลขฐานแปด %x แสดงผลข้อมูลเป็นเลขฐานสิบหก %f แสดงผลข้อมูลชนิดจำนวนทศนิยม (6 ตำแหน่ง) %e แสดงผลข้อมูลเป็นจำนวนทศนิยมและอยู่ในรูปยกกำลัง %c แสดงผลข้อมูลชนิดอักขระ %s แสดงผลข้อมูลชนิดข้อความ %p แสดงผลข้อมูลชนิดตัวชี้ตำแหน่ง

28 Input Format of Scanf and Printf Function(2)
รูปแบบการแสดงผลซึ่งต้องกำหนดให้ตรงกับชนิดของตัวแปร int , unsigned int กำหนดเป็น %d, %o, %x long กำหนดเป็น %ld, %lo, %lx float, double กำหนดเป็น %f, %e char กำหนดเป็น %c

29 Example of Scanf ( ) int age; scanf("%d", &age);
char name[20]; scanf ("%s",name); float point; scanf("%f",&point); int day, month, year; scanf("%d/%d/%d", &day,&month,&year);

30 ไม่ต้องมี & หน้าตัวแปร
Example 1 #include<stdio.h> int main() { float point; char name[20]; printf ("Enter your name : "); scanf ("%s",name); printf ("Enter your point : "); scanf ("%f",&point); return 0; } ตัวแปรข้อความ ไม่ต้องมี & หน้าตัวแปร ตัวแปรทศนิยม ต้องมี & หน้าตัวแปร

31 Example 2 การรับตัวแปรมากกว่า 1 ตัวในคำสั่งเดียว
#include<stdio.h> int main() { char first[20],last[20]; printf ("Enter your name and surname : "); scanf ("%s %s",first,last); printf ("Hi %s %s\nHow are you?",first,last); return 0; } การรับตัวแปรมากกว่า 1 ตัวในคำสั่งเดียว ต้องป้อนข้อมูลให้มีรูปแบบเหมือนกัน Enter your name and surname : Jirasak Sittigorn Hi Jirasak Sittigorn How are you?

32 Example 3 #include<stdio.h> int main() { char name[40];
float gpa; printf ("Enter your name : "); scanf ("%s",name); printf ("Enter your GPA : "); scanf ("%f",&gpa); printf ("Name : %s\n",name); printf ("Gpa : %f",gpa); return 0; }

33 Example 4 #include<stdio.h> int main() { char name[40];
float gpa; printf ("Enter your name : "); scanf ("%[^\n]",name); printf ("Enter your GPA : "); scanf ("%f",&gpa); printf ("Name : %s\n",name); printf ("Gpa : %f",gpa); return 0; }

34 Outline 1 Review Assignment 2 Scanf Function 3 Printf Function 4
The Other Function 5 Assignment #3

35 Show the Results Processing to User
โปรแกรมโดยทั่วไปต้องมีแสดงผลลัพธ์ของการประมวลผลหรือการทำงานของโปรแกรม เพื่อให้ผู้ใช้งานทราบผลลัพธ์ของการทำงาน คำสั่งที่ใช้สำหรับการรับค่าในภาษาซีที่นิยมใช้กัน คือ คำสั่งหรือฟังก์ชัน printf()

36 Printf Function (1) printf() เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ stdio.h ใช้สำหรับแสดงข้อความ ค่าของตัวแปรหรือผลการดำเนินการของนิพจน์ออกทางหน้าจอ printf (format-string, data-list); printf(“format”, argument1, argument2, … ); printf(“message or control",variable list);

37 Format / Carriage Control
Printf Function (2) Format / Carriage Control Format Code Value printf ("Sum = %f \n", a+b);

38 format-string or format or massage or control format
เป็นรูปแบบการแสดงค่าที่ถูกกำหนดตามชนิดของข้อมูลหรือตัวแปร แสดงอยู่ในเครื่องหมาย Double quotation mark (“_”) รูปแบบการรับค่าตัวแปร (% ตามหลังด้วยอักขระ อักขระควบคุมหรือ Escape Character

39 data-list or argument or variable list
คือ ตำแหน่งของตัวแปรที่ต้องการจะนำข้อมูลออกไปแสดง ถ้ามีมากกว่า 1 ตัวแปรหรือ 1 พจน์ขึ้นไปจะต้องคั่นระหว่างตัวแปรหรือนิพจน์ด้วยเครื่องหมาย Comma (,) ซึ่งข้อมูลในส่วนนี้อาจจะเป็น ตัวแปร, นิพจน์ หรือการดำเนินการระหว่างตัวแปรก็ได้

40 Escape Character or Carriage Control
Format การแสดงผล \0 ค่าว่าง (Null Character) \a ส่งเสียง 1 ครั้ง (Bell) \n ขึ้นบรรทัดใหม่ (New Line) \t เว้นช่องว่างเป็นระยะ 1 tab (Horizon Tab) \r ให้เคอร์เซอร์กลับไปอยู่ซ้ายสุดหรือต้นบรรทัด (Carriage Return) \f ขึ้นหน้าใหม่ (Form Feed) \b ถอยกลับไป 1 ตัวอักษร (Backspace) \' พิมพ์เครื่องหมาย ' \" พิมพ์เครื่องหมาย " \\ พิมพ์เครื่องหมาย \

41 Format Control of Printf Function
ส่วนแสดงชนิดข้อมูล การใช้งาน %d แสดงผลข้อมูลชนิดจำนวนเต็ม %u แสดงผลข้อมูลชนิดจำนวนเต็มบวก (ไม่คิดเครื่องหมาย) %o แสดงผลข้อมูลเป็นเลขฐานแปด %x แสดงผลข้อมูลเป็นเลขฐานสิบหก %f แสดงผลข้อมูลชนิดจำนวนทศนิยม (6 ตำแหน่ง) %e แสดงผลข้อมูลเป็นจำนวนทศนิยมและอยู่ในรูปยกกำลัง %c แสดงผลข้อมูลชนิดอักขระ %s แสดงผลข้อมูลชนิดข้อความ %p แสดงผลข้อมูลชนิดตัวชี้ตำแหน่ง

42 Example of Printf Fuction
int age; printf("Your age is :%d", age); char name[20]; printf ("Your name is :%s",name); float g = 3.5; printf("GPA : %.2f",g); int day, month, year; printf(“Current date: %d/%d/%d",&day,&month,&year);

43 Order of Arguments (Data-list)
printf(“Today is %s , %d %s”.,”Tuesday”, 28, “October”); printf(“\nTemperature is \”%f celcius.\” ”, 24.5); Today is Tuesday , 28 October. Temperature is “ ” celcius.

44 Example of Format Control
#include <stdio.h> int x1 = 32, x2 = 0x7A, x3 = 0527; float y1 = , y2 = 9.23e04; char z = 'A'; char name[11] = "University"; 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); }

45 Mismatch Example of Format Control
#include <stdio.h> int x1 = -456, x2 = 0.72, x3 = 1xe53; float y1 = , y2 = 5.21e03; char z = 'C'; void main() { printf ("x2 = %d x3 = %d", x2, x3); printf ("x1 = %u", x1); printf ("y1 = %e y2 = %f", y1, y2); printf ("z = %d", z); }

46 Format Control : Amount Behind floating point
#include <stdio.h> float x1 = , x2 = 4.149e+03; void main() { printf ("%.4f ", x1); printf ("%.1f ", x1); printf ("%.2e ", x2); printf ("%.7e ", x2); } หมายเหตุ : จำนวนหลักหลังจุดทศนิยมสามารถกำหนดได้โดยคำสั่ง %.nf หรือ %.ne โดย n คือ จำนวนหลักหลังจุดทศนิยมที่ต้องการแสดง

47 Format Control for Message
#include <stdio.h> char message[] = "Engineering"; void main() { printf ("%.6sX\n", message); printf ("%12.6sX\n", message); printf ("%-12.6sX\n", message); } หมายเหตุ : ข้อความสามารถถูกแสดงเพียงบางส่วนเมื่อใช้คำสั่ง %m.ns โดย n คือจำนวนอักขระที่ต้องการให้แสดง และ m คือ พื้นที่ที่ใช้แสดงผล n คือ จำนวนอักขระที่ต้องการให้แสดง %-m.ns ทำให้ข้อความที่ถูกแสดงชิดขอบด้านซ้าย

48 Example of Format Control for Message
#include<stdio.h> main() { printf(":%s:\n", "Hello, world!"); printf(":%15s:\n", "Hello, world!"); printf(":%.10s:\n", "Hello, world!"); printf(":%-10s:\n", "Hello, world!"); printf(":%-15s:\n", "Hello, world!"); printf(":%.15s:\n", "Hello, world!"); printf(":%15.10s:\n", "Hello, world!"); printf(":% s:\n", "Hello, world!"); }

49 Example of Format Control for Numerical
#include<stdio.h> main() { printf("The color: %s\n", "blue"); printf("First number: %d\n", 12345); printf("Second number: %04d\n", 25); printf("Third number: %i\n", 1234); printf("Float number: %3.2f\n", ); printf("Hexadecimal: %x\n", 255); printf("Octal: %o\n", 255); printf("Unsigned value: %u\n", 150); printf("Just print the percentage sign %%\n", 10); }

50 Result

51 Example of Printf & Scanf Function
/* scanf example */ #include <stdio.h> int main () { char str [80]; int i; printf ("Enter your family name: "); scanf ("%s",str); printf ("Enter your age: "); scanf ("%d",&i); printf ("Mr. %s , %d years old.\n",str,i); printf ("Enter a hexadecimal number: "); scanf ("%x",&i); printf ("You have entered %#x (%d).\n",i,i); return 0; }

52 The Use of Carriage Control
#include <stdio.h> void main() { printf("Information\tTechnology\n"); printf ("C Lang"); printf ("\b\b\b\bProgramming"); }

53 Outline 1 Review Assignment 2 Scanf Function 3 Printf Function 4
The Other Function 5 Assignment #3

54 The Other Functions putchar(ch); puts(str); ch = getchar();
gets(str);

55 Show the Information : Putchar Function
putchar ( ch ) : เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ stdio.h ใช้สำหรับอักขระออกทางหน้าจอครั้งละตัวอักขระ ch : เป็นตัวแปรที่เก็บข้อมูลชนิด char ที่ต้องการแสดงผล ซึ่งแสดงอยู่ภายใน ‘_’ putchar(ch);

56 Example of Putchar Function
#include <stdio.h> char Name = ‘A'; void main() { putchar (Name); printf(“\n”); putchar ('b'); }

57 Show the Information : Puts Function
puts( str ) :เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ stdio.h ใช้แสดงข้อความออกทางหน้าจอ ซึ่งหลังจากแสดงข้อความแล้วจะขึ้นบรรทัดใหม่ str : เป็นตัวแปรที่เก็บข้อมูลชนิด string ที่ต้องการแสดงผล ซึ่งแสดงอยู่ภายใน “_” puts(str);

58 Example of Puts Function
#include <stdio.h> char message[] = "C Programming"; void main() { puts (message); puts (“is very easy”); }

59 Putchar & Puts Function
#include <stdio.h> char ch = 'A', str[] = "Computer"; char message[] = "C Programming"; void main() { putchar (ch); putchar (' '); putchar (str[1]); putchar ('\n'); puts (str); puts (message); }

60 Receiving the Information : Getchar Function
getchar ( ) : เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ stdio.h โดยรับข้อมูลได้ครั้งละ 1 อักขระเท่านั้น เมื่อป้อนหรือรับข้อมูลจากคีย์บอร์ดแล้วต้องกด Enter ch : เป็นตัวแปรชนิด char ที่ต้องการเก็บข้อมูล ch = getchar();

61 Example of Getchar Function (1)
#include <stdio.h> #include <conio.h> void main() { char ch; ch = getchar(); printf("Input Char is :%c",ch);} }

62 Example of Getchar Function (2)
#include <stdio.h> #include <conio.h> char x; main() { clrscr(); printf ("Enter your grade : "); x = getchar(); printf ("Programming Language Grade : %c",x); }

63 Receiving the Information : Getch Function
getch ( ) : เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ conio.h ใช้รับข้อมูลครั้งละ 1 อักขระ และจะทำงานให้คำสั่งต่อไปโดยไม่แสดงค่าที่รับเข้ามา ch : เป็นตัวแปรชนิด char ที่ต้องการเก็บข้อมูล ch = getch();

64 Example of Getch Function (1)
#include <stdio.h> #include <conio.h> char x; main() { clrscr(); printf ("Enter your grade : "); x = getch(); printf ("Programming Language Grade : %c",x); }

65 What is the missing & what is the results?
void main () { char ch1,ch2; printf ("Enter Character 1 : "); ch1 = getchar(); printf ("Enter Character 2 : "); ch2 = getch(); puts ("\n**** Output ****"); printf ("Char 1 = %c\nChar 2 = %c", ch1,ch2); } Enter Character 1 : J Enter Character 2 : **** Output **** Char 1 = J Char 2 = O พิมพ์ O แต่ไม่แสดงออกมา

66 Example of Getche Function (1)
#include <conio.h> #include <stdio.h> void main() { char ch; ch = getche(); printf("Input Char is :%c",ch); }

67 Example of Getche Function (2)
#include<stdio.h> #include<conio.h> main() { printf("Waiting for a character to be pressed from the keyboard to exit."); getche(); return 0; }

68 Receiving the Information : Gets Function
gets ( str ) เป็นฟังก์ชันมาตรฐานในภาษา C ซึ่งอยู่ใน Library ที่ชื่อ stdio.h ใช้รับข้อมูลชนิดข้อความโดยเฉพาะ อีกทั้งคำสั่งที่ใช้รับข้อมูลชนิดข้อความจากผู้ใช้งาน โดยสามารถใส่ข้อมูลที่มีการเว้นช่องว่างภายในได้ str : เป็นตัวแปรที่ใช้เก็บข้อความ โดยเราต้องสร้างเตรียมไว้ก่อนที่จะเรียกใช้ฟังก์ชัน gets() gets(str);

69 Example of Gets Function (1)
#include <stdio.h> #include <conio.h> char message[30]; main() { clrscr(); printf ("Enter your message : "); gets(message); printf ("Your message is %s", message); }

70 Example of Gets Function (2)
/* gets example */ #include <stdio.h> int main() { char string [256]; printf ("Insert your full address: "); gets (string); printf ("Your address is: %s\n",string); return 0; }

71 Outline 1 Review Assignment 2 Scanf Function 3 Printf Function 4
The Other Function 5 Assignment #3


ดาวน์โหลด ppt Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

งานนำเสนอที่คล้ายกัน


Ads by Google