شرح C++

الدوال الرياضية

مكتبة cmath

أغلب الدوال الرياضية في <cmath>:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << sqrt(144) << endl;   // 12 (الجذر التربيعي)
    cout << pow(2, 10) << endl;  // 1024 (الأُس)
    cout << abs(-7) << endl;     // 7 (القيمة المطلقة)
    return 0;
}

التقريب

cout << round(4.6);   // 5 (لأقرب صحيح)
cout << ceil(4.1);    // 5 (لأعلى)
cout << floor(4.9);   // 4 (لأسفل)

دوال شائعة

الدالةالوظيفة
sqrt(x)الجذر التربيعي
pow(x, y)x أُس y
abs(x)القيمة المطلقة
round/ceil/floorالتقريب
max(a, b)الأكبر
min(a, b)الأصغر
cout << max(10, 20);   // 20
cout << min(10, 20);   // 10

الأرقام العشوائية

تحتاج <cstdlib> و<ctime>:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));            // بذرة عشوائية (مرّة واحدة)
    int dice = rand() % 6 + 1; // رقم بين 1 و6
    cout << dice;
    return 0;
}
  • rand() % 6 يعطي 0–5، و+ 1 يجعله 1–6.
  • srand(time(0)) يضمن أرقامًا مختلفة كل تشغيل.

أخطاء شائعة

  • نسيان #include <cmath> قبل sqrt/pow.
  • نسيان srand(time(0)) فتتكرّر نفس الأرقام "العشوائية" كل تشغيل.

🎯 التالي: جملة switch.