2015-11-11 66 views
-1

我有寫在這裏的代碼,這工作,但需要拼命改善。C++/CLI我需要改進代碼

//////////////////////Split into sentence///////////////////////// 
String^ text = textBox1->Text; 
cli::array<String^>^ sentence = text->Split('.', '?', '!'); 
for (int i = 0; i < sentence->Length; ++i) { 
    datagridview->Rows->Add(); 
    datagridview->Rows[i]->Cells[1]->Value = i + 1; 
    datagridview->Rows[i]->Cells[3]->Value = sentence[i]; 
} 
//////////////////////Split into words///////////////////////// 
cli::array<String^>^ word = text->Split(' '); 
for (int ii = 0; ii < word->Length; ++ii) { 
    datagridview->Rows[ii]->Cells[4]->Value = ii + 1; 
    datagridview->Rows[ii]->Cells[5]->Value = word[ii]; 
    datagridview->Rows->Add(); 
} 

在代碼中輸入文本並將其作爲拆分句子和單詞。在下面的圖片中,您將看到我的代碼輸出: Datagridview

正如您所看到的句子長度不起作用。

我想輸出將類似於下面的圖片。 How out put should look

+0

什麼是您的輸入數據是什麼樣子? –

+0

輸入數據是一個帶有「這是一個第二個參數」的文本框。文本。 –

+1

不要忽略datagridview-> Rows-> Add()的返回值。它返回一個對添加行的引用。你想要使用它,像現在這樣對行進行索引是一個嚴重的錯誤。 –

回答

2

你實際上並沒有在你的代碼中填充句子長度單元格(第2列),所以這就是爲什麼沒有出現在那裏的原因。

你需要沿着這些路線的東西:

String^ text = textBox1->Text; 
cli::array<String^>^ sentences = text->Split('.', '?', '!'); 
for each (String^ sentence in sentences) { 
    cli::array<String^>^ words = sentence->Split(' '); 
    for each (String^ word in words) { 
     int rowIndex = datagridview->Rows->Add(); 
     datagridview->Rows[rowIndex]->Cells[1]->Value = i + 1; 
     datagridview->Rows[rowIndex]->Cells[2]->Value = sentence->Length; // This is the line you're missing 
     datagridview->Rows[rowIndex]->Cells[3]->Value = sentence; 
     datagridview->Rows[rowIndex]->Cells[4]->Value = ii + 1; 
     datagridview->Rows[rowIndex]->Cells[5]->Value = word; 
    }  
} 

編輯 - 試試這個,然後:

String^ text = textBox1->Text; 
cli::array<String^>^ sentences = text->Split('.', '?', '!'); 
for (int sentenceIndex = 0; sentenceIndex < sentences->Length; ++sentenceIndex) { 
    String^ sentence = sentences[sentenceIndex]; 
    cli::array<String^>^ words = sentence->Split(' '); 
    for (int wordIndex = 0; wordIndex < words->Length; ++wordIndex) { 
     int rowIndex = datagridview->Rows->Add(); 
     datagridview->Rows[rowIndex]->Cells[1]->Value = sentenceIndex + 1; 
     datagridview->Rows[rowIndex]->Cells[2]->Value = sentence->Length; // This is the line you're missing 
     datagridview->Rows[rowIndex]->Cells[3]->Value = sentence; 
     datagridview->Rows[rowIndex]->Cells[4]->Value = wordIndex + 1; 
     datagridview->Rows[rowIndex]->Cells[5]->Value = words[wordIndex];; 
    }  
} 
+0

我用你的代碼示例工作得很好,但仍然有一些小問題[鏈接](https://imgur.com/m2tn02l)\。 –

+0

這將是偉大的數字計數器將工作 –