C++/CLI 我需要改进代码
C++/CLI I need to improve code
我在这里编写了代码,它可以工作,但急需改进。
//////////////////////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();
}
在代码中输入文本并拆分句子和单词。在下图中,您将看到我的代码输出:
如您所见,句子长度不起作用。
我希望输出类似于下图。
您实际上并没有在代码中填充句子长度单元格(第 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];;
}
}
我在这里编写了代码,它可以工作,但急需改进。
//////////////////////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();
}
在代码中输入文本并拆分句子和单词。在下图中,您将看到我的代码输出:
如您所见,句子长度不起作用。
我希望输出类似于下图。
您实际上并没有在代码中填充句子长度单元格(第 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];;
}
}