Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. โปรแกรม C++ คำนวณความต่างของเวลา

    รับช่วงเวลา 2 ช่วง (ชั่วโมง นาที วินาที) คำนวณหาความต่าง ตัวอย่าง: 8:06:02 และ 3:09:03 → Diff = 4:56:59 โค้ดโปรแกรม #include <iostream> using namespace std; int main() { int h1, m1, s1, h2, m2, s2; int diff_h, diff_m, diff_s; cout << Enter time period 1 (h m s): ;

  2. ฟังก์ชัน iscntrl() ใน C++

    iscntrl() ตรวจสอบว่าตัวอักษรเป็น Control Character หรือไม่ (Non-printable, ไม่ใช่ Space) เช่น \n, \t, \r, Bell ประกาศใน <ctype.h> รูปแบบ int iscntrl(int ch); ตัวอย่าง 1: นับ Control Characters ใน String #include <iostream> #include <ctype.h> using namespace std; int main() {

  3. Pure Function ใน C++

    Pure Function ส่งคืนผลลัพธ์เดียวกันสำหรับ Input เดียวกันเสมอ ไม่มี Side Effects (ไม่แก้ไข Argument, ไม่ I/O, ไม่แก้ Global State) Pure Functions ตัวอย่าง sin(), sqrt(), strlen(), max(), pow(), floor() Impure Functions ตัวอย่าง rand() (ส่งคืนค่าต่างกันทุกครั้ง) time() (ขึ้นกับเวลาจริง) ฟังก์ชันท

  4. โปรแกรม C++ ใช้งาน Circular Queue

    Circular Queue เชื่อมตำแหน่งสุดท้ายกับแรกเป็นวงกลม ใช้ Array ขนาดคงที่ ตัวอย่างโค้ดสมบูรณ์ #include <iostream> using namespace std; const int MAX = 5; int cqueue[MAX]; int front = -1, rear = -1; void insertCQ(int val) { if ((front == 0 && rear == MAX-1) || (front == rear + 1)) {

  5. โปรแกรม C++ ใช้งาน Priority Queue

    Priority Queue ไม่ปฏิบัติตาม FIFO แต่มี Priority (ความสำคัญ) สำหรับแต่ละ Element Item Priority สูงกว่า ถูกประมวลผลก่อน Item Priority เท่ากัน ใช้ FIFO โครงสร้างคลาส Priority_Queue insert(int item, int priority) − ใส่ตาม Priority (น้อย = สำคัญกว่า) del() − ลบ Front (Priority สูงสุด) show() − แสดง

  6. ฟังก์ชัน getchar_unlocked() ใน C

    getchar_unlocked() เป็นเวอร์ชัน Non-thread-safe ของ getchar() เร็วกว่าแต่ไม่ปลอดภัยใน Multi-threaded Environment Deprecated ใน Windows รูปแบบ int getchar_unlocked(void); ตัวอย่างโค้ด #include <stdio.h> int main() { char val; val = getchar_unlocked(); printf(Enter the character :

  7. ฟังก์ชัน isalpha() และ isdigit() ใน C/C++

    isalpha() ตรวจสอบว่าตัวอักษรเป็น Alphabetic (A-Z, a-z) ประกาศใน <ctype.h> ส่งคืน Non-zero หากเป็นตัวอักษร มิฉะนั้น 0 รูปแบบ int isalpha(int ch); ตัวอย่าง #include <stdio.h> #include <ctype.h> int main() { char val1 = s; char val2 = 8; if (isalpha(val1)) print

  8. ฟังก์ชัน isless() ใน C/C++

    isless() ตรวจสอบว่าอากิวเมนต์แรก น้อยกว่า อากิวเมนต์ที่สอง อย่างเคร่งครัด (Strict Less Than) ประกาศใน <math.h>/<cmath> ส่งคืน bool รูปแบบ bool isless(value1, value2); เทียบเท่า: value1 < value2 แต่ปลอดภัยกับ NaN (ไม่โยน Exception) ตัวอย่างโค้ด #include <stdio.h> #include <

  9. ฟังก์ชัน islessgreater() ใน C/C++

    islessgreater() ตรวจสอบว่าอากิวเมนต์แรก น้อยกว่า หรือ มากกว่า อากิวเมนต์ที่สอง (ไม่รวมเท่ากัน) ประกาศใน <math.h> (C) / <cmath> (C++) ส่งคืน bool รูปแบบ bool islessgreater(value1, value2); เทียบเท่า: (value1 > value2) || (value1 < value2) ตัวอย่าง 1: ค่าต่างกัน #include <iostr

  10. Header Files stdio.h และ stdlib.h ใน C

    stdio.h (Standard Input Output) ฟังก์ชัน I/O หลัก Functionคำอธิบาย printf()พิมพ์ข้อมูลจอภาพ scanf()อ่านข้อมูลจากคีย์บอร์ด getc()อ่านตัวอักษรจากไฟล์ putc()เขียนตัวอักษรลงไฟล์ fopen()เปิดไฟล์ fclose()ปิดไฟล์ remove()ลบไฟล์ fflush()ล้าง Buffer ตัวอย่าง #include <stdio.h> int main() { char

  11. Standard Header Files ใน C

    Header Files ใน C มีฟังก์ชันไลบรารีมาตรฐาน ใช้ #include นำเข้า Header Files สำคัญ Headerคำอธิบาย <stdio.h>Input/Output Functions (printf, scanf, fopen, ฯลฯ) <conio.h>Console I/O (getch, clrscr - Non-standard, Windows Only) <stdlib.h>General Utilities (malloc, free, exit, atoi, ra

  12. Static Variables ใน C

    Static Variables เริ่มต้นค่าเพียงครั้งเดียว คงค่าระหว่างการเรียกฟังก์ชัน มีอายุการใช้งานตลอดโปรแกรม ค่าเริ่มต้นเป็น 0 รูปแบบ static datatype variable_name = value; ตัวอย่างโค้ด #include <stdio.h> int main() { auto int a = -28; // Auto (Local) Variable static int b = 8; //

  13. การล้าง Input Buffer ใน C/C++

    fflush(stdin) ใช้ล้าง Output Buffer แต่บาง Compiler รองรับล้าง Input Buffer ด้วย (Non-standard) ส่งคืน 0 หากสำเร็จ มิฉะนั้น EOF รูปแบบ int fflush(FILE *stream); ตัวอย่างโค้ด #include <stdio.h> #include <stdlib.h> int main() { char s[20]; printf(Enter the string : \n); scan

  14. โปรแกรมแปลงตัวพิมพ์เล็ก-ใหญ่ใน C

    แปลงเป็นตัวพิมพ์ใหญ่ (Uppercase) = a && s[i] <= z) s[i] = s[i] - 32; // ASCII: a=97, A=65 diff=32 } printf(\nString in Upper Case = %s, s); return 0; } ผลลัพธ์ Enter a string : hello world! String in Upper Case = HELLO WORLD! แปลงเป็นตัวพิมพ์เล็ก (Lowercase) = A && s[i] <=

  15. Character Arithmetic ใน C

    Character Arithmetic ใช้ดำเนินการคณิตศาสตร์กับตัวอักษร (Char) ใน C โดยอัตโนมัติแปลงเป็นค่า ASCII (Integer) ตัวอย่างโค้ด #include <stdio.h> int main() { char s = m; char t = z - y; printf(%d\n, s); // 109 (ASCII of m) printf(%c\n, s); // m printf(%d\n, s + 1

  16. การกลับสตริง (Reverse String) ใน C/C++

    ตัวอย่างโค้ด (C) #include <stdio.h> #include <string.h> int main() { char s[50], t; int i = 0, j = 0; printf(\nEnter the string to reverse :); fgets(s, sizeof(s), stdin); // ใช้ fgets แทน gets (ปลอดภัยกว่า) s[strcspn(s, \n)] = 0; // ลบ newline j = strlen(

  17. การส่งอาเรย์แบบ Pass by Value ใน C

    ใน C ไม่สามารถส่งอาเรย์แบบ Pass by Value ตรงๆ ได้ (Array Decays to Pointer) แต่ตัวอย่างนี้แสดงการส่งอาเรย์ไปฟังก์ชัน (จริงๆ คือ Pass Pointer) ตัวอย่างโค้ด #include <stdio.h> float avg(float a[]) { int i; float avg, sum = 0.0; for (i = 0; i < 6; ++i) sum += a[i]; av

  18. คำสั่ง #pragma Directive ใน C/C++

    #pragma ให้ข้อมูลเพิ่มเติมแก่ Compiler สำหรับฟีเจอร์พิเศษ รูปแบบ #pragma token_name Directive ที่พบบ่อย Directiveคำอธิบาย #pragma startupรันฟังก์ชันก่อน main() #pragma exitรันฟังก์ชันก่อนจบโปรแกรม #pragma warnควบคุม Warning Messages #pragma GCC dependencyตรวจสอบวันที่ไฟล์ แจ้งเตือนถ้าไฟล์อื่นใหม่

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