随笔分类 - c/c++
摘要:1、 #include <stdio.h> int count_bits(unsigned x) { int bits = 0; while(x) { if(x & 1U) bits++; x >>= 1; } return bits; } int int_bits(void) { return c
阅读全文
摘要:c语言中将一个十进制数按照二进制输出 1、 #include <stdio.h> int main(void) { int bits = 0; unsigned tmp = ~0U; while(tmp) { if(tmp & 1U) bits++; tmp >>= 1; } int i; unsi
阅读全文
摘要:c语言中输出不同数据类型在内存中的占位 1、 #include <stdio.h> int main(void) { int n = 0; unsigned x = ~0U; while(x) { if(x & 1U) n++; x >>= 1; } printf("the number of bi
阅读全文
摘要:c语言中输出十进制数转换为二进制数后包含1的数目 1、 #include <stdio.h> int main(void) { unsigned a; puts("please input a nonnegative integers."); printf("a = "); scanf("%u",
阅读全文
摘要:c语言中sizeof运算符,输出不同数据类型的长度 1、 a、sizeof运算符的符号是字节 b、各种数据类型的有符号型和无符号型长度一致 c、一般情况 short <= int <= long。 #include <stdio.h> int main(void) { puts("show the
阅读全文
摘要:c语言中的位以及不同的数据类型可以表示的数值的范围。 1、计算机中的所有数据类型都是用0和1的组合来表示的,这个0和1的占位就是位。 位是具有大量内存空间的运行环境的数据存储单元,可保存具有两种取值的对象。 不同数据类型在内存上的占位决定了其可以表示数值的范围。 比如无符号整型在内存上的占位为n,那
阅读全文
摘要:c语言中在编译器中判断char属于signed char 还是 unsigned char。 1、 判断CHAR_MIN非0,则输出“signed”, 如果为0,则输出“unsigned”,因为unsigned型的最小值为0. #include <stdio.h> #include <limits.
阅读全文
摘要:c语言中输出整型和字符型9中数据类型在编译器中可以表示的数值范围。 1、 a、无符号整型的显示使用 %u b、long型数据的显示使用 %l c、无符号整型的最小值为0. #include <stdio.h> #include <limits.h> int main(void) { printf("
阅读全文
摘要:1、 #include <stdio.h> int main(void) { float x1, sum1 = 0.0; for(x1 = 0.0; x1 <= 1.0; x1 += 0.01) { sum1 += x1; } int i; float sum2 = 0.0; for(i = 1;
阅读全文
摘要:1、 利用浮点进行循环的时候,计算机不能保证计算机内部转换为二进制后不发生数据丢失,因此随着循环的进行,会发生误差的积累。 #include <stdio.h> int main(void) { int i; float x1 = - 0.01, x2; for(i = 0; i <= 100; i
阅读全文
摘要:1、 #include <stdio.h> #include <math.h> int main(void) { double x; puts("please input an double value."); printf("x = "); scanf("%lf", &x); printf("re
阅读全文
摘要:1、 #include <stdio.h> int main(void) { float x1, sum1 = 0.0; for(x1 = 0.0; x1 <= 1.0; x1 += 0.01) { sum1 += x1; } int i; float x2, sum2 = 0.0; for(i =
阅读全文
摘要:1、 #include <stdio.h> int main(void) { float x1 = -0.01; int x2 = 0; int i; float sum1 = 0.0 , sum2 = 0.0; for(i = 0; i <= 100; i++) { x1 += 0.01; sum
阅读全文
摘要:1、 #include <stdio.h> int main(void) { float x1 = -0.01; int x2 = 0; int i; for(i = 0; i <= 100; i++) { printf("x = %f x = %f\n", x1 += 0.01, x2++ / 1
阅读全文
摘要:创建一个程序,输入一个实数作为面积,求面积为该实数的正方形的边长。 #include <stdio.h> #include <math.h> int main(void) { double area; puts("please input the area."); printf("area = ")
阅读全文
摘要:1、创建一个程序,使用sizeof运算符显示3种浮点型的长度。 3种浮点型: float; double; long double; #include <stdio.h> int main(void) { printf("sizeof(float) = %d\n", sizeof(float));
阅读全文
摘要:c语言中<math.h>头文件,计算两点之间的距离。 <math.h>头文件包含基本数学函数的函数原型声明。 1、 #include <stdio.h> #include <math.h> double dist(double x1, double y1, double x2, double y2)
阅读全文
摘要:c语言中整数的显示 以十进制、二进制、八进制、十六进制显示整数 1、 #include <stdio.h> int count_bits(unsigned x) { int bits = 0; while(x) { if(x & 1U) bits++; x >>= 1; } return bits;
阅读全文
摘要:编写inverse_n函数,返回将无符号整数x的第pos为开始的n位取反后的值。 1、 #include <stdio.h> unsigned inverse_n(unsigned x, int pos, int n) { int i; for(i = pos; i <= pos + n - 1;
阅读全文
摘要:编写reset_n函数,返回将无符号整数x的第pos为开始的n位设为0后的值。 1、 #include <stdio.h> unsigned reset_n(unsigned x, int pos, int n) { int i; for(i = pos; i <= pos + n - 1; i++
阅读全文

浙公网安备 33010602011771号