130242014052.邹宏亮-第3次实验

一、实验目的

1.理解不同体系结构风格的具体内涵。

2.学习体系结构风格的具体实践。

二、实验环境

硬件: (依据具体情况填写)

软件:Java或任何一种自己熟悉的语言

三、实验内容

 

“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每一个单词又是字母的有序集合。通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被“循环地移动”。KWIC检索系统以字母表的顺序输出一个所有行循环移动的列表。

尝试用不同的策略实现这个系统。选择2-3种体系结构风格来实现。

四、实验步骤:

一、面向对象程序风格

1、体系结构图:

 输入数据 ->排序比较->找出最长字符串长度->分割字符串->输出

2、简述体系结构各部件的主要功能,实现思想。

上述的面向对象风格,将问题分解为排序比较、找出list中最长的字符串的长度、分割字符串  、输出(Output)对象。

排序比较函数  bool compare(const string& s, const string& t)  

找出list中最长的字符串的长度  string::size_type width(const list<string>& v) 

分割字符串  list<string> split(const string& s)  

 ....

3、写出主要的代码

#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <cctype>
#include <algorithm>

using namespace std;

bool compare(const string& s, const string& t)
{

string temp1, temp2;
string::size_type i = 0,j = 0;
while (i != s.size())
{
temp1 += tolower(s[i]);
i++;
}

while (j != t.size())
{
temp2 += tolower(t[j]);
j++;
}

return temp1 < temp2;
}

string::size_type width(const list<string>& v)
{
list<string> t = v;
string::size_type maxlen = 0;
for (list<string>::iterator iter= t.begin(); iter != t.end(); ++iter)
{
maxlen = max(maxlen, (*iter).size() );
}
return maxlen;
}

list<string> split(const string& s)
{
list<string> ret;
typedef string::size_type string_size;
string_size i = 0;

while (i != s.size())
{
while (i != s.size() && isspace(s[i]))
{
++i;
}


string_size j = i;

while (j != s.size() && !isspace(s[j]))
{
++j;
}

if (i != j)
{
ret.push_back(s.substr(i, j-i));
i = j;
}
}
return ret;
}

int main()
{
cout << "请输入一段文字:" << endl;

string words;
list<string> texts, ntexts;


while (cin >> words && (words != "EOF"))
{
texts.push_back(words);
}


string str;
for (list<string>::iterator iter = texts.begin();
iter != texts.end(); iter++)
{
str = str + (*iter) + " ";
}
ntexts.push_back(str);


string first = texts.front();
string end = texts.back();


list<string>::iterator first_iter = texts.begin();
list<string>::iterator end_iter = texts.end();
while (*first_iter != end)
{

list<string>::iterator temp = texts.begin();
texts.push_back(*temp);
texts.erase(temp);

string str;
for (list<string>::iterator iter = texts.begin();
iter != texts.end(); iter++)
{

str = str + (*iter) + " ";
}
ntexts.push_back(str);

first_iter = texts.begin();
end_iter = texts.end();
}

cout << "轮转集合是:" << endl;
list<string>::iterator niter = ntexts.begin();

string::size_type width1 = width(ntexts);
for (; niter != ntexts.end(); niter++)
{
cout << *niter << endl;
}


ntexts.sort(compare);

cout << "排序后的轮转集合是:" << endl;
for (niter = ntexts.begin(); niter != ntexts.end(); niter++)
{
cout << *niter << endl;
}

list<string> nt1, nt2;
list<string>::iterator iter = ntexts.begin();
while (iter != ntexts.end())
{
list<string> lwords = split(*iter);
list<string>::iterator l_itrer = lwords.begin();
string s;
if (*l_itrer == first)
{
for (; l_itrer != lwords.end(); ++l_itrer)
{
s += *l_itrer + " ";
}
nt1.push_back(s + string(width1-s.size(), ' '));
nt2.push_back(string(width1, ' '));
s = " ";
}
else if (*l_itrer == end)
{
s = *l_itrer + " ";
nt1.push_back(s + string(width1-s.size(), ' '));

s = " ";
for (++l_itrer; l_itrer != lwords.end(); ++l_itrer)
{
s += *l_itrer + " ";
}
nt2.push_back(string(width1-s.size(), ' ') + s);
}
else
{
do
{
s += *l_itrer + " ";
++l_itrer;
}while (*l_itrer != first);
nt1.push_back(s + string(width1-s.size(), ' '));
s = " ";
for (; l_itrer != lwords.end(); ++l_itrer)
{
s += *l_itrer + " ";
}
nt2.push_back(string(width1-s.size(), ' ') + s);
}

++iter;
}

list<string>::iterator nit1 = nt1.begin();
list<string>::iterator nit2 = nt2.begin();
cout << "最终的结果是:" << endl;
for (; nit1 != nt1.end(); ++nit1, ++nit2)
{
cout << *nit2 << " " << *nit1 << endl;
}

return 0;
}

实验截图:

 

posted on 2017-11-19 12:28  dr.z-zhl  阅读(253)  评论(0编辑  收藏  举报