街言要百圍 , }嵌套计语句 实例说明 通过前两个实例的学习,读者对if语句三种形式应该有了较全面的了解。在此,还将向 读者介绍一下嵌套的语句。在语句中又包含一个或多个f语句称为语句的嵌套,实际上 是三种形式的语句混合使用的一种情况。 本实例将要实现的功能是,根据给出的输血者的资料(包括性别和体重),程序判断输出 输血者对应的输血量。 y平 知识要点 嵌套if语句的一般形式如下: if ( if()语句1 else语句2 if()语句3 ese语句4 注意f和else的配对关系。从最内层开始,else总是与它上面最近的(未曾配对的) f配对。如果与else的数目不一样,为实现程序设计者的意图,可以加花括号来确定配 对关系。 程序源码 该应用程序的源代码如下 a include <stdio. h> void main() /sex代表输血者的性别, weight代表输血着的体重,cuba9e代表输血量 int sex, weight, cubage; printf(“请给出输血者的性别和体重:"); scanf(8d, 3d", &sex, weight): i(sex>=0)若变量sex的数值为非负数,则表示为男性 2
一蔫基融舢识蔫 if(weight >e 120 cubage = 200; printf{"此人应该输血:8d毫升Ⅶn”, cubage); else cubage 180: printf("此人应该输血:划d毫升\n", cubage); else//否则,表示为女性 if(weight >* 100) cubage =150: print("此人应该输血:8d毫升n", cubage else cubage 120 rint:("此人应该输血:8d毫升\n", cubage 程序分析 程序根据输血者的性别和体重来判断他们的输血量。对于男性,体重超过120公斤的输血 量为200毫升,低于120公斤的输血量为180毫升。对于女性,体重超过100公斤的将输血 150毫升,否则输血量为120毫升
@自彩程百 12) switch语句 实例说明 C语言的一个多分支选择语句是 switch语句,这种语句把一个表达式的值和一个整数或 字符常量表中的元素逐一比较,发现匹配时,与匹配常数关联的域将被执行。 本实例将使用 switch语句解决一个有趣的小问题,问题表述如下:给出一个不多于5位 的正整数,要求:①求它是几位数;②分别打印出每一位数字;③按逆序打印出各位数字。例 如,原数为489,应输出984 知识要点 前面介绍的f语句只能处理从两者间选择其一,当要实现几种可能之一时,就要用i.clse if甚至多重的嵌套来实现,当分支较多时,程序变得复杂冗长,可读性降低。C语言提供了 switch选择语句专门处理多路分支的情形,使程序变得简洁。 switch语句的一般形式是: switch(expression) case constant I statement sequence; break; case constant2: statement sequencei break: case constant 3: statement sequence; break de fault statement sequence; 表达式 expression必须对整数求值,因此可使用字符或整数值,但不能使用浮点表达式。 表达式 expression的值顺序与case语句中的常量逐一比较,发现匹配时,与该case语句关联 的语句序列被执行,直到遇到 break语句或达到 switch语句结尾时停止。若未发现匹配,则执 行 default语句。 default是可选的,若未选中,则不发生任何操作
第一篇·基似高 程序源码 该应用程序的源代码如下 i include <stdio. h> void main() int num; //下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 int indiv, ten, hundred, thousand; int ten thousand, hundred thousand, placei printf("请输入一个整数(0~999999):"); scanf( 8d",&num)i //判断变量num的位数 if(num>99999) place =6: else if(num >9999) else if(num> 999) place =; else if(nun >99) 3 else if(num >9) place =2; else a printf("place 8d\n", place); printf("每位数字为:") /求出nim在各位上的值 hundred thousand num/100000; ten thousand =(num- hundred thousand* 100000)/10000; thousand =(num hundred thousand*100000- ten thousand*10000)/1000: hundred (num -hundred thousand*100000- ten thousand*10000 thousand*1000)/100; ten s (num- hundred thousand*- ten thousand*10000 thousand*1000- hundred*100)/10: indiv s num hundred thousand*100000- ten thousand*10000 thousand*1000- hundred*100-ten*10 25
侣自彩雅程百 //判断交量num的位数,并根据位数做出相应的输出 switch(place) case 1: printf("sd", indiv)i printf("\n反序数字为:”); printf("8d\n", indiv)i break; case 2: printf("8d, sd", ten, indiv): printf("\n反序数字为:"); printf ("&dsd\n", indiv, ten); break; case 3: printf("8d, id, id", hundred, ten, indiv): printf("n反序数字为:"); printf("8d8did\n", indiv, ten, hundred); break case 4: printf("&d, 8d id", thousand, hundred, ten, indiv)i printf("\n反序数字为:"); printf("sdadid&d\n", indiv, ten, hundred, thousand:i break: case 5: printf("d, id, sd, sd, sd", ten_ thousand, thousand, hundred, ten, indiv): printf("\n反序数字为:") printf(*8dsdsdidBd\n", indiv, ten, hundred, thousand, ten thousand)i break case 6: printf("sd, d, sd, sd, sd, sd", hundred thousand, ten thousand, thousand, hundred, ten, indiv) printf("\n反序数字为:") printf("sdsdsdsdBdsd\n", indiv, ten, hundred, thousand, ten thousand, hundred thousand); default: printf("Not find. \n"); break; 程序分析) 本例是读者阅读本书以来所遇到的最长的一个程序,请不要惊慌,本例代码虽然较长,但 在逻辑上还是比较容易理解的 本例的流程可简单表示如下所示 输入一个正整数(不低于五位);判断所输入整数的位数并输出:求出整数在各位上的值: 使用 switch语句判断所输入整数的位数,然后输出整数在各位上的值;最后输出所输入整数 26