div示例
C ++ div()函数 (C++ div() function)
div() function is a library function of cstdlib header. It is used for integral division, it accepts two parameters (numerator and denominator) and returns a structure that contains the quot (quotient) and rem (remainder).
div()函数是cstdlib标头的库函数。 它用于整数除法,它接受两个参数( 分子和分母 ),并返回一个包含quot ( 商 )和rem ( remaind )的结构。
Syntax of div() function:
div()函数的语法:
C++11:
C ++ 11:
div_t div (int numer, int denom);
ldiv_t div (long int numer, long int denom);
lldiv_t div (long long int numer, long long int denom);
Parameter(s):
参数:
numer – represents the value of numerator.
numer –代表分子的值。
denom – represents the value of denominator.
denom –代表分母的值。
Return value:
返回值:
The return type of this function is div_t / ldiv_t / lldiv_t, returns a structure that contains the quot (quotient) and rem (remainder).
该函数的返回类型是div_t / ldiv_t / lldiv_t,返回包含QUOT( 商 )和REM( 剩余 )的结构。
Example:
例:
Input:
long int n = 10;
long int d = 2;
ldiv_t result;
Function call:
result = div(n, d);
Output:
result.quot = 5
result.rem = 0
C ++代码演示div()函数的示例 (C++ code to demonstrate the example of div() function)
// C++ code to demonstrate the example of
// div() function
#include <iostream>
#include <cstdlib>
using namespace std;
// main() section
int main()
{
long int n = 10;
long int d = 2;
ldiv_t result;
result = div(n, d);
cout << "Numerator : " << n << endl;
cout << "Denominator: " << d << endl;
cout << "Quotient : " << result.quot << endl;
cout << "Remainder : " << result.rem << endl;
cout << endl;
n = 100;
d = 12;
result = div(n, d);
cout << "Numerator : " << n << endl;
cout << "Denominator: " << d << endl;
cout << "Quotient : " << result.quot << endl;
cout << "Remainder : " << result.rem << endl;
cout << endl;
n = 300;
d = 123;
result = div(n, d);
cout << "Numerator : " << n << endl;
cout << "Denominator: " << d << endl;
cout << "Quotient : " << result.quot << endl;
cout << "Remainder : " << result.rem << endl;
cout << endl;
return 0;
}
Output
输出量
Numerator : 10
Denominator: 2
Quotient : 5
Remainder : 0
Numerator : 100
Denominator: 12
Quotient : 8
Remainder : 4
Numerator : 300
Denominator: 123
Quotient : 2
Remainder : 54
Reference: C++ div() function
参考: C ++ div()函数
翻译自: https://www.includehelp.com/cpp-tutorial/div-function-with-example.aspx
div示例