1. 程式人生 > >小練習:分段函式

小練習:分段函式

        分段函式要求實現的功能如下:

自變數取值: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下執行通過