div.c

来自「标准c库代码,可以应用于各个系统提供了大量的基本函数」· C语言 代码 · 共 85 行

C
85
字号
/*FUNCTION<<div>>---divide two integersINDEX	divANSI_SYNOPSIS	#include <stdlib.h>	div_t div(int <[n]>, int <[d]>);TRAD_SYNOPSIS	#include <stdlib.h>	div_t div(<[n]>, <[d]>)	int <[n]>, <[d]>;DESCRIPTIONDivide@tex$n/d$,@end tex@ifinfo<[n]>/<[d]>,@end ifinforeturning quotient and remainder as two integers in a structure <<div_t>>.RETURNSThe result is represented with the structure. typedef struct. {.  int quot;.  int rem;. } div_t;	where the <<quot>> field represents the quotient, and <<rem>> the	remainder.  For nonzero <[d]>, if `<<<[r]> = div(<[n]>,<[d]>);>>' then	<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.	When <[d]> is zero, the <<quot>> member of the result has the same	sign as <[n]> and the largest representable magnitude.	To divide <<long>> rather than <<int>> values, use the similar	function <<ldiv>>.PORTABILITY	<<div>> is ANSI, but the behavior for zero <[d]> is not specified by	the standard.No supporting OS subroutines are required.*/#include <limits.h>#include <stdlib.h>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;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?