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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: could not chdir to h
- 下一篇: 关于虚拟机的IP地址经常改变问题的解法