明解C语言入门篇练习题第七章

练习7-1

#include <stdio.h>
int main(void)
{
	int n = 1;
	printf("sizeof 1  : %u\n",sizeof(1));
	printf("sizeof +1 : %u\n", sizeof(+1));
	printf("sizeof -1 : %u\n", sizeof(-1));
	printf("sizeof(unsigned)-1 : %u\n", sizeof(unsigned) - 1);
	printf("sizeof(double)-1   : %u\n", sizeof(double) - 1);
	printf("sizeof((double)-1) : %u\n", sizeof((double)-1));//将int型的-1强制转化为double型-1.0
	printf("sizeof n+2     : %u\n", sizeof n+2);
	printf("sizeof (n+2)   : %u\n", sizeof(n+2));
	printf("sizeof (n+2.0) : %u\n", sizeof(n+2.0));
	return 0;
}

练习7-2

#include <stdio.h>
#include <math.h>
int main(void)
{
	unsigned int n;
	int offset;
	printf("输入n:");
	scanf("%d",&n);
	printf("输入左移位数offset:");
	scanf("%d",&offset);
	printf("%u << %d = %u\n",n,offset,n<<offset);
	printf("%u * pow(2,%d) = %d\n",n,offset,(int)(n*pow(2.0,offset)));

	printf("输入n:");
	scanf("%d", &n);
	printf("输入右移位数offset:");
	scanf("%d", &offset);
	printf("%u >> %d = %u\n", n, offset, n >> offset);
	printf("%u / pow(2,%d) = %d\n", n, offset, (int)(n/pow(2.0, offset)));
	return 0;
}

练习7-3

#include <stdio.h>
unsigned rrotate(unsigned x, int n)
{
	unsigned int y;
	y = x >> n;
	return y;
}
unsigned lrotate(unsigned x, int n)
{
	unsigned int y;
	y = x << n;
	return y;
}
int main(void)
{
	unsigned int x;
	int n;
	printf("输入x:");
	scanf("%d",&x);
	printf("输入左移位数n:");
	scanf("%d",&n);
	printf("%u << %d = %u\n",x,n, rrotate(x,n));

	printf("输入x:");
	scanf("%d", &x);
	printf("输入右移位数n:");
	scanf("%d", &n);
	printf("%u >> %d = %u\n", x, n,lrotate(x,n));
	return 0;
}

练习7-4 待做


练习7-5 待做


练习7-6

#include <stdio.h>
#include <limits.h>
int main(void)
{
	unsigned int x;
	x = UINT_MAX;
	printf("x = %u\n",x);
	printf("x + 1 = %u\n",x+1);
	return 0;
}

练习7-7

#include <stdio.h>
int main(void)
{
	float x;
	double y;
	long double z;

	printf("float x = ");
	scanf("%f",&x);
	printf("x = %lf\n",x);

	printf("double y = ");
	scanf("%lf", &y);
	printf("y = %lf\n", y);

	printf("long double z = ");
	scanf("%lf", &z);
	printf("z = %lf\n", z);

	return 0;
}

练习7-8

#include <stdio.h>
int main(void)
{
	float x;
	double y;
	long double z;
	printf("size of float = %u\n",sizeof(float));
	printf("size of double = %u\n", sizeof(double));
	printf("size of long double = %u\n", sizeof(long double));
	return 0;
}

练习7-9

#include <stdio.h>
#include <math.h>
int main(void)
{
	double area;
	printf("input area of a square:");
	scanf("%lf",&area);
	printf("each side of the square: %.2lf\n",sqrt(area));
	return 0;
}

练习7-10

#include <stdio.h>
int main(void)
{
	float x1 = -0.01;
	float x2 = 0;
	int i;
	for (i = 0;i<=100;i++)
	{
		printf("x = %f    x = %f\n", x1+=0.01,(x2++)/100);
	}
	return 0;
}

练习7-11

#include <stdio.h>

int main(void)
{
	float x1 = -0.01;
	float x2 = 0.0;
	int i;
	float sum1 = 0.0;
	float sum2 = 0.0;
	for (i=0;i<=100;i++)
	{
		printf("x = %f    x = %f\n", x1+=0.01,(x2++)/100);
		sum1 += x1;
		sum2 += x2/100;
	}
	printf("sum = %f   sum = %f",sum1,sum2);
	return 0;
}


  • 0
    点赞
  • 0
    评论
  • 9
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值