div() function in C++


C / C++ 库函数 div_t div(int numer, int denom) 将 numer(分子)除以 denom(分母)。以下是 div() 函数的声明。

div_t div(int numer, int denom)

参数是分子分母。此函数在 <cstdlib> 中定义的结构中返回一个值,该结构有两个成员。对于 div_t:int quot; int rem;

示例

 Live Demo

#include <iostream>
#include <cstdlib>
using namespace std;
int main () {
   div_t output;
   output = div(27, 4);
   cout << "Quotient part of (27/ 4) = " << output.quot << endl;
   cout << "Remainder part of (27/4) = " << output.rem << endl;
   output = div(27, 3);
   cout << "Quotient part of (27/ 3) = " << output.quot << endl;
   cout << "Remainder part of (27/3) = " << output.rem << endl;
   return(0);
}

输出

Quotient part of (27/ 4) = 6
Remainder part of (27/4) = 3
Quotient part of (27/ 3) = 9
Remainder part of (27/3) = 0

更新于: 2019年7月30日

172 个浏览

开始你的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.