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

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

Lecture 4 องค์ประกอบภาษา C To do: Hand back assignments

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


งานนำเสนอเรื่อง: "Lecture 4 องค์ประกอบภาษา C To do: Hand back assignments"— ใบสำเนางานนำเสนอ:

1 Lecture 4 องค์ประกอบภาษา C To do: Hand back assignments
Give out handouts to anybody who missed them last class Quick comments on PS3 Quick comments on grades READING: S+S: Appendix C

2 แนะนำVisual C++ 6 Menu Bar Tool Bar Code Editor Project Workspace
Output Window

3 Objectives Understand Rule of Command in C and C++
โครงสร้างภาษา C(Structure in C) การใส่หมายเหตุ(Comment) การอินพุตและเอาท์พุตเบื้องต้น(Basic Input,Output) ตัวปฏิบัติการ(Operation) การกำหนดตัวแปร(Variable) คำสงวน(Reserved Word) No problem…that’s just about a semester worth of material in 80 minutes.

4 Structure in C #include <Library> /*ส่วนประกาศ Declaration */
/*ตัวแปรค่าคงที่ข้อมูล แบบโกบอล(Global)*/ Main() /*ฟังก์ชันหลัก*/ { /*ตัวแปร/ค่าคงที่/ชนิดข้อมูล แบบโลคอล(Local)*/ } ชื่อฟังก์ชัน() /* โปรแกรมย่อย(sub program)*/

5 โปรแกรมแรก โปแกรมแรก #include <stdio.h> // class for stream input/output #include <conio.h> // class for stream input/output int main () // start of main function { printf(“Hello Word:“); return 0; } #include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello Word:\n“; return 0; }

6 การใส่หมายเหตุ(Comments)
รูปแบบการใช้งาน: //comment /*comment*/ Examples: // This text is treated as a comment till end of line. /* This text is treated as a comment until the following symbols are found: */

7 คำสั่งในการรับและแสดงผลข้อมูล
เป็นคำสั่งแสดงข้อมูลออกทางจอภาพ รูปแบบการใช้งาน printf(“Control”,Argument_list) Control หมายถึง รหัสที่ใช้ควบคุมการแสดงผล ต้องอยู่ในเครื่องหมายคำพูด ” “ แบ่งออกเป็น 3 ส่วน ข้อความ(Text) ระหัสรูปแบบ(Format Code) ระหัสควบคุม(Control Code) Argument_list หมายถึงตัวแปรและค่าคงที่

8 ประเภทของรหัสรูปแบบ Format Code วัตถุประสงค์การใช้งาน %c
การแสดงผลแบบ 1 อักขระ %s การแสดงผลแบบข้อความ %d การแสดงผลแบบเลขจำนวนเต็ม %f การแสดงผลแบบเลขจำนวนทศนิยม %e การแสดงผลแบบเลขจำนวนเต็มผลกำลัง %g %h การแสดงผลแบบShot Integer %p การแสดงค่า Address ของตัวแปร %l %x การแสดงผลแบบเลขฐาน 16 Hexadecimal

9 ประเภทของรหัสควบคุม Control Code วัตถุประสงค์การใช้งาน \t
เลื่อนระยะทางแนวนอน \n ขึ้นบรรทัดใหม่ \b เลื่อนข้อมูลถอยหลัง 1 ตัวอักษร \r เลื่อนcursorไปอยู่ต้นบรรทัด

10 Argument_list #include <stdio.h> // class for stream input/output #include <conio.h> // class for stream input/output using namespace std; // use the standard namespace int main () // start of main function { int T1=1000; char T2 =”Rit” char T3=“G” float T4=10.50; clrscr(); //clear screen printf(“Display T1 %d \n “,T1); printf(“Display T2 %s \n “,T2); printf(“Display T3 %c \n “,T3); printf(“Display T4 %.2f \n “,T4); return 0; } Output Display T1 1000 Display T2 Rit Display T3 G Display T

11 คำสั่งในการรับและแสดงผลข้อมูล
เป็นคำสั่งรับข้อมูลผ่านทางจอภาพ รูปแบบการใช้งาน scanf(“Control”,Argument_list) Control หมายถึง รหัสรหัสรูปแบบ(Format code) ต้องอยู่ในเครื่องหมายคำพูด ” “ โดยมี % นำหน้าอักขระในการรับข้อมูล ข้อความ(Text) ระหัสรูปแบบ(Format Code) ระหัสควบคุม(Control Code) Argument_list หมายถึงตัวแปรโดยมีเครื่องหมาย & อยู่หน้าตัวแปร

12 ประเภทของรหัสรูปแบบ Format Code วัตถุประสงค์การใช้งาน %c
ใช้รับข้อมูลที่เป็นตัวอักษร 1 ตัว รวมทั้งเครื่องหมายและช่องว่างต่าง %s ใช้รับข้อมูลแบบข้อความ %d ใช้รับข้อมูลแบบเลขจำนวนเต็ม %f ใช้รับข้อมูลแบบเลขจำนวนทศนิยม %e ใช้รับข้อมูลแบบเลขจำนวนเต็มยกกำลัง %g %h ใช้รับข้อมูลแบบShot Integer %p ใช้รับข้อมูลตัวแปร Pointer %x ใช้รับข้อมูลแบบเลขฐาน 16 Hexadecimal

13 Argument_list #include <stdio.h> // class for stream input/output #include <conio.h> // class for stream input/output int main () // start of main function { char R1; char R2[5] ; int R3; float R4; clrscr(); //clear screen printf(“Enter Character :“); scanf(“%c”,&R1); printf(“Enter String :“); scanf(“%s”,&R2); printf(“Enter Integer : “); scanf(“%d”,&R3); printf(“Enter Float :“); scanf(“%f”,&R4); printf(“%c %s %d %.2f \n”,R1,R2,R3,R4); return 0; } Output Enter Character: A Enter String : Good Enter Integer :25 Enter Float:12 A Good

14 ตัวดำเนินการส่งออก เป็นคำสั่งรับข้อมูลผ่านทางจอภาพ รูปแบบการใช้งาน
cout<<expression<<expression…<<expression; << หมายถึง ตัวดำเนินการส่งออก expression(นืพจน์) หมายถึง คำสั่งที่ต้องการให้แสดง

15 ตัวอักขระและสายอักขระ
สายอักขระ(string) สัญลักษณ์ “Hello” เรียกว่าสายอักขระ(string) โดยจะอยู่ในเครื่องหมายอัญประกาศ (” “) ตัวอักขระ(character) จะหมายถึงตัวอักษรหรือตัวเลขตัวโดดๆ โดยจะต้องอยุ่ภายในเครื่องหมาย อัญประกาศเดี่ยว (‘ ‘) โดยที่คอมพิวเตอร์สามารถอ่านเข้าใจความหมายและนำไปเก็บไว้ในหน่วยความจำได้ ซึ่งคอมพิวเตอร์ส่วนใหญ่จะใช้ชุดตัวอักขระรหัสแอสกี (ASCII = American Standard Code for Information Interchange) เช่น ‘A’ หรือ ‘a’

16 โปรแกรมHello แบบต่างๆ โปแกรมแรก
#include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello Word:\n“; return 0; } #include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello “ <<“Word:\n“; return 0; } #include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello “ <<“Word:” <<‘ \n ’; return 0; }

17 โปรแกรมHello แบบต่างๆ
โปแกรมแรก โปรแกรมHello แบบต่างๆ #include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello “<<‘W’<<‘o’<<‘r’<<‘d’<<‘\n’ ; return 0; } โปรแกรมจะทำการส่งสายอักขระ ออกไป 1ขุด และส่งอักขระออกไปอีก 5 ชุด

18 ตัวดำเนินการรับเข้า เป็นคำสั่งรับข้อมูลผ่านทางคีย์บอร์ด
รูปแบบการใช้งาน cin>>ตัวแปร; >> หมายถึง ตัวดำเนินการรับเข้า ตัวแปร(Vriable) เป็นที่เก็บค่าเมื่อทำการนำค่าเข้ามาแล้ว

19 ตัวอย่างการรับข้อมูลจำนวนเต็ม
#include <iostream.h> // class for stream input/output int main () // start of main function { int age; cout<<“How old are you:” ; cin>>age; cout<<”In 10 years, you will be”<<age+10; return 0; } การใช้ cin จะต้องมีการประกาศตัวเตรียมประมวลผลคือ #include <iostream.h> วัตถุ cin หรือ cout เปรียบได้กับเป็นรางน้ำใช้เป็นช่องทางผ่านจำนวนของตัวอักษร

20 Applying the software development method
Case study: Converting Miles to Kilometers PROBLEM Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion. ANALYSIS The problem states that you prefer to deal in metric measurements, so you must convert distance measurements in miles to kilometers. Therefore, the problem input is distance in miles and the problem output is distance in kilometers. To write the program, you need to know the relationship between miles and kilometers. One mile equal kilometers.

21 DESIGN Data requirements Problem input miles the distance in miles
Problem output kms the distance in kilometers Relevant formula 1 miles = kilometers DESIGN Algorithm 1. Get the distance in miles. 2. Convert the distance to kilometers. 3. Display the distance in kilometers. Now decide whether any steps of the algorithm need further refinement. Case study: Converting Miles to Kilometers

22 Algorithm with refinements
Step 2 Refinement 2.1 the distance in kilometers is times the distance in miles. Algorithm with refinements 1. Get the distance in miles. 2. Convert the distance to kilometers. 2.1 the distance in kilometers is times the distance in miles. 3. Display the distance in kilometers. Desk-checking the algorithm: If step 1 gets a distance of 10.0 miles, step 2.1 would convert it to 1.609 x or kilometers. And this correct result would be displayed by step 3. Case study: Converting Miles to Kilometers

23 IMPLEMENTATION // Miles.cpp
// Converts distance in miles to kilometers. #include <stdio.h> // standard input/output using namespace std; // use the standard namespace int main () // start of main function { const float km_per_mile = 1.609; // km in a mile float miles, // input: distance in miles kms; // output: distance in kilometers // Get the distance in miles. printf("Enter the distance in miles: “); scanf( “ %f ”, &miles); // Convert the distance to kilometers. kms = km_per_mile * miles; // Display the distance in kilometers. printf (“The distance in kilometers is %f \n“, kms ); return 0; } Enter the distance in miles: 10.0 The distance in kilometers is 16.09 Case study: Converting Miles to Kilometers

24 ตัวดำเนินการ(Operation)
ตัวดำเนินการทางคณิตศาสตร์(Arithmetic Operation) ตัวดำเนินการเปรียบเทียบ(Comparative Operation) ตัวดำเนินการทางตรรกะ(Logical Operation) ตัวดำเนินการเพิ่มและลดค่า(Increment and Decrement Operation) ตัวดำเนินการแบบบิต(Bitwise Operation) ตัวดำเนินการกำหนดค่า(Assignment Operation) No problem…that’s just about a semester worth of material in 80 minutes.

25 ตัวดำเนินการทางคณิตศาสตร์(Arithmetic Operation)
ความหมาย (Meaning) ตัวอย่าง (Example) + Addition A+B - Subtraction A-B * Multiplication A*B / Division A/B % Modulus A%B

26 Integer Division and Modulus
Operation Integer Division and Modulus Examples of integer division: 15 / 3 = 5 1 / 2 = 0 0 / 15 = 0 15 / 0 undefined Examples of integer modulus (yielding a remainder): 7 % 2 = 1 299 % 100 = 99 -15 % 7 = -1 (system dependent) 15 % -7 = 1 (system dependent) 15 % 0 undefined % cannot be used with float or double.

27 ตัวดำเนินการเปรียบเทียบ(Comparative Operation)
ความหมาย (Meaning) ตัวอย่าง (Example) > Greater than A>B < Less than A<B >= Greater than or Equal A>=B <= Less than or Equal A<=B == Equal A==B != Not Equal A!=B

28 ตัวดำเนินการทางตรรกะ(Logical Operation)
ความหมาย (Meaning) ตัวอย่าง (Example) && AND A>B&&C<D || OR A>B||B<C ! NOT !A&&!B ตัวดำเนินการเพิ่มและลดค่า(Increment and Decrement Operation) ตัวดำเนินการ (Operation) ความหมาย (Meaning) ตัวอย่าง (Example) ++ Increment A++ หรือ ++A -- Decrement A- - หรือ - -A

29 A>>=B มาจาก A=A>>B
Operation ตัวดำเนินการกำหนดค่า(Assignment Operation) ตัวดำเนินการ (Operation) ความหมาย (Meaning) ตัวอย่าง (Example) = Assignment A=B += Addition A+=B มาจาก A=A+B -= Subtraction A-=B มาจาก A=A-B *= Multiplication A*=B มาจาก A=A*B /= Divide A/=B มาจาก A=A/B %= Modulus A%=B มาจาก A=A%B &= Bitwise And A&=B มาจาก A=A&B |= Bitwise Inclusive Or A|=B มาจาก A=A|B ^= Bitwise Exclusive Or A^=B มาจาก A=A^B >>= Left Shift A>>=B มาจาก A=A>>B

30 ลำดับการทำก่อน ภาษาซี ++ มีเครื่องหมายการดำเนินการอยู่หลายตัว ดังนั้นเราจำเป็นที่จะต้องทราบถึงลำดับของการคำนวณ โดยส่วนใหญ่แล้วตัวดำเนินการทางคณิตศาสตร์ จะมีลำดับในการทำก่อนสูงกว่าตัวดำเนินเการอื่นๆ นิพจน์ * 5 ลำดับการประมวลผลคือ (3*5) = = 27 และยิ่งไปกว่านั้นตัวดำเนินการทางคณิตศาสตร์ จะมีลำดับการทำก่อนสูงกว่าตัวดำเนินการกำหนดค่า นิพจน์ n=42 - 3* 5 จะทำการประมวลผลได้ 27 ก่อนที่จะกำหนดค่าให้กับ n

31 ตารางลำดับการทำก่อน

32 การเพิ่มและการลดค่า โปแกรมแรก
#include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello Word:\n“; return 0; } #include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello “ <<“Word:\n“; return 0; } #include <iostream.h> // class for stream input/output int main () // start of main function { cout<<“Hello “ <<“Word:” <<‘ \n ’; return 0; }

33 ตัวอย่างการเพิ่มและการลดค่า
#include <iostream.h> // class for stream input/output int main () // start of main function { int m=44, n=66; cout<<“m = “ <<m<<“,n = “<<n<<endl; ++m; - - n; m+ +; n- -; return 0; }

34 ตัวอย่างการเพิ่มและการลดค่า ก่อนและหลัง
#include <iostream.h> // class for stream input/output int main () // start of main function { int m=66; n= ++ m; cout<<“m = “ <<m<<“,n = “<<n<<endl; n = m++; cout<<“m = “ <<m++<<endl; cout<<“m = “ <<m<<endl; cout<<“m = “ <<++m<<endl; return 0; }

35 คำสงวน (List of All C++ Reserved Words)
Operation คำสงวน (List of All C++ Reserved Words) and default inline pret_cast typename and_eq delete int return union asm do long short unsigned auto double mutable signed using bitand dynamic_cast namespace sizeof virtual bitor else new static void bool enum not static_cast volatile break explicit not_eq struct wchar_t case export operator switch while catch extern or template xor char false or_eq this xor_eq class float private throw compl for protected true const friend public try const_cast goto register typedef continue if reinter typeid


ดาวน์โหลด ppt Lecture 4 องค์ประกอบภาษา C To do: Hand back assignments

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


Ads by Google