部分解答参考了网上已有解答
EX.6-1
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <conio.h>
using namespace std;
string::size_type width(constvector<string>& v)
{
string::size_typemaxlen = 0;
for (vector<string>::const_iterator iter =v.begin(); iter != v.end(); ++iter)
maxlen =max(maxlen, (*iter).size());
return maxlen;
}
vector<string> frame(constvector<string>& v)
{
vector<string>ret;
string::size_typemaxlen = width(v);
string border(maxlen+ 4, '*');
ret.push_back(border);
for (vector<string>::const_iterator iter =v.begin(); iter != v.end(); ++iter) {
ret.push_back("* " + *iter +string(maxlen-(*iter).size(),' ') + " *");
}
ret.push_back(border);
return ret;
}
vector<string> hcat(constvector<string>& left,constvector<string>& right)
{
vector<string>ret;
string::size_type width1 = width(left) + 1;
vector<string>::size_typei = 0, j = 0;
while (i != left.size() || j != right.size()) {
string s;
if (i != left.size()) {
s = left[i++];
}
s += string(width1- s.size(), ' ');
if (j != right.size()) {
s += right[j++];
}
ret.push_back(s);
}
return ret;
}
int main()
{
vector<string>content, output;
string s;
while (getline(cin, s)) {
content.push_back(s);
}
output =hcat(content, frame(content));
for (vector<string>::const_iterator iter =output.begin(); iter != output.end(); ++iter) {
cout <<*iter << endl;
}
getch();
return 0;
}
EX.6-2
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cctype>
#include <conio.h>
using namespace std;
bool not_url_char(char c)
{
static const stringurl_ch ="~;/?:@=&$-_.+!*'(),";
return !(isalnum(c) || find(url_ch.begin(),url_ch.end(), c) != url_ch.end());
}
string::const_iterator url_end(string::const_iterator b,string::const_iterator e)
{
return find_if(b, e, not_url_char);
}
string::const_iterator url_beg(string::const_iterator b,string::const_iterator e)
{
static const stringsep ="://";
typedef string::const_iterator iter;
iter i = b;
while ((i = search(i, e, sep.begin(), sep.end())) !=e) {
if (i != b && i + sep.size() != e) {
iter beg = i;
//beg[-1]相当于*(beg-1)
while (beg != b && isalpha(beg[-1]))
--beg;
//i[sep.size()]相当于*(i + sep.size())
if (beg != i && !not_url_char(i[sep.size()]))
return beg;
}
i += sep.size();
}
return e;
}
vector<string> find_urls(conststring& s)
{
vector<string>ret;
typedef string::const_iterator iter;
iter b = s.begin(), e= s.end();
while (b != e) {
b = url_beg(b, e);
if (b != e) {
iter after =url_end(b, e);
ret.push_back(string(b,after));
b = after;
}
}
return ret;
}
int main()
{
vector<string>lines, output_url;
string s;
while (getline(cin, s)) {
lines.push_back(s);
}
for (vector<string>::const_iterator iter =lines.begin(); iter != lines.end(); ++iter) {
output_url =find_urls(*iter);
for (vector<string>::const_iterator it = output_url.begin();it != output_url.end(); ++it) {
cout <<*it << endl;
}
}
getch();
return 0;
}
EX.6-3
#include <iostream>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
int main()
{
vector<int> u(0, 100);
vector<int> v;
copy(u.begin(),u.end(), v.begin());
getch();
return 0;
}
EX.6-4
#include <iostream>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
int main()
{
vector<int> u(0, 100);
vector<int> v;
copy(u.begin(), u.end(), back_inserter(v));
// copy(u.begin(),u.end(), inserter(v, v.begin()));
getch();
return 0;
}
EX.6-5
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <vector>
#include <conio.h>
#include "Student_info.h"
#include "grade.h"
#include "median.h"
using std::cin;
using std::cout;
using std::endl;
using std::domain_error;
using std::string;
using std::vector;
using std::back_inserter;
using std::remove_copy;
using std::max;
using std::streamsize;
using std::sort;
using std::setprecision;
//假设学生上交的家庭作业数量对他们的家庭作业成绩并不会产生影响,对没做家庭作业的学生用作为家庭作业的总成绩
double optimistic_median(constStudent_info& s) {
vector<double> nonzero;
remove_copy(s.homework.begin(), s.homework.end(),
back_inserter(nonzero), 0);
if (nonzero.empty())
return grade(s.midterm, s.final, 0);
else
return grade(s.midterm, s.final, median(nonzero));
}
int main()
{
vector<Student_info>students;
Student_info record;
string::size_typemaxlen = 0;
while (read(cin, record)) {
maxlen =max(maxlen, record.name.size());
students.push_back(record);
}
sort(students.begin(),students.end(), compare);
for (vector<Student_info>::size_type i = 0; i!= students.size(); ++i) {
cout <<students[i].name << string(maxlen + 1 - students[i].name.size(),' ');
try{
double final_grade = optimistic_median(students[i]);
streamsize prec= cout.precision();
cout <<setprecision(3) << final_grade << setprecision(prec);
} catch(domain_error e) {
cout <<e.what();
}
cout <<endl;
}
getch();
return 0;
}
EX.6-9
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <vector>
#include <conio.h>
#include "Student_info.h"
#include "grade.h"
#include "median.h"
using std::cin;
using std::cout;
using std::endl;
using std::domain_error;
using std::string;
using std::vector;
using std::back_inserter;
using std::remove_copy;
using std::max;
using std::streamsize;
using std::sort;
using std::setprecision;
//假设学生上交的家庭作业数量对他们的家庭作业成绩并不会产生影响,对没做家庭作业的学生用作为家庭作业的总成绩
double optimistic_median(constStudent_info& s) {
vector<double> nonzero;
remove_copy(s.homework.begin(), s.homework.end(),
back_inserter(nonzero), 0);
if (nonzero.empty())
return grade(s.midterm, s.final, 0);
else
return grade(s.midterm, s.final, median(nonzero));
}
int main()
{
vector<Student_info>students;
Student_info record;
string::size_typemaxlen = 0;
while (read(cin, record)) {
maxlen =max(maxlen, record.name.size());
students.push_back(record);
}
sort(students.begin(),students.end(), compare);
for (vector<Student_info>::size_type i = 0; i!= students.size(); ++i) {
cout <<students[i].name << string(maxlen + 1 - students[i].name.size(),' ');
try{
double final_grade = optimistic_median(students[i]);
streamsize prec= cout.precision();
cout <<setprecision(3) << final_grade << setprecision(prec);
} catch(domain_error e) {
cout <<e.what();
}
cout <<endl;
}
getch();
return 0;
}