/* FUNCTION ---divide two integers INDEX div ANSI_SYNOPSIS #include div_t div(int , int ); TRAD_SYNOPSIS #include div_t div(, ) int , ; DESCRIPTION Divide @tex $n/d$, @end tex @ifinfo /, @end ifinfo returning quotient and remainder as two integers in a structure . RETURNS The result is represented with the structure . typedef struct . { . int quot; . int rem; . } div_t; where the field represents the quotient, and the remainder. For nonzero , if `' then equals `'. When is zero, the member of the result has the same sign as and the largest representable magnitude. To divide rather than values, use the similar function . PORTABILITY is ANSI, but the behavior for zero is not specified by the standard. No supporting OS subroutines are required. */ #include #include div_t _DEFUN (div, (n, d), int n _AND int d) { div_t res; if (d) { res.quot = abs (n) / abs (d); res.rem = abs (n) % abs (d); if ((n < 0 && d > 0) || (n >= 0 && d < 0)) res.quot = -res.quot; if (n < 0) res.rem = -res.rem; } else { if (n < 0) res.quot = INT_MIN; else res.quot = INT_MAX; res.rem = 0; } return res; }