IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言程序设计:现代方法 第5章 课后习题答案 -> 正文阅读

[C++知识库]C语言程序设计:现代方法 第5章 课后习题答案

除书籍作者给出的题解外,其余为个人所解,仅供参考

第5章 选择语句

练习题

  1. (a) 1
    (b) 1
    (c) 1
    (d) 0
  2. (a) 1
    (b) 1
    (c) 1
    (d) 1
  3. (a) 1; 3 4 5
    (b) 0; 7 8 9
    (c) 1; 8 8 9
    (d) 1; 2 1 1
  4. (i > j) - (i < j)
  5. 合法,条件恒成立打印输出
  6. 合法,条件不成立什么也不做
  7. 结果相同皆为17
  8.  if (age >= 13 && age <= 19)
         teenager = true;
     else
         teenager = false;
    
  9. 等价
  10. onetwo
  11. switch (area_code) {
        case 229: printf("Albany"); break;
        case 404: case 470: case 678: case 770:
            printf("Atlanta"); break;
        case 478: printf("Macon"); break;
        case 706: case 762:
            printf("Columbus"); break;
        case 912: printf("Savannah"); break;
        default: printf("Area code not recognized"); break;
    }
    

编程题

  1.  int num;
     printf("Enter a number: ");
     scanf("%d", &num);
     if (num >= 0 && num <= 9)
         printf("The number %d has 1 digits\n", num);
     else if (num >= 10 && num <= 99)
         printf("The number %d has 2 digits\n", num);
     else if (num >= 100 && num <= 999)
         printf("The number %d has 3 digits\n", num);
     else
         printf("The number %d has 4 digits\n", num);
    
  2.  int hours, minutes;
    
     printf("Enter a 24-hour time: ");
     scanf("%d:%d", &hours, &minutes);
    
     printf("Equivalent 12-hour time: ");
     if (hours == 0)
         printf("12:%.2d AM\n", minutes);
     else if (hours < 12)
         printf("%d:%.2d AM\n", hours, minutes);
     else if (hours == 12)
         printf("%d:%.2d PM\n", hours, minutes);
     else
         printf("%d:%.2d PM\n", hours - 12, minutes);
    
  3.  int mount, commission, competitor_commission;
     float price, value;
     printf("Enter price and mount: ");
     scanf("%d%d", &price, &mount);
    
     if (mount < 2000)
         competitor_commission = 33.3f * mount;
     else
         competitor_commission = 33.2f * mount;
    
     value = price * mount;
     if (value < 2500.f)
         commission = 30.00f + .017f * value;
     else if (value < 6250.00f)
         commission = 56.00f + .0066f * value;
     else if (value < 20000.00f)
         commission = 76.00f + .0034f * value;
     else if (value < 50000.00f)
         commission = 100.00f + .0022f * value;
     else if (value < 500000.00f)
         commission = 155.00f + .0011f * value;
     else
         commission = 255.00f + .0009f * value;
    
     if (commission < 39)
         commission = 39.00f;
    
     printf("Commission: $%.2f\n", commission);
     printf("Commission of competitor: $%.2f\n", competitor_commission);
    
  4.  int speed;
     printf("Enter wind speed: ");
     scanf("%f", &speed);
    
     if (speed < 1)
         printf("Calm");
     else if (speed >= 1 && speed <= 3)
         printf("Light air\n");
     else if (speed >= 4 && speed <= 27)
         printf("Breeze\n");
     else if (speed >= 28 && speed <= 47)
         printf("Gale\n");
     else if (speed >= 48 && speed <= 63)
         printf("Storm\n");
     else
         printf("Hurricane\n");
    
  5.  float value, taxi;
     printf("Enter your value of income: ");
     scanf("%d", &value);
    
     if (value <= 750)
         taxi = value * 0.01f;
     else if (value <= 2250)
         taxi = (value - 750) * 0.02f + 7.5f;
     else if (value <= 3750)
         taxi = (value - 2250) * 0.03f + 37.5f;
     else if (value <= 5250)
         taxi = (value - 3750) * 0.04f + 82.5f;
     else if (value <= 7000)
         taxi = (value - 5250) * 0.05f + 142.5f;
     else
         taxi = (value - 7000) * 0.06f + 230.0f;
    
     printf("Your taxi: %d\n", taxi);
    
  6.  int check_digit, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
     first_sum, second_sum, total;
    
     printf("Enter the first (single) digit: ");
     scanf("%1d", &d);
     printf("Enter first group of five digits: ");
     scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5);
     printf("Enter second group of five digits: ");
     scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5);
     printf("Enter the last (single) digit: ");
     scanf("%1d", &check_digit);
    
     first_sum = d + i2 + i4 + j1 + j3 + j5;
     second_sum = i1 + i3 + i5 + j2 + j4;
     total = 3 * first_sum + second_sum;
    
     if (check_digit == 9 - ((total - 1) % 10))
         printf("VALID\n");
     else
         printf("NOT VALID\n");
    
  7.  int a, b, c, d;
     int min1, min2, max1, max2;
    
     printf("Enter four integers: ");
     scanf("%d%d%d%d", &a, &b, &c, &d);
    
     if (a < b) min1 = a, max1 = b;
     else min1 = b, max1 = a;
    
     if (c < d) min2 = c, max2 = d;
     else min2 = d, max2 = c;
    
     if (min1 > min2) min1 = min2;
     if (max1 < max2) max1 = max2;
    
     printf("Largest: %d\n", max1);
     printf("Smallest: %d", min1);
    
  8.  int hours, minutes;
     printf("Enter a 24-hours time: ");
     scanf("%d:%d", &hours, &minutes);
    
     int time = hours * 60 + minutes;
     if (time < 480 || time >= 1140)
         printf("Cloest departure time is 8:00 a.m., arriving at 10:16 a.m.\n");
     else if (time < 583)
         printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
     else if (time < 679)
         printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m");
     else if (time < 767)
         printf("Closest departure time is 12:47 a.m., arriving at 3:00 p.m");
     else if (time < 840)
         printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
     else if (time < 945)
         printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
     else if ()
         printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
     else
         printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
    
  9.  int month, day, year, early_month = 0, early_day = 0, early_year = 0;
    
     printf("Enter a date (mm/dd/yy): ");
     scanf("%d/%d/%d", &early_month, &early_day, &early_year);
     printf("Enter a date (mm/dd/yy): ");
     scanf("%d/%d/%d", &month, &day, &year);
    
     if (year < early_year) {
             early_month = month;
             early_day = day;
             early_year = year;
         } else if (year == early_year && month < early_month) {
             early_month = month;
             early_day = day;
             early_year = year;
         } else if (year == early_year && month == early_month && day < early_day) {
             early_month = month;
             early_day = day;
             early_year = year;
         }
    
     printf("%d/%.2d/%.2d is the earliest date\n", early_month, early_day, early_year);
    
  10. int grade;
    
    printf("Enter numerical grade: ");
    scanf("%d", &grade);
    
    if (grade < 0 || grade > 100) {
        printf("Illegal grade\n");
        return 0;
    }
    
    switch (grade/10) {
        case 10: case 9: printf("A\n"); break;
        case 8: printf("B\n"); break;
        case 7: printf("C\n"); break;
        case 6: printf("D\n"); break;
        case 5: case 4: case 3: case 2: case 1: case 0:
            printf("F\n"); break;
    }
    
  11. int m, n;
    
    printf("Enter a two-digit number: ");
    scanf("%1d%1d", &m, &n);
    printf("Your entered the number ");
    switch (m) {
        case 1:
            switch (n) {
                case 0: printf("ten\n"); break;
                case 1: printf("eleven\n"); break;
                case 2: printf("twelve\n"); break;
                case 3: printf("thirteen\n"); break;
                case 4: printf("fourteen\n"); break;
                case 5: printf("fifteen\n"); break;
                case 6: printf("sixteen\n"); break;
                case 7: printf("seventeen\n"); break;
                case 8: printf("eighteen\n"); break;
                case 9: printf("nineteen\n"); break;
            }
            return 0;
        case 2: printf("twenty"); break;
        case 3: printf("thirty"); break;
        case 4: printf("forty"); break;
        case 5: printf("fifty"); break;
        case 6: printf("sixty"); break;
        case 7: printf("seventy"); break;
        case 8: printf("eighty"); break;
        case 9: printf("ninety"); break;
    }
    switch (n) {
        case 0: break;
        case 1: printf("-one\n"); break;
        case 2: printf("-two\n"); break;
        case 3: printf("-three\n"); break;
        case 4: printf("-four\n"); break;
        case 5: printf("-five\n"); break;
        case 6: printf("-six\n"); break;
        case 7: printf("-seven\n"); break;
        case 8: printf("-eight\n"); break;
        case 9: printf("-nine\n"); break;
    }
    
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-31 15:16:03  更:2021-08-31 15:17:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 16:29:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码