Accelerated C++:通过示例进行编程实践——练习解答(第5章)

5-0. Compile, execute, and test the programs in this chapter.

a、根据学生成绩及格与否进行分类,及格、不及格分别存储。

//classify the students by grades
//fgrade(vector<Student_info>& s),classify students into two parts,fail and pass;file be placed in grade.h/.cpp
bool fgrade(vector<Student_info>& s)
{
return grade(s)<60;
}

//1.store by two new vector:fail and pass;this function was placed in Student_info.h/.cpp
vector<Student_info> extract_fails(vector<Student_info>& s)
{
    vector<Student_info> fail,pass;
    typedef vector<Student_info>::size_type size_tp;
    size_tp size=s.size();
    for(int i=0;i<size;++i)
    {
        if(fgrade(s[i]))
            fail.push_back(s[i]);
        else
            pass.push_back(s[i]);
    }
    s=pass;
    return fail;
}
//2.use one new vector to store fail students,pass still in old vector;this function was placed in Student_info.h/.cpp
vector<Student_info> extract_fails(vector<Student_info>& s)
{
    vector<Student_info> fail,pass;
    typedef vector<Student_info>::size_type size_tp;
    for(size_tp i=0;i<s.size();)
    {
        if(fgrade(s[i]))
        {
            fail.push_back(s[i]);
            i=s.erase(s.begin()+i);
        }
        else
            ++i;
    }
    return fail;
}
//3.use the iterator instead the index;file still be placed in Student_info.h/.cpp
vector<Student_info> extract_fails(vector<Student_info>& s)
{
    vector<Student_info> fail;
    
    for(vector<Student_info>::iterator it=s.begin();it!=s.end();)
    {
        if(fgrade(it))
        {
            fail.push_back(*it);
            it=s.erase(it);
        }
        else
            ++it;
    }
    return fail;
}
//4.new data Structure--list  to improve the efficiency
list<Student_info> extract_fails(list<Student_info>& s)
{
    list<Student_info> fail;
    
    for(list<Student_info>::iterator it=s.begin();it!=s.end();)
    {
        if(fgrade(it))
        {
            fail.push_back(*it);
            it=s.erase(it);
        }
        else
            ++it;
    }
    return fail;
}

b、字符串连接

this is an **************
example    * this is an *
to         * to         *
illustrate * illustrate *
framing    * framing    *
           **************
//str_join.h
#ifndef str_join_H
#define str_join_H
#include <string>
#include <vector>

std::string::size_type width(const std::vector<std::string>& v);
std::vector<std::string> frame(const std::vector<std::string>& v);
std::vector<std::string> hcat(const std::vector<std::string>& left,const std::vector<std::string>& right);

#endif
//str_join.cpp
#include "str_join.h"
#include <iostream>
using namespace std;

string::size_type width(const vector<string>& v)
{
	string::size_type maxlen=0;
	for(vector<string>::const_iterator it=v.begin();
		it!=v.end();++it)
	{
		string::size_type size=(*it).size();
		if(maxlen<size)
			maxlen=size;
	}
	return maxlen;
}

vector<string> frame(const vector<string>& v)
{
	vector<string> ret;
	string::size_type sz=width(v);
	string border(sz+4,'*');
	ret.push_back(border);

	for(vector<string>::const_iterator it=v.begin();
		it!=v.end();++it)
	{
		ret.push_back("* "+*it+string(sz-(*it).size(),' ')+" *");
	}

	ret.push_back(border);
	return ret;
}

vector<string> hcat(const vector<string>& left,const vector<string>& right)
{
	vector<string> ret;
	string::size_type wt=width(left)+1;

	vector<string>::size_type i=0,j=0;
	while(i!=left.size()||j!=right.size())
	{
		string s;
		if(i!=left.size())
			s=left[i++];
		s+=string(wt-s.size(),' ');
		if(j!=right.size())
			s+=right[j++];
		ret.push_back(s);
	}

	return ret;
}
//main.cpp
#include "str_join.h"
#include <iostream>
using namespace std;

int main()
{
	string str;
	vector<string> vec;
	vector<string> vec_f;
	vector<string> vec_j;

	while(getline(cin,str))
		vec.push_back(str);
	vec_f=frame(vec);
	
	vec_j=hcat(vec,vec_f);

	for(vector<string>::const_iterator it=vec_j.begin();
		it!=vec_j.end();++it)
	{
		cout<<endl<<*it<<endl;
	}

	return 0;
}
lyj@qt:~/Desktop/vm/bin/Debug$ ./vm
this is an 
example
to
illustrate
framing
this is an **************
example  * this is an *
to            * example  *
illustrate  * to            *
framing   * illustrate  *
               * framing    *
               **************
lyj@qt:~/Desktop$

c、字符串分裂split

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
vector<string> split(const string& s)
{
	vector<string> ret;
	string::size_type size=s.size(),i=0,j;
	while(i!=size)
	{
		while(i!=size && isspace(s[i]))++i;
		j=i;
		while(j!=size && !isspace(s[j]))++j;
		if(j!=i)
		{
			ret.push_back(s.substr(i,j-i));
			i=j;
		}
	}
	return ret;
}
int main()
{
	string str;
	vector<string> vec;
	while(getline(cin,str))
		vec=split(str);
	for(vector<string>::const_iterator it=vec.begin();
		it!=vec.end();++it)
		cout<<*it<<endl;
	return 0;
}
lyj@qt:~/Desktop$ ./split
This is an example to illustrate the string split!
This
is
an
example
to
illustrate
the
string
split!
lyj@qt:~/Desktop$ 

5-1. Design and implement a program to produce a permuted index. A permuted index is one in which each phrase is indexed by every word in the phrase. So, given the following input,

The quick brown fox 
jumped over the fence

the output would be

      The quick     brown fox 
jumped over the     fence
The quick brown     fox 
                    jumped over the fence
         jumped     over the fence
            The     quick brown fox 
    jumped over     the fence
                    The quick brown fox

A good algorithm is suggested in The AWK Programming Language by Aho, Kernighan, and Weinberger (Addison-Wesley, 1988). That solution divides the problem into three steps:

  1. Read each line of the input and generate a set of rotations of that line. Each rotation puts the next word of the input in the first position and rotates the previous first word to the end of the phrase. So the output of this phase for the first line of our input would be
    The quick brown fox
    quick brown fox The
    brown fox The quick
    fox The quick brown
    
    Of course, it will be important to know where the original phrase ends and where the rotated beginning begins.
  2. Sort the rotations.
  3. Unrotate and write the permuted index, which involves finding the separator, putting the phrase back together, and writing it properly formatted.

Ans:Permuted index(置换索引),算法思想如下:


tips:对以下文件进行简单的功能介绍:

split文件:进行字符串分裂,以便进行短语轮转;

compare文件:sort(begi

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值