第一章查找單詞例題-算法設計與分析報告C/C++版

設計如圖

PS:上面的設計報告pdf文檔已上傳,需要可以下載 

完整代碼如下 

//author:rgh
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void solve(string str,vector<string>&words)//產生所有單詞
{
string w;
int i=0;
int j=str.find(" ");// 查找第一個空格
while(j!=-1)//找到單詞後循環
{
w=str.substr(i,j-i);//提取一個單詞
words.push_back(w);//將單詞添加到words中
i=j+1;
j=str.find(" ",i);//查找下一個空格 
}

if(i<str.length()-1)//處理最後一個單詞
{
w=str.substr(i);//提取最後一個單詞
words.push_back(w);//最後單詞添加到words中
}
}
int main()
{
	string str="The following code computes the intersection of two arrays";
	vector<string>words;
	solve(str,words);
	cout<<"所有的單詞:"<<endl;//輸出結果
	vector<string>::iterator it;
	for(it=words.begin();it!=words.end();++it)
	cout<<" "<<*it<<endl;
	return 0; 
	}

喜歡的話關注一下吧,打賞也行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章