Skip to content

Commit

Permalink
提交chapter07
Browse files Browse the repository at this point in the history
  • Loading branch information
ShujiaHuang committed Aug 5, 2021
1 parent 792e058 commit 99fce4c
Show file tree
Hide file tree
Showing 21 changed files with 1,164 additions and 1 deletion.
341 changes: 340 additions & 1 deletion practice/booknotes.md

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions source/chapter07/arfupt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// arfupt.cpp -- an array of function pointers
#include <iostream>
// various notations, same signatures
const double * f1(const double ar[], int n);
const double * f2(const double [], int);
const double * f3(const double *, int);

int main()
{
using namespace std;
double av[3] = {1112.3, 1542.6, 2227.9};

// pointer to a function
const double *(*p1)(const double *, int) = f1;
auto p2 = f2; // C++0x automatic type deduction
// pre-C++0x can use the following code instead
// const double *(*p2)(const double *, int) = f2;
cout << "Using pointers to functions:\n";
cout << " Address Value\n";
cout << (*p1)(av,3) << ": " << *(*p1)(av,3) << endl;
cout << p2(av,3) << ": " << *p2(av,3) << endl;

// pa an array of pointers
// auto doesn't work with list initialization
const double *(*pa[3])(const double *, int) = {f1,f2,f3};
// but it does work for initializing to a single value
// pb a pointer to first element of pa
auto pb = pa;
// pre-C++0x can use the following code instead
// const double *(**pb)(const double *, int) = pa;
cout << "\nUsing an array of pointers to functions:\n";
cout << " Address Value\n";
for (int i = 0; i < 3; i++)
cout << pa[i](av,3) << ": " << *pa[i](av,3) << endl;
cout << "\nUsing a pointer to a pointer to a function:\n";
cout << " Address Value\n";
for (int i = 0; i < 3; i++)
cout << pb[i](av,3) << ": " << *pb[i](av,3) << endl;

// what about a pointer to an array of function pointers
cout << "\nUsing pointers to an array of pointers:\n";
cout << " Address Value\n";
// easy way to declare pc
auto pc = &pa;
// pre-C++0x can use the following code instead
// const double *(*(*pc)[3])(const double *, int) = &pa;
cout << (*pc)[0](av,3) << ": " << *(*pc)[0](av,3) << endl;
// hard way to declare pd
const double *(*(*pd)[3])(const double *, int) = &pa;
// store return value in pdb
const double * pdb = (*pd)[1](av,3);
cout << pdb << ": " << *pdb << endl;
// alternative notation
cout << (*(*pd)[2])(av,3) << ": " << *(*(*pd)[2])(av,3) << endl;
// cin.get();
return 0;
}

// some rather dull functions

const double * f1(const double * ar, int n)
{
return ar;
}
const double * f2(const double ar[], int n)
{
return ar+1;
}
const double * f3(const double ar[], int n)
{
return ar+2;
}
67 changes: 67 additions & 0 deletions source/chapter07/arfupt1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// arfupt.cpp -- an array of function pointers
#include <iostream>
// various notations, same signatures
const double * f1(const double ar[], int n);
const double * f2(const double [], int);
const double * f3(const double *, int);

int main()
{
using namespace std;
double av[3] = {1112.3, 1542.6, 2227.9};

// pointer to a function
typedef const double *(*p_fun)(const double *, int);
p_fun p1 = f1;
auto p2 = f2; // C++0x automatic type deduction
cout << "Using pointers to functions:\n";
cout << " Address Value\n";
cout << (*p1)(av,3) << ": " << *(*p1)(av,3) << endl;
cout << p2(av,3) << ": " << *p2(av,3) << endl;

// pa an array of pointers
p_fun pa[3] = {f1,f2,f3};
// auto doesn't work with list initialization
// but it does work for initializing to a single value
// pb a pointer to first element of pa
auto pb = pa;
cout << "\nUsing an array of pointers to functions:\n";
cout << " Address Value\n";
for (int i = 0; i < 3; i++)
cout << pa[i](av,3) << ": " << *pa[i](av,3) << endl;
cout << "\nUsing a pointer to a pointer to a function:\n";
cout << " Address Value\n";
for (int i = 0; i < 3; i++)
cout << pb[i](av,3) << ": " << *pb[i](av,3) << endl;

// what about a pointer to an array of function pointers
cout << "\nUsing pointers to an array of pointers:\n";
cout << " Address Value\n";
// easy way to declare pc
auto pc = &pa;
cout << (*pc)[0](av,3) << ": " << *(*pc)[0](av,3) << endl;
// slightly harder way to declare pd
p_fun (*pd)[3] = &pa;
// store return value in pdb
const double * pdb = (*pd)[1](av,3);
cout << pdb << ": " << *pdb << endl;
// alternative notation
cout << (*(*pd)[2])(av,3) << ": " << *(*(*pd)[2])(av,3) << endl;
// cin.get();
return 0;
}

// some rather dull functions

const double * f1(const double * ar, int n)
{
return ar;
}
const double * f2(const double ar[], int n)
{
return ar+1;
}
const double * f3(const double ar[], int n)
{
return ar+2;
}
26 changes: 26 additions & 0 deletions source/chapter07/arrfun1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// arrfun1.cpp -- functions with an array argument
#include <iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n); // prototype
int main()
{
using namespace std;
int cookies[ArSize] = {1,2,4,8,16,32,64,128};
// some systems require preceding int with static to
// enable array initialization

int sum = sum_arr(cookies, ArSize);
cout << "Total cookies eaten: " << sum << "\n";
// cin.get();
return 0;
}

// return the sum of an integer array
int sum_arr(int arr[], int n)
{
int total = 0;

for (int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
37 changes: 37 additions & 0 deletions source/chapter07/arrfun2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// arrfun2.cpp -- functions with an array argument
#include <iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n);
// use std:: instead of using directive
int main()
{
int cookies[ArSize] = {1,2,4,8,16,32,64,128};
// some systems require preceding int with static to
// enable array initialization

std::cout << cookies << " = array address, ";
// some systems require a type cast: unsigned (cookies)

std::cout << sizeof cookies << " = sizeof cookies\n";
int sum = sum_arr(cookies, ArSize);
std::cout << "Total cookies eaten: " << sum << std::endl;
sum = sum_arr(cookies, 3); // a lie
std::cout << "First three eaters ate " << sum << " cookies.\n";
sum = sum_arr(cookies + 4, 4); // another lie
std::cout << "Last four eaters ate " << sum << " cookies.\n";
// std::cin.get();
return 0;
}

// return the sum of an integer array
int sum_arr(int arr[], int n)
{
int total = 0;
std::cout << arr << " = arr, ";
// some systems require a type cast: unsigned (arr)

std::cout << sizeof arr << " = sizeof arr\n";
for (int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
78 changes: 78 additions & 0 deletions source/chapter07/arrfun3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// arrfun3.cpp -- array functions and const
#include <iostream>
const int Max = 5;

// function prototypes
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n); // don't change data
void revalue(double r, double ar[], int n);

int main()
{
using namespace std;
double properties[Max];

int size = fill_array(properties, Max);
show_array(properties, size);
if (size > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;
}

int fill_array(double ar[], int limit)
{
using namespace std;
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0) // signal to terminate
break;
ar[i] = temp;
}
return i;
}

// the following function can use, but not alter,
// the array whose address is ar
void show_array(const double ar[], int n)
{
using namespace std;
for (int i = 0; i < n; i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
}
}

// multiplies each element of ar[] by r
void revalue(double r, double ar[], int n)
{
for (int i = 0; i < n; i++)
ar[i] *= r;
}
31 changes: 31 additions & 0 deletions source/chapter07/arrfun4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// arrfun4.cpp -- functions with an array range
#include <iostream>
const int ArSize = 8;
int sum_arr(const int * begin, const int * end);
int main()
{
using namespace std;
int cookies[ArSize] = {1,2,4,8,16,32,64,128};
// some systems require preceding int with static to
// enable array initialization

int sum = sum_arr(cookies, cookies + ArSize);
cout << "Total cookies eaten: " << sum << endl;
sum = sum_arr(cookies, cookies + 3); // first 3 elements
cout << "First three eaters ate " << sum << " cookies.\n";
sum = sum_arr(cookies + 4, cookies + 8); // last 4 elements
cout << "Last four eaters ate " << sum << " cookies.\n";
// cin.get();
return 0;
}

// return the sum of an integer array
int sum_arr(const int * begin, const int * end)
{
const int * pt;
int total = 0;

for (pt = begin; pt != end; pt++)
total = total + *pt;
return total;
}
40 changes: 40 additions & 0 deletions source/chapter07/arrobj.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//arrobj.cpp -- functions with array objects
#include <iostream>
#include <array>
#include <string>
const int Seasons = 4;
const std::array<std::string, Seasons> Snames =
{"Spring", "Summer", "Fall", "Winter"};

void fill(std::array<double, Seasons> * pa);
void show(std::array<double, Seasons> da);
int main()
{
std::array<double, 4> expenses;
fill(&expenses);
show(expenses);
// std::cin.get();
// std::cin.get();
return 0;
}

void fill(std::array<double, Seasons> * pa)
{
for (int i = 0; i < Seasons; i++)
{
std::cout << "Enter " << Snames[i] << " expenses: ";
std::cin >> (*pa)[i];
}
}

void show(std::array<double, Seasons> da)
{
double total = 0.0;
std::cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
std::cout << Snames[i] << ": $" << da[i] << '\n';
total += da[i];
}
std::cout << "Total: $" << total << '\n';
}
21 changes: 21 additions & 0 deletions source/chapter07/calling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// calling.cpp -- defining, prototyping, and calling a function
#include <iostream>

void simple(); // function prototype

int main()
{
using namespace std;
cout << "main() will call the simple() function:\n";
simple(); // function call
cout << "main() is finished with the simple() function.\n";
// cin.get();
return 0;
}

// function definition
void simple()
{
using namespace std;
cout << "I'm but a simple function.\n";
}
Loading

0 comments on commit 99fce4c

Please sign in to comment.