书内代码部分
程序清单 3.1,limits.cpp
// limits.cpp -- some integer limits
#include <iostream>
#include <climits> // use limits.h for older systems
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
// sizeof operator yields size of type or of variable
cout << "int is " << sizeof(int) << " bytes." << endl;
cout << "short is " << sizeof n_short << " bytes." << endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << endl;
cout << "Maximum values: " << endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong << endl << endl;
cout << "Minimum int value = " << INT_MIN << endl;
cout << "bits per byte = " << CHAR_BIT << endl;
return 0;
}
输出结果:
程序清单 3.2,exceed.cpp
// exceed.cpp -- exceeding some integer limits
#include <iostream>
#include <climits>
#define ZERO 0
int main()
{
using namespace std;
short sam = SHRT_MAX;
unsigned short sue = sam; // okay if variable sam already defined
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl
<< "Add $1 to each account." << endl << "Now ";
sam = sam + 1;
sue = sue + 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\nPoor Sam!" << endl;
sam = ZERO;
sue = ZERO;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl;
cout << "Take $1 from each account." << endl << "Now ";
sam--;
sue--;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl << "Lucky Sue!" << endl;
return 0;
}
输出结果:
程序清单 3.3,hexoct.cpp
// hexoct1.cpp -- show hex and octal literals
#include <iostream>
int main()
{
using namespace std;
int chest = 42; // decimal integer literal
int waist = 0x42; // hexadecimal integer literal
int inseam = 042; // octal integer literal
cout << "Monsieur cuts a striking figure!\n";
cout << "chest = " << chest << " (42 in decimal)\n";
cout << "waist = " << waist << " (0x42 in hex)\n";
cout << "inseam = " << inseam << " (042 in octal)\n";
return 0;
}
输出结果:
程序清单 3.4,hexoct2.cpp
// hexoct2.cpp -- display values in hex and octal
#include <iostream>
using namespace std;
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
cout << "Monsieur cuts a striking fiagure!" << endl;
cout << "chest = " << chest << " (decimal for 42)" << endl;
cout << hex; // manipulator for changing number base
cout << "waist = " << waist << " (hexadecimal for 42)" << endl;
cout << oct; // manipulator for changing number base
cout << "inseam = " << inseam << " (octal for 42)" << endl;
return 0;
}
输出结果:
程序清单 3.5,chartype.c
// chartype.cpp -- the char type
#include <iostream>
int main()
{
using namespace std;
char ch;
cout << "Enter a character: " << endl;
cin >> ch;
cout << "Hola! ";
cout << "Thank you for the " << ch << " character." << endl;
return 0;
}
输出结果:
程序清单 3.6,morechar.cpp
// morechar.cpp -- the char type and int type contrasted
#include <iostream>
int main()
{
using namespace std;
char ch = 'M'; // assign ASCII code for M to ch
int i = ch; // store same code in an int
cout << "The ASCII code for " << ch << " is " << i << endl;
cout << "Add one to the character code:" << endl;
++ch;
cout << "The ASCII code for " << ch << " is " << i << endl;
// using the cout.put() member function to display a char
cout << "Displaying char ch using cout. put(ch): ";
cout.put(ch);
// using cout.put() to display a char constant
cout.put('!');
cout << endl << "Done" << endl;
return 0;
}
输出结果:
程序清单 3.7,bondini.cpp
// bondini.cpp -- using escape sequences
#include <iostream>
int main()
{
using namespace std;
cout << "\aOperation \"HyperHype\" is now activated!\n";
cout << "Enter your agent code:________\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered " << code << "...\n";
cout << "\aCode verified! Proceed with Plan Z3!\n";
return 0;
}
输出结果:
程序清单 3.8,floatnum.cpp
// floatnum.cpp floating-point types
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point
float tub = 10.0 / 3.0; // good to about 6 places
double mint = 10.0 / 3.0; // good to about 15 places
const float million = 1.0e6;
cout << "tub = " << tub;
cout << ", a million tubs = " << million * tub;
cout << ", \nand ten million tubs = ";
cout << 10 * million * tub << endl;
cout << "mint = " << mint << " and a million mints = ";
cout << million * mint << endl;
return 0;
}
输出结果:
程序清单 3.9,fltadd.cpp
// fltadd.cpp -- precision problems with float
#include <iostream>
int main()
{
using namespace std;
float a = 2.34E+22f;
float b = a + 1.0f;
cout << "a = " << a << endl;
cout << "b - a = " << b - a << endl;
return 0;
}
输出结果:
程序清单 3.10,arith.cpp
// arith.cpp -- some C++ arithmetic
#include <iostream>
int main()
{
using namespace std;
float hats, heads;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Enter a number: ";
cin >> hats;
cout << "Enter another number: ";
cin >> heads;
cout << "hats = " << hats << "; heads = " << heads << endl;
cout << "hats + heads = " << hats + heads << endl;
cout << "hats - heads = " << hats - heads << endl;
cout << "hats * heads = " << hats * heads << endl;
cout << "hats / heads = " << hats / heads << endl;
return 0;
}
输出结果 :
程序清单 3.11,divide.cpp
// divide.cpp -- integer and floating-point division
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Integer division: 9/5 = " << 9 / 5 << endl;
cout << "Floating-point division: 9.0/5.0 = ";
cout << 9.0 / 5.0 << endl;
cout << "Mixid division: 9.0/5 = " << 9.0 / 5 << endl;
cout << "double constants: 1e7/9.0 = ";
cout << 1.e7 / 9.0 << endl;
cout << "float constants: 1e7f/9.0f = ";
cout << 1.e7f / 9.0f << endl;
return 0;
}
输出结果:
程序清单 3.12,modulus.cpp
// modulus.cpp -- uses % operator to convert lbs to stone
#include <iostream>
int main()
{
using namespace std;
const int Lbs_per_stn = 14;
int lbs;
cout << "Enter your weight in pounds: ";
cin >> lbs;
int stone = lbs / Lbs_per_stn; // whole stone
int pounds = lbs % Lbs_per_stn; // remainder in pounds
cout << lbs << " pounds are " << stone
<< " stone, " << pounds << " pound(s).\n";
return 0;
}
输出结果:
程序清单 3.13,assign.cpp
// init.cpp -- type changes on initialization
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
float tree = 3; // int converted to float
int guess(3.9832); // double converted to int
int debt = 7.2E12; // result not defind in C++
cout << "tree = " << tree << endl;
cout << "guess = " << guess << endl;
cout << "debt = " << debt << endl;
return 0;
}
输出结果:
程序清单 3.14,typecast.cpp
// typecast.cpp -- forcing type changes
#include <iostream>
int main()
{
using namespace std;
int auks, bats, coots;
// the following statement adds the values as double,
// then converts the result to int
auks = 19.99 + 11.99;
// these statements add value as int
bats = (int)19.99 + (int)11.99; // old C syntax
coots = int(19.99) + int(11.99); // new C++ syntax
cout << "auks = " << auks << ", bats = " << bats;
cout << ", coots = " << coots << endl;
char ch = 'Z';
cout << "The code for " << ch << " is "; // print as char
cout << int(ch) << endl; // print as int
cout << "Yes, the code is ";
cout << static_cast<int>(ch) << endl; // using static_cast
return 0;
}
输出结果:
编程练习部分
题目 1,方法:计算并打印。示例代码:3_1.cpp
#include <iostream>
const int inchPerFeet = 12;
int main()
{
using namespace std;
cout << "Please enter your height:___\b\b\b";
int height;
cin >> height;
cout << "Your height is " << height / inchPerFeet << " feet and ";
cout << height % inchPerFeet << " inches." << endl;
return 0;
}
输出结果:
题目 2, 方法:计算并输出结果。示例代码:3_2.cpp
#include <iostream>
const double inchPerFeet = 12.0;
const double meterPerInch = 0.0254;
const double lbsPerKilo = 2.2;
double CaculateBmi(double height, double weight);
int main()
{
using namespace std;
cout << "Please enter your height in feet first: ";
double heightFeet;
cin >> heightFeet;
cout << "Please enter your height in inch then: ";
double heightInch;
cin >> heightInch;
cout << "Please enter your weight in pound: ";
double pound;
cin >> pound;
double height = (heightFeet * inchPerFeet + heightInch) * meterPerInch;
double weight = pound / lbsPerKilo;
cout << "Your BMI is " << CaculateBmi(height, weight) << endl;
return 0;
}
double CaculateBmi(double height, double weight)
{
return weight / (height * height);
}
输出结果:
题目 3, 方法:计算并输出。示例代码:3_3.cpp
#include <iostream>
int main()
{
using namespace std;
const double minutesPerDegrees = 60.0;
const double secondsPerMinutes = 60.0;
cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: ";
double degrees;
cin >> degrees;
cout << "Next, enter the minutes of arc: ";
double minutes;
cin >> minutes;
cout << "Finally, enter the seconds of arc: ";
double seconds;
cin >> seconds;
cout << degrees << " degrees, " << minutes << " minutes, " << seconds
<< " seconds = " << degrees + minutes / minutesPerDegrees
+ seconds / secondsPerMinutes / minutesPerDegrees << " degrees" << endl;
return 0;
}
输出结果:
题目 4,方法:计算并打印。示例代码:3_4.cpp
#include <iostream>
int main()
{
const int hoursPerDay = 24;
const int minutesPerHour = 60;
const int secondsPerMinutes = 60;
using namespace std;
cout << "Please enter the seconds: ";
long long seconds;
cin >> seconds;
long long days = seconds / (hoursPerDay * minutesPerHour * secondsPerMinutes);
long long hours = (seconds % (hoursPerDay * minutesPerHour * secondsPerMinutes)) /
(minutesPerHour * secondsPerMinutes);
long long minutes = (seconds % (minutesPerHour * secondsPerMinutes)) / secondsPerMinutes;
long long second = seconds % secondsPerMinutes;
cout << seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes
<< " minutes, " << second << " seconds" << endl;
return 0;
}
输出结果:
题目 5,方法:计算并打印。示例代码:3_5.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "Please enter the population of the world: ";
long long worldPopulation;
cin >> worldPopulation;
cout << "Please enter the population of America: ";
long long americaPopulation;
cin >> americaPopulation;
cout << "The population of the US is " << double(americaPopulation) / double(worldPopulation)
* 100.0 << "% of the world population." << endl;
return 0;
}
输出结果:
题目 6,方法:计算并打印结果。示例代码:3_6.cpp
#include <iostream>
int main()
{
using namespace std;
const double distance = 100.0;
cout << "Please enter the distance you drived(miles): ";
double miles;
cin >> miles;
cout << "Please enter the gasoline you consumed(gallons): ";
double gasoline;
cin >> gasoline;
cout << "You consumed " << gasoline / miles * distance << " gallons gasoline per " << distance
<< " miles." << endl;
return 0;
}
输出结果:
题目 7,方法:计算并打印结果。示例代码:3_7.cpp
#include <iostream>
int main()
{
using namespace std;
const double distance = 100.0;
const double milesPerKilometer = 0.6214;
const double litersPerGallon = 3.875;
cout << "Please enter the gasolines you consumed per " << distance << " kilometers(liter): ";
double gasolinesInLiter;
cin >> gasolinesInLiter;
double distanceInMile = (distance * milesPerKilometer) / (gasolinesInLiter / litersPerGallon);
cout << "You can drive " << distanceInMile << " miles per gallon" << endl;
return 0;
}
输出结果: