2011-06-06 31 views
0
int countConsonant(string str, int consonant) 
{ 
    int length = str.length(); 
    consonant = 0; 

     for (int i = 0; i < length; i++) 
     { 
      if(str[i] == 'b'&& str[i] == 'c' && str[i] == 'd'&& str[i] == 'f' 
       && str[i] == 'g'&& str[i] == 'h'&& str[i] == 'j' && str[i] == 'k'&& str[i] == 'l'&& str[i] == 'm' 
       && str[i] == 'n'&& str[i] == 'p'&& str[i] == 'q'&& str[i] == 'r'&& str[i] == 's'&& str[i] == 't' 
       && str[i] == 'v'&& str[i] == 'w'&& str[i] == 'x'&& str[i] == 'y'&& str[i] == 'z') 

       consonant = consonant + 1; 
     } 
     return consonant; 
} 
+3

大聲笑,你的代碼基本上說,如果str [i]是所有的字母一次的輔音。這是一個超級字符! – Marlon 2011-06-06 02:32:42

回答

8

你接近,但應與邏輯或||檢查,不符合邏輯和&&str[i]根本不能等於兩個不同的東西)。

的C++標準03允許您使用的關鍵字andor - 和notxor等 - 而不是這些神祕的(新程序員)的符號,但這並沒有抓獲廣泛 - 或許是因爲微軟的編譯器在這方面沒有默認爲符合標準 - 大概是爲了避免打破具有這些名稱的變量,函數,類型等的現有客戶端代碼。因此,爲了便於攜帶和簡化,許多圖書館和教科書都避免使用這些關鍵字。

另一種可能更簡潔一些的方法是使用來自<cctype>,然後檢查它不是元音。更快的方法傾向於使用從字符值到bool的數組,但請注意由於有符號字符值或> = 128位非ASCII值而在數組外部建立索引。如果還要考慮大寫/小寫 - 在測試之前,您可能需要在角色上使用tolower()(即char c = tolower(str[i])); if (c == '...)。

其他說明:你的函數應該:

  • 通過const引用(即const std::string& str)接受其std::string參數,以避免調用上下文值的不必要的耗時拷貝到一個獨立的局部變量這個功能。複製不會造成任何實際的功能損害,但是沒有必要。
  • 使consonant是一個局部變量而不是函數參數,因爲任何輸入的值立即用0代替,並且結果由函數返回而不是寫入consonant(這是不可能的,因爲它是按值傳遞的,而不是通過指針/引用傳遞)。
+0

謝謝!!!!我會接受,當它允許我! – 2011-06-06 02:32:39

0

查看正則表達式。它允許您指定要檢查的字符列表。您將檢查字符串中的每個字符與列表並增加計數器。

http://www.johndcook.com/cpp_regex.html

+3

我認爲在OP的作業分配中不允許使用正則表達式。 – 2011-06-06 02:30:48

+0

我不認爲應該用正則表達式攻擊所有東西'... KISS。 – Xeo 2011-06-06 02:34:23

+1

@xeo - 哈哈。在我看來,正則表達式更簡單=)。 @paul正則表達式對於你的hw任務來說是過分的,但它們非常棒。如果你想擴展你的編程知識,請閱讀它們! – mrtsherman 2011-06-06 02:36:14

2

第一:&&,直至有返回false或到達最終將測試一切。你想要的是||這將測試,直到條件是true
第二:什麼更短?測試輔音或測試元音?元音當然。只要把該測試中的額外功能(注:我假設一個有效的,字母的輸入字符串這裏):

function is_vowel(c) : bool 
    for each vowel test 
    if c == that vowel 
     return true 
    return false 

之後,只需用一個簡單的替換!is_vowel(str[i])你的大條件語句。 :)最後但並非最不重要的一點,你想增量你的consonant變量,並且有一個特殊的運算符:增量運算符! (酷的名字,是吧?)

++consonant; // better than consonant = consonant + 1; but same result 
+1

對於元音部分 - 字符可能不是元音而不是輔音! – mrtsherman 2011-06-06 02:34:21

+0

@ mrtsherman:恩,如果他們不是字母..我假設OP得到一個有效的輸入字符串,你是對的。 – Xeo 2011-06-06 02:35:12

0

您正在使用&&(和),這是在這種情況下,一個邏輯錯誤首發。您想使用||(或)。

您還可以通過檢查是否不是元音來縮短代碼。

int countConsonant(string str, int consonant) 
{ 
    int length = str.length(); 
    int consonant = 0; 

    for (int i = 0; i < length; i++) 
    { 
     if(!(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || 
      str[i] == 'u')) 

     consonant = consonant + 1; 
    } 
    return consonant; 
} 

其他人已經發布了這種代碼的替代形式作爲一個函數,你只要傳入你想測試的變量。

+0

關閉。但不完全相同。字符'='現在是一個常量。 – 2011-06-06 02:45:31

+0

@Martin你是對的,我做了一些我不應該對字符串中的字符做出的假設。 – Pete 2011-06-06 02:54:09

0
#include <iostream> 
#include <string> 
#include <cctype> 
using namespace std; 

size_t countConsonant(const string& s) { 
    const string vowels("aeiou"); 
    size_t count = 0; 
    for (string::const_iterator i = s.begin(); i != s.end(); ++i) { 
     if (vowels.find(tolower(*i)) == string::npos) { 
      ++count; 
     } 
    } 
    return count; 
} 

int main() { 
    cout << countConsonant("abcd") << endl; 
} 

或者,如果你不希望非阿爾法東西來搭配,你可以做這樣的:

#include <iostream> 
#include <string> 
#include <cctype> 
using namespace std; 

size_t countConsonant(const string& s) { 
    const string cons("bcdfghjklmnpqrstvwxyz"); 
    size_t count = 0; 
    for (string::const_iterator i = s.begin(); i != s.end(); ++i) { 
     if (cons.find(tolower(*i)) != string::npos) { 
      ++count; 
     } 
    } 
    return count; 
} 

int main() { 
    cout << countConsonant("abcd") << endl; 
} 
+1

第二個實現更好。或者你可以templatise計數接受一個'const char *'並實例化輔音或元音或其他需要的東西.... – 2011-06-06 03:18:20

3

如何:

#include <algorithm> 
#include <iostream> 
#include <string> 


bool isVowel(char c) 
{ 
    switch (c) 
    { 
     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
     { 
      return true; 
     } 
     default: 
     { 
      return false; 
     } 
    } 
} 


bool isConsonant(char c) 
{ 
    return !isVowel(c); 
} 


int main(int argc, char *argv[]) 
{ 
    std::string test = "hello"; 
    std::size_t numVowels = std::count_if(test.begin(), test.end(), isConsonant); 
    std::cout << "Number of vowels is: " << numVowels << std::endl; 
    return 0; 
} 
+2

isConsonant should'return!isVowel(c)&& isalpha(c);' – Marlon 2011-06-06 02:41:48

+0

我喜歡基礎知識。但'='這個字符現在是一個常量。但不是一個開關,你可以使用一個255的數組來表示輔音的真/假。 – 2011-06-06 02:43:50

+0

@Martin:注意,使用'signed char'可以導致負數組索引查找,因此需要先將其轉換爲'unsigned'或範圍檢查(較慢)。無論如何,'switch'相當不錯 - 編譯器應該生成相當不錯的代碼,儘管空間/速度的折衷可能不是你想要的。 – 2011-06-06 02:49:44

0

方式矯枉過正這個作業,但僅供參考 - 面向對象的方法:

struct Consonants 
{ 
    bool x_[256]; 

    Consonants() 
    { 
     // constructor sets x_[i] to true or false to indicate 
     // whether character with ASCII number i is a consonant... 

     for (int i = 0; i < 256; ++i) 
      x_ = false; 

     // note: ASCIIZ string finishes with NUL, so *p becomes false 
     //  and for loop exits 
     for (const char* p = "bcdfghjklmnpqrstvwxyz" 
          "BCDFGHJKLMNPQRSTVWXYZ"; *p; ++p) 
      x_[*p] = true; 
    }; 

    int in(const std::string& str) const 
    { 
     int result = 0; 
     for (int i = 0; i < str.size(); ++i) 
      if (x_[(unsigned char)str[i]]) 
       ++result; 
     return result; 
    } 
}; 

用法:

Consonants consonants; // create an "utility" object once 

int c1 = consonants.in("hello world"); // use as often as desired 
int c2 = consonants.in("goodbye cruel world"); 
相關問題