6.3w+
社区成员
以后贴代码请贴在这儿
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <utility>
#include <locale>
using namespace std;
void strip_cap(vector <string> * words);
typedef pair <int , int> location;
typedef vector <location> loc;
typedef vector <string> ftext;
typedef vector <ftext * , loc *> ftext_loc;
ftext_loc * seperate_words(const vector <string> * text_file) //分离单词
{
int line_num = 0 , word_num = 0;
int word_num_in_line = 0;
// string text_line;
vector <string> * words = new vector <string>;
vector <location> * word_location = new vector <location>;
for (line_num = 0 ; line_num < text_file->size() ; line_num ++)
{
string::size_type pos = 0 , prev = 0;
string::size_type interpunction_index = 0;
string interpunction = ",.?!;!!:'= <>";
string text_line = (* text_file)[line_num];
while ((interpunction_index = text_line.find_first_of(interpunction , interpunction_index)) != string::npos)
{
text_line.erase(interpunction_index , 1); //去掉文本里的标点符号
interpunction_index++;
}
while ((pos = text_line.find_first_of(' ' , pos)) != string::npos) //分离出每个单词
{
words->push_back(text_line.substr(prev , pos - prev));
word_num_in_line++;
word_location->push_back(make_pair(line_num+1 , word_num_in_line)); //单词的行列位置
strip_cap(words); //大写换小写
prev = ++pos;
word_num++;
cout < <"The " < <word_num_in_line < <" word in line " < <line_num+1 < <" is : " < <(*words)[word_num - 1] < <endl;
}
words->push_back(text_line.substr(prev , pos - prev)); //最后一个单词
word_num_in_line += 1;
word_location->push_back(make_pair(line_num+1 , word_num_in_line)); //最后一个单词的位置
strip_cap(words);
word_num++;
word_num_in_line = 0;
cout < <"The last word in line " < <line_num + 1 < <" is : " < <(*words)[word_num - 1] < <endl;
}
return new ftext_loc(words , word_location);
}
vector <string> * retrieve_text() //读取文本
{
string file_name;
cout < <"please enter the file name :";
cin>>file_name;
ifstream infile(file_name.c_str() , ios::in);
if (!infile)
{
cerr < <"oops! unable to open files : "
< <file_name < <"--bailing out"
< <endl;
exit(-1);
}
else
cout < <"\n";
vector <string> * lines_of_text = new vector <string> ;
string textline;
typedef pair <string::size_type , int> status;
status maxline;
int line_num = 1;
while (getline(infile , textline , '\n'))
{
cout < <"line read : " < <textline < <'\n';
if (maxline.first < textline.size())
{
maxline.first = textline.size();
maxline.second = line_num;
}
lines_of_text->push_back(textline);
line_num++;
}
cout < <"max size of every lines is : " < <maxline.first < <'\n';
cout < <"there are " < <line_num < <" line all together ." < <'\n';
return lines_of_text;
}
void strip_cap(vector <string> * words)
{
string cap("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
vector <string>::iterator iter = words->begin();
while (iter != words->end())
{
string::size_type pos = 0;
while((pos = (*iter).find_first_of(cap , pos)) != string::npos)
(*iter)[pos] = tolower((*iter)[pos]);
iter++;
}
}
void main()
{
vector <string> * example = new vector <string>;
example = retrieve_text();
seperate_words(example);
}