c++ Primer Plus (第六版)

#if 0
#include
int main(){
unsigned int yards {}, feet{}, inches {};
std::cout << "Enter a distance as yards, feet, and inches "
<< “with the three values separated by spaces:”
<< std::endl;
std::cin >> yards >> feet >> inches;
const unsigned feet_per_yard {3};
const unsigned inches_per_foot {12};
unsigned total_inches {};
total_inches = inches + inches_per_foot * (yards * feet_per_yard + feet);
std::cout << “The distances corresponds to " << total_inches << " inches.\n”;

std::cout << "Enter a distance in inches: ";
std::cin >> total_inches;
feet = total_inches / inches_per_foot;
inches = total_inches % inches_per_foot;
yards = feet / feet_per_yard;
feet = feet % feet_per_yard;
std::cout << "The distances corresponds to "
<< yards << " yards "
<< feet << " feet "
<< inches << " inches. " << std::endl;

}
#endif

#if 0

#include
#include
int main(){
char letter {};
std::cout << "Enter a letter: ";
std::cin >> letter;
if(std::isalpha(letter)){
switch (std::tolower(letter)){
case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’:
std::cout << "You entered a vowel. " << std::endl;
break;
default:
std::cout << “You entered a consonant.” << std::endl;
break;
}
}
else{
std::cout << “You did not enter a letter.” << std::endl;
}
}

#endif

#if 0
#include
int main(){
const unsigned size {6};
unsigned height [size] {26, 37, 47, 55, 62, 75};
unsigned total {};
for(size_t i {}; i < size; ++i){
total += height[i];
}
const unsigned average {total /size};
std::cout << “The average height is " << average << std::endl;
unsigned count {};
for(size_t i {}; i < size; ++i){
if(height[i] < average) ++count;
}
std::cout << count << " people are below average height.” << std::endl;
}
#endif

#if 0
#include
#include
#include //setw()
int main(){
int values[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
std::cout << “There are " << sizeof(values) / sizeof(values[0])
<< " elements in the array.” << std::endl;
int sum {};
for(size_t i {}; i < std::size(values); ++i){
sum += values[i];
}
std::cout << "The sum of the array elements is " << sum << std::endl;

const double pi {3.14159265358979323846};
for(double radius {2.5}; radius <= 20.0; radius += 2.5){
std::cout << "radius = " << std::setw(12) << radius
<< " area = " << std::setw(12)
<< pi * radius * radius << std::endl;
}const size_t perline {3};
size_t linecount {};
for(double radius {0.2}; radius <= 3.0+ 0.0001; radius += .2){
std::cout << std::fixed << std::setprecision (2)
<< " radius = " << std::setw(5) << radius
<< " area = " << std::setw(6) << pi * radius * radius
<< " delta to 3 = " << std::scientific << ((radius + 0.2) - 3.0) << std::endl;
if (perline == ++linecount){
std::cout << std::endl;
linecount = 0;}
}
std::cout << std::endl;

}

/*
There are 10 elements in the array.
The sum of the array elements is 129
radius = 2.5 area = 19.635
radius = 5 area = 78.5398
radius = 7.5 area = 176.715
radius = 10 area = 314.159
radius = 12.5 area = 490.874
radius = 15 area = 706.858
radius = 17.5 area = 962.113
radius = 20 area = 1256.64

radius = 0.20 area = 0.13 radius = 0.40 area = 0.50 radius = 0.60 area = 1.13
radius = 0.80 area = 2.01 radius = 1.00 area = 3.14 radius = 1.20 area = 4.52
radius = 1.40 area = 6.16 radius = 1.60 area = 8.04 radius = 1.80 area = 10.18
radius = 2.00 area = 12.57 radius = 2.20 area = 15.21 radius = 2.40 area = 18.10
radius = 2.60 area = 21.24 radius = 2.80 area = 24.63 //少radius = 3.0

radius = 0.20 area = 0.13 delta to 3 = -2.60e+00
radius = 0.40 area = 0.50 delta to 3 = -2.40e+00
radius = 0.60 area = 1.13 delta to 3 = -2.20e+00

radius = 0.80 area = 2.01 delta to 3 = -2.00e+00
radius = 1.00 area = 3.14 delta to 3 = -1.80e+00
radius = 1.20 area = 4.52 delta to 3 = -1.60e+00

radius = 1.40 area = 6.16 delta to 3 = -1.40e+00
radius = 1.60 area = 8.04 delta to 3 = -1.20e+00
radius = 1.80 area = 10.18 delta to 3 = -1.00e+00

radius = 2.00 area = 12.57 delta to 3 = -8.00e-01
radius = 2.20 area = 15.21 delta to 3 = -6.00e-01
radius = 2.40 area = 18.10 delta to 3 = -4.00e-01

radius = 2.60 area = 21.24 delta to 3 = -2.00e-01
radius = 2.80 area = 24.63 delta to 3 = 4.44e-16
radius = 3.00 area = 28.27 delta to 3 = 2.00e-01

*/
#endif

#if 0 //p94 2020年12月24日 星期四 19时37分23秒
#include
#include
int main(){
int i {1};
int value1 {1};
int value2 {2};
int value3 {3};
std::cout << (value1 += ++i, value2 += ++i, value3 += ++i) << std::endl;
// value1 = ++(1+1) = 3, value2 = ++(1+3) = 5, value3 = ++(1+5) = 7
unsigned int limit {};
std::cout << “This program calculates n! and the sum of the integers”
<< " up to n for values 1 to limit.\n";
std::cout << "What upper limit for n would you like? ";
std::cin >> limit;

std::cout << std::setw(8) << "integer" << std::setw(8) << " sum"
<< std::setw(20) << " factorial " << std::endl;
for(unsigned long long n {1}, sum {}, factorial {1}; n <= limit; ++n){
sum += n;
factorial *= n;
std::cout << std::setw(8) << n << std::setw(8) << sum
<< std::setw(20) << factorial << std::endl;
}

}
/*
wannian07@wannian07-PC:~/Desktop$ g++ -std=c++17 hello.cpp -o hello
wannian07@wannian07-PC:~/Desktop$ ./hello
7
This program calculates n! and the sum of the integers up to n for values 1 to limit.
What upper limit for n would you like? 10
integer sum factorial
1 1 1
2 3 2
3 6 6
4 10 24
5 15 120
6 21 720
7 28 5040
8 36 40320
9 45 362880
10 55 3628800

*/
#endif

#if 0 // p94
#include
#include
#include //setw()
int main(){
int values[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
int total {};
for(int x : values){
total += x;
}
std::cout << "total = " << total << std::endl;
}
#endif

#if 0
#include
int main(){
double dval;
double *pd = &dval;
double *pd2 = pd;

int *pi = pd;
pi = &dval;

}

#endif

#if 0
#include
#include “stock00.h”

int main(){
std::cout << “What’s up, Doc!\n”;
return 0;
}
#endif

#if 0 //myfirst.cpp
#include

int main(){
std::cout << “Come up and C++ me some time.”;
std::cout << std::endl;
std::cout << “You won’t regret it!” <<std::endl;
return 0;
}

#endif

#if 0 //carrot.cpp
#include

int main(){
int carrots;
carrots = 25;
std::cout << "I have ";
std::cout << carrots;
std::cout << " carrots. ";
std::cout << std::endl;
carrots = carrots - 1;
std::cout << "Crunch, crunch. Now I have " << carrots << " carrots. " << std::endl;
return 0;
}

#endif

#if 0 //getinfo.cpp
#include

int main(){
int carrots;
std::cout << “How many carrots do you have?” << std::endl;
std::cin >> carrots;
std::cout << "Here are two more. ";
carrots = carrots + 2;
std::cout << "Now you have " << carrots << " carrots. " << std::endl;
return 0;
}

#endif

#if 0 //2.4 sqrt.cpp p45/953
#include
#include
int main(){
double area;
std::cout << "Enter the floor area, in square feet, of your home: ";
std::cin >> area;
double side;
side = sqrt(area);
std::cout << “That’s the equivalent of a square "
<< side << " feet to the side.” << std::endl;
std::cout << “How fascinating!” << std::endl;
return 0;
}

#endif

#if 0
#include
void simon(int);

int main(){
simon(3);
std::cout << "Pick an integer: ";
int count;
std::cin >> count;
simon(count);
std::cout << "Done! " << std::endl;
return 0;
}

void simon(int n){
std::cout << “Simon says touch your toes " << n << " times.” << std::endl;
}
#endif

#if 0 //convert.cpp
#include
int stonetolb(int);

int main(){
int stone;
std::cout << "Enter the weight in stone: ";
std::cin >> stone;
int pounds = stonetolb(stone);
std::cout << stone << " stone = ";
std::cout << pounds << " pounds. " << std::endl;
return 0;
}

int stonetolb(int sts){
return 14 * sts;
}
#endif

#if 0 //limits.cpp
#include
#include
int main(){
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
std::cout << “int is " << sizeof(int) << " bytes.” << std::endl;
std::cout << “short is " << sizeof n_short << " bytes.” << std::endl;
std::cout << “long is " << sizeof n_long << " bytes.” << std::endl;
std::cout << “long long is " << sizeof n_llong << " bytes.” << std::endl;

std::cout << "Maximum values: " << std::endl;
std::cout << "int: " << n_int << std::endl;
std::cout << "short: " << n_short << std::endl;
std::cout << "long: " << n_long << std::endl;
std::cout << "long long: " << n_llong << std::endl;std::cout << "Maximum int value = " << INT_MIN << std::endl;
std::cout << "Bits per byte = " << CHAR_BIT << std::endl;
return 0;

}

#endif

#if 0 // exceed.cpp
#include
#define ZERO 0
#include

int main(){
short sam = SHRT_MAX;
unsigned short sue = sam;
std::cout << “Sam has " << sam << " dollars and Sue has " << sue;
std::cout << " dollars deposited.” << std::endl
<< "Add $1 to each account. " << std::endl << "Now ";
sam = sam + 1;
sue = sue + 1;
std::cout << “Sam has " << sam << " dollars and Sue has " << sue;
std::cout << " dollars deposited.\nPoor Sam!” << std::endl;
sam = ZERO;
sue = ZERO;
std::cout << “Sam has " << sam << " dollars and Sue has " << sue;
std::cout << " dollars deposited.” << std::endl;
std::cout << "Take $1 from each account. " << std::endl << "Now ";
sam = sam -1;
sue = sue -1;
std::cout << “Sam has " << sam << " dollars and Sue has " << sue;
std::cout << " dollars deposited.” << std::endl << “Lucky Sue!” << std::endl;
return 0;
}

#endif

#if 0 //hexoct.cpp
#include

int main(){
int chest = 42;;
int waist = 0x42;
int inseam = 042;
std::cout << “Monsieur cuts a strinking figure!\n”;
std::cout << “chest = " << chest << " (42 in decimal)\n”;
std::cout << “waist = " << waist << " (0x42 in hex)\n”;
std::cout << “inseam = " << inseam << " (0x42 in octal)\n”;
return 0;
}

#endif

#if 0 //hexoct2.cpp
#include

int main(){
int chest = 42;;
int waist = 0x42;
int inseam = 042;
std::cout << “Monsieur cuts a strinking figure!” << std::endl;
std::cout << “chest = " << chest << " (decimal for 42)”;
std::cout << “waist = " << waist << " (0x42 in hex)\n”;
std::cout << “inseam = " << inseam << " (0x42 in octal)\n”;
return 0;
}

#endif

#if 0 // hexoct2.cpp
#include

int main(){
int chest = 42;
int waist = 42;
int inseam = 42;
std::cout << "Monsieur cuts a strinking figure! " << std::endl;
std::cout << "chest = " << chest << " (decimal for 42) " << std::endl;
std::cout << std::hex;
std::cout << "waist = " << waist << " (hexadecimal for 42) " << std::endl;
std::cout << std::oct;
std::cout << "inseam = " << inseam << " (octal for 42) " << std::endl;
return 0;
}

#endif

#if 0 // 3.5 chartype.cpp
#include

int main(){
char ch;
std::cout << "Enter a character: " << std::endl;
std::cin >> ch;
std::cout << "Hola! ";
std::cout << "Thank you for the " << ch << " character. " << std::endl;
return 0;
}

#endif

#if 0 // 3.6 morechar.cpp
#include

int main(){
char ch = ‘M’;
int i = ch;
std::cout << "The ASCII code for " << ch << " is " << i << std::endl;
std::cout << "Add one to the character code: " << std::endl;
ch = ch + 1;
i = ch;
std::cout << "The ASCII code for " << ch << " is " << i << std::endl;
std::cout << "Displaying char ch using cout.put(ch): ";
std::cout.put(ch);
std::cout.put(’!’);
std::cout << std::endl << “Done” << std::endl;
return 0;
}

#endif

#if 0 //3.7 bondini.cpp
#include

int main(){
std::cout << “\aOperation “HyperHype” is now activated!\n”;
std::cout << “Enter your agent code:______\b\b\b\b\b\b”;
long code;
std::cin >> code;
std::cout << "\aYou entered " << code << “…\n”;
std::cout << “\aCode verified! Proceed with Plan Z3!\n”;
return 0;
}

#endif

#if 0 //https://www.cnblogs.com/ECJTUACM-873284962/p/10747751.html
#include
#include
int main(){
auto value1 = 1;
auto value2 = 2.33;
auto value3 = ‘a’;
std::cout << "value1 的类型是 " << typeid(value1).name() << std::endl;
std::cout << "value2 的类型是 " << typeid(value2).name() << std::endl;
std::cout << "value3 的类型是 " << typeid(value3).name() << std::endl;

return 0;

}
//wannian07@wannian07-PC:~/Desktop$ ./hello
//value1 的类型是 i
//value2 的类型是 d
//value3 的类型是 c

#endif

#if 0

#include
#include
using namespace std;

std::string func(){
return “Hello World!”;
}

int main(int argc, const char *argv[])
{
decltype(func()) a;
int i;
decltype(i) b;
std::cout << "a 的类型是 " << typeid(a).name() << std::endl;
std::cout << "b 的类型是 " << typeid(b).name() << std::endl;
return 0;
}

#endif

#if 0
#include

int main(){
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];

// std::cout << “Enter your name:\n”;
// std::cin >> name;
// std::cout << “Enter your favorite dessert:\n”;
// std::cin >> dessert;
// std::cout << "I have some delicious " << dessert;
// std::cout << " for you, " << name << “.\n”;

// std::cout << “Enter your name:\n”;
// std::cin.getline(name, ArSize);
// std::cout << “Enter your favorite dessert:\n”;
// std::cin.getline(dessert, ArSize);
// std::cout << "I have some delicious " << dessert;
// std::cout << " for you, " << name << “.\n”;

// std::cout << “Enter your name:\n”;
// std::cin.get(name, ArSize).get();
// std::cout << “Enter your favorite dessert:\n”;
// std::cin.get(dessert, ArSize).get();
// std::cout << "I have some delicious " << dessert;
// std::cout << " for you, " << name << “.\n”;

std::cout << "What year was your house built?\n";
int year;
std::cin >> year;
std::cout << "What is its street address?\n";
char address[80];
std::cin.getline(address, 80);
std::cout << "Year built: " << year << std::endl;
std::cout << "Address: " << address << std::endl;
std::cout << "Done!\n";
return 0;

}

#endif

#if 0 //numstr.cpp
#include
int main(){
std::cout << “What year was your house built?\n”;
int year;
std::cin >> year;
std::cout << “What is its street address?\n”;
char address[80];
std::cin.getline(address, 80);
std::cout << "Year built: " << year << std::endl;
std::cout << "Address: " << address << std::endl;
std::cout << “Done!\n”;
return 0;
}
#endif

#if 0 // strtype1.cpp
#include
#include
int main(){
char charr1[20];
char charr2[20] = “jaguar”;
std::string str1;
std::string str2 = “panther”;

std::cout << "Enter a kind of feline: ";
std::cin >> charr1;
std::cout << "Enter another kind of feline: ";
std::cin >> str1;
std::cout << "Here are some felines:\n";
std::cout << charr1 << " " << charr2 << " "
<< str1 << " " << str2 << std::endl;
std::cout << "The third letter in " << charr2 << "  is " << charr2[2] << std::endl;
std::cout << "The third letter in " << str2 << " is "
<< str2[2] << std::endl;
return 0;

}
#endif

#if 0 //strtype2.cpp
#include
#include
int main(){
std::string s1 = “penguin”;
std::string s2, s3;
std::cout << “You can assign one string object to another: s2 = s1\n”;
s2 = s1;
std::cout << "s1: " << s1 << ", s2: " << s2 << std::endl;
std::cout << “You can assign a C-style string to a string object.\n”;
std::cout << “s2 = “buzzard”\n”;
s2 = “buzzard”;
std::cout << "s2: " << s2 << std::endl;
std::cout << “You can concatenate strings: s3 = s1 + s2\n”;
s3 = s1 + s2;
std::cout << "s3: " << s3 << std::endl;
std::cout << “You can append strings.\n”;
s1 += s2;
std::cout << "s1 += s2 yields s1 = " << s1 << std::endl;
s2 += “for a day”;
std::cout << “s2 += " for a day” yields s2 = " << s2 << std::endl;
return 0;
}
#endif

#if 0 // strtype3.cpp
#include
#include
#include
int main(){
char charr1[20];
char charr2[20] = “jaguar”;
std::string str1;
std::string str2 = “panther”;
str1 = str2;
strcpy(charr1, charr2);

str1 += " paste";
strcat(charr1, " juice");
int len1 = str1.size();
int len2 = strlen(charr1);std::cout << "The string " << str1 << " contains " <<

len1 << " characters.\n";
std::cout << “The string " << charr1 << " contains " << len2 << " characters.\n”;
return 0;
}
#endif

#if 0//strtype4.cpp
#include
#include
#include
int main() {
char charr[20];
wchar_t title[] = L"Chief Astrogator";
char16_t name[] = u"Felonia Ripova";
char32_t car[] = U"Humber Super Snipe";
std::cout << R"(Jim “King” Tutt uses “\n” instead of endl.)" << ‘\n’;
std::string str;
std::cout << "Length of string in charr before input: " << strlen(charr) << std::endl;
std::cout << "Length of string in str before input: "
<< str.size() << std::endl;
std::cout << “Enter a line of text:\n”;
std::cin.getline(charr, 20);
std::cout << "You entered: " << charr << std::endl;
std::cout << “Enter another line of text:\n”;
getline(std::cin, str);
std::cout << "You entered: " << str << std::endl;
std::cout << "Length of string in charr after input: " << strlen(charr) << std::endl;
std::cout << "Length of string in str after input: " << str.size() << std::endl;
return 0;
}
#endif

#if 0 // 4.11 structur.cpp
#include
struct inflatable
{
char name[20];
float volume;
double price;
};

int main(){
inflatable guest = {
“Glorious Goria”,
1.88,
29.99
};
inflatable pal = {
“Audacious Arthur”,
3.12,
32.99
};
std::cout << “Expand your guest list with " << guest.name;
std::cout << " and " << pal.name << " !\n”;
std::cout << “You can have both for $”;
std::cout << guest.price + pal.price << “!\n”;

return 0;

}

#endif

#if 0 // 4.12 assgn_st.cpp
#include
struct inflatable
{
char name[20];
float volume;
double price;
};

int main(){
inflatable bouquet = {
“sunflowers”,
0.20,
12.49
};
inflatable choice;
std::cout << “bouquet: " << bouquet.name << " for $”;
std::cout << bouquet.price << std::endl;

choice = bouquet;
std::cout << "choice: " << choice.name << " for $";
std::cout << choice.price << std::endl;
return 0;

}
#endif

#if 0 // 4.13 arrstruc.cpp
#include
struct inflatable{
char name[20];
float volume;
double price;
};

int main(){
inflatable guests[2] = {
{“Bambi”, 0.5, 21.99},
{“Godzilla”, 2000, 564.99}
};

std::cout << "The guests " << guests[0].name << " and " << "\nhave a combined volume of " <<

guests[0].volume + guests[1].volume << " cubic feet.\n";
}
#endif

#if 0 //4.14 address.cpp
#include
int main(){
int donuts = 6;
double cups = 4.5;
std::cout << "donuts value = " << donuts;
std::cout << " and donuts address = " << &donuts << std::endl;
std::cout << "cups value = " << cups;
std::cout << " and cups address = " << &cups << std::endl;
return 0;
}
#endif

#if 0 // 4.15 pointer.cpp
#include
int main(){
int updates = 6;
int * p_updates;
p_updates = &updates;
std::cout << "Values: updates = " << updates;
std::cout << ", *p_updates = " << *p_updates << std::endl;
std::cout << "Addresses: &updates = " << &updates;
std::cout << ", p_updates = " << p_updates << std::endl;
*p_updates = *p_updates + 1;
std::cout << "Now updates = " << updates << std::endl;
}
#endif

#if 0 // 4.16 init_ptr.cpp
#include
int main(){
int higgens = 5;
int * pt = & higgens;
std::cout << "Value of higgens = " << higgens <<
"; Address of higgens = " << &higgens << std::endl;
std::cout << "Value of *pt = " << *pt << "; Value of pt = " << pt << std::endl;
}
#endif

#if 0 //4.17 use_new.cpp
#include
int main(){
int nights = 1001;
int *pt = new int;
*pt = 1001;
std::cout << "nights value = ";
std::cout << nights << ": location " << &nights << std::endl;
std::cout << "int ";
std::cout << "value = " << *pt << ": location = " << pt << std::endl;
double *pd = new double;
*pd = 10000001.0;
std::cout << "double ";
std::cout << "value = " << *pd << ": location = " << pd << std::endl;
std::cout << "location of pointer pd: " << &pd << std::endl;
std::cout << "size of pt = " << sizeof(pt);
std::cout << ": size of *pt = " << sizeof(*pt) << std::endl;
std::cout << "size of pd = " << sizeof pd;
std::cout << ": size of *pd = " << sizeof(*pd) << std::endl;
}
#endif

#if 0 // 4.18 arraynew.cpp
#include
int main(){
double * p3 = new double [3];
p3[0] = 0.2;
p3[1] = 0.5;
p3[2] = 0.8;
std::cout << "p3[1] is " << p3[1] << “.\n”;
p3 = p3 + 1;
std::cout << "Now p3[0] is " << p3[0] << " and " ;
std::cout << "p3[1] is " << p3[1] << “.\n”;
p3 = p3 - 1;
delete [] p3;
return 0;
}
#endif

#if 0 // 4.19 addpntrs.cpp
#include
int main(){
double wages[3] = {10000.0, 20000.0, 30000.0};
short stacks[3] = {3, 2, 1};
double *pw = wages;
short *ps = &stacks[0];
std::cout << "pw = " << pw << ", *pw = " << *pw << std::endl;
pw = pw + 1;
std::cout << “add 1 to the pw pointer:\n”;
std::cout << "ps = " << ps << ", *ps = " << *ps << std::endl;
ps = ps + 1;
std::cout << “add 1 to the ps pointer:\n”;
std::cout << "ps = " << ps << ", ps = " << *ps << “\n\n”;
std::cout << “access two elements with array notation\n”;
std::cout << "stacks[0] = " << stacks[0] << ", stacks[0] = " << stacks[0] << ", stacks[1] = " << stacks[1] << std::endl;
std::cout << “access two elements with pointer notatioin\n”;
std::cout << “*stacks = " << *stacks << “, *(stacks + 1) = " << *(stacks + 1) << std::endl;
std::cout << sizeof(wages) << " = size of wages array\n”;
std::cout << sizeof(pw) << " = size of pw pointer\n”;

}
#endif

#if 0 // 4.20 ptrstr.cpp
#include
#include
int main(){
char animal[20] = “bear”;
const char * bird = “wren”;
char * ps;
std::cout << animal << " and “;
std::cout << bird << " \n”;
std::cout << "Enter a kind of animal: ";
std::cin >> animal;
ps = animal;
std::cout << ps << “!\n”;
std::cout << “Before using strcpy():\n”;
std::cout << animal << " at " << (int *) animal << std::endl;
std::cout << ps << " at " << (int *) ps << std::endl;
ps = new char[strlen(animal) + 1];
strcpy(ps, animal);
std::cout << “After using strcpy():\n”;
std::cout << animal << " at " << (int *) animal << std::endl;
std::cout << ps << " at " << (int *) ps << std::endl;
delete [] ps;
return 0;
}

#endif

#if 0 //4.21 newstrct.cpp
#include
struct inflatable {
char name[20];
float volume;
double price;
};
int main(){
inflatable * ps = new inflatable;
std::cout << "Enter name of inflatable item: ";
std::cin.get(ps->name, 20);
std::cout << "Enter volume in cubic feet: ";
std::cin >> (*ps).volume;
std::cout << “Enter price: $”;
std::cin >> ps->price;
std::cout << "Name: " << (*ps).name << std::endl;
std::cout << “Volume: " << ps->volume << " cubic feet\n”;
std::cout << “Price: $” << ps->price << std::endl;
delete ps;
return 0;
}
#endif

#if 0 //4.22 delete.cpp
#include
#include
char * getname(void);

int main(){
char * name;
name = getname();
std::cout << name << " at " << (int *)name << “\n”;
delete [] name;
name = getname();
std::cout << name << " at " << (int *)name << “\n”;
delete [] name;
return 0;
}

char * getname(){
char temp[80];
std::cout << "Enter last name: ";
std::cin >> temp;
char * pn = new char[strlen(temp) + 1];
strcpy(pn, temp);

return pn;

}

#endif

#if 0 //4.23 mixtypes.cpp
#include
struct antarctica_years_end{
int year;
};

int main(){
antarctica_years_end s01, s02, s03;
s01.year = 1998;
antarctica_years_end * pa = &s02;
pa->year = 1999;
antarctica_years_end trio[3];
trio[0].year = 2003;
std::cout << trio->year << std::endl;
const antarctica_years_end * arp[3] = { &s01, &s02, &s03};
std::cout << arp[1]->year << std::endl;
const antarctica_years_end **ppa = arp;
auto ppb = arp;
std::cout << (ppa)->year << std::endl;
std::cout << (
(ppb+1))->year << std::endl;
return 0;
}
#endif

#if 0 //4.24 choices.cpp
#include
#include
#include
int main(){
double a1[4] {1.2, 2.4, 3.6, 4.8};
std::vector a2(4);
a2[0] = 1.0/3.0;
a2[1] = 1.0/5.0;
a2[2] = 1.0/7.0;
a2[3] = 1.0/9.0;
std::array<double, 4> a3 {3.14, 2.72, 1.62, 1.4};
std::array<double, 4> a4;
a4 = a3;
std::cout << "a1[2]: " << a1[2] << " at " << &a1[2] << std::endl;
std::cout << "a2[2]: " << a2[2] << " at " << &a2[2] << std::endl;
std::cout << "a3[2]: " << a3[2] << " at " << &a3[2] << std::endl;
std::cout << "a4[2]: " << a4[2] << " at " << &a4[2] << std::endl;

a1[-2] = 20.2;
std::cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << std::endl;
std::cout << "a3[2]: " << a3[2] << " at " << &a3[2] << std::endl;
std::cout << "a4[2]: " << a4[2] << " at " << &a4[2] << std::endl;
return 0;

}
#endif

#if 0 // 5.1 forloop.cpp
#include
int main(){
int i;
for(i = 0; i < 5; i++)
std::cout << “C++ knows loops.\n”;
std::cout << “C++ knows when to stop.\n”;
return 0;
}
#endif

#if 0 // 5.2 num_test.cpp
#include
int main(){
std::cout << "Enter the starting countdown value: ";
int limit;
std::cin >> limit;
int i;
for(i = limit; i ; i–)
std::cout << "i = " << i << “\n”;
std::cout << "Done now that i = " << i << “\n”;
return 0;
}
#endif

#if 0 // 5.3 express.cpp
#include
int main(){
int x;

std::cout << "The expression x = 100 has the value ";
std::cout << (x = 100) << std::endl;
std::cout << "Now x = " << x << std::endl;
std::cout << "The expression x < 3 has the value ";
std::cout << (x < 3) << std::endl;
std::cout << "The expression x > 3 has the value ";
std::cout << (x > 3) << std::endl;
std::cout.setf(std::ios_base::boolalpha);
std::cout << "The expression x < 3 has the value ";
std::cout << (x < 3) << std::endl;
std::cout << "The expression x > 3 has the value ";
std::cout << (x > 3) << std::endl;

}
#endif

#if 0 // 5.4 formore.cpp
#include
const int ArSize = 16;

int main(){
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for(int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i -1];
for(int i = 0; i < ArSize; i++)
std::cout << i << "! = " << factorials[i] << std::endl;
return 0;
}
#endif

#if 0 // 5.5 bigstep.cpp
#include
int main(){
std::cout << "Enter an integer: ";
int by;
std::cin >> by;
std::cout << "Counting by " << by << “s:\n”;
for(int i = 0; i < 100; i = i + by)
std::cout << i << std::endl;
return 0;
}
#endif

#if 0 // 5.6 forstr1.cpp
#include
int main(){
std::cout << "Enter a word: ";
std::string word;
std::cin >> word;

for(int i = word.size()-1;i >= 0 ; i--)
std::cout << word[i];
std::cout << "\nBye.\n";
return 0;

}
#endif

#if 0 // 5.7 plus_one.cpp
#include
int main(){
int a = 20;
int b = 20;

std::cout << "a    = " << a << ":   b = " << b << "\n";
std::cout << "a++  = " << a++ << ": ++b = " << ++b << "\n";
std::cout << "a    = " << a << ":   b = " << b << "\n";
return 0;

}
#endif

#if 0 // 5.8 block.cpp
#include
int main(){
std::cout << "The Amazing Accounto will sum and average ";
std::cout << “five numbers for you.\n”;
std::cout << “Please enter five values:\n”;
double number;
double sum = 0.0;
for(int i = 1; i <= 5; i++){
std::cout << "Value " << i << ": ";
std::cin >> number;
sum += number;
}
std::cout << "Five exquisite choices indeed! ";
std::cout << "They sum to " << sum << std::endl;
std::cout << "and average to " << sum / 5 << “.\n”;
std::cout << “The Amazing Accounto birds you adieu!\n”;

return 0;

}
#endif

#if 0 // 5.9 forstr2.cpp
#include
int main(){
std::cout << "Enter a word: ";
std::string word;
std::cin >> word;
char temp;
int i, j;
for(j = 0, i = word.size()-1; j < i ; --i, ++j) {
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
std::cout << word << “\nDone\n”;

return 0;

}
#endif

#if 0 // 5.10 equal.cpp
#include
int main(){
int quizscores[10] = {20, 20, 20, 20, 20, 19, 20, 18, 20, 20};
std::cout << “Doing it right:\n”;
int i;
for(i = 0; quizscores[i] == 20; i++)
std::cout << “quiz " << i << " is a 20\n”;
std::cout << “Doing it dangerously wrong:\n”;
for(i = 0; quizscores[i] = 20; i++)
std::cout << “quiz " << i << " is a 20\n”;
return 0;
}
#endif

#if 0 // 5.11 compstr1.cpp
#include
#include
int main(){
char word[5] = “?ate”;
for(char ch = ‘a’; strcmp(word, “mate”); ch++){
std::cout << word << std::endl;
word[0] = ch;
}
std::cout << "After loop ends, word is " << word << std::endl;
return 0;
}
#endif

#if 0 // 5.12 compstr2.cpp
#include
#include
int main(){
std::string word = “?ate”;
for(char ch = ‘a’; word != “mate”; ch++){
std::cout << word << std::endl;
word[0] = ch;
}
std::cout << "After loop ends, word is " << word << std::endl;
return 0;
}
#endif

#if 0 // 5.13 while.cpp
#include
const int ArSize = 20;
int main(){
char name[ArSize] ;
std::cout << "Your first name, please: ";
std::cin >> name;
std::cout << “Here is your name, verticalized and ASCIIized:\n”;
int i = 0;
while (name[i] != ‘\0’){
std::cout << name[i] << ": " << int(name[i]) << std::endl;
i++;
}
return 0;
}
#endif

#if 0 // 5.14 waiting.cpp
#include
int main(){
std::cout << "Enter the delay time, in seconds: ";
float secs;
std::cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC;
std::cout << “starting\a\n”;
clock_t start = clock();
while (clock() - start < delay)
;
std::cout << “done \a\n”;

return 0;

}
#endif

#if 0 // 5.15 dowhile.cpp
#include
int main(){
int n;
std::cout << "Enter numbers in the range 1-10 to find ";
std::cout << “my favorite number\n”;

do{
std::cin >> n;
} while(n != 7);
std::cout << "Yes, 7 is my favorite.\n";return 0;

}
#endif

#if 0 // 5.151 forloop.cpp p169/953 2021年01月23日 星期六 08时07分03秒
#include
int main(){
double prices[5] {4.99, 10.99, 6.87, 7.99, 8.49};
// for(double x : prices)
// std::cout << x << std::endl;

// double x;
// for(double &x : prices)
// x = x * 0.80;
// std::cout << x << std::endl;

for(int x :{3, 4, 1, 8, 6})
std::cout << x << " ";
std::cout << '\n';
return 0;

}
#endif

#if 0 // 5.16 textin.cpp
#include
int main(){
char ch;
int count = 0;
std::cout << “Enter characters; enter # to quit:\n”;
std::cin >> ch;
while (ch != ‘#’){
std::cout << ch;
++count;
std::cin >> ch;
}
std::cout << std::endl << " characters read\n";
return 0;
}
#endif

#if 0 // 5.17 textin2.cpp
#include
int main(){
char ch;
int count = 0;

std::cout << "Enter characters; enter # to quit:\n";
std::cin.get(ch);
while(ch != '#'){
std::cout << ch;
++count;
std::cin.get(ch);
}
std::cout << std::endl << count << " characters read.\n";
return 0;

}
#endif

#if 0 // 5.18 textin3.cpp
#include
int main(){
char ch;
int count = 0;
std::cin.get(ch);
while (std::cin.fail() == false){
std::cout << ch;
++count;
std::cin.get(ch);
}
std::cout << std::endl << count << " characters read.\n";
return 0;
}
#endif

#if 0 // 5.19 textin4.cpp
#include
int main(){
int ch;
int count = 0;
while ((ch = std::cin.get()) != EOF){
std::cout.put(char(ch));
++count;
}
std::cout << std::endl << count << “characters read.\n”;

return 0;

}
#endif

#if 0 // 5.20 nested.cpp
#include
const int Cities = 5;
const int Years = 4;

int main(){
/*
const char * cities[Cities] ={
“Gribble City”,
“Gribbletown”,
“New Gribble”,
“San Gribble”,
“Gribble Vista”
};

char cities[Cities][25] ={
"Gribble City",
"Gribbletown",
"New Gribble",
"San Gribble",
"Gribble Vista"
};

*/

const std::string cities[Cities] = {
"Gribble City",
"Gribbletown",
"New Gribble",
"San Gribble",
"Gribble Vista"
};int maxtemps[Years][Cities] ={
{96, 100, 87, 101, 105},
{96, 98, 91, 107, 104},
{97, 101, 93, 108, 107},
{98, 103, 95, 109, 108}
};std::cout << "Maximum temperatures for 2008 - 2011\n\n";
for(int city = 0; city < Cities; ++city){
std::cout << cities[city] << ":\t";for(int year = 0; year < Years; ++year)std::cout << maxtemps[year][city] << "\t";std::cout << std::endl;
}
return 0;

}
#endif

#if 0 // 6.1 if.cpp
#include
int main(){
char ch;
int spaces = 0;
int total = 0;
std::cin.get(ch);
while(ch != ‘.’){
if(ch == ’ ')
++spaces;
++total;
std::cin.get(ch);
}
std::cout << spaces << " spaces, " << total;
std::cout << " characters total in sentence.\n";
return 0;
}
wannian07@wannian07-PC:~/Desktop$ gedit hello.cpp
wannian07@wannian07-PC:~/Desktop$ g++ hello.cpp -o hello
wannian07@wannian07-PC:~/Desktop$ ./hello
The balloonist was an airhead
with lofty goals.
6 spaces, 46 characters total in sentence.

#endif

#if 0 // 6.2 ifelse.cpp
#include
int main(){
char ch;

std::cout << "Type, and I shall repeat.\n";
std::cin.get(ch);
while(ch != '.'){
if(ch == '\n')std::cout << ch;
else

// std::cout << ++ch;
/*
wannian07@wannian07-PC:~/Desktop$ ./hello
Type, and I shall repeat.
An ineffable joy suffused me as I beheld
Bo!jofggbcmf!kpz!tvggvtfe!nf!bt!J!cfifme
the wonders of modern computing.
uif!xpoefst!pg!npefso!dpnqvujoh
Please excues the slight confusion.

     */std::cout << ch + 1;/*wannian07@wannian07-PC:~/Desktop$ ./helloType, and I shall repeat.An ineffable joy suffused me as I beheld66111331061111021031039899109102331071121223311611810310311811610210133110102339811633743399102105102109101the wonders of modern computing.11710510233120112111101102115116331121033311011210110211511133100112110113118117106111104Please excues the slight confusion.*/std::cin.get(ch);
}
std::cout << "\nPlease excues the slight confusion.\n";
return 0;

}
#endif

#if 0 // 6.3 ifelseif.cpp
#include
const int Fave = 27;
int main(){
int n;

std::cout << "Enter a number in the range 1-100 to find";
std::cout << "my favorite number: ";
do {
std::cin >> n;
if(n < Fave)std::cout << "Too low -- guess again: ";
else if (n > Fave)std::cout << "Too high -- guess again: ";
else std::cout << Fave << " is right!\n";
} while (n != Fave);
return 0;

}
#endif

#if 0 // 6.4 or.cpp
#include
int main(){

std::cout << "This program may reformat your hand disk\n""and destroy all your data.\n""Do you wish to continue? <y/n> ";
char ch;
std::cin >> ch;
if(ch == 'y' || ch == 'Y')std::cout << "You were warned!\a\a\n";
else if (ch == 'n' || ch == 'N')std::cout << "A wise choice ... bye\n";
else std::cout << "That wasn't a y or n! Apparently you ""can't follow\ninstructions, so ""I'll trash your disk anyway.\a\a\a\n";    return 0;

}
#endif

#if 0 // 6.5 add.cpp
#include
const int ArSize = 6;
int main(){

float naaq[ArSize];
std::cout << "Enter the NAAQs (New Age Awareness Quotients) " << "of\nyour neighbors. Program terminates "<< "when you make\n" << ArSize << " enteries "<< "or enter a negative value.\n";
int i = 0;
float temp;
std::cout << "First value: ";
std::cin >> temp;
while(i < ArSize && temp >= 0){naaq[i] = temp;++i;if(i < ArSize){std::cout << "Next value: ";std::cin >> temp;       }
}
if( i == 0)std::cout << "No data--bye\n";
else{std::cout << "Enter your NAAQ: ";float you;std::cin >> you;int count = 0;for(int j = 0; j < i; j++)if(naaq[j] > you)++count;std::cout << count;std::cout << " of your neighbors have greater awareness of \n"<< "the New Age than you do.\n";
}return 0;

}
#endif

#if 0 // 6.6 more_and.cpp
#include
const char * qualify[4] = {
“10,000-meter race.\n”,
“mud tug-of-war.\n”,
“masters canoe jousting.\n”,
“pie-throwing festival.\n”
};

int main(){
int age;

std::cout << "Enter you age in years: ";
std::cin >> age;
int index;
if(age > 17 && age < 35)index = 0;
else if(age >= 35 && age < 50)index = 1;
else if(age >= 50 && age < 65)index = 2;
else index = 3;
std::cout << "You qualify for the " << qualify[index];
return 0;

}
#endif

#if 0 // 6.7 not.cpp p195/953 2021年01月24日 星期日 19时46分34秒
#include
#include
bool is_int(double);
int main(){
double num;

std::cout << "Yo, dude! Enter an integer value: ";
std::cin >> num;
while( !is_int(num)){std::cout << "Out of range -- please try again: ";std::cin >> num;
}
int val = int (num);
std::cout << "You've entered the integer " << val << "\nBye\n";
return 0;

}

bool is_int(double x){
if(x <= INT_MAX && x >= INT_MIN)
return true;
else
return false;
}
#endif

#if 0 // cctypes.cpp P 195/953 2021年01月25日 星期一 19时04分37秒
#include
#include

int main(){
std::cout << “Enter text for analysis, and type @”
" to terminate input.\n";
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;

std::cin.get(ch);
while(ch != '@'){
if(isalpha(ch))chars++;
else if(isspace(ch))whitespace++;
else if(isdigit(ch))digits++;
else if(ispunct(ch))punct++;
else others++;
std::cin.get(ch);
}
std::cout << chars << " letters, "<< whitespace << " whitespace, "<< digits << " digits, "<< punct << " punctuations, "<< others << " others.\n";
return 0;

}
wannian07@wannian07-PC:~/Desktop$ g++ hello.cpp -o hello
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter text for analysis, and type @ to terminate input.
AdrenalVision International producer Adrienne Vismonger
announced production of their new 3-D film, a remarke of
“My Dinner with Andre,” sheduled for 2013. " “Wait until”
you see the the new scene with an enraged Collossipede!"@ "
177 letters, 35 whitespace, 5 digits, 9 punctuations, 0 others.

#endif

#if 0 //condit.cpp
#include
int main(){
int a, b;
std::cout << "Enter two integers: ";
std::cin >> a >> b;
std::cout << "The larger of " << a << " and " << b;
int c = a > b ? a : b;
std::cout << " is " << c << std::endl;
return 0;
}
#endif

#if 0 //switch.cpp
#include
void showmenu();
void report();
void comfort();
int main(){
showmenu();
int choice;
std::cin >> choice;
while (choice != 5){
switch(choice){
case 1 : std::cout << “\a\n”;
break;
case 2 : report();
break;
case 3 : std::cout << “The boss was in all day.\n”;
break;
case 4 : comfort();
break;
default : std::cout << “That’t not a choice.\n”;
}
showmenu();
std::cin >> choice;
}
std::cout << “Bye!\n”;
return 0;
}

void showmenu(){
std::cout << “Please enter 1, 2, 3, 4, or 5:\n”
“1) alarm 2) report\n”
“3) alibi 4) comfort\n”
“5) quit\n”;
}

void report(){
std::cout << “It’s been an excellent week for business.\n”
“Sales are up 120%. Expenses are down 35%.\n”;
}

void comfort(){
std::cout << “Your employees think you are the finest CEO\n”
“in the industry. The board of directors think\n”
“you are the finest CEO in the industry.\n”;
}
#endif

#if 0 //6.11 enum.cpp
#include
enum {red, orange, yellow, green, blue, violet, indigo};

int main(){
std::cout << "Enter color code (0-6): ";
int code;
std::cin >> code;
while(code >= red && code <= indigo){
switch (code)
{
case red : std::cout << “Her lips were read.\n”; break;
case orange : std::cout << “Her hair was orange.\n”; break;
case yellow : std::cout << “Her shoes were yellow.\n”; break;
case green : std::cout << “Her nails were green.\n”; break;
case blue : std::cout << “Her sweatsuit was blue.\n”; break;
case violet : std::cout << “Her eyes were voilet.\n”; break;
case indigo : std::cout << “Her mood was indigo.\n”; break;

 }std::cout << "Enter color code (0-6): ";std::cin >> code;
}   std::cout << "Bye\n";
return 0;

}
#endif

#if 0 // 6.12 jump.cpp 2021年01月25日 星期一 20时07分26秒
#include
const int ArSize = 80;
int main(){
char line[ArSize];
int spaces = 0;
std::cout << “Enter a line of text:\n”;
std::cin.get(line, ArSize);
std::cout << “Complete line:\n” << line << std::endl;
std::cout << “Line through first period:\n”;
for(int i = 0; line[i] != ‘\0’; i++){
std::cout << line[i];
if(line[i] == ‘.’)
break;
if(line[i] != ’ ')
continue;
spaces++;
}
std::cout << “\n” << spaces << " spaces\n";
std::cout << “Done.\n”;
return 0;
}
#endif

#if 0 //6.13 cinfish.cpp 2021年01月26日 星期二 19时03分50秒
#include
const int Max = 5;
int main(){
double fish[Max];
std::cout << “Please enter the weights of your fish.\n”;
std::cout << “You may enter up to " << Max
<< " fish < q to terminate >.\n”;
std::cout << "fish #1: ";
int i = 0;
while (i < Max && std::cin >> fish[i]) {
if(++i < Max)
std::cout << “fish #” << i + 1 << ": “;
}
double total = 0.0;
for(int j = 0; j < i; j++)
total += fish[j];
if(i == 0)
std::cout << “No fish\n”;
else
std::cout << total / i << " = average weight of "
<< i << " fish\n”;
std::cout << “Done.\n”;

if(!std::cin){std::cin.clear();std::cin.get();
}
std::cin.get();
std::cin.get();
return 0;

}
#endif

#if 0 // 6.14 cingolf.cpp
#include
const int Max = 5;
int main(){
int golf[Max];
std::cout << “Please enter your golf scores.\n”;
std::cout << “You must enter " << Max << " rounds.\n”;
int i;
for(i = 0; i < Max; i++){
std::cout << “round #” << i+1 << ": ";
while(!(std::cin >> golf[i])) {
std::cin.clear();
while(std::cin.get() != ‘\n’)
continue;
std::cout << "Please enter a number: “;
}
}
double total = 0.0;
for(i = 0; i < Max; i++)
total += golf[i];
std::cout << total / Max << " = average score "
<< Max << " rounds\n”;
return 0;
}
#endif

#if 0 // 6.15 outfile.cpp 2021年01月26日 星期二 19时30分56秒
#include
#include
int main(){
char automobile[50];
int year;
double a_price;
double d_price;
std::ofstream outFile;
outFile.open(“carinfo.txt”);
std::cout << "Enter the make and model of automobile: ";
std::cin.getline(automobile, 50);
std::cout << "Enter the model year: ";
std::cin >> year;
std::cout << "Enter the original asking price: ";
std::cin >> a_price;
d_price = 0.913 * a_price;

std::cout << std::fixed;
std::cout.precision(2);
std::cout.setf(std::ios_base::showpoint);
std::cout << "Make and model: " << automobile << std::endl;
std::cout << "Year: " << year << std::endl;
std::cout << "Was asking $" << a_price << std::endl;
std::cout << "Now asking $" << d_price << std::endl;outFile << std::fixed;
outFile.precision(2);
outFile.setf(std::ios_base::showpoint);
outFile << "Make and model: " << automobile << std::endl;
outFile << "Year: " << year << std::endl;
outFile << "Was asking $" << a_price << std::endl;
outFile << "Now asking $" << d_price << std::endl;
outFile.close();
return 0;

}
#endif

#if 0 //6.16 sumafile.cpp 2021年01月27日 星期三 18时45分10秒
#include
#include
#include
const int SIZE = 60;
int main(){
char filename[SIZE];
std::ifstream inFile;
std::cout << "Enter name of data file: ";
std::cin.getline(filename, SIZE);
inFile.open(filename);
if(!inFile.is_open()){
std::cout << "Could not open the file " << filename << std::endl;
std::cout << “Program terminating.\n”;
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while (inFile.good()){
++count;
sum += value;
inFile >> value;
}
if(inFile.eof())
std::cout << “End of file reached.\n”;
else if (inFile.fail())
std::cout << “Input terminated by data mismatch.\n”;
else
std::cout << “Input terminated for unknown reason.\n”;
if(count == 0)
std::cout << “No data processed.\n”;
else{
std::cout << "Items read: " << count << std::endl;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Average: " << sum / count << std::endl;
}
inFile.close();
return 0;
}
#endif

#if 0 // 7.1 calling.cpp 2021年01月27日 星期三 19时02分01秒
#include
void simple();
int main(){
std::cout << “main() will call the simple() function:\n”;
simple();
std::cout << “main() is finished with the simple() function.\n”;
return 0;
}
void simple(){
std::cout << “I’m but a simple function.\n”;
}
#endif

#if 0 //7.2 protos.cpp 2021年01月27日 星期三 19时05分40秒
#include
void cheers(int);
double cube(double x);
int main(){
cheers(5);
std::cout << "Give me a number: ";
double side;
std::cin >> side;
double volume = cube(side);
std::cout << "A " << side << "-foot cube has a volume of “;
std::cout << volume << " cubic feet.\n”;
cheers(cube(2));
return 0;
}

void cheers(int n){
for(int i = 0; i < n; i++)
std::cout << "Cheers! ";
std::cout << std::endl;
}

double cube(double x){
return xxx;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Cheers! Cheers! Cheers! Cheers! Cheers!
Give me a number: 5
A 5-foot cube has a volume of 125 cubic feet.
Cheers! Cheers! Cheers! Cheers! Cheers! Cheers! Cheers! Cheers!

#endif

#if 0 //7.3 twoarg.cpp 2021年01月27日 星期三 19时14分22秒
#include
void n_chars(char, int);
int main(){
int times;
char ch;
std::cout << "Enter a character: ";
std::cin >> ch;
while(ch != ‘q’){
std::cout << "Enter an integer: ";
std::cin >> times;
n_chars(ch, times);
std::cout << “\nEnter another character or press the”
" q-key to quit: ";
std::cin >> ch;
}
std::cout << "The value of times is " << times << “.\n”;
std::cout << “Bye\n”;
return 0;
}

void n_chars(char c, int n){
while(n-- > 0)
std::cout << c;
}
#endif

#if 0 //7.4 lotto.cpp 2021年01月27日 星期三 19时21分50秒
#include
long double probability(unsigned numbers, unsigned picks);
int main(){
double total, choices;
std::cout << “Enter the total number of choices on the game card and\n”
“the number of picks allowed:\n”;
while((std::cin >> total >> choices) && choices <= total){
std::cout << "You have one chance in “;
std::cout << probability(total, choices);
std::cout << " of winning.\n”;
std::cout << "Next two numbers (q to quit): ";
}
std::cout << “bye\n”;
return 0;
}

long double probability(unsigned numbers, unsigned picks){
long double result = 1.0;
long double n;
unsigned p;
for(n = numbers, p = picks; p > 0; n–, p–)
result = result * n / p;
return result;
}
#endif

#if 0 // 7.5 arrfun1.cpp 2021年01月30日 星期六 07时25分39秒
#include
const int ArSize = 8;
int sum_arr(int arr[], int n);
int main(){
int cookies[ArSize] {1, 2, 4, 8, 16, 32, 64, 128};
int sum = sum_arr(cookies, ArSize);
std::cout << "Total cookies eaten: " << sum << “\n”;
return 0;
}

int sum_arr(int arr[], int n){
int total = 0;
for(int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
#endif

#if 0 //arrfun2.cpp
#include
const int ArSize = 8;
int sum_arr(int arr[], int n);
int main(){
int cookies[ArSize] {1, 2, 4, 8, 16, 32, 64, 128};
std::cout << cookies << " = array address, “;
std::cout << sizeof cookies << " = sizeof cookies\n”;
int sum = sum_arr(cookies, ArSize);
std::cout << "Total cookies eaten: " << sum << “\n”;
sum = sum_arr(cookies + 4, 4);
std::cout << “Last four eaters ate " << sum << " cookies.\n”;
return 0;
}

int sum_arr(int arr[], int n){
int total = 0;
std::cout << arr << " = arr, “;
std::cout << sizeof arr << " = sizeof arr\n”;
for(int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
0x7ffd8baec1f0 = array address, 32 = sizeof cookies
0x7ffd8baec1f0 = arr, 8 = sizeof arr
Total cookies eaten: 255
0x7ffd8baec200 = arr, 8 = sizeof arr
Last four eaters ate 240 cookies.

#endif

#if 0 //arrfun3.cpp
#include
const int Max = 5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);
int main(){
double properties[Max];
int size = fill_array(properties, Max);
show_array(properties, size);
if(size > 0){
std::cout << "Enter revaluation factor: ";
double factor;
while(!(std::cin >> factor)){
std::cin.clear();
while(std::cin.get() != ‘\n’)
continue;
std::cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
std::cout << “Done.\n”;
std::cin.get();
std::cin.get();
return 0;
}

int fill_array(double ar[], int limit){
double temp;
int i;
for(i = 0; i < limit; i++){
std::cout << “Enter vlaue #” << (i + 1) << ": ";
std::cin >> temp;
if(!std::cin){
std::cin.clear();
while(std::cin.get() != ‘\n’)
continue;
std::cout << “Bad input; input process terminated.\n”;
break;
}
else if(temp < 0)
break;
ar[i] = temp;
}
return i;
}

void show_array(const double ar[], int n){
for(int i = 0; i < n; i++){
std::cout << “Property #” << (i + 1) << “: $”;
std::cout << ar[i] << std::endl;
}
}

void revalue(double r, double ar[], int n){
for(int i = 0; i < n; i++)
ar[i] *= r;
}
#endif

#if 0 // arrfun4.cpp 2021年01月31日 星期日 07时48分35秒
#include
const int ArSize = 8;
int sum_arr(const int * begin, const int * end);
int main(){
int cookies[ArSize] {1, 2, 4, 8, 16, 32, 64, 128};
int sum = sum_arr(cookies, cookies + ArSize);
std::cout << "Total cookies eaten: " << sum << std::endl;
sum = sum_arr(cookies, cookies + 3);
std::cout << “First three eaters ate " << sum << " cookies.\n”;
sum = sum_arr(cookies + 4, cookies + 8);
std::cout << “Last four eaters ate " << sum << " cookies.\n”;

return 0;

}

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;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Total cookies eaten: 255
First three eaters ate 7 cookies.
Last four eaters ate 240 cookies.

#endif

#if 0 // strgfun.cpp
#include
unsigned int c_in_str(const char * str, char ch);
int main(){
char mmm[15] = “minimum”;
char * wail = “ululate”;
unsigned int ms = c_in_str(mmm, ‘m’);
unsigned int us = c_in_str(wail, ‘u’);
std::cout << ms << " m characters in " << mmm << std::endl;
std::cout << us << " u characters in " << wail << std::endl;
return 0;
}

unsigned int c_in_str(const char * str, char ch){
unsigned int count = 0;
while (*str){
if(*str == ch)
count++;
str++;
}
return count;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
3 m characters in minimum
2 u characters in ululate

#endif

#if 0 //strgback.cpp 2021年01月31日 星期日 08时02分44秒
#include
char * buildstr(char c, int n);
int main(){
int times;
char ch;
std::cout << "Enter a character: ";
std::cin >> ch;
std::cout << "Enter an integer: ";
std::cin >> times;
char *ps = buildstr(ch, times);
std::cout << ps << std::endl;
delete [] ps;
ps = buildstr(’+’, 20);
std::cout << ps << “-DONE-” << ps << std::endl;
delete [] ps;
return 0;
}

char * buildstr(char c, int n){
char * pstr = new char[n + 1];
pstr[n] = ‘\0’;
while(n-- > 0)
pstr[n] = c;
return pstr;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter a character: V
Enter an integer: 46
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
+++++++++++++++++++±DONE-++++++++++++++++++++

#endif

#if 0 //travel.cpp 2021年01月31日 星期日 08时10分25秒
#include
struct travel_time{
int hours;
int mins;
};

const int Mins_per_hr = 60;

travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);

int main(){
travel_time day1 {5, 45};
travel_time day2 {4, 55};
travel_time trip = sum (day1, day2);
std::cout << "Two-day total: ";
show_time(trip);

travel_time day3 {4, 32};
std::cout << "Three-day total: ";
show_time(sum(trip, day3));return 0;

}

travel_time sum(travel_time t1, travel_time t2){
travel_time total;
total.mins = (t1.mins + t2.mins) % Mins_per_hr;
total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / Mins_per_hr;
return total;
}

void show_time(travel_time t){
std::cout << t.hours << " hours, " << t.mins << " minutes\n";
}

wannian07@wannian07-PC:~/Desktop$ ./hello
Two-day total: 10 hours, 40 minutes
Three-day total: 15 hours, 12 minutes

#endif

#if 0 // 7.12 atrctfun.cpp 2021年01月31日 星期日 08时19分41秒
#include
#include

struct polar {
double distance;
double angle;
};

struct rect {
double x;
double y;
};

polar rect_to_polar(rect xypos);
void show_polar(polar dapos);

int main(){
using namespace std;//如何修改此句话?
rect rplace;
polar pplace;
std::cout << "Enter the x and y values: ";
while(std::cin >> rplace.x >> rplace.y){
pplace = rect_to_polar(rplace);
show_polar(pplace);
std::cout << "Next two numbers (q to quit): ";
}
std::cout << “Done.\n”;
return 0;
}

polar rect_to_polar(rect xypos){
polar answer;
answer.distance =
sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}

void show_polar(polar dapos){
const double Rad_to_deg = 57.29577951;

std::cout << "distance = " << dapos.distance;
std::cout << ", angle = " << dapos.angle * Rad_to_deg;
std::cout << " degrees\n";

}
/*
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter the x and y values: 30 40
distance = 50, angle = 53.1301 degrees
Next two numbers (q to quit): -100 100
distance = 141.421, angle = 135 degrees
Next two numbers (q to quit): q
Done.

*/
#endif

#if 0//7.13 strctptr.cpp
//strctptr.cpp–functions with pointer to structure arguments
#include
#include
struct polar{
double distance;
double angle;
};

struct rect{
double x;
double y;
};

void rect_to_polar(const rect * pxy, polar * pda);
void show_polar(const polar * pda);

int main(){
//using namespace std;
rect rplace;
polar pplace;
std::cout << "Enter the x and y values: ";
while(std::cin >> rplace.x >> rplace.y){
rect_to_polar(&rplace, &pplace);
show_polar(&pplace);
std::cout << "Next two numbers (q to quit): ";
}
std::cout << “Done.\n”;
return 0;
}

void show_polar (const polar *pda){
const double Rad_to_deg = 57.29577951;
std::cout << "distance = " << pda->distance;
std::cout << “, angle = " << pda->angle * Rad_to_deg;
std::cout << " degrees\n”;
}

void rect_to_polar(const rect * pxy, polar * pda){
pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
pda->angle = atan2(pxy->y, pxy->x);
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter the x and y values: 10 29
distance = 30.6757, angle = 70.9744 degrees
Next two numbers (q to quit): q
Done.

#endif

#if 0// 7.14 topfive.cpp 2021年02月06日 星期六 07时51分42秒
//topfive.cpp – handling an array of string objects
#include
#include
const int SIZE = 5;
void display(const std::string sa[], int n);
int main(){
std::string list[SIZE];
std::cout << “Enter your " << SIZE << " favorite astronomical sights:\n”;
for(int i = 0; i < SIZE; i++){
std::cout << i + 1 << ": ";
getline(std::cin, list[i]);
}

std::cout << "Your list: \n";
display(list, SIZE);return 0;

}

void display(const std::string sa[], int n){
for(int i = 0; i < n; i++)
std::cout << i + 1 << ": " << sa[i] << std::endl;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter your 5 favorite astronomical sights:
1: Orion Nebula
2: M13
3: Saturn
4: Jupiter
5: Moon
Your list:
1: Orion Nebula
2: M13
3: Saturn
4: Jupiter
5: Moon

#endif

#if 0 //7.15 arrobj.cpp
//arrobj.cpp – functions with array objects
#include
#include
#include
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, Seasons> expenses;
fill(&expenses);
show(expenses);
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] << std::endl;
total += da[i];
}
std::cout << “Total Expenses: $” << total << std::endl;
}

wannian07@wannian07-PC:~/Desktop$ ./hello
Enter Spring expenses: 212
Enter Summer expenses: 256
Enter Fall expenses: 208
Enter Winter expenses: 244

EXPENSES
Spring: $212
Summer: $256
Fall: $208
Winter: $244
Total Expenses: $920

#endif

#if 0 // 7.16 recur.cpp 2021年02月06日 星期六 18时57分32秒
#include
void countdown(int n);

int main(){
countdown(4);
return 0;
}

void countdown(int n){
std::cout << "Counting down … " << n << std::endl;
if(n > 0)
countdown(n-1);
std::cout << n << “: Kaboom!\n”;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Counting down … 4
Counting down … 3
Counting down … 2
Counting down … 1
Counting down … 0
0: Kaboom!
1: Kaboom!
2: Kaboom!
3: Kaboom!
4: Kaboom!

#endif

#if 0 //7.17 ruler.cpp
#include
const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int level);

int main(){
char ruler[Len];
int i;
for(i = 1; i < Len - 2; i++)
ruler[i] = ’ ';
ruler[Len - 1] = ‘\0’;
int max = Len - 2;
int min = 0;
ruler[min] = ruler[max] = ‘|’;
std::cout << ruler << std::endl;
for(i = 1; i <= Divs; i++){
subdivide(ruler, min, max, i);
std::cout << ruler << std::endl;
for(int j = 1; j < Len - 2; j++)
ruler[j] = ’ ';
}
return 0;
}

void subdivide(char ar[], int low, int high, int level){
if(level == 0)
return;
int mid = (high + low) / 2;
ar[mid] = ‘|’;
subdivide(ar, low, mid, level - 1);
subdivide(ar, mid, high, level - 1);
}
wannian07@wannian07-PC:~/Desktop$ ./hello
| |
| | |
| | | | |
| | | | | | | | |
| | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

#endif

#if 0 // 7.18 fun_ptr.cpp
#include
double betsy(int);
double pam(int);
void estimate(int lines, double (*pf)(int));

int main(){
int code;
std::cout << "How many lines of code do you need? ";
std::cin >> code;
std::cout << “Here’s Betsy’s estimate:\n”;
estimate(code, betsy);
std::cout << “Here’s Pam’s estimate:\n”;
estimate(code, pam);
return 0;
}

double betsy(int lns){
return 0.05 * lns;
}

double pam(int lns){
return 0.03 * lns + 0.0004 * lns * lns;
}

void estimate(int lines, double (*pf)(int)){
std::cout << lines << " lines will take “;
std::cout << (*pf)(lines) << " hour(s)\n”;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
How many lines of code do you need? 30
Here’s Betsy’s estimate:
30 lines will take 1.5 hour(s)
Here’s Pam’s estimate:
30 lines will take 1.26 hour(s)
wannian07@wannian07-PC:~/Desktop$ ./hello
How many lines of code do you need? 100
Here’s Betsy’s estimate:
100 lines will take 5 hour(s)
Here’s Pam’s estimate:
100 lines will take 7 hour(s)

#endif

#if 0 //7.19 arfupt.cpp
#include
const double * f1(const double ar[], int n);
const double * f2(const double [], int);
const double * f3(const double *, int);

int main(){
double av[3] {1112.3, 1542.6, 2227.9};
const double *(*p1)(const double *, int) = f1;
auto p2 = f2;
std::cout << “Using pointers to functions:\n”;
std::cout << “Address Value\n”;
std::cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << std::endl;
std::cout << p2(av, 3) << ": " << *p2(av, 3) << std::endl;

const double *(*pa[3])(const double *, int ) {f1, f2, f3};
auto pb = pa;
std::cout << "\nUsing an array of pointers to functions:\n";
std::cout << " Address Value\n";
for(int i = 0; i < 3; i++)std::cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << std::endl;
std::cout << "\nUsing a pointer to a pointer to a function:\n";
std::cout << " Address Value\n";
for(int i = 0; i < 3; i++)
std::cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << std::endl;std::cout << "\nUsing pointers to an array of pointers:\n";
std::cout << " Address Value\n";
auto pc = &pa;
std::cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << std::endl;
const double *(*(*pd)[3])(const double *, int) = &pa;
const double * pdb = (*pd)[1](av, 3);
std::cout << pdb << ": " << *pdb << std::endl;
std::cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd)[2])(av, 3) << std::endl;
return 0;

}

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;
}

wannian07@wannian07-PC:~/Desktop$ ./hello
Using pointers to functions:
Address Value
0x7ffc25c01830: 1112.3
0x7ffc25c01838: 1542.6

Using an array of pointers to functions:
Address Value
0x7ffc25c01830: 1112.3
0x7ffc25c01838: 1542.6
0x7ffc25c01840: 2227.9

Using a pointer to a pointer to a function:
Address Value
0x7ffc25c01830: 1112.3
0x7ffc25c01838: 1542.6
0x7ffc25c01840: 2227.9

Using pointers to an array of pointers:
Address Value
0x7ffc25c01830: 1112.3
0x7ffc25c01838: 1542.6
0x7ffc25c01840: 2227.9

#endif

#if 0 // 8.1 inline.cpp
#include
inline double square(double x) { return x * x; }
int main(){
double a, b;
double c = 13.0;
a = square(5.0);
b = square(4.5 + 7.5);
std::cout << " a = " << a << ", b = " << b << “\n”;
std::cout << " c = " << c;
std::cout << ", c squared = " << square(c++) << “\n”;
std::cout << "Now c = " << c << “\n”;
return 0;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
a = 25, b = 144
c = 13, c squared = 169
Now c = 14

#endif

#if 0 //8.2 firstref.cpp
#include
int main(){
int rats = 101;
int & rodents = rats;
std::cout << "rats = " << rats;
std::cout << ", rodents = " << rodents << std::endl;
rodents++;
std::cout << "rats = " << rats;
std::cout << ", rodents = " << rodents << std::endl;

std::cout << "rats address = " << &rats;
std::cout << ", rodents address = " << &rodents << std::endl;
return 0;

}
wannian07@wannian07-PC:~/Desktop$ ./hello
rats = 101, rodents = 101
rats = 102, rodents = 102
rats address = 0x7ffc659c72f4, rodents address = 0x7ffc659c72f4

#endif

#if 0//sceref.cpp 8.3 2021年02月08日 星期一 18时44分38秒
#include
int main(){
int rats = 101;
int & rodents = rats;
std::cout << "rats = " << rats;
std::cout << ", rodents = " << rodents << std::endl;
std::cout << "rats, address = " << &rats;
std::cout << ", rodents address = " << &rodents << std::endl;
int bunnies = 50;
rodents = bunnies;
std::cout << "bunnies = " << bunnies;
std::cout << ", rats = " << rats;
std::cout << ", rodents = " << rodents << std::endl;
std::cout << "bunnies address = " << &bunnies;
std::cout << ", rodents address = " << &rodents << std::endl;
return 0;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
rats = 101, rodents = 101
rats, address = 0x7ffeafcb1f64, rodents address = 0x7ffeafcb1f64
bunnies = 50, rats = 50, rodents = 50
bunnies address = 0x7ffeafcb1f60, rodents address = 0x7ffeafcb1f64

#endif

#if 0 //swaps.cpp
#include
void swapr(int & a, int & b);
void swapp(int * p, int * q);
void swapv(int a, int b);
int main(){
int wallet1 = 300;
int wallet2 = 350;
std::cout << “wallet1 = $” << wallet1;
std::cout << " wallet2 = $" << wallet2 << std::endl;

std::cout << "Using references to swap contents:\n";
swapr(wallet1, wallet2);
std::cout << "wallet1 = $" << wallet1;
std::cout << " wallet2 = $" << wallet2 << std::endl;std::cout << "Trying to use passing by value:\n";
swapv(wallet1, wallet2);
std::cout << "wallet1 = $" << wallet1;
std::cout << " wallet2 = $" << wallet2 << std::endl;
return 0;

}

void swapr(int & a, int & b){
int temp;
temp = a;
a = b;
b = temp;
}

void swapp(int * p, int * q){
int temp;
temp = * p;
* p = * q;
* q = temp;
}

void swapv(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
wallet1 = $300 wallet2 = $350
Using references to swap contents:
wallet1 = $350 wallet2 = $300
Trying to use passing by value:
wallet1 = $350 wallet2 = $300

#endif

#if 0 //cubes.cpp
#include
double cube(double a);
double refcube(double &ra);

int main(){
double x = 3.0;
std::cout << cube(x);
std::cout << " = cube of " << x << std::endl;
std::cout << refcube(x);
std::cout << " = cube of " << x << std::endl;
return 0;
}

double cube (double a){
a *= a * a;
return a;
}

double refcube(double &ra){
ra *= ra * ra;
return ra;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
27 = cube of 3
27 = cube of 27

#endif

#if 0 //strtref.cpp 8.6 2021年02月08日 星期一 19时35分00秒
#include
#include
struct free_throws{
std::string name;
int made;
int attempts;
float percent;
};

void display(const free_throws & ft);
void set_pc(free_throws & ft);
free_throws & accumulate(free_throws & target, const free_throws & source);

int main(){
free_throws one {“Ifelsa Branch”, 13, 14};
free_throws two {“Andor Knott”, 10, 16};
free_throws three {“Minnie Max”, 7, 9};
free_throws four {“Whily Looper”, 5, 9};
free_throws five {“Long Long”, 6, 14};
free_throws team {“Throwgoods”, 0, 0};

free_throws dup;
set_pc(one);
display(one);
accumulate(team, one);
display(team);
display(accumulate(team, two));
accumulate(accumulate(team, three), four);
display(team);dup = accumulate(team, five);
std::cout << "Displaying team:\n";
display(team);
std::cout << "Displaying dup after assignment:\n";
display(dup);
set_pc(four);
accumulate(dup, five) = four;
std::cout << "Displaying dup after ill-advised assignment:\n";
display(dup);
return 0;

}

void display(const free_throws & ft){
std::cout << "Name: " << ft.name << ‘\n’;
std::cout << " Made: " << ft.made << ‘\t’;
std::cout << "Attemps: " << ft.attempts << ‘\t’;
std::cout << "Percent: " << ft.percent << ‘\n’;
}

void set_pc(free_throws & ft){
if(ft.attempts != 0)
ft.percent = 100.0f *float(ft.made) /float(ft.attempts);
else
ft.percent = 0;
}

free_throws & accumulate(free_throws & target, const free_throws & source){
target.attempts += source.attempts;
target.made += source.made;
set_pc(target);
return target;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Name: Ifelsa Branch
Made: 13 Attemps: 14 Percent: 92.8571
Name: Throwgoods
Made: 13 Attemps: 14 Percent: 92.8571
Name: Throwgoods
Made: 23 Attemps: 30 Percent: 76.6667
Name: Throwgoods
Made: 35 Attemps: 48 Percent: 72.9167
Displaying team:
Name: Throwgoods
Made: 41 Attemps: 62 Percent: 66.129
Displaying dup after assignment:
Name: Throwgoods
Made: 41 Attemps: 62 Percent: 66.129
Displaying dup after ill-advised assignment:
Name: Whily Looper
Made: 5 Attemps: 9 Percent: 55.5556

#endif

#if 0 //8.7 strquote.cpp 2021年02月09日 星期二 19时01分22秒
#include
#include
std::string version1(const std::string & s1, const std::string & s2);
const std::string & version2(std::string & s1, const std::string & s2);
const std::string & version3(std::string & s1, const std::string & s2);

int main(){
std::string input;
std::string copy;
std::string result;
std::cout << "Enter a string: ";
getline(std::cin, input);
copy = input;
std::cout << "Your string as entered: " << input << std::endl;
result = version1(input, “***”);
std::cout << "Your string enhanced: " << result << std::endl;
std::cout << "Your orginal string: " << input << std::endl;

result = version2(input, "###");
std::cout << "Your string enhanced: " << result << std::endl;
std::cout << "Your original string: " << input << std::endl;
std::cout << "Resetting original string.\n";
input = copy;
result = version3(input, "@@@");
std::cout << "Your string enhanced: " << result << std::endl;
std::cout << "Your original string: " << input << std::endl;
return 0;

}

std::string version(const std::string & s1, const std::string & s2){
std::string temp;
temp = s2 + s1 + s2;
return temp;
}

const std::string & version2(std::string & s1, const std::string & s2){
s1 = s2 + s1 + s2;
return s1;
}

const std::string & version3(std::string & s1, const std::string & s2){
std::string temp;
temp = s2 + s1 + s2;
return temp;
}
#endif

#if 0
#include
int main(){

return 0;

}
#endif

#if 0 // 8.8 filefunc.cpp 2021年02月17日 星期三 08时46分05秒
#include
#include
#include

void file_it(std::ostream & os, double fo, const double fe[], int n);
const int LIMIT = 5;

int main(){
std::ofstream fout;
const char * fn = “ep-data.txt”;
fout.open(fn);
if(!fout.is_open()){
std::cout << "Can’t open " << fn << “. Bye.\n”;
exit(EXIT_FAILURE);
}
double objective;
std::cout << "Enter the focal length of your "
"telescope objective in mm: ";
std::cin >> objective;
double eps[LIMIT];
std::cout << “Enter the focal lengths, in mm of " << LIMIT
<< " eyepieces: \n”;
for(int i = 0; i < LIMIT; i++){
std::cout << “Eyepiece #” << i + 1 << ": ";
std::cin >> eps[i];
}
file_it(fout, objective, eps, LIMIT);
file_it(std::cout, objective, eps, LIMIT);
std::cout << “Done\n”;

return 0;

}

void file_it(std::ostream & os, double fo, const double fe[], int n){
std::ios_base::fmtflags initial;
initial = os.setf(std::ios_base::fixed);
os.precision(0);
os << “Focal length of objective: " << fo << " mm\n”;
os.setf(std::ios::showpoint);
os.precision(1);
os.width(12);
os << “f.1. eyepiece”;
os.width(15);
os << "magnification " << std::endl;
for(int i = 0; i < n; i++){
os.width(12);
os << fe[i];
os.width(15);
os << int (fo /fe[i] + 0.5) << std::endl;
}
os.setf(initial);
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter the focal length of your telescope objective in mm: 1800
Enter the focal lengths, in mm of 5 eyepieces:
Eyepiece #1: 30
Eyepiece #2: 19
Eyepiece #3: 14
Eyepiece #4: 8.8
Eyepiece #5: 7.5
Focal length of objective: 1800 mm
f.1. eyepiece magnification
30.0 60
19.0 95
14.0 129
8.8 205
7.5 240
Done

#endif

#if 0 // 8.9 left.cpp 2021年02月17日 星期三 21时08分20秒
#include
const int ArSize = 80;
char * left(const char * str, int n = 1);
int main(){
char sample[ArSize];
std::cout << “Enter a string:\n”;
std::cin.get(sample, ArSize);
char * ps = left(sample, 4);
std::cout << ps << std::endl;
delete [] ps;
ps = left(sample);
std::cout << ps << std::endl;
delete [] ps;

return 0;

}

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];
while ( i <= n)
p[i++] = ‘\0’;
return p;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter a string:
forthcoming
fort
f

#endif

#if 0 //8.10 leftover.cpp
#include
unsigned long left(unsigned long num, unsigned ct);
char * left(const char * str, int n = 1);

int main(){
char * trip = “Hawaii!!”;
unsigned long n = 12345678;
int i;
char * temp;
for(i = 1; i < 10; i++){
std::cout << left(n, i) << std::endl;
temp = left(trip, i);
std::cout << temp << std::endl;
delete [] temp;
}
return 0;
}

unsigned long left(unsigned long num, unsigned ct){
unsigned digits = 1;
unsigned long n = num;
if(ct == 0 || num == 0)
return 0;
while (n /= 10)
digits++;
if(digits > ct){
ct = digits - ct;
while (ct–)
num /= 10;
return num;
}
else
return num;
}

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];
while(i <= n)
p[i++] = ‘\0’;
return p;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
1
H
12
Ha
123
Haw
1234
Hawa
12345
Hawai
123456
Hawaii
1234567
Hawaii!
12345678
Hawaii!!
12345678
Hawaii!!

#endif

#if 0 //8.11 funtemp.cpp 2021年02月17日 星期三 21时25分31秒
#include
template
void Swap(T &a, T &b);

int main(){
int i = 10;
int j = 20;
std::cout << "i, j = " << i << ", " << j << “.\n”;
std::cout << “Using compiler-generated int swapper:\n”;
Swap(i, j);
std::cout << "Now i, j = " << i << ", " << j << “.\n”;
double x = 24.5;
double y = 81.7;
std::cout << "x, y = " << x << ", " << y << “.\n”;
std::cout << “Using compiler-generated double swapper:\n”;
Swap(x, y);
std::cout << "Now x, y = " << x << ", " << y << “.\n”;
return 0;
}

template
void Swap(T &a, T &b){
T temp;
temp = a;
a = b;
b = temp;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
i, j = 10, 20.
Using compiler-generated int swapper:
Now i, j = 20, 10.
x, y = 24.5, 81.7.
Using compiler-generated double swapper:
Now x, y = 81.7, 24.5.

#endif

#if 0 //8.12 twotemps.cpp 2021年02月17日 星期三 21时34分41秒
#include
template
void Swap(T &a, T &b);

template
void Swap(T *a, T *b, int n);
void Show(int a[]);
const int Lim = 8;

int main(){
int i = 10, j = 20;
std::cout << "i, j = " << i << ", " << j << “.\n”;
std::cout << “Using compiler-generated int swapper:\n”;
Swap(i, j);
std::cout << "Now i, j = " << i << ", " << j << “.\n”;

int d1[Lim]  {0, 7, 0, 4, 1, 7, 7, 6};
int d2[Lim]  {0, 7, 2, 0, 1, 9, 6, 9};
std::cout << "Original arrays:\n";
Show(d1);
Show(d2);
std::cout << std::endl;
Swap(d1, d2, Lim);
std::cout << "Swapped arrays:\n";
Show(d1);
Show(d2);
return 0;

}

template
void Swap(T &a, T &b){
T temp;
temp = a;
a = b;
b = temp;
}

template
void Swap(T a[], T b[], int n){
T temp;
for(int i = 0; i < n; i++){
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}

void Show (int a[]){
std::cout << a[0] << a[1] << “/”;
std::cout << a[2] << a[3] << “/”;
for(int i = 4; i < Lim; i++)
std::cout << a[i];
std::cout << std::endl;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
i, j = 10, 20.
Using compiler-generated int swapper:
Now i, j = 20, 10.
Original arrays:
07/04/1776
07/20/1969

Swapped arrays:
07/20/1969
07/04/1776

#endif

#if 0 //p288 twoswap.cpp 2021年02月17日 星期三 21时52分56秒
#include
template
void Swap(T &a, T &b);

struct job{
char name[40];
double salary;
int floor;
};

template<> void Swap(job &j1, job &j2);
void Show(job &j);

int main(){
std::cout.precision(2);
std::cout.setf(std::ios::fixed, std::ios::floatfield);
int i = 10, j = 20;
std::cout << "i, j = " << i << ", " << j << “.\n”;
std::cout << “Using compiler-generated int swapper:\n”;
Swap(i, j);
std::cout << "Now i, j = " << i << ", " << j << “.\n”;

job sue {"Susan Yaffee", 73000.60, 7};
job sidney {"Sidney Taffee", 78060.72, 9};
std::cout << "Before job swapping:\n";
Show(sue);
Show(sidney);
std::cout << std::endl;
Swap(sue, sidney);
std::cout << "After job swapping:\n";
Show(sue);
Show(sidney);
return 0;

}

template
void Swap(T &a, T &b){
T temp;
temp = a;
a = b;
b = temp;
}

template <> void Swap(job &j1, job &j2){
double t1;
int t2;
t1 = j1.salary;
j1.salary = j2.salary;
j2.salary = t1;
t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}

void Show (job &j){
std::cout << j.name << ": KaTeX parse error: Expected 'EOF', got '}' at position 58: … << std::endl; }̲ wannian07@wann… ./hello
i, j = 10, 20.
Using compiler-generated int swapper:
Now i, j = 20, 10.
Before job swapping:
Susan Yaffee: $73000.60 on floor 7
Sidney Taffee: $78060.72 on floor 9

After job swapping:
Susan Yaffee: $78060.72 on floor 9
Sidney Taffee: $73000.60 on floor 7

#endif

#if 0 //8.14 temptempover.cpp 2021年02月18日 星期四 19时12分44秒
#include
template
void ShowArray(T arr[], int n);

template
void ShowArray(T * arr[], int n);

struct debts{
char name[50];
double amount;
};

int main(){
int things[6] {13, 31, 103, 301, 310, 130};
struct debts mr_E[3] {
{“Ima Wolfe”, 2400.0},
{“Ura Foxe”, 1300.0},
{“Iby Stout”, 1800.0}
};
double * pd[3];

for(int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
std::cout << "Listing Mr. E's debts:\n";
ShowArray(pd, 3);
ShowArray(things, 6);
return 0;

}

template
void ShowArray(T arr[], int n){
std::cout << “template A\n”;
for(int i = 0; i < n; i++)
std::cout << arr[i] << ’ ';
std::cout << std::endl;
}

template
void ShowArray(T * arr[], int n){
std::cout << “template B\n”;
for(int i = 0; i < n; i++)
std::cout << *arr[i] << ’ ';
std::cout << std::endl;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Listing Mr. E’s debts:
template B
2400 1300 1800
template A
13 31 103 301 310 130

#endif

#if 0//8.15 choices.cpp
#include
template
T lesser(T a, T b){
return a < b ? a: b;
}

int lesser(int a, int b){
a = a < 0 ? -a : a;
b = b < 0 ? -b : b;
return a < b ? a : b;
}
int main(){
int m = 20;
int n = -30;
double x = 15.5;
double y = 25.9;

std::cout << lesser(m, n) << std::endl;
std::cout << lesser(x, y) << std::endl;
std::cout << lesser<>(m, n) << std::endl;
std::cout << lesser<int>(x, y) << std::endl;
return 0;

}
wannian07@wannian07-PC:~/Desktop$ ./hello
20
15.5
-30
15

#endif

#if 0//9.2 file1.cpp 2021年02月18日 星期四 20时01分25秒

#include
#include “coordin.h”

int main(){
rect rplace;
polar pplace;
std::cout << "Enter the x and y values: ";
while(std::cin >> rplace.x >> rplace.y){
pplace = rect_to_polar(rplace);
show_polar(pplace);
std::cout << "Next two numbers(q to quit): ";
}
std::cout << “Bye!\n”;
return 0;
}
wannian07@wannian07-PC:~/Desktop$ g++ hello.cpp -o hello file2.cpp
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter the x and y values: 120 80
distance = 144.222, angle = 33.6901 degrees
Next two numbers(q to quit): 120 50
distance = 130, angle = 22.6199 degrees
Next two numbers(q to quit): q
Bye!

#endif

#if 0 //9.4 auto.cpp p307 2021年02月18日 星期四 20时10分42秒
#include
void oil(int x);

int main(){
int texas = 31;
int year = 2011;
std::cout << "In main(), texas = " << texas << ", &texas = ";
std::cout << &texas << std::endl;
std::cout << "In main(), year = " << year << ", &year = ";
std::cout << &year << std::endl;
oil(texas);
std::cout << "In main(), texas = " << texas << ", &texas = ";
std::cout << &texas << std::endl;
std::cout << "In main(), year = " << year << ", &year = ";
std::cout << &year << std::endl;
return 0;
}

void oil(int x){
int texas = 5;
std::cout << "In oil(), texas = " << texas << ", &texas = ";
std::cout << &texas << std::endl;
std::cout << "In oil(), x = " << x << ", &x = ";
std::cout << &x << std::endl;
{
int texas = 113;
std::cout << "In block, texas = " << texas;
std::cout << ", &texas = " << &texas << std::endl;
std::cout << "In block, x = " << x << ", &x = ";
std::cout << &x << std::endl;
}
std::cout << "Post-block texas = " << texas;
std::cout << ", &texas = " << &texas << std::endl;
}
wannian07@wannian07-PC:~/Desktop$ ./hello
In main(), texas = 31, &texas = 0x7fff3d2bf4bc
In main(), year = 2011, &year = 0x7fff3d2bf4b8
In oil(), texas = 5, &texas = 0x7fff3d2bf49c
In oil(), x = 31, &x = 0x7fff3d2bf48c
In block, texas = 113, &texas = 0x7fff3d2bf498
In block, x = 31, &x = 0x7fff3d2bf48c
Post-block texas = 5, &texas = 0x7fff3d2bf49c
In main(), texas = 31, &texas = 0x7fff3d2bf4bc
In main(), year = 2011, &year = 0x7fff3d2bf4b8

#endif

#if 0 //9.5 external.cpp 2021年02月19日 星期五 18时54分23秒
#include
double warming = 0.3;
void update(double dt);
void local();

int main(){
std::cout << “Global warming is " << warming << " degrees.\n”;
update(0.1);
std::cout << “Global warming is " << warming << " degrees.\n”;
local();
std::cout << “Global warming is " << warming << " degrees.\n”;
return 0;
}
wannian07@wannian07-PC:~/Desktop$ g++ hello.cpp -o hello support.cpp

wannian07@wannian07-PC:~/Desktop$ ./hello
Global warming is 0.3 degrees.
Updating global warming to 0.4 degrees.
Global warming is 0.4 degrees.
Local warming = 0.8 degrees.
But global warming = 0.4 degrees.
Global warming is 0.4 degrees.

#endif

#if 0 // 9.7 twofile1.cpp 2021年02月19日 星期五 19时03分49秒
#include
int tom = 3;
int dick = 30;
static int harry = 300;
void remote_access();

int main(){
std::cout << “main() reports the following addresses:\n”;
std::cout << &tom << " = &tom, " << &dick << " = &dick, “;
std::cout << &harry << " = &harry\n”;
remote_access();
return 0;
}
wannian07@wannian07-PC:~/Desktop$ g++ hello.cpp -o hello twofile2.cpp

wannian07@wannian07-PC:~/Desktop$ ./hello
main() reports the following addresses:
0x601050 = &tom, 0x601054 = &dick, 0x601058 = &harry
remote_access() reports the following addresses:
0x601050 = &tom, 0x60105c = &dick, 0x601060 = &harry

#endif

#if 0//9.9 static.cpp 2021年02月19日 星期五 19时11分33秒
#include
const int ArSize = 10;
void strcount(const char * str);

int main(){
char input[ArSize];
char next;
std::cout << “Enter a line: \n”;
std::cin.get(input, ArSize);
while(std::cin){
std::cin.get(next);
while(next != ‘\n’)
std::cin.get(next);
strcount(input);
std::cout << “Enter next line(empty line to quit):\n”;
std::cin.get(input, ArSize);
}
std::cout << “Bye\n”;
return 0;
}

void strcount(const char * str){
static int total = 0;
int count = 0;
std::cout << “”" << str << “” contains “;
while(*str++)
count++;
total += count;
std::cout << count << " characters\n”;
std::cout << total << " characters total\n";
}
wannian07@wannian07-PC:~/Desktop$ ./hello
Enter a line:
nice pants
“nice pant” contains 9 characters
9 characters total
Enter next line(empty line to quit):
thanks
“thanks” contains 6 characters
15 characters total
Enter next line(empty line to quit):
parting is such sweet sorrow
“parting i” contains 9 characters
24 characters total
Enter next line(empty line to quit):
ok
“ok” contains 2 characters
26 characters total
Enter next line(empty line to quit):

Bye

#endif

#if 0 //9.10 newplace.cpp 2021年02月19日 星期五 19时22分14秒
#include
#include
const int BUF = 512;
const int N = 5;
char buffer[BUF];

int main(){
double *pd1, *pd2;
int i;
std::cout << “Calling new and placement new:\n”;
pd1 = new double[N];
pd2 = new (buffer) double[N];
for(i = 0; i < N; i++)
pd2[i] = pd1[i] = 1000 + 20.0 * i;
std::cout << “Memory addresses:\n” << " heap: " << pd1
<< " static: " << (void *)buffer << std::endl;
std::cout << “Memory contents:\n”;
for(i = 0; i < N; i++){
std::cout << pd1[i] << " at " << &pd1[i] << "; ";
std::cout << pd2[i] << " at " << &pd2[i] << std::endl;
}
std::cout << “\nCalling new and placement new a second time:\n”;
double *pd3, *pd4;
pd3 = new double[N];
pd4 = new(buffer) double[N];
for(i = 0; i < N; i++)
pd4[i] = pd3[i] = 1000 + 40.0 * i;
std::cout << “Memory contents:\n”;
for(i = 0; i < N; i++){
std::cout << pd3[i] << " at " << &pd3[i] << "; ";
std::cout << pd4[i] << " at " << &pd4[i] << std::endl;
}

std::cout << "\nCalling new and placement new a third time:\n";
delete[] pd1;
pd1 = new double[N];
pd2 = new (buffer + N * sizeof(double)) double[N];
for(i = 0; i < N; i++)pd2[i] = pd1[i] = 1000 + 60.0 * i;
std::cout << "Memory contents:\n";
for(i = 0; i < N; i++){std::cout << pd1[i] << " at " << &pd1[i] << "; ";std::cout << pd2[i] << " at " << &pd2[i] << std::endl;
}
delete [] pd1;
delete [] pd3;
return 0;

}
wannian07@wannian07-PC:~/Desktop$ ./hello
Calling new and placement new:
Memory addresses:
heap: 0x15c5030 static: 0x6021a0
Memory contents:
1000 at 0x15c5030; 1000 at 0x6021a0
1020 at 0x15c5038; 1020 at 0x6021a8
1040 at 0x15c5040; 1040 at 0x6021b0
1060 at 0x15c5048; 1060 at 0x6021b8
1080 at 0x15c5050; 1080 at 0x6021c0

Calling new and placement new a second time:
Memory contents:
1000 at 0x15c5060; 1000 at 0x6021a0
1040 at 0x15c5068; 1040 at 0x6021a8
1080 at 0x15c5070; 1080 at 0x6021b0
1120 at 0x15c5078; 1120 at 0x6021b8
1160 at 0x15c5080; 1160 at 0x6021c0

Calling new and placement new a third time:
Memory contents:
1000 at 0x15c5030; 1000 at 0x6021c8
1060 at 0x15c5038; 1060 at 0x6021d0
1120 at 0x15c5040; 1120 at 0x6021d8
1180 at 0x15c5048; 1180 at 0x6021e0
1240 at 0x15c5050; 1240 at 0x6021e8

#endif

#if 0//9.13 namessp.cpp 2021年02月19日 星期五 19时59分06秒
#include
#include “namesp.h”
void other(void);

void another(void);

int main(){
using debts::Debt;
using debts::showDebt;
Debt golf { {“Benny”, “Goatsniff”}, 120.0};
showDebt(golf);
other();
another();
return 0;
}

void other(void){
using namespace debts;
Person dg {“Doodles”, “Glister”};
showPerson(dg);
std::cout << std::endl;
Debt zippy[3];
int i;
for(i = 0; i < 3; i++)
getDebt(zippy[i]);
for(i = 0; i < 3; i++)
showDebt(zippy[i]);
std::cout << “Total debt: $” << sumDebts(zippy, 3) << std::endl;
return;
}

void another(void){
using pers::Person;
Person collector {“Milo”, “Rightshift”};
pers::showPerson(collector);
std::cout << std::endl;
}
wannian07@wannian07-PC:~/Desktop$ g++ hello.cpp -o hello namesp.cpp

wannian07@wannian07-PC:~/Desktop$ ./hello
Goatsniff, Benny: $120
Glister, Doodles
Enter first name: Arabella
Enter last name: Binx
Enter debt: 100
Enter first name: Cleve
Enter last name: Delaproux
Enter debt: 120
Enter first name: Eddie
Enter last name: Fiotox
Enter debt: 200
Binx, Arabella: $100
Delaproux, Cleve: $120
Fiotox, Eddie: $200
Total debt: $420
Rightshift, Milo

#endif

#if 0 //10.3 usestock0.cpp 2021年02月20日 星期六 19时08分46秒
#include
#include “stock00.h”

int main(){
Stock fluffy_the_cat;
fluffy_the_cat.acquire(“NanoSmart”, 20, 12.50);
fluffy_the_cat.show();
fluffy_the_cat.buy(15, 18.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(400, 20.00);
fluffy_the_cat.show();
fluffy_the_cat.buy(300000, 40.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(300000, 0.125);
fluffy_the_cat.show();
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello stock00.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Company: NanoSmart Shares: 20
Share Price: $12.5 Total Worth: $250
Company: NanoSmart Shares: 35
Share Price: $18.125 Total Worth: $634.375
You can’t sell more than you have! Transaction is aborted.
Company: NanoSmart Shares: 35
Share Price: $18.125 Total Worth: $634.375
Company: NanoSmart Shares: 300035
Share Price: $40.125 Total Worth: $1.20389e+07
Company: NanoSmart Shares: 35
Share Price: $0.125 Total Worth: $4.375

#endif

#if 0 // 10.6 usestock2.cpp 2021年02月20日 星期六 19时28分56秒
#include
#include “stock10.h”

int main(){
{
std::cout << “Using constructors to create new objects\n”;
Stock stock1(“NanoSmart”, 12, 20.0);
stock1.show();
Stock stock2 = Stock (“Boffo Objects”, 2, 2.0);
stock2.show();

 std::cout << "Assigning stock1 to stock2:\n";stock2 = stock1;std::cout << "Listing stock1 and stock2:\n";stock1.show();stock2.show();std::cout << "Using constructors to create new objects\n";stock1 = Stock("Nifty Foods", 10, 50.0); std::cout << "Revised stock1:\n";stock1.show();std::cout << "Done\n";
}
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello stock10.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Using constructors to create new objects
Constructor using NanoSmart called
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Constructor using Boffo Objects called
Company: Boffo Objects Shares: 2
Share Price: $2.000 Total Worth: $4.00
Assigning stock1 to stock2:
Listing stock1 and stock2:
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Using constructors to create new objects
Constructor using Nifty Foods called
Bye, Nifty Foods!
Revised stock1:
Company: Nifty Foods Shares: 10
Share Price: $50.000 Total Worth: $500.00
Done
Bye, NanoSmart!
Bye, Nifty Foods!

#endif

#if 0 //10.9 usestock2.cpp 2021年02月20日 星期六 19时45分34秒
#include
#include “stock20.h”
const int STKS = 4;

int main(){
Stock stocks[STKS] {
Stock(“NanoSmart”, 12, 20.0),
Stock(“Boffo Objects”, 200, 2.0),
Stock(“Monolithic Obelisks”, 130, 3.25),
Stock(“Fleep Enterprises”, 60, 6.5)
};
std::cout << “Stock holdings:\n”;
int st;
for(st = 0; st < STKS; st++)
stocks[st].show();
const Stock * top = &stocks[0];

for(st = 1; st < STKS; st++)top = &top->topval(stocks[st]);
std::cout << "\nMost valuable holding:\n";
top->show();
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello stock20.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Stock holdings:
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Company: Boffo Objects Shares: 200
Share Price: $2.000 Total Worth: $400.00
Company: Monolithic Obelisks Shares: 130
Share Price: $3.250 Total Worth: $422.50
Company: Fleep Enterprises Shares: 60
Share Price: $6.500 Total Worth: $390.00

Most valuable holding:
Company: Monolithic Obelisks Shares: 130
Share Price: $3.250 Total Worth: $422.50

#endif

#if 0 // 10.12 stacker.cpp 2021年02月20日 星期六 20时17分25秒
#include
#include
#include “stack.h”

int main(){
Stack st;
char ch;
unsigned long po;
std::cout << “Please enter A to add a purchase order, \n”
<< “P, to process a PO, o Q to quit.\n”;
while(std::cin >> ch && toupper(ch) != ‘Q’){
while(std::cin.get() != ‘\n’)
continue;
if(!isalpha(ch)){
std::cout << ‘\a’;
continue;
}
switch(ch){
case ‘A’:
case ‘a’: std::cout << "Enter a PO number to add: “;
std::cin >> po;
if(st.isfull())
std::cout << “stack already full\n”;
else
st.push(po);
break;
case ‘P’:
case ‘p’: if (st.isempty())
std::cout << “stack already empty\n”;
else{
st.pop(po);
std::cout << “PO #” << po << " popped\n”;
}
break;
}
std::cout << “Please enter A to add a purchase order, \n”
<< “P to process a PO, or Q to quit.\n”;
}
std::cout << “Bye\n”;
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Please enter A to add a purchase order,
P, to process a PO, o Q to quit.
A
Enter a PO number to add: 17885
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
PO #17885 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: 17965
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: 18002
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
PO #18002 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
PO #17965 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
stack already empty
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
Q
Bye

#endif

#if 0//usetime0.cpp 2021年02月21日 星期日 07时52分12秒
#include
#include “mytime0.h”

int main(){
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
std::cout << "planning time = ";
planning.Show();
std::cout << std::endl;

std::cout << "coding time = ";
coding.Show();
std::cout << std::endl;std::cout << "fixing time = ";
fixing.Show();
std::cout << std::endl;total = coding.Sum(fixing);
std::cout << "coding.Sum(fixing) = ";
total.Show();
std::cout << std::endl;
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello mytime0.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
planning time = 0 hours, 0 minutes
coding time = 2 hours, 40 minutes
fixing time = 5 hours, 55 minutes
coding.Sum(fixing) = 8 hours, 35 minutes

#endif

#if 0 //usetime1.cpp 2021年02月21日 星期日 08时05分37秒
#include
#include “mytime1.h”

int main(){
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
std::cout << "planning time = ";
planning.Show();
std::cout << std::endl;

std::cout << "coding time = ";
coding.Show();
std::cout << std::endl;std::cout << "fixing time = ";
fixing.Show();
std::cout << std::endl;total = coding + fixing;
std::cout << "coding + fixing = ";
total.Show();
std::cout << std::endl;   Time morefixing(3, 28);
std::cout << "more fixing time = ";
morefixing.Show();
std::cout << std::endl;
total = morefixing.operator+(total);
std::cout << "morefixing.operator+(total) = ";
total.Show();
std::cout << std::endl;
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello mytime1.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
planning time = 0 hours, 0 minutes
coding time = 2 hours, 40 minutes
fixing time = 5 hours, 55 minutes
coding + fixing = 8 hours, 35 minutes
more fixing time = 3 hours, 28 minutes
morefixing.operator+(total) = 12 hours, 3 minutes

#endif

#if 0 //usetime2.cpp 2021年02月21日 星期日 08时13分24秒
#include
#include “mytime2.h”

int main(){
Time weeding(4, 35);
Time waxing(2, 47);
Time total;
Time diff;
Time adjusted;

std::cout << "weeding time = ";
weeding.Show();
std::cout << std::endl;std::cout << "waxing time = ";
waxing.Show();
std::cout << std::endl;std::cout << "total work time = ";
total = weeding + waxing;
total.Show();
std::cout << std::endl;diff = weeding - waxing;
std::cout << "weeding time - waxing time = ";
diff.Show();
std::cout << std::endl;adjusted = total * 1.5;
std::cout << "adjuested work time = ";
adjusted.Show();
std::cout << std::endl;
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello mytime2.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
weeding time = 4 hours, 35 minutes
waxing time = 2 hours, 47 minutes
total work time = 7 hours, 22 minutes
weeding time - waxing time = 2 hours, 0 minutes
adjuested work time = 11 hours, 3 minutes

#endif

#if 0 //usetime3.cpp 2021年02月21日 星期日 18时58分19秒
#include
#include “mytime3.h”

int main(){
Time aida(3, 35);
Time tosca(2, 48);
Time temp;

std::cout << "Aida and Tosca:\n";
std::cout << aida << "; " << tosca << std::endl;
temp = aida + tosca;
std::cout << "Aida + Tosca: " << temp << std::endl;
temp = aida * 1.17;
std::cout << "Aida * 1.17: " << temp << std::endl;
std::cout << "10.0 * Tosca: " << 10.0 * tosca << std::endl;
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello mytime3.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Aida and Tosca:
3 hours, 35 minutes; 2 hours, 48 minutes
Aida + Tosca: 6 hours, 23 minutes
Aida * 1.17: 4 hours, 11 minutes
10.0 * Tosca: 28 hours, 0 minutes

#endif

#if 0 // 11.15 randwalk.cpp 2021年02月21日 星期日 19时52分06秒
#include
#include
#include
#include “vector.h”

int main(){
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
std::cout << "Enter target distance (q to quit): ";
while (std::cin >> target){
std::cout << "Enter step length: ";
if(!(std::cin >> dstep))
break;

 while(result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}std::cout << "After " << steps << " steps, the subject ""has the following location:\n";std::cout << result << std::endl;result.polar_mode();std::cout << " or\n" << result << std::endl;std::cout << "Average outward distance per step = "<< result.magval()/steps << std::endl;steps = 0;result.reset(0.0, 0.0);std::cout << "Enter target distance (q to quit): ";
}
std::cout << "Bye!\n";
std::cin.clear();
while (std::cin.get() != '\n')continue;
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello vector.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter target distance (q to quit): 50
Enter step length: 2
After 250 steps, the subject has the following location:
(x, y) = (-51.1531, -4.93902)
or
(m, a) = (51.391, -174.485)
Average outward distance per step = 0.205564
Enter target distance (q to quit): 50
Enter step length: 2
After 206 steps, the subject has the following location:
(x, y) = (45.9403, 19.8005)
or
(m, a) = (50.0257, 23.3163)
Average outward distance per step = 0.242843
Enter target distance (q to quit): 50
Enter step length: 1
After 1214 steps, the subject has the following location:
(x, y) = (46.8323, 18.5804)
or
(m, a) = (50.3835, 21.6404)
Average outward distance per step = 0.0415021
Enter target distance (q to quit): q
Bye!

#endif

#if 0 // 11.18 stone.cpp 2021年02月22日 星期一 19时14分15秒
#include
#include “stonewt.h”
void display(const Stonewt & st, int n);

int main(){
Stonewt incognito = 275;
Stonewt wolfe(285.7);
Stonewt taft(21, 8);
std::cout << "The celebrity weighed ";
incognito.show_stn();
std::cout << "The detective weighed ";
wolfe.show_stn();
std::cout << "The President weighted ";
taft.show_lbs();
incognito = 276.8;
taft = 325;
std::cout << "After dinner, the celebrity weighed ";
incognito.show_stn();
std::cout << "After dinner, the President weighed ";
taft.show_lbs();
display(taft, 2);
std::cout << “The wrestler weighed even more.\n”;
display(422, 2);
std::cout << “No stone left unearned\n”;
return 0;
}

void display(const Stonewt & st, int n){
for(int i = 0; i < n; i++){
std::cout << "Wow! ";
st.show_stn();
}
}

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello stonewt.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
The celebrity weighed 19 stone, 9 pounds
The detective weighed 20 stone, 5.7 pounds
The President weighted 302 pounds
After dinner, the celebrity weighed 19 stone, 10.8 pounds
After dinner, the President weighed 325 pounds
Wow! 23 stone, 3 pounds
Wow! 23 stone, 3 pounds
The wrestler weighed even more.
Wow! 30 stone, 2 pounds
Wow! 30 stone, 2 pounds
No stone left unearned

#endif

#if 0 //11.21 stone1.cpp 2021年02月22日 星期一 19时27分07秒
#include
#include “stonewt1.h”

int main(){
Stonewt poppins(9, 2.8);
double p_wt = poppins;
std::cout << "Convert to double => ";
std::cout << “Poppins: " << p_wt << " pounds.\n”;
std::cout << "Convert to int => ";
std::cout << “Poppins: " << int(poppins) << " pounds.\n”;
return 0;
}

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello stonewt1.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Convert to double => Poppins: 128.8 pounds.
Convert to int => Poppins: 129 pounds.

#endif

#if 0 // 12.3 vegnews.cpp 2021年02月22日 星期一 19时41分38秒
#include
#include “strngbad.h”

void callme1 (StringBad &);
void callme2 (StringBad);

int main(){
using std::endl;{
std::cout << “Starting an inner block.\n”;
StringBad headline1 (“Celery Stalks at Mindnight”);
StringBad headline2 (“Lettuce Prey”);
StringBad sports(“Spinach Leaves Bowl for Dollars”);
std::cout << "headline1: " << headline1 << std::endl;
std::cout << "headline2: " << headline2 << std::endl;
std::cout << "sports: " << sports << std::endl;
callme1(headline1);
std::cout << "headline1: " << headline1 << std::endl;
callme2(headline2);
std::cout << "headline2: " << headline2 << std::endl;
std::cout << “Initialize one object to another:\n”;
StringBad sailor = sports;
std::cout << "sailor: " << sailor << std::endl;
std::cout << “Assign one object to another:\n”;
StringBad knot;
knot = headline1;
std::cout << "knot: " << knot << std::endl;
std::cout << “Exiting the block.\n”;
}
std::cout << “End of main()\n”;
return 0;
}

void callme1(StringBad & rsb){
std::cout << “String passed by reference:\n”;
std::cout << " “” << rsb << “”\n";
}

void callme2(StringBad sb){
std::cout << “String passed by value:\n”;
std::cout << " “” << sb << “”\n";
}

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello strngbad.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Starting an inner block.
1: “Celery Stalks at Mindnight” object created
2: “Lettuce Prey” object created
3: “Spinach Leaves Bowl for Dollars” object created
headline1: Celery Stalks at Mindnight
headline2: Lettuce Prey
sports: Spinach Leaves Bowl for Dollars
String passed by reference:
“Celery Stalks at Mindnight”
headline1: Celery Stalks at Mindnight
String passed by value:
“Lettuce Prey”
“Lettuce Prey” object deleted, 2 left
headline2:
Initialize one object to another:
sailor: Spinach Leaves Bowl for Dollars
Assign one object to another:
3: “C++” default object created
knot: Celery Stalks at Mindnight
Exiting the block.
“Celery Stalks at Mindnight” object deleted, 2 left
“Spinach Leaves Bowl for Dollars” object deleted, 1 left
" Pg" object deleted, 0 left
*** Error in `./hello’: double free or corruption (fasttop): 0x0000000001675080 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7fc68a71dbfb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7fc68a723fc6]
/lib/x86_64-linux-gnu/libc.so.6(+0x7780e)[0x7fc68a72480e]
./hello[0x400f6c]
./hello[0x400c1e]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fc68a6cd2e1]
./hello[0x40093a]
======= Memory map: ========
00400000-00402000 r-xp 00000000 103:04 11534889 /home/wannian07/Desktop/c++Primer Plus/hello
00601000-00602000 r–p 00001000 103:04 11534889 /home/wannian07/Desktop/c++Primer Plus/hello
00602000-00603000 rw-p 00002000 103:04 11534889 /home/wannian07/Desktop/c++Primer Plus/hello
01663000-01695000 rw-p 00000000 00:00 0 [heap]
7fc684000000-7fc684021000 rw-p 00000000 00:00 0
7fc684021000-7fc688000000 —p 00000000 00:00 0
7fc68a6ad000-7fc68a842000 r-xp 00000000 103:03 4982692 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fc68a842000-7fc68aa42000 —p 00195000 103:03 4982692 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fc68aa42000-7fc68aa46000 r–p 00195000 103:03 4982692 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fc68aa46000-7fc68aa48000 rw-p 00199000 103:03 4982692 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fc68aa48000-7fc68aa4c000 rw-p 00000000 00:00 0
7fc68aa4c000-7fc68aa62000 r-xp 00000000 103:03 4982984 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fc68aa62000-7fc68ac61000 —p 00016000 103:03 4982984 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fc68ac61000-7fc68ac62000 r–p 00015000 103:03 4982984 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fc68ac62000-7fc68ac63000 rw-p 00016000 103:03 4982984 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fc68ac63000-7fc68ad66000 r-xp 00000000 103:03 4983468 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fc68ad66000-7fc68af65000 —p 00103000 103:03 4983468 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fc68af65000-7fc68af66000 r–p 00102000 103:03 4983468 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fc68af66000-7fc68af67000 rw-p 00103000 103:03 4983468 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fc68af67000-7fc68b0d9000 r-xp 00000000 103:03 4983940 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fc68b0d9000-7fc68b2d9000 —p 00172000 103:03 4983940 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fc68b2d9000-7fc68b2e3000 r–p 00172000 103:03 4983940 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fc68b2e3000-7fc68b2e5000 rw-p 0017c000 103:03 4983940 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fc68b2e5000-7fc68b2e9000 rw-p 00000000 00:00 0
7fc68b2e9000-7fc68b30c000 r-xp 00000000 103:03 4982244 /usr/lib/x86_64-linux-gnu/ld-2.24.so
7fc68b4e4000-7fc68b4ea000 rw-p 00000000 00:00 0
7fc68b50b000-7fc68b50c000 rw-p 00000000 00:00 0
7fc68b50c000-7fc68b50d000 r–p 00023000 103:03 4982244 /usr/lib/x86_64-linux-gnu/ld-2.24.so
7fc68b50d000-7fc68b50e000 rw-p 00024000 103:03 4982244 /usr/lib/x86_64-linux-gnu/ld-2.24.so
7fc68b50e000-7fc68b50f000 rw-p 00000000 00:00 0
7ffdf181d000-7ffdf1840000 rw-p 00000000 00:00 0 [stack]
7ffdf1990000-7ffdf1993000 r–p 00000000 00:00 0 [vvar]
7ffdf1993000-7ffdf1994000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
已放弃

#endif

#if 0 //12.6 sayings1.cpp 2021年02月23日 星期二 19时26分38秒
#include
#include “string1.h”
const int ArSize = 10;
const int MaxLen = 81;

int main(){
String name;
std::cout << "Hi, what’s your name?\n>> ";
std::cin >> name;

std::cout << name << ", please enter up to " << ArSize<< " short saying <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for(i = 0; i < ArSize; i++){std::cout << i+1 << ": ";std::cin.get(temp, MaxLen);while (std::cin && std::cin.get() != '\n')continue;if(!std::cin || temp[0] == '\0')break;elsesayings[i] = temp;
}
int total = i;if(total > 0){std::cout << "Here are your sayings:\n";for(i = 0; i < total; i++)    std::cout << sayings[i][0] << ": " << sayings[i] << std::endl;int shortest = 0;
int first = 0;
for (i = 1; i < total; i++){if(sayings[i].length() < sayings[shortest].length())shortest = i;if(sayings[i] < sayings[first])first = i;
}
std::cout << "Shortest saying:\n" << sayings[shortest] << std::endl;
std::cout << "First alphabetically:\n" << sayings[first] << std::endl;
std::cout << "This program used " << String::HowMany()<< " String objects. Bye.\n";
}
elsestd::cout << "No input! Bye.\n";
return 0;

}

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ g++ hello.cpp -o hello string1.cpp

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Hi, what’s your name?

Misty Gutz
Misty Gutz, please enter up to 10 short saying :
1: a fool and his money are soon parted
2: penny wise, pound foolish
3: the love of money is the root of much evil
4: cut of sight, out of mind
5: absence makes the heart grow fonder
6: absinthe makes the hart grow fonder
7:
Here are your sayings:
a: a fool and his money are soon parted
p: penny wise, pound foolish
t: the love of money is the root of much evil
c: cut of sight, out of mind
a: absence makes the heart grow fonder
a: absinthe makes the hart grow fonder
Shortest saying:
penny wise, pound foolish
First alphabetically:
a fool and his money are soon parted
This program used 11 String objects. Bye.

#endif

#if 0// 12.7 sayings2.cpp 2021年02月23日 星期二 19时46分59秒

#include
#include
#include
#include “string1.h”
const int ArSize = 10;
const int MaxLen = 81;

int main(){
String name;
std::cout << "Hi, what’s your name?\n>> ";
std::cin >> name;

std::cout << name << ", please enter up to " << ArSize<< " short saying <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for(i = 0; i < ArSize; i++){std::cout << i+1 << ": ";std::cin.get(temp, MaxLen);while (std::cin && std::cin.get() != '\n')continue;if(!std::cin || temp[0] == '\0')break;elsesayings[i] = temp;
}
int total = i;if(total > 0){std::cout << "Here are your sayings:\n";for(i = 0; i < total; i++)    std::cout << sayings[i] << "\n";String * shortest = &sayings[0];String * first = &sayings[0];for(i = 1; i < total; i++){if(sayings[i].length() < shortest->length())shortest = &sayings[i];if(sayings[i] < *first)first = &sayings[i];}std::cout << "Shortest saying:\n" << * shortest << std::endl;std::cout << "First alphabetically:\n" << * first << std::endl;srand(time(0));int choice = rand() % total;String * favorite = new String(sayings[choice]);std::cout << "My favorite saying:\n" << *favorite << std::endl;delete favorite;
}
elsestd::cout << "Not much to say, eh?\n";std::cout << "Bye.\n";
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Hi, what’s your name?

Kirt Rood
Kirt Rood, please enter up to 10 short saying :
1: a friend in need is a friend indeed
2: neither a borrower nor a lender be
3: a stitch in time saves nine
4: a niche in time saves stine
5: it takes a crook to catch a crook
6: cold hands, warm heart
7:
Here are your sayings:
a friend in need is a friend indeed
neither a borrower nor a lender be
a stitch in time saves nine
a niche in time saves stine
it takes a crook to catch a crook
cold hands, warm heart
Shortest saying:
cold hands, warm heart
First alphabetically:
a friend in need is a friend indeed
My favorite saying:
it takes a crook to catch a crook
Bye.

#endif

#if 0 // 12.8 placenew1.cpp 2021年02月24日 星期三 19时02分36秒
#include
#include
#include
const int BUF = 512;
class JustTesting{
private:
std::string words;
int number;
public:
JustTesting(const std::string & s = “Just Testing”, int n = 0){
words = s; number = n; std::cout << words << " constructed\n";
}
~JustTesting() { std::cout << words << " destroyed\n";}
void Show() const { std::cout << words << ", " << number << std::endl;}
};

int main(){
char * buffer = new char[BUF];
JustTesting *pc1, *pc2;
pc1 = new (buffer) JustTesting;
pc2 = new JustTesting(“Heapl”, 20);

std::cout << "Memory block addresses:\n" << "buffer: "<< (void * )buffer << "  heap: " << pc2 << std::endl;
std::cout << "Memory contents:\n";
std::cout << pc1 << ": ";
pc1->Show();
std::cout << pc2 << ": ";
pc2->Show();JustTesting *pc3, *pc4;
pc3 = new(buffer) JustTesting("Bad Idea", 6);
pc4 = new JustTesting("Heap2", 10);std::cout << "Memory contents:\n";
std::cout << pc3 << ": ";
pc3->Show();
std::cout << pc4 << ": ";
pc4->Show();delete pc2;
delete pc4;
delete [] buffer;
std::cout << "Done\n";return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Just Testing constructed
Heapl constructed
Memory block addresses:
buffer: 0x13b2c20 heap: 0x13b3240
Memory contents:
0x13b2c20: Just Testing, 0
0x13b3240: Heapl, 20
Bad Idea constructed
Heap2 constructed
Memory contents:
0x13b2c20: Bad Idea, 6
0x13b3270: Heap2, 10
Heapl destroyed
Heap2 destroyed
Done

#endif

#if 0 // 12.9 placenew2.cpp 2021年02月24日 星期三 19时02分36秒
#include
#include
#include
const int BUF = 512;
class JustTesting{
private:
std::string words;
int number;
public:
JustTesting(const std::string & s = “Just Testing”, int n = 0){
words = s; number = n; std::cout << words << " constructed\n";
}
~JustTesting() { std::cout << words << " destroyed\n";}
void Show() const { std::cout << words << ", " << number << std::endl;}
};

int main(){
char * buffer = new char[BUF];
JustTesting *pc1, *pc2;
pc1 = new (buffer) JustTesting;
pc2 = new JustTesting(“Heapl”, 20);

std::cout << "Memory block addresses:\n" << "buffer: "<< (void * )buffer << "  heap: " << pc2 << std::endl;
std::cout << "Memory contents:\n";
std::cout << pc1 << ": ";
pc1->Show();
std::cout << pc2 << ": ";
pc2->Show();JustTesting *pc3, *pc4;
pc3 = new(buffer +sizeof(JustTesting) ) JustTesting("Better Idea", 6);
pc4 = new JustTesting("Heap2", 10);std::cout << "Memory contents:\n";
std::cout << pc3 << ": ";
pc3->Show();
std::cout << pc4 << ": ";
pc4->Show();delete pc2;
delete pc4;
pc3->~JustTesting();
pc1->~JustTesting();
delete [] buffer;
std::cout << "Done\n";return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Just Testing constructed
Heapl constructed
Memory block addresses:
buffer: 0x1ba6c20 heap: 0x1ba7240
Memory contents:
0x1ba6c20: Just Testing, 0
0x1ba7240: Heapl, 20
Better Idea constructed
Heap2 constructed
Memory contents:
0x1ba6c48: Better Idea, 6
0x1ba7270: Heap2, 10
Heapl destroyed
Heap2 destroyed
Better Idea destroyed
Just Testing destroyed
Done

#endif

#if 0 // 12.12 bank.cpp 2021年02月24日 星期三 19时38分11秒
#include
#include
#include
#include “queue.h”
const int MIN_PER_HR = 60;

bool newcustomer(double x);

int main(){
std::srand(std::time(0));
std::cout << “Case Study: Bank of Heather Automatic Teller\n”;
std::cout << "Enter maximum size of queue: ";
int qs;
std::cin >> qs;
Queue line(qs);

std::cout << "Enter the number of simulation hours: ";
int hours;
std::cin >> hours;
long cyclelimit = MIN_PER_HR * hours;
std::cout << "Enter the average number of customers per hour: ";
double perhour;
std::cin >> perhour;
double min_per_cust;
min_per_cust = MIN_PER_HR / perhour;Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
long line_wait = 0;for (int cycle = 0; cycle < cyclelimit; cycle++){if(newcustomer(min_per_cust)){if(line.isfull())

/*
if(line.isfull)
hello.cpp:4630:18: 错误:无法将‘Queue::isfull’从类型‘bool (Queue:

板凳——————————————————c++(104)相关推荐

  1. 零起点学算法104——第几天?

    零起点学算法104--第几天? Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lld Description 给定一个日期,输 ...

  2. 【tensorflow】OP_REQUIRES failed at variable_ops.cc:104 Already exists: Resource

    如下代码片段 outputs = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(units=half_depth, use_bias=False, ...

  3. 腾讯绝地求生手游席卷全球,104个国家地区IOS登顶

    PUBG Mobile 腾讯旗下的PUBG Mobile在20日上线后,迅速成为世界级爆款游戏,在104个国家和地区登顶. PUBG Mobile 腾讯旗下的光子工作室这次可算是在全球的火爆了一把.这 ...

  4. 领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. 我的博客今天2岁104天了,我领取了…

    我的博客今天2岁104天了,我领取了徽章. 2011.06.09,我在新浪博客安家. 2011.06.09,我写下了第一篇博文:<看懂这些故事 你做人就很成功了>. 2011.06.09, ...

  6. 编写高质量代码改善C#程序的157个建议——建议104:用多态代替条件语句

    建议104:用多态代替条件语句 假设要开发一个自动驾驶系统.在设计之初,此自动驾驶系统拥有一个驾驶系统命令的枚举类型: enum DriveCommand{Start,Stop} 当前该枚举存在两个命 ...

  7. if the parser found inconsistent certificates on the files in the .apk.104

    当静默安装提示104时,是说升级的APK 和本地已经安装的APK 签名不一致,所以无法升级. 经百度,找到知乎同学@陈子腾的回答,找到了问题所在. 可以比对apk签名的fingerprint. 假定安 ...

  8. Python3.5入门到项目实战(104天课程)

    今天给大家分享一套详细的教程[Python3.5入门到项目实战(104天)],希望能够帮助到正在学Python的你,好了,话不多说,直接上资源: 资源目录 第01部分- 计算机与Linu基础(01-0 ...

  9. pc安装linux内核,PC/104平台嵌入式Linux系统核心定制方法

    摘 要:基于PC/104平台的嵌入式Linux技术在海洋自动观测系统中具有广泛的应用前景,Linux核心定制方法的研究是嵌入式Linux系统研制的关键工作.本文结合PC/104平台嵌入式Linux系统 ...

  10. 1小时内注册公司 政务中心104个窗口同厅办公

    1小时内注册公司 政务中心104个窗口同厅办公 (三湘都市报记者肖雯栎实习生罗巧姣)"今天办事情很顺利,还不到1个小时就把公司注册好了!"在上海工作多年的邓向雷,回到家乡长沙准备自 ...

最新文章

  1. android 绘画,Android绘图基础
  2. NSTimer与Run loop Modes
  3. 单片机 c语言 定义i o,【51单片机】普通I/O口模拟SPI口C语言程序
  4. hbase linux 命令,在linux下操作hbase
  5. python学到哪知道baseline_Python NLTK学习6(创建词性标注器)
  6. License for package Android SDK Build-Tools 28.0.3 not accepted.
  7. 在嵌入式uClibc上移植valgrind
  8. Springboot结合ESAPI——配置XSS防御过滤
  9. 在线抽签html,抽签网页板代码
  10. SketchUp 更新插件,不用重启让更新生效
  11. 数仓建模—数据资产管理
  12. 加法器、半加器、全加器、超前进位加法器
  13. 跳跃游戏 改 dfs
  14. 关于Diy51单片机的趣事
  15. 摩托车一键启动无钥匙进入系统,摩托车PKE无钥匙进入一键启动系统
  16. 不会英语自学php要多久,一个人自学英语要多久 自学英语的方法
  17. 采用特殊硬件指令对密码学算法加速
  18. idea 启动SpringBoot项目出现java程序包:xxx不存在
  19. 美版头条BuzzFeed两天股价涨3倍:因采用ChatGPT上岗写稿
  20. 《游戏的人》笔记——第一章

热门文章

  1. Mac 下拷贝文件到移动硬盘
  2. VUE时间戳和时间相互转换,使用UI库为Ant Design of Vue
  3. gif一键抠图 在线_在线抠图网站,轻松搞定抠图,效果堪比PS!
  4. 无法访问共享计算机文件,电脑无法访问共享文件怎么解决?
  5. php是哪个快递,php快递查询API类(支持各种快递的查询)
  6. 常见几种web攻击方式和防御方法
  7. java macd_MACD到底是什么?
  8. Seaborn 绘图中设置字体及大小
  9. 微信app支付 服务器接口,iOS微信支付——APP调用微信支付接口
  10. Python——批量发送邮件(持续更新)