小练习:分段函数

        分段函数要求实现的功能如下:

自变量取值:1, 2, 3, 4, 5, 6, 7, 8->值为8;

自变量取值:9, 10, 11, 12, 13, 14, 15, 16->值为16;

        依次类推······

#include<stdio.h>
typedef int ElemType;
ElemType Piecewisefun(ElemType item)
{
	/*
	ElemType temp = item & ~0x0007;
	temp >>= 3;
	return(temp + 1)<<3;
	*/
	return(item + 8 - 1)&~7;
}

int main()
{
	for (int i = 1; i < 200; ++i)
	{
		ElemType ret = Piecewisefun(i);
		printf("%4d",ret);
		if (i % 8 == 0)
			printf("\n");
	}
	return 0;
}
本程序在VS2017下运行通过

猜你喜欢

转载自blog.csdn.net/qq_41822235/article/details/80785219