第2章介绍了C++语言中面向过程编程方面
本章介绍了独立函数的众多基本编写原则,并简要讨论了重载(overloaded)以及函数模板(function template),同时也说明了函数指针的运用技巧。
将函数独立出来的做法可带来3个主要好处:
1、以一连串函数调用操作取代重复编写相同的程序代码,可使程序更容易读懂。
2、我们可以在不同的程序中使用这些函数。
3、我们可以更容易地将工作分配给协作开发团队。
作者在第2章使用了一个冒泡排序的小程序介绍函数的通用操作,并简要讨论了指针和引用的关系,笔者在此汇总并附上课后编程练习使得更好地掌握已学知识,若是对您有用的话请点赞或分享提供给它人。
//ch2_main.cpp
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//判断用户输入的位置是否正确;
inline bool is_size_ok(int size)
{
const int max_size = 1024;
if (size <= 0 || size > max_size)
{
cerr << "Oops: requested size is not supported : " << size;
cerr << " -- can't fulfill request.\n";
return false;
}
return true;
}
//定义一个静态数列;
inline const vector<int> *fibon_seq(int size)
{
const int max_size = 1024;
static vector<int> elems;
if (!is_size_ok(size))
{
return 0;
}
for (int ix = elems.size(); ix < size; ++ix)
{
if (ix == 0 || ix == 1)
{
elems.push_back(1);
}
else
{
elems.push_back(elems[ix - 1] + elems[ix - 2]);
}
}
return &elems;
}
//查找指定位置的值;
inline bool fibon_elem(int pos, int &elem)
{
const vector<int> *pseq = fibon_seq(pos);
if (!pseq)
{
elem = 0;
return false;
}
elem = (*pseq)[pos - 1];
return true;
}
//交换2个元素;
inline void swap(int &val1, int &val2)
{
int temp = val1;
val1 = val2;
val2 = temp;
return;
}
//冒泡排序;
inline void bubble_sort(vector<int> &vec, ofstream &os = cout)
{
for (int ix = 0; ix < vec.size(); ++ix)
{
for (int jx = ix + 1; jx < vec.size(); ++jx)
{
if (vec[ix] > vec[jx])
{
swap(vec[ix], vec[jx]);
}
}
}
os << "The sorted datas:" << endl;
return;
}
//打印数列;
inline void display(const vector<int> &vec, ofstream &os = cout)
{
for (int ix = 0; ix < vec.size(); ++ix)
{
os << vec[ix] << ' ';
}
os << endl;
return;
}
//显示数列中指定位置的值;
inline bool print_sequence(int pos)
{
if (pos <= 0 || pos > 1024)
{
cerr << "invalid position: " << pos;
cerr << " -- cannot handle request!\n";
return false;
}
cout << "The Fibonacci Sequence for ";
cout << pos << " positions: \n\t";
switch (pos)
{
default:
case 2:
{
cout << "1 ";
}
case 1:
{
cout << "1 ";
break;
}
}
int elem;
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;
cout << elem << (!(ix % 10) ? "\n\t" : " "); //每10个元素打印换行符和制表符;
}
cout << endl;
return true;
}
//测试冒泡排序;
void test_bubble_sort()
{
int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
vector<int> vec(ia, ia + 8);
bubble_sort5(vec);
display(vec);
ofstream ofil("sorted_data.txt");
bubble_sort(vec, ofil);
display(vec, ofil);
return;
}
//测试查找指定位置上数列的值;
void test_num_sequence()
{
int pos, elem;
cout << "Please enter a position: ";
cin >> pos;
if (fibon_elem(pos, elem))
{
cout << "element # " << pos;
cout << " is " << elem << endl;
}
else
{
cout << "Sorry. Could not calculate element # ";
cout << pos << endl;
}
print_sequence(pos);
return;
}
int main()
{
test_num_sequence();
test_bubble_sort();
return 0;
}
//-----------------------------------------------------------;
//-----------------------------------------------------------;
//Practise2.1.cpp
#include <iostream>
using namespace std;
inline bool fibon_elem(int pos, int &elem); //查找指定位置的值;
inline bool print_sequence(int pos); //显示数列中指定位置的值;
int main()
{
char ch;
int pos, elem;
while (true)
{
cout << "Please enter a position: ";
cin >> pos;
if (fibon_elem(pos, elem))
{
cout << "element # " << pos;
cout << " is " << elem << endl;
}
else
{
cout << "Sorry. Could not calculate element # ";
cout << pos << endl;
}
print_sequence(pos);
cout << "Would you like to try again? (y/n) ";
cin >> ch;
if (ch != 'y' && ch != 'Y')
{
break;
}
}
cout << "Bye." << endl;
return 0;
}
bool fibon_elem(int pos, int &elem)
{
if (pos <= 0 || pos > 1024)
{
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;
}
bool print_sequence(int pos)
{
if (pos <= 0 || pos > 1024)
{
cerr << "invalid position: " << pos;
cerr << " -- cannot handle request!\n";
return false;
}
cout << "The Fibonacci Sequence for ";
cout << pos << " positions: \n\t";
switch (pos)
{
default:
case 2:
{
cout << "1 ";
}
case 1:
{
cout << "1 ";
break;
}
}
int elem;
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;
cout << elem << (!(ix % 10) ? "\n\t" : " "); //每10个元素打印换行符和制表符;
}
cout << endl;
return true;
}
//-----------------------------------------------------------;
//-----------------------------------------------------------;
//Practise2.2.cpp
#include <iostream>
#include <vector>
using namespace std;
inline bool gen_pentagonal(vector<int> &vec, int pos);
inline void display_elems(vector<int> &vec, const string &title);
int main()
{
vector<int> pent;
const string title("Pentagonal Numeric Series");
if (gen_pentagonal(pent, 0))
{
display_elems(pent, title);
}
if (gen_pentagonal(pent, 8))
{
display_elems(pent, title);
}
if (gen_pentagonal(pent, 14))
{
display_elems(pent, title);
}
if (gen_pentagonal(pent, 138))
{
display_elems(pent, title);
}
return 0;
}
bool gen_pentagonal(vector<int> &vec, int pos)
{
if (pos <= 0 || pos > 64)
{
cerr << "Sorry. Invalid position: " << pos << endl;
return false;
}
for (int i = vec.size() + 1; i <= pos; ++i)
{
vec.push_back((i * (3 * i - 1)) / 2);
}
return true;
}
void display_elems(vector<int> &vec, const string &title)
{
cout << title << endl;
for (int ix = 0; ix < vec.size(); ++ix)
{
cout << vec[ix] << ' ';
}
cout << endl;
return;
}
//-----------------------------------------------------------;
//-----------------------------------------------------------;
//Practise2.3.cpp
#include <iostream>
#include <vector>
using namespace std;
inline bool gen_pentagonal(vector<int> &vec, int pos);
inline void help_pentagonal(vector<int> &vec, int pos);
inline void display_elems(vector<int> &vec, const string &title);
int main()
{
vector<int> pent;
const string title("Pentagonal Numeric Series");
if (gen_pentagonal(pent, 0))
{
display_elems(pent, title);
}
if (gen_pentagonal(pent, 8))
{
display_elems(pent, title);
}
if (gen_pentagonal(pent, 14))
{
display_elems(pent, title);
}
if (gen_pentagonal(pent, 138))
{
display_elems(pent, title);
}
return 0;
}
bool gen_pentagonal(vector<int> &vec, int pos)
{
if (pos <= 0 || pos > 64)
{
cerr << "Sorry. Invalid position: " << pos << endl;
return false;
}
if (vec.size() < pos)
{
help_pentagonal(vec, pos);
}
return true;
}
void help_pentagonal(vector<int> &vec, int pos)
{
for (int i = vec.size() + 1; i <= pos; ++i)
{
vec.push_back((i * (3 * i - 1)) / 2);
}
return;
}
void display_elems(vector<int> &vec, const string &title)
{
cout << title << endl;
for (int ix = 0; ix < vec.size(); ++ix)
{
cout << vec[ix] << ' ';
}
cout << endl;
return;
}
//-----------------------------------------------------------;
//-----------------------------------------------------------;
//Practise2.4.cpp
#include <iostream>
#include <vector>
using namespace std;
inline const vector<int> *gen_pentagonal(int pos);
inline bool pentagonal_elem(int pos, int &elem);
int main()
{
int elem;
if (pentagonal_elem(8, elem))
{
cout << "element 8 is " << elem << endl;
}
if (pentagonal_elem(88, elem))
{
cout << "element 88 is " << elem << endl;
}
if (pentagonal_elem(12, elem))
{
cout << "element 12 is " << elem << endl;
}
if (pentagonal_elem(64, elem))
{
cout << "element 64 is " << elem << endl;
}
return 0;
}
inline const vector<int> *gen_pentagonal(int pos)
{
static vector<int> pent;
if (pos > 0 && pos <= 64 && pos > pent.size())
{
for (int i = pent.size() + 1; i <= pos; ++i)
{
pent.push_back((i * (3 * i - 1)) / 2);
}
}
return &pent;
}
inline bool pentagonal_elem(int pos, int &elem)
{
if (pos <= 0 || pos > 64)
{
cerr << "Sorry. Invalid position: " << pos << endl;
elem = 0;
return false;
}
const vector<int> *pent = gen_pentagonal(pos);
elem = (*pent)[pos - 1];
return true;
}
//-----------------------------------------------------------;
//-----------------------------------------------------------;
//Practise2.5.cpp
#include <iostream>
#include <vector>
#include <string>
#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(const string &t1, const string &t2)
{
return t1 > t2 ? t1 : t2;
}
inline int max(const vector<int> &vec)
{
return *max_element(vec.begin(), vec.end());
}
inline float max(const vector<float> &vec)
{
return *max_element(vec.begin(), vec.end());
}
inline string max(const vector<string> &vec)
{
return *max_element(vec.begin(), vec.end());
}
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()
{
string sarray[6] = {"we", "were", "her", "pride", "of", "ten"};
vector<string> svec(sarray, sarray + 6);
int iarray[7] = {12, 70, 2, 169, 1, 5, 29};
vector<int> ivec(iarray, iarray + 7);
float farray[5] = {2.5, 24.8, 18.7, 4.1, 23.9};
vector<float> fvec(farray, farray + 5);
int imax = max(max(ivec), max(iarray, 7));
float fmax = max(max(fvec), max(farray, 5));
string smax = max(max(svec), max(sarray, 6));
cout << "imax should be 169 -- found: " << imax << "\n";
cout << "fmax should be 24.8 -- found: " << fmax << "\n";
cout << "smax should be were -- found: " << smax << "\n";
return 0;
}
//-----------------------------------------------------------;
//-----------------------------------------------------------;
//Practise2.6.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
template <typename T>
T max(T &t1, T &t2)
{
return t1 > t2 ? t1 : t2;
}
template <typename elemtype>
elemtype max(const vector<elemtype> &t)
{
return *max_element(t.begin(), t.end());
}
template <typename arraytype>
arraytype max(const arraytype parray[], int size)
{
return *max_element(parray, parray + size);
}
int main()
{
string sarray[6] = {"we", "were", "her", "pride", "of", "ten"};
vector<string> svec(sarray, sarray + 6);
int iarray[7] = {12, 70, 2, 169, 1, 5, 29};
vector<int> ivec(iarray, iarray + 7);
float farray[5] = {2.5, 24.8, 18.7, 4.1, 23.9};
vector<float> fvec(farray, farray + 5);
int imax = max(max(ivec), max(iarray, 7));
float fmax = max(max(fvec), max(farray, 5));
string smax = max(max(svec), max(sarray, 6));
cout << "imax should be 169 -- found: " << imax << "\n";
cout << "fmax should be 24.8 -- found: " << fmax << "\n";
cout << "smax should be were -- found: " << smax << "\n";
return 0;
}
//-----------------------------------------------------------;
//--------------------2021年6月19日 -------------------------