1/*
2 *测试负数除法和取模运算
3 */
4#include <stdio.h>
5
6 int main( void )
7 {
8 printf ("-5 / 2 = %d\t", -5/2);
9 printf ("-5 %% 2 = %d\n", -5%2);
10 printf ("5 / -2 = %d\t",5/ -2);
11 printf ("5 %% -2 = %d\n",5% -2);
12 printf ("-5 / -2 = %d\t", -5/ -2);
13 printf ("-5 %% -2 = %d\n", -5% -2);
14
15 return 0;
16 }
17
18
19
20/*
21 * -5 / 2 = -2 -5 % 2 = -1 ( -5 = 2 * ( -2 ) + ( -1 ) )
22 * 5 / -2 = -2 5 % -2 = 1 ( 5 = ( -2 ) * ( -2 ) + 1 )
23 * -5 / -2 = 2 -5 % -2 = -1 ( -5 = ( -2 ) * 2 + ( -1 ) )
24 *
25 * 被除数、除数、商和余数满足以下关系:
26 * 被除数 =除数 *商 +余数
27 * 余数和被除数的符号一致,而且其绝对值小于除数
28 * 如果被除数和除数的符号一致,那么商为非负数,否则,为非正数
29 *
30 */
/*
* 测试负数除法和取模运算
*/
#include <stdio.h>
int main ( void )
{
printf ( "-5 / 2 = %d\t", -5 / 2 );
printf ( "-5 %% 2 = %d\n", -5 % 2 );
printf ( "5 / -2 = %d\t", 5 / -2 );
printf ( "5 %% -2 = %d\n", 5 % -2 );
printf ( "-5 / -2 = %d\t", -5 / -2 );
printf ( "-5 %% -2 = %d\n", -5 % -2 );
return 0;
}
/*
* -5 / 2 = -2 -5 % 2 = -1 ( -5 = 2 * ( -2 ) + ( -1 ) )
* 5 / -2 = -2 5 % -2 = 1 ( 5 = ( -2 ) * ( -2 ) + 1 )
* -5 / -2 = 2 -5 % -2 = -1 ( -5 = ( -2 ) * 2 + ( -1 ) )
*
* 被除数、除数、商和余数满足以下关系:
* 被除数 = 除数 * 商 + 余数
* 余数和被除数的符号一致,而且其绝对值小于除数
* 被除数和除数的符号一致,那么商为正数,否则为负数
*
*/