C++题目和我的解答见下面 最后是0 errors 4 warnings,程序无法正确运行,请教为什么?

2025-06-23 07:17:11
推荐回答(1个)
回答1:

你第一个while循环里面写的有点问题,设置断点调试会发现循环跳不出来。可试试这段代码:
#include
#include
#include

using namespace std;

void main()
{
const string line1 = "We were her pride of 10 she named us:";
const string line2 = "Benjamin, Phoenix, the Prodigal";
const string line3 = "and perspicacious pacific Suzanne";
const string sentence = line1 + ' ' + line2 + ' ' + line3;

const char *stmp = sentence.data();
vector< string > vect, vecMin, vecMax;
string strtmp = "";
unsigned int i;

//搜索字符串中的所有单词
for(i = 0; i <= strlen(stmp); i++)
{
if(isalpha(stmp[i]))
{
strtmp += stmp[i];
}
else if(!strtmp.empty())
{
vect.push_back(strtmp);
strtmp = "";
}
}

if(!vect.size())
{
cout<<"找不到单词!"< return;
}

vecMin.push_back(vect[0]);
vecMax.push_back(vect[0]);

for(i = 1; i < vect.size(); i++)
{
if(vecMin[0].length() > vect[i].length())
{
vecMin.clear();
vecMin.push_back(vect[i]);
}
else if(vecMin[0].length() == vect[i].length())
{
vecMin.push_back(vect[i]);
}

if(vecMax[0].length() < vect[i].length())
{
vecMax.clear();
vecMax.push_back(vect[i]);
}
else if(vecMax[0].length() == vect[i].length())
{
vecMax.push_back(vect[i]);
}
}

cout<<"原字符串:"< cout<<"共找到单词"
< for(i = 0; i < vect.size(); i++)
cout<
cout< < for(i = 0; i < vecMin.size(); i++)
cout<
cout< < for(i = 0; i < vecMax.size(); i++)
cout<}