這是我的完整解決方案。我使用命令行參數作爲要處理的單詞的來源。代碼檢查單詞中的每個位置以查看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
_but如何添加一個連字符其間'VCCV'成爲'VCCV' _你甚至嘗試看看['標準:: string'(HTTP的文檔:// EN .cppreference.com/W/CPP /串/ basic_string的)。它有一個['insert'](http://en.cppreference.com/w/cpp/string/basic_string/insert)方法,可以用來插入字符。 –
所以我會用if語句指定適當的條件,然後插入它是否通過? – laura
「我想我需要使用嵌套的if語句」 - 不一定。 (a){if(b){std :: cout <<「A and B」; }}'也可以寫爲'if(a && b){std :: cout <<「A and B」; }'。在實踐中,你選擇人類最易讀的東西;電腦不在乎。 – MSalters