1
2
3
4
5
6
7
8
9
实验A1:表达式11111*11111的值是多少?把51改成61呢?91呢?
#include <stdio.h>
int main()
{
printf("%d\n",11111*11111); //正常数字123454321
printf("%d\n",111111*111111); //数据太大溢出,为负值-539247567
printf("%d\n",111111111*111111111); //结果是整数,但是数值错误1653732529
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
实验A2:把实验A1中的所有数换成浮点数,结果如何  
#include <stdio.h>
int main()
{
printf("%f\n",11111.0*11111.0);//123454321.000000
printf("%f\n",111111.0*111111.0);//12345654321.000000
printf("%f\n",111111111.0*111111111.0);//12345678987654320.000000
return 0;
}
/*又改成%d
#include <stdio.h>
int main()
{
printf("%d\n",11111.0*11111.0);//123454321.000000
printf("%d\n",111111.0*111111.0);//12345654321.000000
printf("%d\n",111111111.0*111111111.0);//12345678987654320.000000
return 0;
}*/

#INF00:出现此报错说明是数值溢出,或者说是除数为0

名称 解释
#IND 这个的情况更复杂,一般来说,它们来自于任何未定义结果(非法)的浮点数运算。"IND"是 indeterminate(不确定) 的缩写,而"nan"是 not a number 的缩写。IND表示NAN, 比如 0/0 log( -1 ) 等等。IND是Windows上的说法,NaN是Linux上的说法,含义是一样的。产生这个值的常见例子有:对负数开平方,对负数取对数,0.0/0.0,0.0*∞, ∞/∞ 等。也可能由于一些操作使得程序中产生了无效数字或者没有给成员变量赋值,使用类似于pow, exp等等函数时常会产生一个无效数字1.#IND00(-1.#IND00 对应符号位 1 , 阶码全1 , 尾数非0 )。
#INF 这个值表示“无穷大inf (infinity 的缩写)”,即超出了计算机可以表示的浮点数的最大范围(或者说超过了 double 类型的最大值)。一般来说是除数为0得出的结果,例如,当一个整数除以0时便会得到一个1.#INF / inf值;相应的,如果一个负整数除以0会得到 -1.#INF / -inf 值。
1
2
3
4
5
6
7
8
9
10
实验A3:表达式sqrt(-10)的值是多少?尝试用各种方式输出。在计算过程中系统会报错吗?  
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f\n",sqrt(-10));//-1.#IND00
printf("%d\n",sqrt(-10));//0
printf("%.10f\n",sqrt(-10));//-1.#IND000000
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
实验A4:表达式1.0/0.0,0.0/0.0的值是多少?尝试用各种方式输出。在计算过程中会报错吗?  
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f\n",1.0/0.0);//1.#INF00
printf("%.2f\n",1.0/0.0);//1.#J
printf("%.10f\n",1.0/0.0);//1.#INF000000
printf("%f\n",0.0/0.0);//-1.#IND00
printf("%.2f\n",0.0/0.0);//-1.#J
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
用程序实验测试int整数的最小值和最大值(精确值)
//决定int最大值的主要原因:根据编译器类型不同而变化。
#include <stdio.h>
#include <math.h>
int main() {
/*int n = 0, i = 0;
while (n >= i)
{
n = i;
i--;
}
printf("%d", n);*/ //-2147483648(最小值)

int n = 0, i = 0;
while (n <= i) {
n = i;
i++;
}
printf("%d", n); //2147483647(最大值)
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//7744 问题(1)函数 floor(x)返回不超过 x 的最大整数。floor(小数不能是整数)
#include<stdio.h>
#include<math.h>
int main()
{
for(int a = 1; a <= 9; a++)
for(int b = 0; b <= 9; b++)
{
int n = a*1100 + b*11; //这里才开始使用 n,因此在这里定义 n
int m = floor(sqrt(n) + 0.5);
if(m*m == n) printf("%d\n", n);
}
return 0;
}
/*读者可能会问:可不可以这样写?if(sqrt(n) == floor(sqrt(n))) printf("%d\n", n),即直接判
断 sqrt(n)是否为整数。理论上当然没问题,但这样写不保险,因为浮点数的运算(和函数)
有可能存在误差。
假设在经过大量计算后,由于误差的影响,整数 1 变成了 0.9999999999,floor 的结果
会是 0 而不是 1。为了减小误差的影响,一般改成四舍五入,即 floor(x+0.5)①。如果难以理
解,可以想象成在数轴上把一个单位区间往左移动 0.5 个单位的距离。floor(x)等于 1 的区间
为[1,2),而 floor(x+0.5)等于 1 的区间为[0.5, 1.5)。
提示 2-7:浮点运算可能存在误差。在进行浮点数比较时,应考虑到浮点误差。*/

数学定理:要计算只包含加法、减法和乘法的整数表达式除以正整数 n 的余数,可以在每步计算之后对 n 取余,结果不变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*p31 程序 2-11 数据统计(fopen 版),如果想把 fopen 版的程序改成读写标准输入输出,只需赋值“fin =stdin; fout =stdout;”即可,不要调用 fopen 和 fclose①。*/
#include<stdio.h>
#define INF 1000000000
int main()
{
FILE *fin, *fout;
fin = fopen("data.in", "rb");
fout = fopen("data.out", "wb");
int x, n = 0, min = INF, max = -INF, s = 0;
while(fscanf(fin, "%d", &x) == 1)
{
s += x;
if(x < min) min = x;
if(x > max) max = x;
n++;
}
fprintf(fout, "%d %d %.3f\n", min, max, (double)s/n);
fclose(fin);
fclose(fout);
return 0;
}

##时间

1
2
#include<time.h>
printf("Time used = %.2f\n", (double)clock() / CLOCKS_PER_SEC);

由于函数内会被系统动态分配内存比较小,所以只有数组定义放在main函数外面时,数组 a 才可以开得很大;放在 main 函数内时,数组稍大就会异常退出。