← 返回首页
C语言系列教程(十二)
发表时间:2021-03-24 12:19:29
选择结构

C语言的程序结构也分为:顺序、选择和循环。

顺序结构就是自上而下,依次执行。我们无需探讨。我们从选择结构开始。

1.简单if语句

实例:

#include <stdio.h>
#include <string.h>

int main(){

    int score;
    printf("%s\n","请输入考试成绩:");
    scanf("%d",&score);
    if(score==100){
        printf("%s","奖励变形金刚一个!"); 
    }   
    return 0;
}

2.if-else语句

实例:

#include <stdio.h>
#include <string.h>

int main(){

    int score;
    printf("%s\n","请输入考试成绩:");
    scanf("%d",&score);

    if(score>100||score<0){
    printf("成绩非法!");
    return -1;
    }

    if(score>=60){
        printf("%s","恭喜你考试及格!"); 
    }else{
    printf("%s","暴打一顿!");
    }   
    return 0;
}

3.多重if-else语句

if- else 语句也可以多个同时使用,构成多个分支,形式如下:

if(判断条件1){
    语句块1
} else  if(判断条件2){
    语句块2
}else  if(判断条件3){
    语句块3
}else  if(判断条件m){
    语句块m
}else{
     语句块n
}

实例:

#include <stdio.h>
#include <string.h>

int main(){

    int score;
    printf("%s\n","请输入考试成绩:");
    scanf("%d",&score);

    if(score>100||score<0){
        printf("成绩非法!");
    return -1;
    }

    if(score>=90&&score<=100){
        printf("%s","成绩优秀!");  
    }else if(score>=80&&score<90){
        printf("%s","成绩良好!");
    }else if(score>=60&&score<80){
        printf("成绩及格!");
    }else{
        printf("不及格!");
    }   
    return 0;
}

4.嵌套if-else语句

在if或者else子句中嵌套if-else语句。

实例:

#include <stdio.h>
#include <string.h>

int main(){
    char gender[10];
    int height;
    printf("请输入性别:");
    scanf("%s",&gender);
    printf("请输入身高(cm):");
    scanf("%d",&height);

    if(strcmp(gender, "男")==0){
    if(height>=180){
       printf("恭喜您,入选校男子篮球队!");
    }else{
       printf("很遗憾您未能入选校男子篮球队!");
    }
    }else{
    if(height>=170){
       printf("恭喜您,入选校女子篮球队!");
    }else{
       printf("很遗憾您未能入选校女子篮球队!");
    }
     }
    return 0;
}

5.switch-case语句

switch 是另外一种选择结构的语句,用来代替简单的、拥有多个分枝的 if else 语句,基本格式如下:

switch(表达式){
    case 整型数值1: 语句 1;
    case 整型数值2: 语句 2;
    ......
    case 整型数值n: 语句 n;
    default: 语句 n+1;
}

case 后面必须是一个整数,或者是结果为整数的表达式,但不能包含任何变量。 default 不是必须的。当没有 default 时,如果所有 case 都匹配失败,那么就什么都不执行。

实例:

#include <stdio.h>
#include <string.h>

int main(){
    int score;
    printf("%s\n","请输入考试成绩:");
    scanf("%d",&score);

    if(score>100||score<0){
        printf("成绩非法!");
    return -1;
    }
    int level = score/10;

    switch(level){
    case 10:
    case 9:
          printf("成绩优秀!");
          break;
        case 8:
          printf("成绩良好!");
          break;
        case 7:
        case 6:
          printf("成绩及格!");
          break;
        default:
          printf("不及格!");
          break;            
    }
    return 0;
}

6.选择结构编程练习

6.1出租车计费问题

某市出租车的起步价为9元,其中起步价包括了3公里,之后每公里收费是2.5元,晚上22:00至次日凌晨6点加收1元钱。编写程序实现输入打车的时间和距离,计算本次打车的费用。

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{

    int hour;//打车的时间,取整数即可。
    int distance;//打车的距离公里数
    double startPrice = 10.0; //默认起步价是10元
    double totalPrice = 0.0; //总费用
    printf("请输入打车的时间(0-23):");
    scanf("%d", &hour);
    printf("请输入打车的距离:");
    scanf("%d", &distance);

    printf("您本次打车是时间是:%d,打车的距离是:%d", hour, distance);

    if (hour >= 7 && hour <= 21)
    {
        startPrice = 9.0;
    }

    if (distance <= 3)
    {
        totalPrice = startPrice;
    }
    else
    {
        totalPrice = startPrice + (distance - 3) * 2.5;
    }

    printf("您本次打车的总费用是:%.2lf", totalPrice);
    return 0;
}