Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. C++ Program: Implement Caesar Cipher (Shift Cipher)

    Caesar Cipher (Shift Cipher) เป็น Monoalphabetic Substitution Cipher ที่ง่ายที่สุด เลื่อนตัวอักษรแต่ละตัวไปข้างหน้าจำนวนคงที่ (Key) ในช่วง 0-25 Encryption & Decryption Encryption: C = (P + k) mod 26 Decryption: P = (C - k + 26) mod 26 Key = 3 เรียกว่า Caesar Cipher อย่างเป็นทางการ (Julius Caesar

  2. C++ Program: Encode Playfair Cipher (Playfair Cipher Encryption)

    บทความนี้ครอบคลุม Playfair Cipher Encryption ซึ่งเป็นคู่ของบทความก่อนหน้า (Decryption) ใช้ Key Table 5×5 และเข้ารหัสแบบ Digraph (คู่ตัวอักษร) Key Table Construction Keyword: TUTORIALS → ลบซ้ำ → TUTORIALS → TUORIALS (เก็บตัวแรก) เติม A-Z (ไม่มี J) ที่เหลือ: B, C, D, E, F, G, H, K, M, N, P, Q, V, W,

  3. C++ Program: Decode Playfair Cipher (Playfair Cipher Decryption)

    Playfair Cipher เป็น Digraph Substitution Cipher เข้ารหัสคู่ตัวอักษร (Pairs) ใช้ Key Table 5×5 (อักขระ 25 ตัว ปกติละ J รวมกับ I) Key Table Generation นำ Keyword มาเขียนซ้าย-ขวา บน-ล่าง ลบตัวซ้ำ เติมอักขระที่เหลือ A-Z (ยกเว้น J) ตามลำดับ Example: Keyword TUTORIALS T U O R I A L S B C D E F G H K

  4. C++ Program: Implement RSA Algorithm (Asymmetric Cryptography)

    RSA เป็น Asymmetric Cryptography Algorithm ใช้ Key Pair: Public Key (e, n) สำหรับ Encrypt และ Private Key (d, n) สำหรับ Decrypt ความปลอดภัยอาศัยความยากในการแยกตัวประกอบจำนวนเต็มขนาดใหญ่ Algorithm Steps เลือก Prime จำนวนใหญ่ 2 ตัว: p, q คำนวณ n = p × q (Modulus) คำนวณ Eulers Totient: φ(n) = (p-1)(q

  5. C++ Program: Implement Hill Cipher (Matrix-based Polygraphic Cipher)

    Hill Cipher เป็น Polygraphic Substitution Cipher ใช้ Linear Algebra เข้ารหัส Block ของตัวอักษรพร้อมกัน (Block Size = Matrix Dimension) โดยใช้ Matrix Multiplication Modulo 26 Mathematical Foundation Key: n×n Invertible Matrix Modulo 26 (Determinant ต้อง Coprime กับ 26) Plaintext: แบ่งเป็น Block ขนา

  6. C++ Program: Implement Vigenère Cipher (Polyalphabetic Substitution)

    Vigenère Cipher เป็น Polyalphabetic Substitution Cipher ใช้ Keyword ซ้ำๆ เพื่อกำหนด Shift ต่างกันสำหรับแต่ละตัวอักษร ทำให้ Frequency Analysis ยากกว่า Caesar/Affine Encryption Formula Eᵢ = (Pᵢ + Kᵢ) mod 26 Pᵢ = Plaintext char (0-25), Kᵢ = Key char (0-25), Eᵢ = Ciphertext char Decryption Formula Pᵢ

  7. C++ Program: Implement Affine Cipher (Affine Cipher)

    Affine Cipher เป็น Monoalphabetic Substitution Cipher ที่แปลงตัวอักษรแต่ละตัวด้วยฟังก์ชันคณิตศาสตร์แบบ Linear Modular Arithmetic Mathematical Foundation Alphabet Size m = 26 (A-Z) Key: (a, b) โดย a ต้อง Coprime กับ m (gcd(a, 26) = 1) เพื่อให้มี Inverse Encryption E(x) = (a * x + b) mod 26 Decrypt

  8. Kadane's Algorithm ใน C++: หา Maximum Subarray Sum

    Kadanes Algorithm หา Maximum Sum ของ Contiguous Subarray ใน O(N) Time, O(1) Space วิธีคิดคือ Dynamic Programming แบบเก็บเฉพาะ State ที่จำเป็น Algorithm Logic ที่ Index i เรามี 2 ทางเลือก: เริ่ม Subarray ใหม่ที่ i: arr[i] ต่อยอด Subarray ก่อนหน้า: currentMax + arr[i] เลือก Max ของทั้งสอง → current

  9. C++ Program: Longest Common Subsequence (LCS) ของหลาย Sequences

    Longest Common Subsequence (LCS) คือ Subsequence ที่ยาวที่สุดที่ปรากฏในทุก Sequence ของชุดที่กำหนด (Subsequence = เก็บลำดับแต่ไม่ต้องติดกัน) ข้อสังเกตสำคัญ บทความต้นฉบับมี Title ว่า Longest Subsequence Common to All Sequences แต่โค้ดตัวอย่างทำ Longest Common Prefix ไม่ใช่ LCS จริง (Prefix ต้องเริ่ม

  10. C++ Program: หา Shortest Common Supersequence (SCS) ของ 2 Strings

    Shortest Common Supersequence (SCS) คือ String สั้นที่สุดที่มีทั้ง String A และ String B เป็น Subsequence (ไม่ต้องติดกัน แต่ต้องเรียงลำดับเดิม) ความสัมพันธ์กับ LCS |SCS| = |A| + |B| - |LCS(A, B)| หา LCS ก่อน แล้ว Merge โดยใส่ Char ที่ไม่ใช่ LCS เข้าไป Algorithm: DP + Reconstruction สร้าง DP Table

  11. C++ Program: หา Longest Common Prefix ของ Array ของ Strings

    โจทย์: หา Longest Common Prefix (LCP) ของชุดสตริงหลายตัว คือ Prefix ที่ยาวที่สุดที่ทุกสตริงมีร่วมกัน Algorithm: Horizontal Scanning ตั้ง Prefix เริ่มต้น = String ตัวแรก Iterate String ตัวถัดไป: หา Common Prefix ระหว่าง Prefix ปัจจุบัน กับ String นี้ อัปเดต Prefix ให้สั้นลงตามที่ Match ถ้า Prefix ว

  12. C++ Program: หาจำนวนวิธี Partition คำ ให้แต่ละส่วนเป็น Palindrome

    โจทย์: หา ทั้งหมด วิธีการแบ่ง String ให้ทุก Substring เป็น Palindrome (อ่านซ้าย-ขวาและขวา-ซ้ายเหมือนกัน) ใช้ Backtracking (DFS) สำรวจทุกความเป็นไปได้ Algorithm: Backtracking เริ่มจาก Index 0 ลองตัด Substring ทุกความยาวเริ่มจาก Index ปัจจุบัน ถ้า Substring นั้นเป็น Palindrome → เพิ่มลง Path และ Rec

  13. C++ Program to Implement Trie (Prefix Tree) Data Structure

    Trie (Prefix Tree) เป็นโครงสร้างข้อมูลแบบ Tree ใช้สำหรับจัดเก็บและค้นหาสตริงอย่างมีประสิทธิภาพ เหมาะสำหรับ Autocomplete, Spell Checker, IP Routing, Dictionary โครงสร้าง Node Array of Children Pointers (ขนาด = Alphabet Size, เช่น 26 สำหรับ A-Z) Flag endOfString บอกว่า Node นี้จบคำหรือไม่ Operatio

  14. Comparator Function ของ qsort() ใน C: เรียงลำดับข้อมูลโครงสร้าง

    qsort() ใน <stdlib.h> ใช้ Quick Sort ต้องการ Comparator Function เพื่อกำหนดลำดับการเรียง ฟังก์ชันนี้รับ Pointer 2 ตัว (const void*) คืนค่า int บอกลำดับ Signature ของ Comparator int comparator(const void* p1, const void* p2); ค่าคืนและความหมาย Return Valueความหมาย < 0Element p1 อยู่ก่อน p

  15. ประเภทของ Errors ใน C/C++: Syntax, Runtime, Linker, Logical, Semantic

    ข้อผิดพลาดใน C/C++ แบ่งได้ 5 ประเภทหลัก ตามขั้นตอนของ Compilation และ Execution 1. Syntax Error (Compile-Time Error) 違反 กฎ Syntax ของภาษา Compiler จับได้ก่อน Compile เสร็จ ตัวอย่าง: ขาด Semicolon #include <stdio.h> int main() { printf(Hello World) // ขาด ; } ผลลัพธ์ Error: expected ; befo

  16. ฟังก์ชัน getopt() ใน C: Parse Command Line Arguments

    getopt() เป็นฟังก์ชันมาตรฐาน POSIX สำหรับแยกวิเคราะห์ Command Line Options แบบ Unix/Linux รองรับ Short Options (Single Character) เช่น -f file.txt -v -o output Syntax int getopt(int argc, char *const argv[], const char *optstring); Optstring Format abc → Options -a, -b, -c (ไม่มี Argument) ab:c:

  17. ฟังก์ชัน mbrtowc() ใน C/C++: แปลง Multibyte เป็น Wide Character

    mbrtowc() (Multi-Byte to Wide Character) แปลงลำดับ Multibyte Character (เช่น UTF-8) เป็น Wide Character (wchar_t) คืนค่าจำนวน Byte ที่ใช้สำหรับตัวอักษรตัวนั้น Syntax size_t mbrtowc(wchar_t* wc, const char* s, size_t max, mbstate_t* ps); พารามิเตอร์ พารามิเตอร์คำอธิบาย wcPointer เก็บ Wide Characte

  18. หา Segmentation Fault ใน C/C++ ด้วย GDB Debugger

    Segmentation Fault (Segfault) เป็น Runtime Error ที่เกิดจาก Memory Access Violation เช่น เข้าถึง Array Index ผิด, Dereference Null Pointer, Stack Overflow, หรือ เขียนทับ Memory ที่ Protected ตัวอย่างโค้ดที่ทำให้เกิด Segfault #include <stdio.h> int main() { int* ptr = NULL; *ptr = 1;

  19. วัดเวลาการ Execute ด้วยความแม่นยำสูงใน C/C++ (chrono)

    Library <chrono> ใน C++11 提供 High-Resolution Clock ที่นับได้ระดับ Nanoseconds เหมาะสำหรับ Benchmarking และ Performance Measurement หลักการทำงาน บันทึกเวลาเริ่มต้น: auto start = Clock::now(); รันโค้ดที่ต้องการวัด บันทึกเวลาสิ้นสุด: auto end = Clock::now(); คำนวณความต่าง: duration_cast<nano

  20. การสุ่มตัวเลขในช่วงที่กำหนด (Range) ในภาษา C

    ฟังก์ชัน rand() ใน C สุ่มตัวเลขได้แค่ช่วง 0 ถึง RAND_MAX เพื่อสุ่มในช่วงที่ต้องการ [lower, upper] ต้องใช้สูตรคณิตศาสตร์ปรับช่วง สูตรการสุ่มในช่วง [L, R] random_number = (rand() % (upper - lower + 1)) + lower; อธิบายสูตร rand() % (upper - lower + 1) → ได้ค่า 0 ถึง (upper - lower) + lower → เลื่อนช

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:59/301  20-คอมพิวเตอร์/Page Goto:1 53 54 55 56 57 58 59 60 61 62 63 64 65