2017-03-08 3 views
0

私は少し失われています。以前私が単語に分割した文字列にいくつかの規則を適用するという本の例題の問題を解決しようとしています。C++分割された文字列に規則を適用する

ルール1から、パターンがvowel-consonant-consonant-vowelの単語間にハイフンを追加する必要があります。たとえば、rulesという単語はru-lesになります。

しかし、ルール2は、パターンがvowel-consonant-vowelである場合、2番目の母音がeであり、単語の最後に出現しない限り、子音の前にハイフネーションする必要があると述べています。これらを適用するためにネストされたif文を使用する必要があると思いますか?

私はすべての単語を持っているがnewWordsという名前istreamstringを用いて分離しますが、どのように私はVC-CVになるためにVCCVわたってるしきハイフンを追加することができますか?本書では、このシナリオで使用するプロセスや機能の種類については言及していません。基本的な質問をお詫び申し上げます、私は研究を通して最善を尽くしていますが、この時点で固まってしまっています。ご協力いただき、ありがとうございます。ありがとうございました。

+1

.cppreference.com/w/cpp/string/basic_string)。文字を挿入するために使用できる['insert'](http://en.cppreference.com/w/cpp/string/basic_string/insert)メソッドがあります。 –

+0

だから私は、適切な条件を指定してif文を使用し、それが通過すれば挿入しますか? – laura

+0

"必ずネストされたif文を使う必要があると思います。 'if(a){if(b){std :: cout <<" AとB "; }} 'は' if(a && b){std :: cout << "AとB"として書くこともできます。 } '。実際には、人間にとって最も読みやすいものを選択します。コンピュータは気にしません。 – MSalters

答えて

0

ここに私の完全な解決策があります。処理する単語のソースとしてコマンドライン引数を使用します。このコードは、word内の各位置を調べて、Rule1またはRule2が適用されているかどうかを確認します。単語内の特定のインデックスにルールが適用されると、ルールはそのインデックスに適用され、ループは残りの単語の検索を続け、一致するルールをさらに探します。

すべての単語が処理された後、ハイフネーションされている場合とされていない場合がある処理済みの単語が出力されます。各コマンドライン引数は1行に出力されます。ここで

は、いくつかのサンプル出力です。(私の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; 
} 

ファイルハイフネーションに保存し、これをコンパイルします。 cppを実行し、-std = C++ 11オプションを指定してコンパイルします。 ?// EN:どのように私は `VCCV`になるためにVCCV``わたってるしきハイフンを追加することができます_あなたも[ `のstd :: STRING`]のドキュメントを見てみてくださいました(HTTP _but

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

私たちは授業中の機能にほとんど触れていませんでしたが、実際にどのように使用されているかは信じられません。これであなたの時間をどうもありがとう、私は自分自身でそれを完全に理解できたとは思いません。どのようにして動作するかを完全に理解するために、私はこのソリューションを使い続けていきます。 – laura

+0

あなたの仕様をとり、main()を英語のように読むことは、とても楽しいことでした。機能しなくても大丈夫です!解決策Laura(上向きの矢印を押す)をupvoteしてください。そうすれば、他の人がこの解決策を指摘するでしょう。 あなたのコースに幸運を祈る! – ScottK

関連する問題