Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. พิมพ์ 1 ถึง 100 ใน C++ โดยไม่ใช้ Loop และ Recursion

    พิมพ์ตัวเลข 1 ถึง 100 โดยไม่ใช้ Loop และ Recursion มีหลายวิธีพิมพ์ตัวเลขโดยไม่ใช้ loop เช่น recursive function, goto statement, สร้างฟังก์ชันนอก main() ตัวอย่างใช้ Goto Statement #include <bits/stdc++.h> using namespace std; int main() { int count = 1; int x; cout x; PRINT:

  2. คีย์เวิร์ด Static ใน C++ (บทความซ้ำ)

    คีย์เวิร์ด Static ใน C++ เมื่อใช้ static ตัวแปร/สมาชิก/ฟังก์ชันจะไม่สามารถแก้ไขได้อีก จองหน่วยความจำตลอดอายุโปรแกรม Static function เรียกได้โดยตรงด้วยชื่อคลาส Static variable เริ่มต้นเพียงครั้งเดียว Compiler เก็บตัวแปรจนจบโปรแกรม ประกาศได้ทั้งในและนอกฟังก์ชัน เป็น local ต่อบล็อก ค่าเริ่มต้นเป็นศูน

  3. โปรแกรม C++ หาเลขฟีโบนัชชีด้วย Recursion

    หาลำดับฟีโบนัชชีด้วย Recursion ตัวอย่างหาลำดับฟีโบนัชชีโดยใช้ recursion ตัวอย่างโค้ด #include <iostream> using namespace std; int fib(int x) { if((x==1)||(x==0)) return x; else return fib(x-1) + fib(x-2); } int main() { int x, i = 0; cout x; cout << \nFibonnaci Series:

  4. โปรแกรม C++ หา Factorial ของจำนวนใหญ่

    หา Factorial ของจำนวนใหญ่ ตัวอย่างหา factorial ใช้ชนิดข้อมูลสำหรับจำนวนใหญ่ ตัวอย่างโค้ด #include <iostream> using namespace std; int fact(unsigned long long int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int main() { unsigned long long int n; cout

  5. โปรแกรม C++ คำนวณ Combination ด้วย Factorial

    คำนวณ Combination ด้วย Factorial ตัวอย่างคำนวณ combination (nCr) โดยใช้ factorial ตัวอย่างโค้ด #include <iostream> using namespace std; int fact(int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int main() { int n, r, result; cout n; cout r;

  6. โปรแกรม C++ ตรวจสอบว่าจำนวนสามารถเขียนเป็นผลรวมของจำนวนเฉพาะสองจำนวนได้หรือไม่

    ตรวจสอบผลรวมของจำนวนเฉพาะสองจำนวน ตัวอย่างตรวจสอบว่าจำนวนใดจำนวนหนึ่งสามารถเขียนเป็นผลรวมของจำนวนเฉพาะสองจำนวนได้หรือไม่ ตัวอย่างโค้ด #include <iostream> using namespace std; int func(int num) { int i, flag = 1; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { fl

  7. โปรแกรม C++ หาเลขฟีโบนัชชีด้วยการวนรอบ (Iteration)

    หาลำดับฟีโบนัชชีด้วย Iteration ตัวอย่างหาลำดับฟีโบนัชชีโดยใช้การวนรอบ (iteration) ตัวอย่างโค้ด #include <iostream> using namespace std; void fib(int num) { int x = 0, y = 1, z = 0; for (int i = 0; i < num; i++) { cout << x << ; z = x + y; x = y; y =

  8. คีย์เวิร์ด static และการใช้งานต่างๆ ใน C++

    คีย์เวิร์ด static ใน C++ เมื่อใช้ static ตัวแปร/สมาชิก/ฟังก์ชันจะไม่สามารถแก้ไขได้อีก จองหน่วยความจำตลอดอายุโปรแกรม Static function เรียกได้โดยตรงด้วยชื่อคลาส Static variable เริ่มต้นเพียงครั้งเดียว Compiler เก็บตัวแปรจนจบโปรแกรม ประกาศได้ทั้งในและนอกฟังก์ชัน เป็น local ต่อบล็อก ค่าเริ่มต้นเป็นศูน

  9. เขียนฟังก์ชัน Power (pow) ใน C++

    ฟังก์ชัน Power (ยกกำลัง) ใน C++ ฟังก์ชัน power ใช้หาค่ายกกำลังจากฐาน (base) และเลขชี้กำลัง (exponent) ผลลัพธ์คือฐานยกกำลังเลขชี้กำลัง 2^5 = 32 ตัวอย่างโปรแกรม #include <iostream> using namespace std; int main() { int x, y, ans = 1; cout x; cout y; for(int i = 0; i < y; i

  10. การประกาศ main ที่ถูกต้องใน C++

    Prototype มาตรฐานของ main() ใน C++ ฟังก์ชัน main() เป็น global function ใช้เริ่มการทำงานของโปรแกรม ทุกโปรแกรมต้องมี main() อาร์กิวเมนต์ command line argc และ argv เป็น optional Prototype มาตรฐาน int main() { body } หรือ int main(int argc, char *argv[]) { body } argc: จำนวนอาร์กิวเมนต์ที่ส่งเข้าม

  11. วิธีใช้ Enum ใน C++

    การใช้ Enumeration (Enum) ใน C++ Enumeration เป็น user-defined datatype ใช้กำหนดชื่อให้ค่าคงที่จำนวนเต็ม ทำให้โปรแกรมอ่านง่ายและบำรุงรักษาง่าย ใช้คีย์เวิร์ด enum ไวยากรณ์ enum enum_name { const1, const2, ... }; enum_name: ชื่อที่ผู้ใช้ตั้ง const1, const2: ค่าของ flag มี 2 วิธีประกาศตัวแปร enum:

  12. การ Define Static Members ใน C++

    ภาพรวม Static Members ใน C++ ประกอบด้วย Static Data Members และ Static Member Functions มีคุณสมบัติ: เป็นของ Class** ไม่ใช่ของ Object แต่ละตัว มีเพียง 1 Instance ในหน่วยความจำ (Shared) Static Data: ต้อง Define นอกคลาส (Definition) ใน Global Scope Static Functions: เรียกได้โดยไม่ต้องมี Objec

  13. วิธีวัด Execution Time ของโค้ดใน C++ (High Resolution Clock)

    ภาพรวม ใน C++11+ สามารถวัดเวลาทำงานของโค้ดได้แม่นยำด้วย std::chrono::high_resolution_clock ซึ่งให้ความละเอียดสูงสุดที่ระบบรองรับ (มักเป็น Nanoseconds) Syntax พื้นฐาน #include <chrono> using namespace std::chrono; auto start = high_resolution_clock::now(); // Code to measure auto stop = high

  14. วิธีจับ Divide by Zero Error ใน C++ (Exception Handling)

    ภาพรวม ใน C++ การหารด้วยศูนย์ (Division by Zero) สำหรับ Integer Types เป็น Undefined Behavior (UB) โปรแกรมอาจ Crash, ให้ผลผิด, หรือทำงานต่อได้ (ขึ้นอยู่กับ Hardware/OS) สำหรับ Floating-Point จะได้ inf หรือ nan ตาม IEEE 754 วิธีป้องกัน: ตรวจสอบก่อนหาร (Pre-check) หรือใช้ Exception Handling (try/catch

  15. การ Initialize ตัวแปรใน C++ (Variable Initialization)

    ภาพรวม การ Initialize ตัวแปรใน C++ หมายถึงการกำหนดค่าเริ่มต้นตอนสร้างตัวแปร มี 2 แบบหลัก: Static Initialization: ค่าคงที่ Compile-time (Global, Static, Constexpr) Dynamic Initialization: ค่าที่คำนวณได้ Runtime (Local Variables, Constructor Calls) Syntax พื้นฐาน Type variable_name = value;

  16. ทำไม C++ ต้อง Cast malloc() แต่ C ไม่ต้อง?

    ภาพรวม ความแตกต่างมาจาก **Type System** ของภาษา: C: void* Implicitly Convertible to Any Pointer Type C++: void* NOT** Implicitly Convertible to Other Pointer Types (Strong Typing) ใน C (Implicit Conversion) // C Code - Valid #include int main() { int* ptr = malloc(10 * sizeof(int)); /

  17. วิธี Return Array จากฟังก์ชันใน C++

    ภาพรวม C++ ไม่สามารถ Return Array โดยตรงได้** (Array ไม่มี Copy Assignment) แต่สามารถ Return Pointer to Array หรือใช้ Modern C++ Containers ได้ ปัญหาของ Raw Array Return // ❌ ผิด: Return Local Array Pointer (Dangling Pointer) int* badFunc() { int arr[5] = {1,2,3,4,5}; return arr; // arr ถ

  18. วิธีแปลง Char เดียวเป็น Int ใน C++

    ภาพรวม ใน C++ Character (char) เก็บเป็นรหัส ASCII/Unicode (Integer) การแปลง Char ตัวเลข (0-9) เป็นค่า Int จริง ทำได้โดยการลบค่า ASCII ของ 0 (ซึ่งเท่ากับ 48) วิธีการแปลง วิธีที่ 1: ลบ 0 (แนะนำ - Readable, Portable) char c = 8; int value = c - 0; // 8 (56) - 0 (48) = 8 cout << value; // 8 วิธีท

  19. ความแตกต่างระหว่าง int และ long ใน C++

    ภาพรวม int และ long เป็น Signed Integer Types ใน C++ ความแตกต่างหลักคือ **ขนาด (Size)** และ **ช่วงค่า (Range)** ซึ่งขึ้นอยู่กับ Platform/Compiler (Implementation-Defined) ข้อกำหนดขั้นต่ำของ C++ Standard TypeMinimum SizeMinimum RangeTypical (64-bit Linux/macOS)Typical (Windows 64-bit) int16 bit

  20. Reference Variable ใน C++ คืออะไร?

    ภาพรวม Reference Variable (ตัวแปรอ้างอิง) คือชื่อเรียกอื่น (Alias) ของตัวแปรที่มีอยู่แล้ว ไม่ใช่ตัวแปรใหม่ ไม่มีหน่วยความจำแยกต่างหาก การดำเนินการกับ Reference คือการดำเนินการกับตัวแปรต้นฉบับโดยตรง คุณสมบัติหลัก ต้อง Initialize ตอนประกาศ (ไม่สามารถประกาศแยกได้) ไม่สามารถเปลี่ยนอ้างอิงไปตัวแปรอ

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:25/301  20-คอมพิวเตอร์/Page Goto:1 19 20 21 22 23 24 25 26 27 28 29 30 31