Programming abstractions in C阅读笔记:p130-p131
《Programming Abstractions In C》学习第52天,p130-p131,总结如下:
一、技术总结
1. pig latin game
通过pig latin game掌握字符复制,指针遍历等操作。
/*
* 输入:字符串,这里采用书中坐着自定义的getline函数
*/
#include <stdio.h>
#include <string.h>
#include "simpio.h"
#define MaxWord 1000
static _Bool IsVowel(char ch); // 书中p34, if适用于非此即彼的两种选择(two-way);如果有多个,那么就使用switch。
static void PigLatin(char *word, char buffer[], int bufferSize);
static char *FindFirstVowel(char *word); // *表示函数FindFirstVowel返回一个指向char的指针
int main() {
char *word;
char translationBuffer[MaxWord + 1];
printf("Enter a word: ");
word = GetLine();
PigLatin(word, translationBuffer, MaxWord + 1);
printf("Pig Latin: %s\n", translationBuffer);
}
/*
* Function:IsVowel
* Usage: isVowel = IsVowel(character)
* -----------------------------------
* 该函数判断字符是否是元音字母,如果是,返回True,否则返回False。
*/
_Bool IsVowel(char ch) {
switch (ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return TRUE;
default:
return FALSE;
}
}
/*
* Function: PigLatin
* Usage: PigLatin(word, buffer, bufferSize);
* ------------------------------------------
* This function translate a word from English to Pig Latin. The
* translated word is written into the array buffer, which has an
* allocated size of bufferSize. The code checks for buffer
* overflow and generate an error if it occurs.
*/
static void PigLatin(char *word, char buffer[], int bufferSize) {
char *vp;
int wordLength;
vp = FindFirstVowel(word);
wordLength = strlen(word);
if (vp == word) {
wordLength += 3;
} else if (vp != NULL) {
wordLength += 2;
}
if (wordLength >= bufferSize) {
Error("Buffer overflow");
}
if (vp == NULL) { // 单词中不存在元音字母:不做任何修改
strcpy(buffer, word);
} else if (vp == word) { // 单词以元音字母开头: 在单词末尾添加way(示例:any > anyway)
strcpy(buffer, word);
strcat(buffer, "way");
} else {
// 单词以辅音字母开头: (1)将辅音字母移动到单词尾部,直到第一个字母是元音字母。
// (2)移动完成后,在单词尾部添加ay(示例:trash > ashtray)
strcpy(buffer, vp);
strncat(buffer, word, vp - word);
strcat(buffer, "ay");
}
}
/*
* FindFirstVowel: 找出单词中的第一个元音字母
*/
static char *FindFirstVowel(char *word) {
char *cp; // 将原来的指针赋值给新的指针,避免原来的指针被修改
// 遍历指针
for (cp = word; *cp != '\0'; cp++) { // 注意:在这里*cp表示的是值
if (IsVowel(*cp)) {
return cp; // 注意:cp++移动之后,cp指向的地址改变了
}
}
return NULL; // 如果找不到,则返回空指针(NULL)
}
完整代码见:https://github.com/codists/Programming-Abstractions-In-C/tree/main/chapter3/piglatingame
二、英语总结
1.check用法分析
答:p130,“The code checks for buffer overflow and gennerates an error if it occurs.”,记忆中check是及物动词,这里为何会跟for?check既可以用作及物动词(vi),也可以用作不及物动词(vt),意思是"to make certain that sth is corret by examinming it.”。
(1)vt.对xxx进行检查。示例:Customs stopped us and checked (= searched) our bags for alcohol and cigarettes(海关拦住了我们,检查我们的包里有没有烟酒)。
(2)vi.例如书中的用法。其实也可以改成及物动词的用法,The code check string length and buffersize for buffer overflow and gennerates an error if it occurs.代码对字符串的长度和buffer的大小进行检查(即判断字符串的长度与buffersize的大小关系),看是否存在缓冲区溢出的情况,如果存在,就报错。
2.up to 什么意思?
答:p129, "If the word begins with the consonant, the function extracts the string of consonant up to the first vowel, moves that collection of consonant to the end of the word"。
“up to”在这里的用法是“prep. until(直到)”,这里是两个词式的介词。
3.concern 什么意思?
答:p131, "Almost half the code in the function, however, is concerned with making sure that the buffer does not overflow.",concern在这里的意思是“vt. about”。
三、参考资料
1. 编程
(1)Eric S.Roberts,《Programming Abstractions in C》:https://book.douban.com/subject/2003414
2. 英语
(1)Etymology Dictionary:https://www.etymonline.com
(2) Cambridage Dictionary:https://dictionary.cambridge.org
欢迎搜索及关注:编程人(a_codists)
Programming abstractions in C阅读笔记:p130-p131的更多相关文章
- Mongodb Manual阅读笔记:CH3 数据模型(Data Models)
3数据模型(Data Models) Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mon ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画 学习目标 熟悉蒙皮动画的术语: 学习网格层级变换 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十一章:模板测试
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十一章:模板测试 代码工程地址: https://github.co ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制 代码工程地址: https://gi ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第四章:Direct 3D初始化
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第四章:Direct 3D初始化 学习目标 对Direct 3D编程在 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二章:矩阵代数
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二章:矩阵代数 学习目标: 理解矩阵和与它相关的运算: 理解矩阵的乘 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第一章:向量代数
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第一章:向量代数 学习目标: 学习如何使用几何学和数字描述 Vecto ...
- 阅读笔记 1 火球 UML大战需求分析
伴随着七天国庆的结束,紧张的学习生活也开始了,首先声明,阅读笔记随着我不断地阅读进度会慢慢更新,而不是一次性的写完,所以会重复的编辑.对于我选的这本 <火球 UML大战需求分析>,首先 ...
- [阅读笔记]Software optimization resources
http://www.agner.org/optimize/#manuals 阅读笔记Optimizing software in C++ 7. The efficiency of differe ...
- 《uml大战需求分析》阅读笔记05
<uml大战需求分析>阅读笔记05 这次我主要阅读了这本书的第九十章,通过看这章的知识了解了不少的知识开发某系统的重要前提是:这个系统有谁在用?这些人通过这个系统能做什么事? 一般搞清楚这 ...
随机推荐
- vue全家桶进阶之路9:常用指令
以下是一些常见的指令: v-bind - 用于绑定一个或多个属性到组件或 HTML 元素上. v-model - 用于双向绑定一个表单元素或组件的值到数据模型上. v-for - 用于循环遍历一个数组 ...
- MassTransit实现Saga模式概览
原文地址:Saga Overview 编排一系列事件的能力是一个强大的功能,而MassTransit使这成为可能. saga是由协调器管理的长期事务.saga是由事件发起的,saga编排事件,saga ...
- Python网页开发神器fac 0.2.9、fuc 0.1.29新版本更新内容介绍
fac项目地址:https://github.com/CNFeffery/feffery-antd-components fuc项目地址:https://github.com/CNFeffery/fe ...
- Python异步编程之web框架 异步vs同步 数据库IO任务压测对比
测试基本信息 主题:比较异步框架和同步框架在数据库IO操作的性能差异 python版本:python 3.8 数据库:mysql 8.0.27 (docker部署) 压测工具:locust web框架 ...
- shell编程-发送消息
需求:利用 Linux 自带的 mesg 和 write 工具,编写一个向用户快速发送消息的脚本,输入用户名作为第一个参数,消息内容为第二个参数.脚本需要检测用户是否登录,是否打开消息功能,以及当前发 ...
- Framework 中使用 Toolkit.Mvvm 的生成器功能
.NET Standard是.NET APIs的正式规范,可在多个.NET实现中使用..NET Standard的动机是为了在.NET生态系统中建立更大的统一性..NET 5及更高版本采用了不同的方法 ...
- 浅谈 thinkphp composer 扩展包加载原理
浅谈 thinkphp composer 扩展包加载原理 本文将介绍 ThinkPHP 中 Composer 扩展包的加载原理,帮助读者更好地理解和应用该功能. 前言 如题,今天感觉好久没有更新博客了 ...
- 逍遥自在学C语言 | 指针的基础用法
前言 在C语言中,指针是一项重要的概念,它允许我们直接访问和操作内存地址. 可以说,指针是C语言一大优势.用得好,你写程序如同赵子龙百万军中取上将首级:用得不好,则各种问题层出不穷,有种双拳难敌四手的 ...
- .NET周报 【6月第3期 2023-06-18】
国内文章 揭秘 Task.Wait https://www.cnblogs.com/eventhorizon/p/17481757.html Task.Wait 是 Task 的一个实例方法,用于等待 ...
- 使用React和Redux进行前端应用程序:现代Web应用程序框架
目录 标题:<27. 使用 React 和 Redux 进行前端应用程序:现代 Web 应用程序框架> 背景介绍: 随着现代 Web 应用程序的发展,前端开发人员需要一种高效的.灵活的框架 ...