板凳——————————————————c++(104)

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
摘要由CSDN通过智能技术生成

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$ ./hello
	Type, and I shall repeat.
	An ineffable joy suffused me as I beheld
	66111331061111021031039899109102331071121223311611810310311811610210133110102339811633743399102105102109101
	the wonders of modern computing.
	11710510233120112111101102115116331121033311011210110211511133100112110113118117106111104
	Please 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 >>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值