2017-03-08 113 views
0

我有點迷路。我正在嘗試解決本書中的一個示例問題,該問題使我將一些規則應用於之前分割爲單詞的字符串。C++應用規則打散字符串

從規則1開始,我需要在具有模式vowel-consonant-consonant-vowel的單詞之間添加連字符,因此例如單詞rules將變爲ru-les

然而,規則2規定,如果模式是vowel-consonant-vowel我應該在輔音之前連字符,除非第二個元音是e,並且出現在單詞的結尾。我想我會需要使用嵌套if語句來應用這些?

我已經得到了所有的單詞使用名爲newWordsistreamstring分開了,但我怎麼可以添加一個連字符其間VCCV成爲VC-CV?本書沒有提到在這種情況下使用什麼樣的過程或功能。我對這個基本問題表示歉意,我正在通過研究努力,但在這一點上已經陷入困境。我非常感謝任何幫助,感謝您的時間。

+1

_but如何添加一個連字符其間'VCCV'成爲'VCCV' _你甚至嘗試看看['標準:: string'(HTTP的文檔:// EN .cppreference.com/W/CPP /串/ basic_string的)。它有一個['insert'](http://en.cppreference.com/w/cpp/string/basic_string/insert)方法,可以用來插入字符。 –

+0

所以我會用if語句指定適當的條件,然後插入它是否通過? – laura

+0

「我想我需要使用嵌套的if語句」 - 不一定。 (a){if(b){std :: cout <<「A and B」; }}'也可以寫爲'if(a && b){std :: cout <<「A and B」; }'。在實踐中,你選擇人類最易讀的東西;電腦不在乎。 – MSalters

回答

0

這是我的完整解決方案。我使用命令行參數作爲要處理的單詞的來源。代碼檢查單詞中的每個位置以查看Rule1或Rule2是否適用。如果規則適用於單詞中的給定索引,則將該規則應用於該索引處,然後循環繼續搜索該單詞的其餘部分,以尋找更多匹配的規則。

處理完所有單詞後,它會輸出可能或不可以用連字符表示的處理過的單詞。每個命令行參數都在一行中輸出。

下面是一些示例輸出:(我的bash promot是 「>」 字符)

> hyphenate 
usage: hyphenate <string to hyphenate> 

> hyphenate "HELLO World! You Rule!" Test Evil One 
HEL-LO World! You Ru-le! 
Test 
E-vil 
One 

下面是代碼:

#include <iostream>  // std::cout 
#include <sstream>  // std::istringstream 
#include <string>  // std::string 
#include <cctype>  // std::tolower 

using namespace std; 

void DisplayUsage(int argc, char** argv) { 
    cout << "usage: hyphenate <string to hyphenate>" << endl; 
} 

bool IsaVowel(char c) { 
    bool Vowel = false; 
    // Simplify switch by using tolower to convert uppercase to lowercase 
    // Only 5 cases to consider 
    switch(tolower(c)) { 
     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
      Vowel = true; 
    } 
    return Vowel; 
} 

bool IsaConsonant(char c) { 
    // A character is a consonant if it is alphabetic and not a vowel 
    return isalpha(c) && !IsaVowel(c); 
} 

bool VCCV(string s, int idx) { 
    bool matchFound = false; 

    // Only check for the pattern if there are enough characters past idx 
    if (idx+3 < s.length()) 
     // return true only if see a VCCV pattern 
     if (IsaVowel(s.at(idx)) 
       && IsaConsonant(s.at(idx+1)) 
       && IsaConsonant(s.at(idx+2)) 
       && IsaVowel (s.at(idx+3))) 
      matchFound = true; 

    return matchFound; 
} 

bool VCV(string s, int idx) { 
    bool matchFound = false; 

    // Only check for the pattern if there are enough characters past idx 
    if (idx+2 < s.length()) 
      // return true only if see a VCV pattern 
      if (IsaVowel(s.at(idx)) 
       && IsaConsonant(s.at(idx+1)) 
       && IsaVowel (s.at(idx+2))) 
      matchFound = true; 

    return matchFound; 
} 

bool VCe(string s, int idx) { 
    bool matchFound = false; 

    // VCe matches if VCV matches, and idx+2 is the last letter, and its an e 
    if (VCV(s,idx) && (idx+2==(s.length()-1)) && (s.compare(idx+2,1,"e") == 0)) 
     matchFound = true; 

    return matchFound; 
} 

bool Rule1(string s, int idx) { 
    return VCCV(s,idx); 
} 

bool Rule2(string s, int idx) { 
    return VCV(s,idx) && !VCe(s,idx); 
} 


// This program assumes compiled using the -std=c++11 standard 
int main(int argc, char** argv) { 
    if (argc < 2) { 
     // Did not provide a string to hyphenate, display usage and exit 
     DisplayUsage(argc, argv); 
     return -1; // Error 
    } 

    // Process each of the supplied parameters which may each contains multiple words 
    for(int i=1; i<argc; i++) { 
     // Get the next command line parameter 
     istringstream newWords(argv[i]); 

     while (!newWords.eof()) { 
      string nextWord; 
      newWords >> nextWord; 

      // Scan the next word to see if any rules apply 
      for(int j=0; j<nextWord.length(); j++) { 
       if (Rule1(nextWord, j)) 
        nextWord.insert(j+2,"-"); 
       if (Rule2(nextWord, j)) 
        nextWord.insert(j+1,"-"); 
      } 

      cout << nextWord << " "; 
     } 
     cout << endl; 
    } 

    return 0; 
} 

若要編譯此,將其保存在文件斷字。然後使用-std = C++ 11選項編譯它。 ?

g++ -o hyphenate -std=c++11 hyphenate.cpp 
+0

我們幾乎沒有觸及課堂上的功能,看到它們在實踐中如何使用,真是不可思議。非常感謝你在這方面的時間,我不確定我是否可以完全憑我自己的想法。我將繼續玩這個解決方案來充分理解它是如何工作的。 – laura

+0

採用你的規格並讓main()讀起來像英語一樣很有趣。艱難地做沒有功能的幫助!請同時提供解決方案Laura(點擊向上箭頭),以便其他人在找到該解決方案時指向此解決方案。 祝你好運! – ScottK