【C语言】学习笔记

文章目录

  • 【C语言】学习笔记
    • 第5章 选择语句
      • 编程题

第5章 选择语句

根据对语句执行顺序的影响,C 语言 的其余语句大多属于以下 3 类。

  • 选择语句(selection statement)。if 语句和 switch 语句允许程序在一组可选项中选择一 条特定的执行路径。
  • 重复语句(iteration statement)。while 语句、do 语句和 for 语句支持重复(循环) 操作。
  • 跳转语句(jump statement)。break 语句、continue 语句和 goto 语句导致无条件地跳 转到程序中的某个位置。(return 语句也属于此类。)

C 语言还有其他两类语句,一类是复合语句(把几条语句组合成一条语句),一类是空语句 (不执行任何操作)。

编程题

【1】编写一个程序,确定一个数的位数:

Enter a number: 374
The number 374 has 3 digits

假设输入的数最多不超过 4 位。提示:利用 if 语句进行数的判定。例如,如果数在 0 和 9 之间,那 么位数为 1;如果数在 10 和 99 之间,那么位数为 2。

#include<stdio.h>int main(void) {int digit = 0;printf("Enter a number:");scanf("%d", &digit);if (digit >= 0 && digit <= 9) {printf("The number %d has 1 digits.", digit);}else if (digit >= 10 && digit <= 99) {printf("The number %d has 2 digits.", digit);}else if (digit >= 100 && digit <= 999) {printf("The number %d has 3 digits.", digit);}else {printf("The number %d has 4 digits.", digit);}return 0;
}

【2】编写一个程序,要求用户输入 24 小时制的时间,然后显示 12 小时制的格式:

Enter a 24-hour time: 21:11
Equivalent 12-hour time: 9:11 PM

注意不要把 12:00 显示成 0:00。

#include<stdio.h>int main(void) {int hour = 0, minute = 0;printf("Enter a 24-hour time:");scanf("%d:%d", &hour, &minute);if (hour > 12 && hour <= 24) {printf("Equivalent 12-hour time: %d:%02d PM", hour % 12, minute);}else {printf("Equivalent 12-hour time: %d:%02d AM", hour, minute);}return 0;
}

【3】修改 5.2 节的 broker.c 程序,做出下面两种改变。

(a) 不再直接输入交易额,而是要求用户输入股票的数量和每股的价格。

(b) 增加语句用来计算经纪人竞争对手的佣金(少于 2000 股时佣金为每股 33 美元+3 美分,2000 股或更 多股时佣金为每股 33 美元+2美分)。在显示原有经纪人佣金的同时,也显示出竞争对手的佣金。

#include<stdio.h>int main(void) {float commission, value, price, ccommission;int amount;printf("Enter amount and the price of stock: ");scanf("%d %f", &amount, &price);value = amount * price;if (value < 2500.00f)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;elsecommission = 255.00f + .0009f * value;if (commission < 39.00f)commission = 39.00f;printf("Commission: $%.2f\n", commission);if (amount < 2000) {ccommission = 33 + 0.03 * amount;}else {ccommission = 33 + 0.02 * amount;}printf("Commission of Others: $%.2f\n", ccommission);return 0;
}

【4】下表中展示了用于测量风力的蒲福风级的简化版本。

编写一个程序,要求用户输入风速(海里/小时),然后显示相应的描述。

#include<stdio.h>int main(void) {float windspeed;printf("Enter the wind speed (n mile/hour): ");scanf("%f", &windspeed);if (windspeed < 1.0f) {printf("Calm");}else if (windspeed < 3.0f) {printf("Light air");}else if (windspeed < 27.0f) {printf("Breeze");}else if (windspeed < 47.0f) {printf("Gale");}else if (windspeed < 63.0f) {printf("Storm");}else {printf("Hurricane");}return 0;
}

【5】在美国的某个州,单身居民需要缴纳下表中列出的所得税。

编写一个程序,要求用户输入应纳税所得额,然后显示税金。

#include<stdio.h>int main(void) {float income, tax;printf("Enter the taxable income: ");scanf("%f", &income);if (income < 750.0f) {tax = income * 0.01;}else if (income < 2250.0f) {tax = 7.5 + (income - 750) * 0.02;}else if (income < 3750.0f) {tax = 37.5 + (income - 2250) * 0.03;}else if (income < 5250.0f) {tax = 82.5 + (income - 3750) * 0.04;}else if (income < 7000.0f) {tax = 142.5 + (income - 5250) * 0.05;}else {tax = 230 + (income - 7000) * 0.06;}printf("The tax is %.2f", tax);return 0;
}

【6】修改 4.1 节的 upc.c 程序,使其可以检测 UPC 的有效性。在用户输入 UPC 后,程序将显示 VALID 或 NOT VALID。

#include<stdio.h>int main(void) {int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total, check;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 Check digits:");scanf("%1d", &check);first_sum = d + i2 + i4 + j1 + j3 + j5;second_sum = i1 + i3 + i5 + j2 + j4;total = 3 * first_sum + second_sum;if (check == (9 - ((total - 1) % 10))) {printf("VALID\n");}else {printf("NOT VALID\n");}return 0;
}

【7】编写一个程序,从用户输入的 4 个整数中找出最大值和最小值:

Enter four integers: 21 43 10 35
Largest: 43
Smallest: 10

要求尽可能少用 if 语句。提示:4 条 if 语句就足够了。

#include<stdio.h>int main(void) {int num1, num2, num3, num4;int max1, max2, min1, min2;printf("Enter four integers: ");scanf("%d %d %d %d", &num1, &num2, &num3, &num4);if (num1 > num2) {max1 = num1;min1 = num2;}else {max1 = num2;min1 = num1;}if (num3 > num4) {max2 = num3;min2 = num4;}else {max2 = num4;min2 = num3;}printf("Largest: %d\n", max1 > max2 ? max1 : max2);printf("Smallest: %d\n", min1 < min2 ? min1 : min2);return 0;
}

【8】下表给出了从一个城市到另一个城市的每日航班信息。

编写一个程序,要求用户输入一个时间(用 24 小时制的时分表示)。程序选择起飞时间与用户输入 最接近的航班,显示出相应的起飞时间和抵达时间。

Enter a 24-hour time: 13:15
Closest departure time is 12:47 p.m., arriving at 3:00 p.m.

提示:把输入用从午夜开始的分钟数表示。将这个时间与表格里(也用从午夜开始的分钟数表示) 的起飞时间相比。例如,13:15 从午夜开始是 13×60+15 = 795 分钟,与下午 12:47(从午夜开始是 767 分钟)最接近。

#include<stdio.h>int main(void) {int hour, minute;int input_minutes;printf("Enter a 24-hour time: ");scanf("%d:%d", &hour, &minute);input_minutes = hour * 60 + minute;if (input_minutes < (615) / 2) {printf("Closet departure time is 9:45 PM, arriving at 11:58 PM.");}else if (input_minutes < (8 * 60 + 9 * 60 + 43) / 2) {printf("Closet departure time is 9:43 AM, arriving at 10:16 AM.");}else if (input_minutes < (9 * 60 + 43 + 11 * 60 + 19) / 2) {printf("Closet departure time is 11:19 AM, arriving at 1:31 PM.");}else if (input_minutes < (12 * 60 + 47 + 14 * 60) / 2) {printf("Closet departure time is 12:47 PM, arriving at 3:00 PM.");}else if (input_minutes < (14 * 60 + 15 * 60 + 45) / 2) {printf("Closet departure time is 2:00 PM, arriving at 4:08 PM.");}else if (input_minutes < (15 * 60 + 45 + 19 * 60) / 2) {printf("Closet departure time is 3:45 PM, arriving at 5:55 PM.");}else if (input_minutes < (19 * 60 + 21 * 60 + 45) / 2) {printf("Closet departure time is 7:00 PM, arriving at 9:20 PM.");}else {printf("Closet departure time is 9:45 PM, arriving at 11:58 PM.");}return 0;
}

【9】编写一个程序,提示用户输入两个日期,然后显示哪一个日期更早:

Enter first date (mm/dd/yy): 3/6/08
Enter second date (mm/dd/yy): 5/17/07
5/17/07 is earlier than 3/6/08
#include<stdio.h>int main(void) {int fm, fd, fy;int sm, sd, sy;printf("Enter first date (mm/dd/yy): ");scanf("%d/%d/%d", &fm, &fd, &fy);printf("Enter second date (mm/dd/yy): ");scanf("%d/%d/%d", &sm, &sd, &sy);if (fy > sy || (fy == sy && fm > sm) || (fy == sy && fm == sm && fd > sd)) {printf("%d/%d/%02d is earlier than %d/%d/%02d", sm, sd, sy, fm, fd, fy);}else {printf("%d/%d/%02d is earlier than %d/%d/%02d", fm, fd, fy, sm, sd, sy);}return 0;
}

【10】利用 switch 语句编写一个程序,把用数字表示的成绩转换为字母表示的等级。

Enter numerical grade: 84
Letter grade: B

使用下面的等级评定规则:A 为 90~100,B 为 80~89,C 为 70~79,D 为 60~69,F 为 0~59。如 果成绩高于 100 或低于 0,则显示出错消息。提示:把成绩拆分成 2 个数字,然后使用 switch 语句 判定十位上的数字。

#include<stdio.h>int main(void) {int score;int digit;printf("Enter numerical grade: ");scanf("%d", &score);if (score > 100 || score < 0) {printf("Error input score, exit.");return 0;}digit = score / 10;switch (digit) {case 10:case 9:printf("Letter grade: A");break;case 8:printf("Letter grade: B");break;case 7:printf("Letter grade: C");break;case 6:printf("Letter grade: D");break;default:printf("Letter grade: F");}return 0;
}

【11】编写一个程序,要求用户输入一个两位数,然后显示该数的英文单词:

Enter a two-digit number: 45
You entered the number forty-five.

提示:把数分解为两个数字。用一个 switch 语句显示第一位数字对应的单词(“twenty”“thirty”等), 用第二个 switch 语句显示第二位数字对应的单词。不要忘记 11~19 需要特殊处理。

#include<stdio.h>int main(void) {int number;int one_digit, two_digit;printf("Enter a two-digit number: ");scanf("%d", &number);printf("You entered the number ");if (number >= 20 || number < 10) {two_digit = number / 10;one_digit = number % 10;switch (two_digit) {case 9:printf("ninety-");break;case 8:printf("eighty-");break;case 7:printf("seventy-");break;case 6:printf("sixty-");break;case 5:printf("fifty-");break;case 4:printf("forty-");break;case 3:printf("thirty-");break;case 2:printf("twenty-");break;default:printf("......");}switch (one_digit) {case 9:printf("nine");break;case 8:printf("eight");break;case 7:printf("seven");break;case 6:printf("six");break;case 5:printf("five");break;case 4:printf("four");break;case 3:printf("three");break;case 2:printf("two");break;case 1:printf("one");break;default:printf("......");}}else {switch (number) {case 11:printf("eleven");break;case 12:printf("twelve");break;case 13:printf("thirteen");break;case 14:printf("fourteen");break;case 15:printf("fifteen");break;case 16:printf("sixteen");break;case 17:printf("seventeen");break;case 18:printf("eighteen");break;case 19:printf("nineteen");break;}}return 0;
}

【C语言】学习笔记 第5章 选择语句 编程题相关推荐

  1. 《Python编程:从入门到实战》(第2版)学习笔记 第5章 if语句

    [写在前面]为进一步提高自己的python代码能力,打算把几本经典书籍重新过一遍,形成系统的知识体系,同时适当记录一些学习笔记,我尽量及时更新!先从经典的<Python编程:从入门到实战> ...

  2. java学习笔记-第七章:面向对象编程(基础部分)

    第七章:面向对象编程(基础部分) 总体内容 类与对象 引出类与对象 类与对象概述 类与对象的关系示意图 属性概念及其细节 类与对象快速入门案例 对象内存布局 类与对象内存分配机制 引申:java内存的 ...

  3. go var type 互转_Go语言学习笔记(第九章) 结构体

    Go语言基础之结构体 Go语言中没有"类"的概念,也不支持"类"的继承等面向对象的概念.Go 通过类型别名(alias types)和结构体的形式支持用户自定义 ...

  4. R语言学习笔记-机器学习1-3章

    在折腾完爬虫还有一些感兴趣的内容后,我最近在看用R语言进行简单机器学习的知识,主要参考了<机器学习-实用案例解析>这本书. 这本书是目前市面少有的,纯粹以R语言为基础讲解的机器学习知识,书 ...

  5. 《Go语言圣经》学习笔记 第十一章 测试

    <Go语言圣经>学习笔记 第十一章 测试 目录 go test 测试函数 测试覆盖率 基准测试 剖析 示例函数 注:学习<Go语言圣经>笔记,PDF点击下载,建议看书. Go语 ...

  6. 《Go语言圣经》学习笔记 第六章 方法

    <Go语言圣经>学习笔记 第六章 方法 目录 方法声明 基于指针对象的方法 通过嵌入结构体来扩展类型 方法值和方法表达式 示例:Bit数组 封装 注:学习<Go语言圣经>笔记, ...

  7. 《Go语言圣经》学习笔记 第五章函数

    <Go语言圣经>学习笔记 第五章 函数 目录 函数声明 递归 多返回值 匿名函数 可变参数 Deferred函数 Panic异常 Recover捕获异常 注:学习<Go语言圣经> ...

  8. 《Go语言圣经》学习笔记 第四章 复合数据类型

    <Go语言圣经>学习笔记 第四章 复合数据类型 目录 数组 Slice Map 结构体 JSON 文本和HTML模板 注:学习<Go语言圣经>笔记,PDF点击下载,建议看书. ...

  9. 《Go语言圣经》学习笔记 第三章 基础数据类型

    <Go语言圣经>学习笔记 第三章 基础数据类型 目录 整型 浮点数 复数 布尔型 字符串 常量 注:学习<Go语言圣经>笔记,PDF点击下载,建议看书. Go语言小白学习笔记, ...

  10. R语言学习笔记——入门篇:第一章-R语言介绍

    R语言 R语言学习笔记--入门篇:第一章-R语言介绍 文章目录 R语言 一.R语言简介 1.1.R语言的应用方向 1.2.R语言的特点 二.R软件的安装 2.1.Windows/Mac 2.2.Lin ...

最新文章

  1. 任正非:我若贪生怕死,何来让你们去英勇奋斗
  2. python中循环结构分有,python常见循环结构有哪些
  3. 网络(10)-HTTPS证书申请及配置
  4. CUDA精进之路(零):CUDA开篇
  5. 19 岁就在南大读博的“00 后”女生:这是我的学习经历...
  6. hibernate 反向生实体类 and 为什么老是多一个id
  7. android 插件开发 过时,Android Sutdio ( Intelij ) 插件开发
  8. 两表关联去重查询全部数据
  9. POI库读取xlsx和xls格式excel以及解决安卓上的适配
  10. mysql查询姓名第二个字_第二篇:MySQL中SELECT查询语句总结
  11. Java实现验证码制作
  12. 推荐四个不错的公众号
  13. 有关计算机的英语作文一千字,关于网络的英语作文
  14. 我的jQuery学习之路_笔记(五)
  15. 实验3 微程序控制器实验
  16. 怎么把画好的流程图插入到Word中?
  17. maya Xgen导入UE4中,UE4毛发动力学
  18. java漏斗代码_集算示例:10 行代码解决漏斗转换计算
  19. python数据分析大作业-python大作业实验报告
  20. 享元模式(Java)

热门文章

  1. 夫妻间爱的细节(转)
  2. 使用图表分析2020北京积分落户的数据
  3. 使用httpclient模拟登陆
  4. .editorconfig的配置说明
  5. qt5.9支持linux,科学网—Qt5.9发布了,Qt 5.9长期支持版之性能改善 - 陈德鹏的博文...
  6. 目标识别笔记:鸢尾花数据集决策树/随机森林MATLAB仿真
  7. 合肥医药区域营销经理执行力提升秘籍
  8. 【博弈】非完全信息博弈基础与CFR算法简介
  9. 8051单片机-汇编实现简易红绿灯
  10. 树的不同形态(源自:小红书2019校园招聘笔试)