初入C语言——选择结构经典例题运用(判断是否为闰年及具体月份天数、身高预测题)


前言

作为刚步入大学一学期的新手小白,很高兴在这里写下自己的第一篇文章,接下来我将会不定期分享自己在初步学习C语言中遇到的入门而又经典的例题、低级但又常犯的错误、以及自己学习中的心得体会与疑难问题。在我分享的过程中如果有错误或有更优解,希望大家能够为我指正,期待着能和优秀的你们共同进步。

一、何为选择结构

(简单介绍)
选择结构分if语句和switch语句两种
它是通过某个给定条件进行判断,条件为真或假时分别执行不同的程序内容。
(1) if语句的基本格式:

if( 条件1 )
 {
    满足条件1,于是执行这里的代码。
 }
else if( 条件2 )
 {
    满足条件2,于是执行这里的代码。
 }
else if( 条件3 )
 {
    满足条件3,于是执行这里的代码。
 }
else
 {
    以上条件均不满足,执行这里的代码。
 }

(2) switch语句的基本格式

switch(整形表达式)
{
  case 整数常量表达式1 :
   语句......
      ...;
   break;
  case 整数常量表达式2 :
   语句......  
      ...  ;
   break;
  default:
   语句......
      ...  ;
    
}

二、例题讲解(判断是否为闰年及具体月份天数、身高预测题)

1.switch例题讲解()
(判断是否为闰年及具体月份天数)
问:
从键盘输入某年某月(包括闰年),用switch语句编程输出该年的该月拥有的天数。要求考虑闰年以及输入月份不在合法范围内的情况。已知闰年的2月有29天,平年的2月有28天。
测试用例:

输入:2023 2        输出:28天

输入:2020 2        输出:29天

代码如下:

方法一:(最初的想法,易懂但很是麻烦,建议看一看思路,不建议使用)

#include<stdio.h>
int main( )
{
	int year =0;
	int month =0;
	int a;   //定义一个a 用于下面switch判断是否为闰年
	printf("输入年份和月份");
	scanf_s("%d %d",& year,& month); //vs2022要写成scanf_s,更安全
	if (year%4 ==0 && year % 100 != 0 || year % 400 == 0)
		a = 1;
	else a = 0;
	switch (a)  //判断是否为闰年
	{
	case 1:
		switch (month)  // 判断闰年各个月份天数  (switch语句可嵌套使用)
		{
		case 1:printf(" %d年%d月 有31天\n", year, month); break;
		case 2:printf(" %d年%d月 有29天\n", year, month); break;
		case 3:printf(" %d年%d月 有31天\n", year, month); break;
		case 4:printf(" %d年%d月 有30天\n", year, month); break;
		case 5:printf(" %d年%d月 有31天\n", year, month); break;
		case 6:printf(" %d年%d月 有30天\n", year, month); break;
		case 7:printf(" %d年%d月 有31天\n", year, month); break;
		case 8:printf(" %d年%d月 有30天\n", year, month); break;
		case 9:printf(" %d年%d月 有30天\n", year, month); break;
		case 10:printf(" %d年%d月 有31天\n", year, month); break;
		case 11:printf(" %d年%d月 有30天\n", year, month); break;
		case 12:printf(" %d年%d月 有31天\n", year, month); break;
		}
		break;
		 //注意:这个break很重要,不加的话,输入闰年的月份,天数会显示两次
	    //,因为大的(用于判断是否为闰年的)case1和case0里面的case项都有满足条件的都符合了!!!
	case 0:
		switch (month)    //判断非闰年各个月份天数
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:printf("%d年%d月有31天\n", year, month);
			break;
		case 4:
		case 6:
		case 9:
		case 11:printf("%d年%d月有30天\n",year, month);
		case 2:printf("%d年%d月有28天\n", year, month);
			break;
		}
       
	}
  return 0;
}

方法二(常规方法,很优化,建议使用)

#include<stdio.h>
int main()
{
	int year, month;
	printf("请输入年份和月份");
	scanf_s("%d%d", &year, &month);//vs2022要写成scanf_s,更安全
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:printf("31天");
		break;
	case 4:
	case 6:
	case 9:
	case 11:printf("30天");
		break;
	case 2:
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
			printf("29天");
		else
			printf("28天");
		break;
	default: printf("Int error!");
	}
	return 0;
}

运行结果:
在这里插入图片描述
在这里插入图片描述

注意:switch在使用时,一定要充分了解运用它的特性,
如:不遇到break就一直往下运行等…
用的好就可以简化很多行不必要的代码,用的不好也是极其容易出错的。

2.if例题讲解
(身高预测题)
问:
设faHeight为其父身高,moHeight为其母身高,身高预测公式为
男性成人时身高 = (faHeight + moHeight) * 0.54 cm
女性成人时身高 = (faHeight * 0.923 + moHeight) / 2.0 cm
此外,如果喜爱体育锻炼,那么可增加身高2%;如果有良好的卫生饮食习惯,那么可增加身高1.5%。
请编程从键盘输入用户的性别(用字符型变量sex存储,输入字符F表示女性,输入字符M表示男性)、父母身高(用实型变量存储,faHeight为其父身高,moHeight为其母身高)、是否喜爱体育锻炼(用字符型变量sports存储,输入字符Y表示喜爱,输入字符N表示不喜爱)、是否有良好的饮食习惯等条件(用字符型变量diet存储,输入字符Y表示良好,输入字符N表示不好),利用给定公式和身高预测方法对身高进行预测。
测试用例:

输入:M 180 160 Y Y        输出:190

输入: F 180 160 Y Y        输出:169

分析:(本题乍一看好像很是复杂,但仔细去想其实不难,就是选择语句的熟练运用,题中已经给出了身高预测公式,只需要设计好合适的选择语句即可解题)

方法一(只使用if语句解题)

#include<stdio.h>
int main()
{
	char sex, sports, diet;
	float myHeight, faHeight, moHeight;
	printf("Are you a boy(M) or a girl(F)? "); 
	scanf_s(" %c", &sex);
printf("Please input your father's height(cm): ");
	scanf_s("%f", &faHeight);
	printf("Please input your mother's height(cm): ");
	scanf_s("%f", &moHeight);
	printf("Do you like sports(Y/N)? ");
	scanf_s(" %c", &sports);
	printf("Do you have a good habit of diet(Y/N)? ");
	scanf_s(" %c", &diet);
	if (sex == 'M' || sex == 'm')  //判断性别(输入大小写都可,提高容错率)
		myHeight = (faHeight + moHeight) * 0.54;
	else
		myHeight = (faHeight * 0.923 + moHeight) / 2.0;
	if (sports == 'Y' || sports == 'y')  //判断是否运动(输入大小写都可,提高容错率)

		myHeight = myHeight * (1 + 0.02);
	if (diet == 'Y' || diet == 'y')   //判断饮食习惯(输入大小写都可,提高容错率)

		myHeight = myHeight * (1 + 0.015);
	printf("%.0f\n", myHeight);
	return 0;
}

运行结果:在这里插入图片描述
在这里插入图片描述

方法二(选择语句的嵌套使用 if、switch嵌套使用)

#include<stdio.h>
int main()                                        
{
	float faHeight, maHeight;
	char sex, diet, sports;
scanf_s(" %c%f%f %c %c", &sex,1,&faHeight, &maHeight,&diet,1, &sports,1);
//注意:在每个%c前面都多打了个空格,目的是防止在运行时输入完数据习惯性按一下空格而被读取,导致运行出错。
//注:因为scanf_s在读取字符串时,(只有字符串)需要提供一个数字以表明读取多少个字符,以防止溢出。只有用vs时这样
	
	switch (sex)  
	{
	case 'F':  //当性别为女时,分四种情况讨论
		if (diet == 'Y' && sports == 'Y')
			printf("%.0f", (faHeight * 0.923 + maHeight) / 2 * 1.035);
		else if (diet == 'N' && sports == 'Y')
			printf("%.0f", (faHeight * 0.923 + maHeight) / 2 * 1.02);
		else if (diet == 'N' && sports == 'N')
			printf("%.0f", (faHeight * 0.923 + maHeight) / 2 * 1);
		else 
			printf("%.0f", (faHeight * 0.923 + maHeight) / 2 * 1.015);
		break;
	case'M':  //当性别为男时,分四种情况讨论

		 if (diet == 'Y' && sports == 'Y')
			printf("%.0f", (faHeight + maHeight) * 0.54 * 1.035);
		else if (diet == 'N' && sports == 'Y')
			printf("%.0f", (faHeight + maHeight) * 0.54 * 1.02);
		else if (diet == 'N' && sports == 'N')
			printf("%.0f", (faHeight + maHeight) * 0.54);
		else if (diet == 'Y' && sports == 'N')
			printf("%.0f", (faHeight + maHeight) * 0.54 * 1.015);
	}  
	return 0;
}

在这里插入图片描述

总结

以上内容就是今天的分享,因为我也是c语言初学者,如果有哪些地方写的不对或者有更好的想法,希望大家评论区指正。

  • 43
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值