forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
792e058
commit 99fce4c
Showing
21 changed files
with
1,164 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
Oops, something went wrong.