例2-4 分段函数

计算分段函数

程序核心——条件语句

程序

#inclde<stdio.h>
int main(void)
{
	double x,y;//定义两个双精度浮点型变量
	 
	printf("Enter x (x>=0):\n");//输入提示 
	
	scanf("%lf",&x);//调用scanf函数输入数据,变量x前加&,%lf中l是long的缩写; 
	
	if(x<=15)//使用条件语句if-else 
	{
		y=4*x/3;
	}
	else
	{
		y=2.5*x-10.5; 
	}
	printf("y=f(%f)=%0.2f\n",x,y);//输出结果
	
	return 0; 
}

结果

Enter x (x>=0):
9.5
y=f(9.500000)=12.67


Process exited after 9.255 seconds with return value 0
请按任意键继续. . .

分析

使用条件语句、scanf()函数

posted on 2019-03-11 19:36  凯*凯  阅读(259)  评论(0编辑  收藏  举报

导航