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
28c7a58
commit a1f5253
Showing
17 changed files
with
765 additions
and
0 deletions.
There are no files selected for viewing
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,28 @@ | ||
// arrtemp.cpp -- using template functions with array template | ||
#include <iostream> | ||
#include <array> | ||
#include <string> | ||
template <class T, size_t n> | ||
void display(const std::array<T, n> & ar); | ||
int main() | ||
{ | ||
std::array<int, 5> ai = {1,2,3,4,5}; //,6,7,8,9,22}; | ||
std::array<std::string, 5> as = | ||
{ | ||
"string under construction", | ||
"stupid string indeed", | ||
"there's nothing to see", | ||
"nothing to do", | ||
"but enjoy all that is" | ||
}; | ||
display(ai); | ||
display(as); | ||
// std::cin.get(); | ||
return 0; | ||
} | ||
template <class T, size_t n> | ||
void display(const std::array<T, n> & ar) | ||
{ | ||
for (int i = 0; i < 5; i++) | ||
std::cout << ar[i] << std::endl; | ||
} |
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,32 @@ | ||
// choices.cpp -- choosing a template | ||
#include <iostream> | ||
|
||
template<class T> | ||
T lesser(T a, T b) // #1 | ||
{ | ||
return a < b ? a : b; | ||
} | ||
|
||
int lesser (int a, int b) // #2 | ||
{ | ||
a = a < 0 ? -a : a; | ||
b = b < 0 ? -b : b; | ||
return a < b ? a : b; | ||
} | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
int m = 20; | ||
int n = -30; | ||
double x = 15.5; | ||
double y = 25.9; | ||
|
||
cout << lesser(m, n) << endl; // use #2 | ||
cout << lesser(x, y) << endl; // use #1 with double | ||
cout << lesser<>(m, n) << endl; // use #1 with int | ||
cout << lesser<int>(x, y) << endl; // use #1 with int | ||
|
||
// cin.get(); | ||
return 0; | ||
} |
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,28 @@ | ||
// cubes.cpp -- regular and reference arguments | ||
#include <iostream> | ||
double cube(double a); | ||
double refcube(double &ra); | ||
int main () | ||
{ | ||
using namespace std; | ||
double x = 3.0; | ||
|
||
cout << cube(x); | ||
cout << " = cube of " << x << endl; | ||
cout << refcube(x); | ||
cout << " = cube of " << x << endl; | ||
// cin.get(); | ||
return 0; | ||
} | ||
|
||
double cube(double a) | ||
{ | ||
a *= a * a; | ||
return a; | ||
} | ||
|
||
double refcube(double &ra) | ||
{ | ||
ra *= ra * ra; | ||
return ra; | ||
} |
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,61 @@ | ||
//filefunc.cpp -- function with ostream & parameter | ||
#include <iostream> | ||
#include <fstream> | ||
#include <cstdlib> | ||
using namespace std; | ||
|
||
void file_it(ostream & os, double fo, const double fe[],int n); | ||
const int LIMIT = 5; | ||
int main() | ||
{ | ||
ofstream fout; | ||
const char * fn = "ep-data.txt"; | ||
fout.open(fn); | ||
if (!fout.is_open()) | ||
{ | ||
cout << "Can't open " << fn << ". Bye.\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
double objective; | ||
cout << "Enter the focal length of your " | ||
"telescope objective in mm: "; | ||
cin >> objective; | ||
double eps[LIMIT]; | ||
cout << "Enter the focal lengths, in mm, of " << LIMIT | ||
<< " eyepieces:\n"; | ||
for (int i = 0; i < LIMIT; i++) | ||
{ | ||
cout << "Eyepiece #" << i + 1 << ": "; | ||
cin >> eps[i]; | ||
} | ||
file_it(fout, objective, eps, LIMIT); | ||
file_it(cout, objective, eps, LIMIT); | ||
cout << "Done\n"; | ||
// cin.get(); | ||
// cin.get(); | ||
return 0; | ||
} | ||
|
||
void file_it(ostream & os, double fo, const double fe[],int n) | ||
{ | ||
// save initial formatting state | ||
ios_base::fmtflags initial; | ||
initial = os.setf(ios_base::fixed, ios_base::floatfield); | ||
std::streamsize sz = os.precision(0); | ||
os << "Focal length of objective: " << fo << " mm\n"; | ||
os.precision(1); | ||
os.width(12); | ||
os << "f.l. eyepiece"; | ||
os.width(15); | ||
os << "magnification" << endl; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
os.width(12); | ||
os << fe[i]; | ||
os.width(15); | ||
os << int (fo/fe[i] + 0.5) << endl; | ||
} | ||
// restore initial formatting state | ||
os.setf(initial, ios_base::floatfield); | ||
os.precision(sz); | ||
} |
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 @@ | ||
// firstref.cpp -- defining and using a reference | ||
#include <iostream> | ||
int main() | ||
{ | ||
using namespace std; | ||
int rats = 101; | ||
int & rodents = rats; // rodents is a reference | ||
|
||
cout << "rats = " << rats; | ||
cout << ", rodents = " << rodents << endl; | ||
rodents++; | ||
cout << "rats = " << rats; | ||
cout << ", rodents = " << rodents << endl; | ||
|
||
// some implementations require type casting the following | ||
// addresses to type unsigned | ||
cout << "rats address = " << &rats; | ||
cout << ", rodents address = " << &rodents << endl; | ||
// cin.get(); | ||
return 0; | ||
} |
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,35 @@ | ||
// funtemp.cpp -- using a function template | ||
#include <iostream> | ||
// function template prototype | ||
template <typename T> // or class T | ||
void Swap(T &a, T &b); | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
int i = 10; | ||
int j = 20; | ||
cout << "i, j = " << i << ", " << j << ".\n"; | ||
cout << "Using compiler-generated int swapper:\n"; | ||
Swap(i,j); // generates void Swap(int &, int &) | ||
cout << "Now i, j = " << i << ", " << j << ".\n"; | ||
|
||
double x = 24.5; | ||
double y = 81.7; | ||
cout << "x, y = " << x << ", " << y << ".\n"; | ||
cout << "Using compiler-generated double swapper:\n"; | ||
Swap(x,y); // generates void Swap(double &, double &) | ||
cout << "Now x, y = " << x << ", " << y << ".\n"; | ||
// cin.get(); | ||
return 0; | ||
} | ||
|
||
// function template definition | ||
template <typename T> // or class T | ||
void Swap(T &a, T &b) | ||
{ | ||
T temp; // temp a variable of type T | ||
temp = a; | ||
a = b; | ||
b = temp; | ||
} |
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,20 @@ | ||
// inline.cpp -- using an inline function | ||
#include <iostream> | ||
|
||
// an inline function definition | ||
inline double square(double x) { return x * x; } | ||
int main() | ||
{ | ||
using namespace std; | ||
double a, b; | ||
double c = 13.0; | ||
|
||
a = square(5.0); | ||
b = square(4.5 + 7.5); // can pass expressions | ||
cout << "a = " << a << ", b = " << b << "\n"; | ||
cout << "c = " << c; | ||
cout << ", c squared = " << square(c++) << "\n"; | ||
cout << "Now c = " << c << "\n"; | ||
// cin.get(); | ||
return 0; | ||
} |
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,35 @@ | ||
// left.cpp -- string function with a default argument | ||
#include <iostream> | ||
const int ArSize = 80; | ||
char * left(const char * str, int n = 1); | ||
int main() | ||
{ | ||
using namespace std; | ||
char sample[ArSize]; | ||
cout << "Enter a string:\n"; | ||
cin.get(sample,ArSize); | ||
char *ps = left(sample, 4); | ||
cout << ps << endl; | ||
delete [] ps; // free old string | ||
ps = left(sample); | ||
cout << ps << endl; | ||
delete [] ps; // free new string | ||
// cin.get(); | ||
// cin.get(); | ||
return 0; | ||
} | ||
|
||
// This function returns a pointer to a new string | ||
// consisting of the first n characters in the str string. | ||
char * left(const char * str, int n) | ||
{ | ||
if(n < 0) | ||
n = 0; | ||
char * p = new char[n+1]; | ||
int i; | ||
for (i = 0; i < n && str[i]; i++) | ||
p[i] = str[i]; // copy characters | ||
while (i <= n) | ||
p[i++] = '\0'; // set rest of string to '\0' | ||
return p; | ||
} |
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,60 @@ | ||
// leftover.cpp -- overloading the left() function | ||
#include <iostream> | ||
unsigned long left(unsigned long num, unsigned ct); | ||
char * left(const char * str, int n = 1); | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
char * trip = "Hawaii!!"; // test value | ||
unsigned long n = 12345678; // test value | ||
int i; | ||
char * temp; | ||
|
||
for (i = 1; i < 10; i++) | ||
{ | ||
cout << left(n, i) << endl; | ||
temp = left(trip,i); | ||
cout << temp << endl; | ||
delete [] temp; // point to temporary storage | ||
} | ||
// cin.get(); | ||
return 0; | ||
|
||
} | ||
|
||
// This function returns the first ct digits of the number num. | ||
unsigned long left(unsigned long num, unsigned ct) | ||
{ | ||
unsigned digits = 1; | ||
unsigned long n = num; | ||
|
||
if (ct == 0 || num == 0) | ||
return 0; // return 0 if no digits | ||
while (n /= 10) | ||
digits++; | ||
if (digits > ct) | ||
{ | ||
ct = digits - ct; | ||
while (ct--) | ||
num /= 10; | ||
return num; // return left ct digits | ||
} | ||
else // if ct >= number of digits | ||
return num; // return the whole number | ||
} | ||
|
||
// This function returns a pointer to a new string | ||
// consisting of the first n characters in the str string. | ||
char * left(const char * str, int n) | ||
{ | ||
if(n < 0) | ||
n = 0; | ||
char * p = new char[n+1]; | ||
int i; | ||
for (i = 0; i < n && str[i]; i++) | ||
p[i] = str[i]; // copy characters | ||
while (i <= n) | ||
p[i++] = '\0'; // set rest of string to '\0' | ||
return p; | ||
} |
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,25 @@ | ||
// secref.cpp -- defining and using a reference | ||
#include <iostream> | ||
int main() | ||
{ | ||
using namespace std; | ||
int rats = 101; | ||
int & rodents = rats; // rodents is a reference | ||
|
||
cout << "rats = " << rats; | ||
cout << ", rodents = " << rodents << endl; | ||
|
||
cout << "rats address = " << &rats; | ||
cout << ", rodents address = " << &rodents << endl; | ||
|
||
int bunnies = 50; | ||
rodents = bunnies; // can we change the reference? | ||
cout << "bunnies = " << bunnies; | ||
cout << ", rats = " << rats; | ||
cout << ", rodents = " << rodents << endl; | ||
|
||
cout << "bunnies address = " << &bunnies; | ||
cout << ", rodents address = " << &rodents << endl; | ||
// cin.get(); | ||
return 0; | ||
} |
Oops, something went wrong.