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

Multiplicative Operators ใน C++ คืออะไร?

Multiplicative operators คือ -

  • Multiplication (*)
  • Division (/)
  • Modulus หรือ "remainder from division" (%)

Binary operators นี้มี left-to-right associativity ใช้ operands ชนิด arithmetic Modulus operator (%) มีข้อกำหนดเข้มงวดกว่าคือ operands ต้องเป็น integral type

Multiplication operator ให้ผลลัพธ์จากการคูณ operand แรกด้วยตัวที่สอง Division operator ให้ผลลัพธ์จากการหาร operand แรกด้วยตัวที่สอง Modulus operator ให้เศษจาก expression e1 - (e1 / e2) * e2 โดย e1 คือ operand แรก e2 คือ operand ที่สอง ทั้งคู่เป็น integral types

ตัวอย่าง

#include <iostream>
using namespace std;
int main() {
    int a = 4, b = 3, c;
    c = a * b;  // Multiplication
    cout << c << endl;
    c = a / b;  // Division ทั้งคู่เป็น int ผลลัพธ์เป็น int ทศนิยมถูกตัด
    cout << c << endl;
    c = a % b;  // Modulo
    cout << c << endl;
    return 0;
}

ผลลัพธ์

12
1
1