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

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

Variable Data type Arithmetic operator

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


งานนำเสนอเรื่อง: "Variable Data type Arithmetic operator"— ใบสำเนางานนำเสนอ:

1 Variable Data type Arithmetic operator

2 ตัวแปร (Variable) ใช้………………………………….. ระหว่างที่โปรแกรมทำงาน ซึ่งค่านี้จะถูกเก็บไว้ ในหน่วยความจำ โดยจะมีชื่อตัวแปรไว้อ้างอิงตำแหน่งในหน่วยความจำ ว่าเก็บไว้ ตำแหน่งใด ตัวแปรสามารถเปลี่ยนแปลงค่าที่เก็บได้ ก่อนจะสร้างตัวแปรจะต้อง รู้ชนิดของข้อมูล ว่าข้อมูลที่ต้องการเก็บนั้นมีประเภทใด : ข้อความ, ตัวเลขจำนวนเต็ม, ทศนิยม, ค่าความจริง ... รู้ขนาดของข้อมูลที่ต้องการเก็บ : ตัวเลขที่ใช้มีกี่หลัก, ข้อความมี ประมาณกี่อักขระ

3 การตั้งชื่อตัวแปร ต้องขึ้นต้นด้วย a – z หรือ A-Z หรือ _ (underscore)
ตำแหน่งถัดไปสามารถเป็นตัวอักษร หรือ ตัวเลข หรือ _ ได้ ไม่สามารถขึ้นต้นด้วยตัวเลข ไม่ให้มีการเว้นวรรค ควรตั้งชื่อให้สื่อความหมายได้ดี ไม่ใช้ชื่อตัวแปรที่ซ้ำกับคำสงวน หรือคีย์เวิร์ด

4 __abstract 2 abstract __alignof Operator array __asm __assume __based bool __box 2 break case catch __cdecl char class const const_cast continue __declspec default __delegate 2 delegate delete deprecated 1 dllexport 1 dllimport 1 do double dynamic_cast else enum enum class enum struct event __event __except explicit extern false __fastcall __finally finally float for for each, in __forceinline friend friend_as __gc 2 gcnew generic goto __hook 3 __identifier if __if_exists __if_not_exists initonly __inline inline int __int8 __int16 __int32 __int64 __interface interface class interface struct interior_ptr __leave literal long __m64 __m128 __m128d __m128i

5 __multiple_inheritance
mutable naked 1 namespace new __nogc 2 noinline 1 __noop noreturn 1 nothrow 1 novtable 1 nullptr operator __pin 2 private __property 2 property property 1 protected public __raise ref struct ref class register reinterpret_cast return safecast __sealed 2 sealed selectany 1 short signed __single_inheritance sizeof static static_cast __stdcall struct __super switch template this thread 1 throw true try __try/__except, __try/__finally __try_cast 2 typedef typeid typename __unaligned __unhook 3 union unsigned using declaration, using directive uuid 1 __uuidof value struct value class __value 2 virtual __virtual_inheritance void volatile __w64 __wchar_t, wchar_t  while

6 การประกาศตัวแปร เป็นการ……………………………………….สำหรับตัวแปรนั้นๆ มีรูปแบบ คือ
datatype var_name; datatype var_name1, var_name2; เช่น int radius; float width, height; และสามารถกำหนดค่าเริ่มต้นให้กับตัวแปรได้ radius = 5; height = 3.14;

7 Case sensitive language
คือ ตัวอักษรพิมพ์ใหญ่กับตัวพิมพ์เล็ก จะมีความหมายแตกต่างกัน int 1num; int num1: char hello; char hEllo; int check_digit; int for; double void;

8 Data type ชนิดจำนวนเต็ม เช่น boolean, char, int, long, short
ชนิดข้อมูลแบบพื้นฐาน (Build-in type) เป็นชนิดข้อมูลที่ถูกกำหนดไว้แล้ว สามารถนำมาใช้ได้ทันที ชนิดจำนวนเต็ม เช่น boolean, char, int, long, short ชนิดจำนวนจริง เช่น float, double, long double ชนิดที่เก็บข้อมูลเป็นเซตจำนวนเต็ม เช่น enum ชนิดสร้างใหม่ตามมาตราฐาน c++ เช่น คลาส string ชนิดอื่นๆ เช่น pointer, array, class, reference

9 boolean bool x = true; bool y = false; bool z1 = 10; bool z2 = -10;
เก็บเฉพาะค่า…………………………………….. เท่านั้น 0 แทนค่าเท็จ นอกนั้นจำนวนเต็มใดๆ ยกเว้น 0 จะแทนค่าจริงทั้งหมด bool x = true; bool y = false; bool z1 = 10; bool z2 = -10; มักใช้กับนิพจน์เงื่อนไขตัดสิน เช่น if-else

10 char char c1 = ‘a’; char c2 = ‘A’; char c3 = “B”; char c4 = ‘5’;
เก็บ…………………………………………………ขนาด 8 บิต ซึ่งสามารถเก็บได้ 256 ค่า char c1 = ‘a’; char c2 = ‘A’; char c3 = “B”; char c4 = ‘5’; จะใช้เลขรหัส ASCII (แอสกี) ในการเก็บข้อมูลรหัสอักขระ ถ้าต้องการเก็บเป็นสตริง (ข้อความ) ก็สามารถทำได้โดย char str1[ ] = “welcome to c++”; char str2[ ] = “A”; char str3[5]; str3 = “abcde”

11 integer short s; s = 10000; int m = 50000; long y = 555555L;
ใช้………………………………………….สามารถเก็บได้ 3 แบบ short int int long int short s; s = 10000; int m = 50000; long y = L; int oct_value = 0123; int hex_value = 0X456;

12 floating point float f1 = 9.8; float f2 = -133.5095F;
ชนิดข้อมูลจำนวนจริง หรือจำนวนที่มีจุดทศนิยม float f1 = 9.8; float f2 = F; double d1 = 10.5e-2; double d2 = d; long double pi = L;

13 ฟังก์ชัน cout cout << “test for cout” int x = 100;
ใช้พิมพ์ข้อมูลออกทาง standard output ในการใช้งานต้องมีการเรียกใช้ library iostream ไว้ในโปรแกรมก่อนทุกครั้ง cout << “test for cout” int x = 100; cout << “x value is ” << x; cout << ‘\n’ cout << “\n”

14 ฟังก์ชัน cin int x,y; cout << “please enter x :”;
ใช้อ่านข้อมูลจาก keyboard ใช้ร่วมกับตัวดำเนินการ >> int x,y; cout << “please enter x :”; cin >> x; //รับข้อมูลจากผู้ใช้เก็บไว้ในตัวแปร x cout << “please enter x and y separated by spacebar”; cin >> x >> y; cout << x << " and " << y ;

15 sizeof ตัวดำเนินการนี้จะคืนขนาดชนิดข้อมูลที่ใช้ว่าใช้พื้นที่หน่วยความจำเก็บข้อมูลขนาดกี่ ไบต์ มีรูปแบบการใช้ดังนี้ sizeof(data_type) ใช้กับชนิดข้อมูลพื้นฐานต่างๆ เช่น int, char, bool sizeof(object) ใช้กับชนิดข้อมูลที่สร้างขึ้นใหม่ เช่น คลาส

16 แบบฝึกหัดการใช้คำสั่ง sizeof
#include <iostream> using namespace std; int main() { cout << "char = " << sizeof(char) << "bytes\n"; cout << "bool = " << sizeof(bool) << "bytes\n"; cout << "short int = " << sizeof(short) << "bytes\n"; cout << "int = " << sizeof(int) << "bytes\n"; cout << "long int = " << sizeof(long) << "bytes\n"; cout << "float = " << sizeof(float) << "bytes\n"; cout << "double = " << sizeof(double) << "bytes\n"; cout << "long double = " << sizeof(long double) << "bytes\n"; cin.get(); return 0; }

17 ตัวดำเนินการและนิพจน์
ตัวดำเนินการ (operator) คือ สัญลักษณ์ที่ใช้ในการกระทำกับข้อมูล เช่น +, -, *, /, %, sizeof นิพจน์ (expression) คือ การนำข้อมูลมากระทำต่อกัน ตั้งแต่หนึ่งจำนวนขึ้นไป เช่น int a, b=1,c=5; a = b+c; c = (23 * ) / b + a;

18 ตัวดำเนินการทางคณิตศาสตร์ (Arithmetic Operator)
ตัวอย่างตัวดำเนินการทางคณิตศาสตร์ เช่น +, -, *, /, % % ตัวดำเนินการ …………………….. คือ …………………………………………. เช่น 5% 3 = 2 5 % 3.0 = ? 100 / 3 = ? 100.0 / 3.0 = ? 100 / 3.0 = ? เมื่อใช้ตัวดำเนินการทางคณิตศาสตร์ ระหว่างข้อมูลต่างชนิดกัน ผลลัพธ์จะเป็นชนิด ข้อมูลที่มีขนาดใหญ่สุด เช่น int กับ long ผลลัพธ์จะเป็น long

19 ตัวดำเนินการทางคณิตศาสตร์
กรณีที่มีหลายๆ นิพจน์ เช่น a + b * c / (d – 10) โปรแกรมจะคำนวนตามตัวดำเนินการที่มีลำดับความสำคัญสูงสุดก่อน ถ้ามีลำดับความสำคัญเท่ากัน จะทำจากซ้ายไปขวา เช่น a + b * c / d – 10 เมื่อ a = 1, b= 4, c = 3, d = 2 ผลลัพธ์จะเท่ากับ ถ้า (a + b) * (c / d) – 10 เมื่อ a = 1, b= 4, c = 3, d = 2 ผลลัพธ์จะเท่ากับ

20 Escape sequence อักขระพิเศษที่ถูกกันไว้ เพื่อตีความหมายเป็นอย่างอื่น จะใช้สัญลักษณ์ ‘\’ เป็น ตัวกัน Escape Sequence Represents \n New line \b Backspace \t Horizontal tab \’ Single quotation mark \a Bell (alert) \\ Backslash \” Double quotation mark \? Literal question mark

21 แบบฝึกหัด ลองแสดงข้อความต่อไปนี้

22 แบบฝึกหัด 1. จงเขียนโปรแกรมหาผลรวมของ 2 ข้อมูล(มีชนิดเป็นเลขทศนิยม) โดยรับค่าจากผู้ใช้ แล้วแสดงผลลัพธ์ออกทางหน้าจอ

23 แบบฝึกหัด 2. จงเขียนโปรแกรมแสดงผลลัพธ์ของสมการ f(x) = x2 + 10/x เมื่อ x เป็นค่าใดๆ (ชนิด จำนวนเต็ม) แต่ไม่ใช่ 0 กำหนดให้รับค่า x จากผู้ใช้งาน

24 แบบฝึกหัด 3. จงเขียนโปรแกรมแสดงการแปลงค่าสกุลเงินจากเงินบาท เป็นเงินดอลลาร์ กำหนดให้ ผู้ใช้กรอกจำนวนเงินหน่วยเป็นบาท (สามารถกรอกเป็นทศนิยมได้) และ 1 ดอลลาร์ เท่ากับ บาท


ดาวน์โหลด ppt Variable Data type Arithmetic operator

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


Ads by Google