我一直在嘗試從輸入中獲取變數。所以我得到每個單詞,并檢查它是否是保留陣列的一部分。如果它是保留陣列的一部分,我會從輸入中獲取下一個單詞,它是一個變數。
下面是保留的陣列:
char *reserved[] = {
"char", NULL
};
該程式有兩個輸入。一個帶type.h,另一個帶ctype.h。第一個輸入有效,但第二個無效:
#include <stdio.h> /*works*/
#include <type.h>
char *reserved[] = {
#include <stdio.h> /* doesn't work */
#include <ctype.h>
char *reserved[] = {
因此,函式從輸入中獲取一個單詞,并檢查它是否為保留字。如果是,則將其添加到變數陣列中,但如果不是保留字,則再次運行該函式:
for(w = word, r = reserved; *r!= NULL; w=word, r ) /* Go to the next reserved word*/
for (; *w == **r; (*r) , w ) /* check if the reserved word matches the word */
if (*w == '\0') { /* if it does, what follows is a variable */
getchar(); /* get the space */
for (v = var, (c=getchar()) =='*'? c = getchar(): c; c != ';' && c != '['; *v = '\0', c = getchar())
*v = c; /* add the variable to the variable array */
return var; /* return start of variable array */
}
if ((isalpha(*var)) == 0) { /* if variable array is empty, recursively run the function */
var = getvar(var, reserved);
}
*r當程式檢查輸入時增加ctype,因為ctype的第一個字符和char保留字的第一個字符匹配。但是單詞不匹配,這次函式再次運行檢查 input char。r當函式被再次呼叫時,指標被設定為保留函式的開始:
for(w = word, r = reserved; *r!= NULL; w=word, r )
所以**r應指向字符c的的char保留字,但它指向h的char。
你能解釋一下,為什么指標沒有指向開始,即使我只是將它設定為指向陣列的開始,當我說r = reserved.
如果您想通過除錯器運行它,我已經附上了下面的代碼。
#include <stdio.h>
#include <ctype.h>
char *getvar(char *var, char *reserved[]);
int main() {
char var[100]; /* stored a variable */
char *v; /* pointer to the start of variable array variable */
char *reserved[] = {
"char", NULL
};
v = getvar(var, reserved); /* returns the start of a variable array */
printf("%s", v); /* prints the variable*/
return 0;
}
char *getvar(char *var, char *reserved[])
{
int c; /* a character*/
char *v, *w, **r; /* a variable, word, and a reserved word character*/
char word[100]; /* an array with a word */
while(((isalpha(c = getchar())) == 0) && c != EOF ); /* skip till you get to a name */
for(w = word; c != ' ' && c!='\n' && c!='\t' && c != EOF; c=getchar(), *w = '\0')
*w = c; /* add name to a word */
for(w = word, r = reserved; *r!= NULL; w=word, r ) /* Go to the next reserved word*/
for (; *w == **r; (*r) , w ) /* check if the reserved word matches the word */
if (*w == '\0') { /* if it does, what follows is a variable */
getchar(); /* get the space */
for (v = var, (c=getchar()) =='*'? c = getchar(): c; c != ';' && c != '['; *v = '\0', c = getchar())
*v = c; /* add the variable to the variable array */
return var; /* return start of variable array */
}
if ((isalpha(*var)) == 0) { /* if variable array is empty, recursively run the function */
var = getvar(var, reserved);
}
return var; /* return start of variable array */
}
uj5u.com熱心網友回復:
你的代碼邏輯不必要的復雜,這使得這個函式很難除錯......你的程式邏輯p中也有很多錯誤,導致無法按原樣進行除錯。我的意思是沒有辦法修復這個指標錯誤(或錯誤)會讓這個程式立即作業......
您要遵守的第一條規則是,程式是一組簡單演算法,分為多個函式,您可以將這些函陣列合成更復雜的演算法。
例如:
// You want to write functions that only do one thing at a time, this will
// make both your life and the reader's life a much more pleasant experience.
//
// This function reads a word from standard input
// params:
// - word: work buffer to hold result.
// - max_length: number of bytes in work buffer.
// returns:
// - the address of buffer 'word'
// - NULL on error
//
const char* read_input(char* word, int max_length)
{
// get a word:
// 1. skip until first alpha char, if EOF function FAILS (used to exit program)
// 2. read until first non-alpha char (EOF implicitely included)
char* w = word;
while (!isalpha(*w = getchar())
if (*w == EOF)
return NULL;
while (isalpha(*( w) = getchar())
if (w >= word max_length - 1)
return NULL; // We're overflowing our storage space !!!
// You may want to print an error message here.
* w = 0; // NULL terminate the string.
return word; // we're successful
}
// this function looks for word pointed to by 'word' in
// NULL terminated array 'keywords'
//
// returns:
// - if successful returns value of pointer 'word'.
// - if failed, returns NULL
//
const char* find_keyword(const char* word, const char** keywords)
{
// Loop through array
// compare strings
// return immediately when found
while (*keywords)
{
if (strcmp(word, *keywords) == 0)
return word;
keywords;
}
return NULL;
}
int main()
{
char word[100];
const char* w, *kw;
const char *reserved[] = { "char", NULL };
// 1. get next keyword from input, if NULL, exit.
// 2. check if it is a reserved keyword.
// 3. give some visual feedback.
while ((w = read_input(word, sizeof(word))) != NULL)
{
kw = find_keyword(w, reserved);
if (kw)
printf("%s\n", kw);
else
printf("keyword \'%s\' not found!\n", w);
}
return 0;
}
請注意,分散關注使代碼更易于撰寫和除錯。它還消除了不必要的復雜且容易出錯的指標演算法。
注意:此代碼僅用于說明。我沒有編譯它,沒有運行它,但它應該與你想要實作的相當接近。如果由于某種原因不能使用 strcmp,比較 2 個字串是一個非常容易和直接撰寫的函式,只要將它與代碼的其余部分隔離即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/360397.html
上一篇:無法在nextjs中匯入模塊
