练习7-1
#include <stdio.h> int main() { int n; printf("%d\t%d\t%d\n", sizeof 1,sizeof(unsigned)-1,sizeof n+2 ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4再减去-1所以显示为3,最后是n+2为6 printf("%d\t%d\t%d\n", sizeof +1, sizeof(double) - 1, sizeof(n + 2));//此行显示结果是 4 7 4 因为1的字节是4,double的字节长是8 -1是7,把(n+2)括起来之后使其为一个值,所以是4; printf("%d\t%d\t%d\n", sizeof - 1, sizeof((double)-1), sizeof (n + 2.0) );//此行结果是 4 8 8 因为1的字节是4,把double-1都括起来之后,就是double类型字节为8,同理最后也是8 }
练习7-2
#include <stdio.h> #include<math.h> int main() { unsigned int number; int x; int i; int number1; printf("请输入初始值:" ); scanf("%d", &number); number1 = number; printf("左移多少位:"); scanf("%d", &x); number <<= x; printf("左移后的值:%d",number); putchar(‘\n‘); for (i = 0; i < x; i++) { number1 *= 2; } printf("乘以2的指数幂的值:%d", number1); }
#include <stdio.h> #include<math.h> int main() { unsigned int number; int x; int i; int number1; printf("请输入初始值:" ); scanf("%d", &number); number1 = number; printf("右移多少位:"); scanf("%d", &x); number >>= x; printf("右移后的值:%d",number); putchar(‘\n‘); for (i = 0; i < x; i++) { number1 /= 2; } printf("除以2的指数幂的值:%d", number1); }
练习7-3
#include <stdio.h> #include<math.h> unsigned rrotate(unsigned x,int n){ x >>=n; return x; } int main() { unsigned int number; int x; printf("请输入初始值:"); scanf("%d", &number); printf("右移多少位:"); scanf("%d", &x); printf("右移后的值:%d", rrotate(number, x)); }
#include <stdio.h> #include<math.h> unsigned lrotate(unsigned x,int n){ x <<=n; return x; } int main() { unsigned int number; int x; printf("请输入初始值:"); scanf("%d", &number); printf("左移多少位:"); scanf("%d", &x); printf("左移后的值:%d", lrotate(number, x)); }
练习7-4
unsigned set(unsigned x, int pos) { unsigned mask = (1u << pos); x = (mask | x); return x; } //将第pos位设置为1
unsigned reset(unsigned x, int pos) { unsigned mask = 1u; if (x & (1u<<pos)) return (x ^ (1u << pos)); else return x; } //将第pos为设置为0
unsigned inverse(unsigned x, int pos) { unsigned mask = 1u << pos; x = (x ^ mask); return x; } //将第pos位取反
练习7-5
unsigned set_n(unsigned x, int pos, int n) { int i; for (i = 0; i < n - pos; i++) { unsigned mask = (1u << (pos + i)); x = (mask | x); } return x; } //一个位段都设置为1
unsigned reset_n(unsigned x, int pos, int n) { unsigned mask = 1u; int i; for (i = 0; i < n-pos; i++) { if (x & (1u<<pos+i)) x= (x ^ (1u << pos+i)); } return x; } //一个位段都设置为0
unsigned inverse_n(unsigned x, int pos, int n) { int i; unsigned mask = 1u; for (i = 0; i < n - pos; i++) { x = (x ^ (mask << pos + i)); } return x; } //一个位段都取反
练习7-6
#include <stdio.h> // 显示unsigned类型的值最大值,同时验证最大值+1是0. int main() { unsigned a = 0; while (++a > 0); printf("%u", a - 1); putchar(‘\n‘); printf("%u", a); }
练习7-7
#include <stdio.h> int main() { float a; double b; long double c; scanf("%f",&a); printf("a=%f", a); putchar(‘\n‘); scanf("%lf", &b); printf("b=%f", b); putchar(‘\n‘); scanf("%lf", &c); printf("c=%f", c); }
练习7-8
#include <stdio.h> int main() { float a; double b; long double c; printf("%d,%d,%d",sizeof(a), sizeof(b), sizeof(c)); }
练习7-9
#include <stdio.h> #include<math.h> int main() { double m; double x; printf("正方形的面积是:"); scanf("%lf", &m); printf("边长是:%.2f", sqrt(m)); }
练习7-10
#include <stdio.h> int main(void) { float x; int i; float m = 0.0; for (i = 0; i <= 100; i++) { x = i / 100.0; printf("x=%f x=%f\n",m,x); m += 0.01; } return 0; }
练习7-11
#include <stdio.h> int main(void) { float x; int i; float m = 0.0; float sum1 = 0; float sum2 = 0; for (i = 0; i <= 100; i++) { x = i / 100.0; printf("x=%f x=%f\n",m,x); m += 0.01; sum1 += m; sum2 += x; } printf("%f,%f", sum1, sum2); return 0; }
原文地址:https://www.cnblogs.com/nightswatch-candle/p/11877302.html
时间: 2024-10-30 07:07:55