0
11

# 练习题答案

```fibon_elem.h
bool fibon_elem(int pos, int& elem)
{
if (pos <= 0 || pos > 45)
{
elem = 0;
return false;
}
elem = 1;
int n_2 = 1, n_1 = 1;
for (int ix = 3;ix <= pos;++ix)
{
elem = n_2 + n_1;
n_2 = n_1;
n_1 = elem;
}
return true;
}

#include <iostream>
#include "fibon_elem.h"

using namespace std;

extern bool fibon_elem(int, int&);

int main()
{
int pos, elem;
char ch;
bool more = true;
while (more)
{
cout << "Please enter a position: ";
cin >> pos;
if (fibon_elem(pos, elem))
{
cout << "element # " << pos
<< " is " << elem << endl;
}
else
{
cout << "Sorry. Counld not calculate element # "
<< pos << endl;
}
cout << "would you like to try again? (y/n) ";
cin >> ch;
if (ch != 'y'  && ch != 'Y')
{
more = false;
}
}
return 0;
}```

```#include <iostream>
#include <vector>
#include <string>

using namespace std;

bool calc_elements(vector<int>& vec, int pos);
void display_elems(vector<int>& vec, const string& title, ostream& os = cout);

int main()
{
vector<int> pent;
int pos;
const string title("Pentagonal Numeric Series");
cout << "Please enter a position: ";
cin >> pos;
if (calc_elements(pent, pos))
{
display_elems(pent, title);
}
return 0;
}

bool calc_elements(vector<int>& vec, int pos)
{
if (pos <= 0 || pos > 100)
{
cerr << "Sorry. Invaild position: " << pos << endl;
return false;
}
for (int ix = vec.size() + 1;ix <= pos;++ix)
{
vec.push_back(ix * (3 * ix - 1) / 2);
}
return true;
}

void display_elems(vector<int>& vec, const string& title, ostream& os)
{
os << '\n' << title << "\n";
for (int ix = 0;ix < vec.size();++ix)
{
os << vec[ix] << '\t';
if ((ix + 1) % 10 == 0)
{
cout << endl;
}
}
os << endl;
}```

```#include <iostream>
#include <vector>
#include <string>

using namespace std;

void really_calc_elems(vector<int>& , int );
inline bool calc_elems(vector<int>&, int);
void display_elems(vector<int>& vec, const string& title, int, ostream& os = cout);

int main()
{
vector<int> pent;
int pos;
char ch;
bool more = true;
const string title("Pentagonal Numeric Series");
while (more)
{
cout << "Please enter a position: ";
cin >> pos;
if (calc_elems(pent, pos))
{
display_elems(pent, title, pos);
}
cout << "would you like to try again? (y/n) ";
cin >> ch;
if (ch != 'y' && ch != 'Y')
{
more = false;
}
}
return 0;
}

inline bool calc_elems(vector<int>& vec, int pos)
{
if (pos <= 0 || pos > 100)
{
cerr << "Sorry. Invalid position: " << pos << endl;
return false;
}
if (vec.size() < pos)
{
really_calc_elems(vec, pos);
}
return true;
}

void really_calc_elems(vector<int>& vec, int pos)
{
for (int ix = vec.size() + 1;ix <= pos;++ix)
{
vec.push_back((ix * (3 * ix - 1) / 2));
}
}

void display_elems(vector<int>& vec, const string& title, int pos, ostream& os)
{
os << '\n' << title << "\n";
for (int ix = 0;ix < pos;++ix)
{
os << vec[ix] << '\t';
if ((ix + 1) % 10 == 0)
{
cout << endl;
}
}
os << endl;
}```

```#include <iostream>
#include <vector>

using namespace std;

inline bool check_validity(int pos);
const vector<int>* pentagonal_series(int pos);
bool pentagonal_elem(int pos, int& elem);

int main()
{
int pos, elem;
char ch;
bool more = true;
while (more)
{
cout << "Please enter a position: ";
cin >> pos;
if (pentagonal_elem(pos, elem))
{
cout << "element " << pos << " is " << elem << endl;
}
cout << "would you like to continue? (y/n) ";
cin >> ch;
if (ch != 'y' && ch != 'Y')
{
more = false;
}
}
return 0;
}

inline bool check_validity(int pos)
{
return (pos <= 0 || pos > 100) ? false : true;
}

const vector<int>* pentagonal_series(int pos)
{
static vector<int> _elems;
if (check_validity(pos) && (pos > _elems.size()))
{
for (int ix = _elems.size() + 1;ix <= pos;++ix)
{
_elems.push_back((ix * (3 * ix - 1)) / 2);
}
}
return &_elems;
}

bool pentagonal_elem(int pos, int& elem)
{
if (!check_validity(pos))
{
cout << "Sorry. Invalid position: " << pos << endl;
elem = 0;
return false;
}
const vector<int>* pent = pentagonal_series(pos);
elem = (*pent)[pos - 1];
return true;
}```

```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

inline int max(int t1, int t2)
{
return t1 > t2 ? t1 : t2;
}

inline float max(float t1, float t2)
{
return t1 > t2 ? t1 : t2;
}

inline string max(string t1, string t2)
{
return t1 > t2 ? t1 : t2;
}

inline int max(const vector<int>& vec)
{
return *max_element(vec.cbegin(), vec.cend());
}

inline float max(const vector<float>& vec)
{
return *max_element(vec.cbegin(), vec.cend());
}

inline string max(const vector<string>& vec)
{
return *max_element(vec.cbegin(), vec.cend());
}

inline int max(const int* parray, int size)
{
return *max_element(parray, parray + size);
}

inline float max(const float* parray, int size)
{
return *max_element(parray, parray + size);
}

inline string max(const string* parray, int size)
{
return *max_element(parray, parray + size);
}

int main()
{
int size = 5;
int n = 1, n2 = 9;
float f = 0.34, f2 = 9.3;
string s = "dfsdg", s2 = "dafsdfsad";
vector<int> ivec = { 12,70,2,169,1,5,29 };
vector<float> fvec = { 2.5,24.8,18.7,4.1,23.9 };
vector<string> svec = { "we","were","her","pride","of","ten" };
int iarray[5] = { 1,2,3,4,5 };
float farray[5] = { 5.0,4.0,3.0,2.0,1.0 };
string sarray[5] = { "a","b","c","asfs","aaa" };
cout << "max(n,n2)=" << max(n, n2) << "\n"
<< "max(f,f2)=" << max(f, f2) << "\n"
<< "max(s,s2)=" << max(s, s2) << "\n"
<< "max(ivec)=" << max(ivec) << "\n"
<< "max(fvec)=" << max(fvec) << "\n"
<< "max(svec)=" << max(svec) << "\n"
<< "max(iarray,size)=" << max(iarray, size) << "\n"
<< "max(farray,size)=" << max(farray, size) << "\n"
<< "max(sarray,size)=" << max(sarray, size) << "\n";
return 0;
}```

```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

template <typename Type>
inline Type new_max(Type t1, Type t2)
{
return t1 > t2 ? t1 : t2;
}

template <typename elemType>
inline elemType new_max(const vector<elemType>& vec)
{
return *max_element(vec.cbegin(), vec.cend());
}

template <typename arrayType>
inline arrayType new_max(const arrayType* parray, int size)
{
return *max_element(parray, parray + size);
}

int main()
{
int size = 5;
int n = 1, n2 = 9;
float f = 0.34, f2 = 9.3;
string s = "dfsdg", s2 = "dafsdfsad";
vector<int> ivec = { 12,70,2,169,1,5,29 };
vector<float> fvec = { 2.5,24.8,18.7,4.1,23.9 };
vector<string> svec = { "we","were","her","pride","of","ten" };
int iarray[5] = { 1,2,3,4,5 };
float farray[5] = { 5.0,4.0,3.0,2.0,1.0 };
string sarray[5] = { "a","b","c","asfs","aaa" };
cout << "max(n,n2)=" << new_max(n, n2) << "\n"
<< "max(f,f2)=" << new_max(f, f2) << "\n"
<< "max(s,s2)=" << new_max(s, s2) << "\n"
<< "max(ivec)=" << new_max(ivec) << "\n"
<< "max(fvec)=" << new_max(fvec) << "\n"
<< "max(svec)=" << new_max(svec) << "\n"
<< "max(iarray,size)=" << new_max(iarray, size) << "\n"
<< "max(farray,size)=" << new_max(farray, size) << "\n"
<< "max(sarray,size)=" << new_max(sarray, size) << "\n";
return 0;
}```

end。

“纸上得来终觉浅，绝知此事要躬行。”

<