其他分享
首页 > 其他分享> > if else if语句的数学问题

if else if语句的数学问题

作者:互联网

一.代码功能
数学中分段问题,此处以水费未背景

二、代码展示

#include <stdio.h>

#define rare1 0.132
#define rare2 0.150
#define rare3 0.300
#define rare4 0.340

#define break1 360
#define break2 468
#define break3 720

#define base1 (rare1*break1)//第一个分界线360
#define base2 (base1+(rare2*(break2-break1)))//第二个分界线468
#define base3 (base1+base2+(rare3*(break3-break2)))//第三个分界线720


int main(void)
{
	double kwh;
	double bill;
	printf("please enter the kwh used.\n");
 
		scanf("%lf",&kwh);

	if(kwh<=break1)
		bill=kwh*rare1;
	else if(kwh<break2)
		bill=base1+(rare2*(kwh-break1));

	else if(kwh<=break3)
		bill=base2+(rare3*(kwh-break2));

	else
		bill=base3+(rare4*(kwh-break3));
	printf("The charge for %.lf kwh is $%1.2f.\n",kwh,bill);

	return 0;

}

三、结果
在这里插入图片描述

标签:语句,分界线,break1,kwh,break2,else,base1,数学,define
来源: https://blog.csdn.net/qq_45941706/article/details/122775296