在過去的五個小時里,我一直在試圖理解為什么它不能正常作業。
該問題明確要求使用 switch() 而不是 if-else(或類似的)來計算鍵入的文本中的單詞、行和字符的數量。然后使用 Ctrl D 或 Ctrl Z 退出程式。
在這里,我通過計算當前鍵入的輸入是否為空格的不同情況來解構計數,并由此根據前一個字母判斷是否有理由將其計為額外的單詞、字符和/或行。( input = punctuation, previous input= character --> word count 加 1,character count 加 1;如果 input = newline 和 previous input !=whitespace --> line counter 加 1 word counter 加 1,等等。)
我的代碼如下:
int main() {
int letter = 0, prev_letter = 0, num_char = 0, num_words = 0, num_lines = 0;
printf("User, please provide any text you wish using letters, spaces, tabs, "
"and enter. \n When done, enter Ctrl D or Ctrl Z on your keyboard.");
while ((letter = getchar()) != 4 &&
letter != 26) // In ASCII, Ctrl D is 4, and Ctrl Z is 26
{
switch (isspace(letter)) {
case 0: // False = is not a whitespace
{
switch (
isalpha(prev_letter)) // checking to see if alphanumeric input or not
{
case 1:
switch (ispunct(letter)) {
case 1:
num_words ;
num_char ; // Punctuation considered as characters in this particular
// sub-exercise.
break;
}
break;
case 0:
num_char ;
break; // All other cases are just another character added in this case
// 0 (Not whitespace)
}
} break;
case 1: {
switch (letter) {
case 9: // 9 =Horizontal tab
{
switch (isspace(prev_letter)) {
case 0:
num_words ; // Assuming if not whitespace, then punctuation or
// character.
break;
default:
break;
}
} break;
case 32: // 32 = Space
{
switch (isspace(prev_letter)) {
case 0:
num_words ; // Assuming if not whitespace, then punctuation or
// character.
break;
default:
break;
}
} break;
case 13: // 13 = Carriage return
{
switch (isspace(prev_letter)) {
case 0:
num_words ;
num_lines ;
break;
default:
num_lines ;
}
} break;
case 10: // 13 = Line Feed
{
switch (isspace(prev_letter)) {
case 0:
num_words ;
num_lines ;
break;
default:
num_lines ;
}
} break;
default:
printf("Test2");
}
} break;
default:
break;
}
prev_letter = letter;
}
printf("Number of characters is: %d. \n", num_char);
printf("Number of words is: %d. \n", num_words);
printf("Number of lines is: %d. \n", num_lines);
return 0;
}```
It seems like isalpha(), ispunct(), isalnum() are not feeding properly my cases.
I have tried breaking it down to individual cases but when inputting text with tabs, spaces, and alphanumeric inputs, it fails to count words, characters, and lines properly.
What am I not seeing properly? Any pointers greatly appreciated.
uj5u.com熱心網友回復:
這似乎是一項奇怪的任務。也許你誤會了,但假設你沒有,讓我看看我是否能弄清楚。
switch將一個變數與多個值進行比較,因此這意味著第一部分是從多個測驗函式中確定一個值。
的一個常見用途switch是檢查enum值,因此您可以從定義一個enum您需要的值開始。您在代碼中使用isalpha()、ispunct()和isspace(),因此我將定義這些enum值。
enum chartype {
IS_ALPHA,
IS_PUNCT,
IS_SPACE,
IS_UNKNOWN
};
if您可以在不使用運算子的情況下選擇列舉值? :。
enum chartype letter_chartype =
isalpha(letter) ? IS_ALPHA
: ispunct(letter) ? IS_PUNCT
: isspace(letter) ? IS_SPACE
: IS_UNKNOWN;
這使您可以為每種字符型別使用一個開關。
switch(letter_chartype) {
case IS_ALPHA:
...
break;
case IS_PUNCT:
...
break;
case IS_SPACE:
...
break;
default:
...
break;
}
這不會為您完成作業,但我希望這能幫助您指明方向。我假設你涵蓋了? :運營商。如果不是,你可能不得不做一些更長、更棘手或更愚蠢的事情(愚蠢往往看起來很聰明,要小心)。
uj5u.com熱心網友回復:
isalpha如果引數不是字母,則回傳零,如果是,則回傳零。
使用if陳述句不switch測驗條件。switch用于邏輯資料是不合邏輯或不切實際的。
使用函式并使用 switch ... case 撰寫您自己的這些函式的實作
示例(使用 GCC 擴展大小寫范圍 - 為了便于攜帶,您需要為每個字母或數字設定單獨的大小寫 [如myispace])
int myisalpha(unsigned char x)
{
switch(x)
{
case 'a' ... 'z':
case 'A' ... 'Z':
return 1;
}
return 0;
}
int myisdigit(unsigned char x)
{
switch(x)
{
case '0' ... '9':
return 1;
}
return 0;
}
int myisspace(unsigned char x)
{
switch(x)
{
case ' ':
case '\n':
case '\r':
case '\t':
case '\v':
case '\f':
return 1;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/534278.html
標籤:C循环开关语句空间
上一篇:如何讓這個計算器只接受前兩個輸入中的數字并回圈直到它們正確
下一篇:如何計算加權回圈?
