第24节 if语句的嵌套

一.用if语句构造多分支结构

#include <stdio.h>
#include <math.h>
int main()
{
	float x, y;
	scanf_s("%f", &x);
	if (x < 2)
	{
		y = x;
	}
	else if (x < 6)
	{
		y = x * x + 1;
	}
	else if (x < 10)
	{
		y = sqrt(x + 1);
	}
	else
	{
		y = 1 / (x + 1);
	}
	printf("%f\n", y);
}

二.if语句的嵌套

1.if语句嵌套就是if语句中包含一个或多个if语句。
2.else总是与它上面最近的,且未配对的if配对。
3.为了避免误用,最好使每一层内嵌的if语句都包含else子句。
4.如果if与else的数目不一样,加花括号来确定配对关系。

#include <stdio.h>
#include <math.h>
int main() {
	double a, b, c, x1, x2, delta;
	scanf_s("%lf %lf %lf", &a, &b, &c);
	delta = b * b - 4 * a * c; //好风格,效率,防止出错,提高可读性,提高可维护性;
	if (delta >= 0)
	{
		if (delta > 0)
		{
			x1 = (-b + sqrt(delta)) / (2 * a);
			x2 = (-b - sqrt(delta)) / (2 * a);
			printf("两个不等的实根:x1=%.2f x2=%.2f\n", x1, x2);
		}
		else
		{
			x1 = -b / (2 * a);
			printf("两个相等的实根,x1=x2=%.2f\n", x1);
		}
	}
	else
	{
		printf("方程无实根!\n");
	}
}

三.练习:运行后x的值?

#include <stdio.h>
#include <math.h>
int main()
{
	int x = 100, a = 10, b = 20, ok1 = 5, ok2 = 0;
	if (a < b)
		if (b != 15)
			if (!ok1)
				x = 1;
			else
				if (ok2)
					x = 10;
	printf("%d", x);
}

增加花括号后,阅读更方便

#include <stdio.h>
#include <math.h>
int main()
{
	int x = 100, a = 10, b = 20, ok1 = 5, ok2 = 0;
	if (a < b)
	{
		if (b != 15)
		{
			if (!ok1)
			{
				x = 1;
			}	
			else if (ok2) 
			{
				x = 10;
			}		
		}	
	}
	printf("%d", x);
}

四.程序阅读

#include <stdio.h>
int main()
{
    int a = 1, b = 2, c = 3;
    if (a <= c)
        if (b == c)
            printf("a = %d\n", a);
        else
            printf("b = %d\n", b);
    printf("c = %d\n", c);
    return 0;
}
运行结果:
b = 2
c = 3
#include <stdio.h>
int main()
{
	int x = 1, y = 1, z = 10;
	if (z < 0)
		if (y > 0)
			x = 3;
		else
			x = 5;
	printf("%d\t", x); //x=1;
	if (z = y < 0)
		x = 3;
	else if (y == 0)
		x = 5;
	else
		x = 7;
	printf("%d\t", x); //x=7;
	printf("%d\t", z); //z=0;
	return 0;
}

五.实践项目

#include <stdio.h>
#include <math.h>
int main()
{
	double x, y;
	scanf_s("%lf", &x);
	if (x < 2)
	{
		y = x;
	}
	else if (x < 6)
	{
		y = x * x + 1;
	}
	else if (x < 10)
	{
		y = sqrt(x + 1);
	}
	else
	{
		y = 1 / (x + 1);
	}
	printf("y=%f", y);
}
#include <stdio.h>
int main()
{
	double x;
	scanf_s("%lf", &x);

	if (x < 0.25)
	{
		printf("请注意!油量低!\n");
	}

	if (x > 0.75)
	{
		printf("油量高,不必停!\n");
	}
}
#include <stdio.h>
#include <math.h>
int main()
{
	double a, b, c, d, x1, x2;
	scanf_s("%lf %lf %lf", &a, &b, &c);
	if (a == 0)
	{
		if (b != 0)
		{
			printf("有一个实根%lf\n", -c / b);
		}
		else if (c == 0)
		{
			printf("解为任意值\n");
		}
		else if (c != 0)
		{
			printf("方程无解!\n");
		}
	}
	else     //表示在a不等于0的前提下;
	{
		d = b * b - 4 * a * c;
		if (d >= 0)
		{
			x1 = (-b + sqrt(d)) / (2 * a);
			x2 = (-b - sqrt(d)) / (2 * a);
			printf("有两个实根:%.2f,%.2f\n", x1, x2);
		}
		else
		{
			x1 = -b / (2 * a);
			x2 = sqrt(-d) / (2 * a);
			printf("有两个虚根:%.2f,%.2f\n", x1, x2);
		}
	}
	return 0;
}
#include <stdio.h>
int main()
{
	double 收入额, 纳税额, 超出额;
	printf("请输入您的收入: ");
	scanf_s("%lf", &收入额);
	超出额 = 收入额 - 3500;
	if (超出额 < 0)
	{
		纳税额 = 0;
	}
	else if (超出额 < 1500)
	{
		纳税额 = 超出额 * 0.03 - 0;
	}
	else if (超出额 < 4500)
	{
		纳税额 = 超出额 * 0.1 - 105;
	}
	else if (超出额 < 9000)
	{
		纳税额 = 超出额 * 0.2 - 555;
	}
	else if (超出额 <35000)
	{
		纳税额 = 超出额 * 0.25 - 1005;
	}
	else if (超出额 < 55000)
	{
		纳税额 = 超出额 * 0.30 - 2755;
	}
	else if (超出额 < 80000)
	{
		纳税额 = 超出额 * 0.35 - 5505;
	}
	else
	{
		纳税额 = 超出额 * 0.45 - 13505;
	}
	printf("应缴税 %.2lf 元,税后收入 %.2lf 元。\n", 纳税额, 收入额 - 纳税额);
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值