程序要求:找出字符串sentence 中,最少字符单词 和 最多字符单词。请大家看看我的代码

ysysbaobei 2008-11-07 11:24:03
程序要求:找出字符串sentence 中,最少字符单词 和 最多字符单词
问题:下面是我写的代码,感觉复杂了点,功能好像实现了。大家帮忙写出好点的程序,我学习了,我初学c++

#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

// 测试下字符串长度
cout << line1.size() << endl; // 37
cout << line2.size() << endl; // 31
cout << line3.size() << endl; // 34
cout << sentence.size() << endl; // 104
string::size_type size = sentence.end() - line3.begin();
cout << size << endl; // 648

// 以line1为例,测试下输出
string::iterator ster = line1.begin();
int count = 0;
for (ster = line1.begin(); ster != line1.end(); ++ster)
{
cout << *ster;
count++;
}
cout << count << endl; // 37, 字母+ 空格 + : = 37个

// pos代表空格在 sentence字符串中的下标位置
string::size_type pos = 0;
int space = 0;
vector <int> ivec; // ivec 存储空格在字符串中的下表位置
while ((pos = sentence.find_first_of(' ', pos)) != string::npos)
{
++space;
ivec.push_back(pos);
cout << pos << " ";
++pos;
}
pos = 0;
ivec.push_back(sentence.size() - pos); // sentence 结束那里添加个空格
cout << sentence.size() - pos << endl;
cout << "There is: " << space + 1 << " characters" << endl; // line1: 9; line2: 4; line3: 4; sentence: 9 + 4 + 4

// ivec2 存储相邻空格下标 的差值,为了统计单词 字符个数的需要
vector <int>::iterator iter = ivec.begin();
vector <int> ivec2;
vector <int>::iterator iter2;
ivec2.push_back(*iter);
for (iter = ivec.begin(); iter != ivec.end() - 1; ++iter)
{
cout << *iter << " ";
ivec2.push_back(*(iter + 1) - *iter);
}
cout << *iter << endl;

// 统计最多字符个数的单词 和 最少字符个数的单词 . ivec3 存储单词的 字符 个数
iter2 = ivec2.begin();
int min = *iter2, max = *iter2;
int flag = 0;
cout << "ivec2.size(): " << ivec2.size() << endl;
vector<int> ivec3;
for (iter2 = ivec2.begin(); iter2 != ivec2.end(); ++iter2, flag = 1)
{
if (iter2 == ivec2.begin()) // 第一个单词单独存储
{
cout << *iter2 << " ";
ivec3.push_back(*iter2);
}
else
{
cout << *iter2 - 1 << " ";
ivec3.push_back(*iter2 - 1);
}
if (min > *iter2 - 1 && flag == 1) // 最少字符单词的 字符个数:min
min = *iter2 - 1;
if (max < *iter2 - 1 && flag == 1) // 最多字符单词的 字符个数:max
max = *iter2 -1;
}
cout << endl << "min character: " << min << endl;
cout << "max character: " << max << endl;
vector<int>::iterator iter3 = ivec3.begin();
int index = 0;

int min_count = 0, max_count = 0; // 统计多少字符 单词的个数 和 最多字符单词的个数
// 开始输出字符最少的 那些单词
iter = ivec.begin();
cout << "the min chararcter: ";
if (*iter3 == min) // 第一个单词为 最少字符 的情况
{
for (index = 0; index <= *iter; ++index)
cout << sentence[index];
cout << " ";
++min_count;
}
index = 0;
for (iter3 = ivec3.begin() + 1, iter = ivec.begin() + 1; iter3 != ivec3.end() && iter != ivec.end(); ++iter3, ++iter)
{
// cout << *iter3 << " ";
if (*iter3 == min)
{
for (index = *(iter - 1) + 1; index < *iter; ++index )
cout << sentence[index];
cout << " ";
++min_count;
}
}
cout << "...Total has: " << min_count << " number danci" << endl;

// 开始输出字符最多的 那些单词
cout << "the max chararcter: ";
iter = ivec.begin();
if (*iter3 == max) // 第一个单词为 最多字符 的情况
{
for (index = 0; index <= *iter; ++index)
cout << sentence[index];
cout << " ";
++max_count;
}
index = 0;
for (iter3 = ivec3.begin() + 1, iter = ivec.begin() + 1; iter3 != ivec3.end() && iter != ivec.end(); ++iter3, ++iter)
{
// cout << *iter3 << " ";
if (*iter3 == max)
{
for (index = *(iter - 1) + 1; index < *iter; ++index )
cout << sentence[index];
cout << " ";
++max_count;
}
}
cout << "...Total has: " << max_count << " number danci" << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
...全文
131 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2008-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hqin6 的回复:]
C/C++ code#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
string s = "We were her pride of 10 she named us:\nBenjamin, Phoenix, the Prodigal\nand perspicacious pacific Suzannne";
stringstream ss,ss2;
ss<<s;
string line,word;
string maxstr,minstr(10,'1');
vector<string> maxstrV,minstrV;
while(getline…
[/Quote]
楼上的定义 iterator it 的时候,应该放在for 循环外面吧
试了你写的程序,比我的好多了,谢谢
问题解决了,结贴
太乙 2008-11-07
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
string s = "We were her pride of 10 she named us:\nBenjamin, Phoenix, the Prodigal\nand perspicacious pacific Suzannne";
stringstream ss,ss2;
ss<<s;
string line,word;
string maxstr,minstr(10,'1');
vector<string> maxstrV,minstrV;
while(getline(ss,line))
{
ss2<<line;
while(ss2>>word)
{
if(word.length()>maxstr.length())
{
maxstr = word;
maxstrV.clear();
maxstrV.push_back(word);
}
else if(word.length()==maxstr.length())
maxstrV.push_back(word);
if(word.length()<minstr.length())
{
minstr = word;
minstrV.clear();
minstrV.push_back(word);
}
else if(word.length()==minstr.length())
minstrV.push_back(word);
}
ss2.clear();
}
for(vector<string>::const_iterator it = maxstrV.begin();it!=maxstrV.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
for(it = minstrV.begin();it!=minstrV.end();it++)
{
cout<<*it<<" ";
}
return EXIT_SUCCESS;
}
太乙 2008-11-07
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s = "We were her pride of 10 she named us:\nBenjamin, Phoenix, the Prodigal\nand perspicacious pacific Suzannne";
stringstream ss,ss2;
ss<<s;
string line,word;
string maxstr,minstr(10,'1');
while(getline(ss,line))
{
ss2<<line;
while(ss2>>word)
{
if(word.length()>maxstr.length())
maxstr = word;
if(word.length()<minstr.length())
minstr = word;
}
ss2.clear();
}
cout<<maxstr<<endl;
cout<<minstr<<endl;
return EXIT_SUCCESS;
}
lann64 2008-11-07
  • 打赏
  • 举报
回复
楼主不会使用字符串流吗?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧