Programming abstractions in C阅读笔记:p130-p131
《Programming Abstractions In C》學(xué)習(xí)第52天,p130-p131,總結(jié)如下:
一、技術(shù)總結(jié)
1. pig latin game
通過pig latin game掌握字符復(fù)制,指針遍歷等操作。
/*
* 輸入:字符串,這里采用書中坐著自定義的getline函數(shù)
*/
#include <stdio.h>
#include <string.h>
#include "simpio.h"
#define MaxWord 1000
static _Bool IsVowel(char ch); // 書中p34, if適用于非此即彼的兩種選擇(two-way);如果有多個(gè),那么就使用switch。
static void PigLatin(char *word, char buffer[], int bufferSize);
static char *FindFirstVowel(char *word); // *表示函數(shù)FindFirstVowel返回一個(gè)指向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)
* -----------------------------------
* 該函數(shù)判斷字符是否是元音字母,如果是,返回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)將輔音字母移動(dòng)到單詞尾部,直到第一個(gè)字母是元音字母。
// (2)移動(dòng)完成后,在單詞尾部添加ay(示例:trash > ashtray)
strcpy(buffer, vp);
strncat(buffer, word, vp - word);
strcat(buffer, "ay");
}
}
/*
* FindFirstVowel: 找出單詞中的第一個(gè)元音字母
*/
static char *FindFirstVowel(char *word) {
char *cp; // 將原來的指針賦值給新的指針,避免原來的指針被修改
// 遍歷指針
for (cp = word; *cp != '\0'; cp++) { // 注意:在這里*cp表示的是值
if (IsVowel(*cp)) {
return cp; // 注意:cp++移動(dòng)之后,cp指向的地址改變了
}
}
return NULL; // 如果找不到,則返回空指針(NULL)
}
完整代碼見:https://github.com/codists/Programming-Abstractions-In-C/tree/main/chapter3/piglatingame
二、英語總結(jié)
1.check用法分析
答:p130,“The code checks for buffer overflow and gennerates an error if it occurs.”,記憶中check是及物動(dòng)詞,這里為何會(huì)跟for?check既可以用作及物動(dòng)詞(vi),也可以用作不及物動(dòng)詞(vt),意思是"to make certain that sth is corret by examinming it.”。
(1)vt.對(duì)xxx進(jìn)行檢查。示例:Customs stopped us and checked (= searched) our bags for alcohol and cigarettes(海關(guān)攔住了我們,檢查我們的包里有沒有煙酒)。
(2)vi.例如書中的用法。其實(shí)也可以改成及物動(dòng)詞的用法,The code check string length and buffersize for buffer overflow and gennerates an error if it occurs.代碼對(duì)字符串的長度和buffer的大小進(jìn)行檢查(即判斷字符串的長度與buffersize的大小關(guān)系),看是否存在緩沖區(qū)溢出的情況,如果存在,就報(bào)錯(cuò)。
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(直到)”,這里是兩個(gè)詞式的介詞。
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
歡迎搜索及關(guān)注:編程人(a_codists)
總結(jié)
以上是生活随笔為你收集整理的Programming abstractions in C阅读笔记:p130-p131的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: could not chdir to h
- 下一篇: 关于虚拟机的IP地址经常改变问题的解法