1 / 30

886201 หลักการ โปรแกรม 1

886201 หลักการ โปรแกรม 1. Lecture 3: ตัวแปร, นิพจน์คณิตศาสตร์, การคำนวณทางคณิตศาสตร์, การดำเนินการกับสายอักขระ. ตัวแปร ( Variable). ในโปรแกรมจะต้องกำหนดตัวแปรที่จะใช้ เพื่อจองเนื้อที่สำหรับ เก็บข้อมูล ในระหว่างการทำงานของโปรแกรม โดยระบุชื่อตัวแปร และชนิดข้อมูลที่จะเก็บ

lucian
Download Presentation

886201 หลักการ โปรแกรม 1

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. 886201 หลักการโปรแกรม 1 Lecture 3: ตัวแปร, นิพจน์คณิตศาสตร์, การคำนวณทางคณิตศาสตร์,การดำเนินการกับสายอักขระ

  2. ตัวแปร (Variable) • ในโปรแกรมจะต้องกำหนดตัวแปรที่จะใช้ เพื่อจองเนื้อที่สำหรับเก็บข้อมูลในระหว่างการทำงานของโปรแกรม โดยระบุชื่อตัวแปร และชนิดข้อมูลที่จะเก็บ • ชื่อของตัวแปรควรสื่อความหมาย • การตั้งชื่อตัวแปรต้องขึ้นต้นด้วยตัวอักษรหรือเครื่องหมาย ‘_’ • ชื่อตัวแปรห้ามมีช่องว่าง ห้ามมีอักขระพิเศษ (ยกเว้น ‘_’) • ชื่อตัวแปรห้ามเป็นคำสงวน เช่น main, double, return, ฯลฯ • ตัวแปรที่เป็นตัวพิมพ์ใหญ่ พิมพ์เล็ก ถือว่าแตกต่างกัน (Case Sensitive)

  3. ชื่อตัวแปร

  4. ชนิดของตัวแปรที่เป็นตัวเลขชนิดของตัวแปรที่เป็นตัวเลข

  5. พิจารณาโปรแกรมต่อไปนี้พิจารณาโปรแกรมต่อไปนี้

  6. พิจารณาโปรแกรมต่อไปนี้พิจารณาโปรแกรมต่อไปนี้

  7. ตัวดำเนินการเลขคณิต (Arithmetic Operators)

  8. int main( ) {int A, B; float C, D; A = 3; B = 8;C = 4.5; D = 1.5; cout << “A*B = ” << A*B<< endl; cout << “C*D = ”<<C*D<< endl; cout << “C/D = ” << C/D << endl; cout << “A DIV B = ” << A/B << endl; cout << “A MOD B = ” << A%B << endl; cout << “B MOD A = ” << B%A<< endl; return 0; } โปรแกรมนี้พิมพ์ผลลัพธ์อะไรออกทางหน้าจอ ?

  9. int main( ) {int A, B; float C, D; A = 3; B = 8;C = 4.5; D = 1.5; cout << “A*B = ” << A*B<< endl; cout << “C*D = ”<<C*D<< endl; cout << “C/D = ” << C/D << endl; cout << “A DIV B = ” << A/B << endl; cout << “A MOD B = ” << A%B << endl; cout << “B MOD A = ” << B%A<< endl; return 0; } โปรแกรมนี้พิมพ์ผลลัพธ์อะไรออกทางหน้าจอ ? A*B = 24 C*D = 6.750000 C/D = 3.000000 A DIV B = 0 A MOD B= 3 B MOD A = 2

  10. ตารางลำดับความสำคัญของตัวดำเนินการตารางลำดับความสำคัญของตัวดำเนินการ *** ถ้าต้องการกำหนดลำดับความสำคัญเอง ให้ใช้วงเล็บจัดกลุ่มการคำนวณตามต้องการ

  11. ตัวอย่างการหาผลลัพธ์จากนิพจน์ที่กำหนดตัวอย่างการหาผลลัพธ์จากนิพจน์ที่กำหนด

  12. ตัวดำเนินการเพิ่มลด (Increment and Decrement Operators) • increment คือการบวกเพิ่มอีก 1 โดยใช้คำสั่ง ++x; หรือ x++;หมายถึงการกำหนดค่า x = x + 1; • decrement คือการลบออก 1 โดยใช้คำสั่ง --x; หรือ x--;หมายถึงการกำหนดค่า x = x - 1;

  13. increment และ decrement แบบ prefix • Prefixคือ ทำบวกหรือลบก่อน (++a, --a) แล้วจึงนำค่าใหม่ไปใช้ ++aหมายถึง a = a+1; แล้วจึงนำค่า a ไปใช้งาน --aหมายถึง a = a-1; แล้วจึงนำค่า a ไปใช้งาน

  14. ตัวอย่าง a = 10; b = 10 + ++a; ได้ a = 11 และ b = 21 a = 5; b = - -a + 7; ได้ a = 4 และ b = 11 a = 5; cout << “a = ” << --a << endl; แสดงผล a = 4 ค่าในตัวแปรคือ 4

  15. increment และ decrement แบบ postfix • Postfixคือ ทำบวกหรือลบทีหลัง (a++, a--)นำค่าของตัวแปรไปใช้ก่อน แล้วจึงเพิ่มหรือลดค่าตัวแปร a++หมายถึง นำค่า a ไปใช้งาน แล้วจึงคำนวณ a = a+1; a--หมายถึง นำค่า a ไปใช้งาน แล้วจึงคำนวณ a = a-1;

  16. ตัวอย่าง a = 10; b = 10 + a++; ได้ a = 11 และ b = 20 a =5; b = a--+ 7; ได้ a = 4 และ b = 12 a = 5; cout << “a = ” << a-- << endl; แสดงผล a = 5 แต่ค่าในตัวแปร a คือ 4

  17. การใช้ compound statement • ใช้เพื่อแทนคำสั่งที่ตัวดำเนินการตัวแรกของนิพจน์ทางขวามือของเครื่องหมายเท่ากับ คือตัวเดียวกับตัวแปรทางซ้ายมือของเครื่องหมายเท่ากับ • มีความหมายดังนี้ a += 10; คือ a = a + 10; a += b; คือ a = a + b; a *= x + y; คือ a = a * (x + y); ต้องคำนวณ expression ทางขวาให้เสร็จเรียบร้อยก่อน โดยให้ใส่วงเล็บครอบไว้ด้วย += , -= , *= , /= , %=

  18. Output on screen amount1 = 10700.000000 amount2 = 5778.000000 pay = 16478.000000 int main() { double amount1 = 10000, amount2 = 5400; double vat = 0.07, pay = 0; amount1 += amount1*vat; cout << “amount1 = ” << amount1 << endl; amount2 += amount2*vat; cout << “amount2 = ” << amount2 << endl; pay += amount1; pay += amount2; cout << “pay = ” << pay; return 0; }

  19. ตัวแปรชนิดตัวอักขระ (char) • เก็บข้อมูลเพียงหนึ่งตัวอักขระ • ใช้เครื่องหมาย ' (single quote) ปิดหน้าและปิดหลัง • หนึ่งอักขระใช้ 8 บิต เช่น 'a', 'C', 'D', '^', '@' • escape character เช่น ‘\n’, ‘\t’, ‘\’’, ‘\”’

  20. ASCII Table

  21. ความสัมพันธ์ระหว่าง char กับ int

  22. สายอักขระ (String) • เก็บข้อมูล 1 ตัวอักขระหรือมากกว่า • ใช้เครื่องหมาย " (double quote) ปิดหน้า และปิดท้าย เช่น "student", "diligent", "industrious" #include <string> using namespace std; ... string name = "Harry";

  23. ตัวอย่าง string fname = "Harry"; string lname = "Morgan"; string name = fname + lname; cout << name << endl; name = fname + " " + lname; cout << name << endl; ผลลัพธ์ที่ได้คือ HarryMorgan Harry Morgan

  24. การหาความยาวของ string int n; n = greeting.length(); cout << “n= ” << n << endl; // n = 13 string greeting = "Hello, World!"; H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12

  25. การหา substring string greeting = "Hello, World!"; string sub = greeting.substr(0, 5); // ผลลัพธ์ที่เก็บในตัวแปร sub คือข้อความ"Hello" H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12

  26. การหา substring string greeting = "Hello, World!"; string sub = greeting.substr(7, 5); // ผลลัพธ์ที่เก็บในตัวแปร sub คือข้อความ"World" H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12

  27. การหา substring string greeting = "Hello, World!"; string sub = greeting.substr(7); // ผลลัพธ์ที่เก็บในตัวแปร sub คือข้อความ"World!" H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12

  28. การพิมพ์ตัวอักษรหนึ่งตัวใน string string greeting = "Hello, World!"; cout<< greeting[7] << endl; // พิมพ์ตัวอักษร W ออกทางหน้าจอ H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12

  29. ตัวอย่าง

  30. ตัวอย่าง

More Related