C++Primer Plus笔记——第三章 处理数据及课后习题答案

一,总结

C++的基本类型分为两组:一组由存储为整数的值组成,另一组由存储为浮点格式的值组成。整型之间通过存储值时使用的内存量及有无符号来区分。整型从最小到最大依次是:boolchar、signedcharunsignedcharshort、unsignedshortint、unsignedintlong、unsignedlong以及 C++11新增的 longlong unsignedlonglong还有一种wchar_t类型,它在这个序列中的位置取决于实现。C++11新增了类型 char16_t和char32_t,它们的宽度足以分别存储16和32位的字符编码。C++确保了char足够大,能够存储系统基本字符集中的任何成员,而wchar_t则可以存储系统扩展字符集中的任意成员,short至少为16 位,而int至少与short样长,long至少为32位.且至少和int样长确切的长度取决于实现

字符通过其数值编码来表示。1/0系统决定了编码是被解释为字符还是数字。

浮点类型可以表示小数值以及比整型能够表示的值大得多的值。3种浮点类型分别是floatdoublelongdouble。C++确保float不比double长,而double不比longdouble长。通常,float使用32位内存,double使用64位,longdouble使用80到128位。

通过提供种长度不同、有符号或无符号的类型,C++使程序员能够根据特定的数据要求选择合适的类型

C++使用运算符来提供对数字类型的算术运算:加、减、乘、除和求模。当两个运算符对同一个操作数进行操作时,C++的优先级和结合性规则可以确定先执行哪种操作。

对变量赋、在运算中使用不同类型,使用强制类型转换时,C++将把值从种类型转换为另一种类型很多类型转换都是安全的即可以在不损失和改变数据的情下完成转换。例如,可以把int值转换为long值,而不会出现任何问题其他一些转换,如将浮点类型转换为整型,则需要更加小心。

开始,读者可能觉得大量的C++基本类型有些多余,尤其是考虑到各种转换规则时。但是很能最终 .将发现,某些时候,只有一种类型是需要的,此时您将感谢C++提供了这种类型。

二,程序清单

程序清单3.1 整型长度

// limits.cpp -- some integer limits

#include "stdafx.h"
#include <iostream>
#include <climits>//use limits.h for older systems 在老系统中用limits.h
int limits()
{
	using namespace std;
	int n_int = INT_MAX;//initialize n_int to max int value 赋予int最大取值
	short n_short = SHRT_MAX;//symbols defined in climits file 这些符号已经在climits文件中被定义了
	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 -- exceeding some integer limits

#include "stdafx.h"
#include <iostream>
#define ZERO 0//make ZERO symbol for 0 value 定义ZERO为0
#include <climits>//defines INT_MAX as largest int value 定义int最大值

int exceed()
{
	using namespace std;
	short sam = SHRT_MAX;//initialize a variable to max value
	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 = sam - 1;
	sue = sue - 1;
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited." << endl << "Lucky Sue!" << endl;
	return 0;
}

程序清单3.3整型字面值

//hexoct.cpp -- show hex and octal literals

#include "stdafx.h"
#include <iostream>

int hexoct()
{
	using namespace std;
	int chest  = 42;  //decimal integer literal 10进制
	int waist  = 0x42;//hexadecimal integer literal 16进制
	int inseam = 042; //octal integer literal 8进制
	
	cout << "Monsieur cut a striking figue!\n";
	cout << "cheat  = " << 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 -- display values in hex and octal

#include "stdafx.h"
#include <iostream>

int hexoct2()
{
	using namespace std;
	int chest  = 42;  
	int waist  = 42;
	int inseam = 42;

	cout << "Monsieur cut a striking figue!\n";
	cout << "cheat  = " << chest << " {decimal for 42}\n";
	cout << hex;//manipulator for changing number base
	cout << "waist  = " << waist << " {hexadecimal for 42}\n";
	cout << oct;//manipulator for changing number base
	cout << "inseam = " << inseam << " {octal for 42}\n";
	return 0;
}
程序清单3.5char型

//chartype.cpp -- the char type
#include "stdafx.h"
#include <iostream>

int chartype()
{
	using namespace std;
	char ch;  //declare a char variable 声明字符变量

	cout << "Enter a character: " << endl;
	cin >> ch;
	cout << "Hola: ";
	cout << "Thank you for the " << ch << " character" << endl;
	return 0;
}

程序清单3.6char型

//morechar.cpp -- the char type and int type contrasted
#include "stdafx.h"
#include <iostream>

int morechar()
{
	using namespace std;
	char ch = 'M';
	int  i  = ch;
	cout << "The ASCII code for " << ch << " is " << i << endl;

	cout << "Add one to the character code: " << endl;
	ch = ch + 1;
	i  = 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);//可以用ASCII码表示字符

	//using cout.put() to display a char constant
	cout.put('!');//可以用('')直接显示字符
	cout << endl << "Done" << endl;
	return 0;
}

程序清单3.7转义序列

//bondini.cpp -- using escape sequences
#include "stdafx.h"
#include <iostream>

int bondini()
{
	using namespace std;
	cout << "\aOpeartion \"HyperHype\" is now activated!\n";//‘\"’是双引号符
	cout << "Enter your agent code:________\b\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 -- floating-point type
#include "stdafx.h"
#include <iostream>

int floatnum()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);//fixed-point定点表示法
	float tub = 10.0 / 3.0;//good to about 6 place
	double mint = 10.0 / 3.0;//good to about 15 place
	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 -- precision problems with float


#include "stdafx.h"
#include <iostream>


int fltadd()
{
	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;
}
//2.34E+22小数点左边有23位数字,在第23上加上1,对于只能表示数字中的前6位或前7位的float类型,因此修改第23位对这个值不会有影响。

程序清单3.10算数运算符

//arith.cpp -- some C++ arithmetic

#include "stdafx.h"
#include <iostream>

int arith()
{
	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 -- integer and floating-point division

#include "stdafx.h"
#include <iostream>

int divide()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Integer division: 9/5 = ";
	cout << 9 / 5 << endl;
	cout << "Floating-point division: 9.0/5.0 = ";
	cout << 9.0 / 5.0 << endl;
	cout << "Mixed division: 9.0/5 = ";
	cout << 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 -- uses % oprator to convert lbs to stone

#include "stdafx.h"
#include <iostream>

int modulus()
{
	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;
	int pounds = lbs % Lbs_per_stn;
	cout << lbs << " pounds are " << stone << " stone, " << pounds << " pound(s).\n";
	return 0;
}

程序清单3.13变量初始化

//assign.cpp -- type changes on-initialization-
#include "stdafx.h"
#include <iostream>

int assign()
{
	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 defined in C++
	cout << "tree = " << tree << endl;
	cout << "guess = " << guess << endl;
	cout << "debt = " << debt << endl;
	
	return 0;
}

程序清单3.14强制类型转换

//typecast.cpp -- forcing type changes


#include "stdafx.h"
#include <iostream>
int typecast()
{
	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 values as int 强制整数相加
	bats = (int) 19.99 + (int) 11.99; //old C syntax C语言句法
	coots = int(19.99) + int(11.99); //new C++ syntax C++句法
	cout << "auks = " << auks << ", bats = " << bats;
	cout << ", coot = " << 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;
}

课后编程练习答案

//practice.cpp -- answer of program practice

#include "stdafx.h"
#include <iostream>

int main()
{
	using namespace std;
	
	//problem 1
	/*cout << "Enter your height(inch):___\b\b\b";
	const int tran = 12;
	int height;
	int inch;
	int feet;
	cin >> height;
	feet = height / tran;
	inch = height % tran;
	cout << " your height(feet) are " << feet << " feet " << inch << " inch.\n";*/
	
	//problem 2
	/*cout << "Enter your height(feet):";
	int feet;
	int inch;
	double height;
	cin >> feet;
	cout << "Enter your height(inch):";
	cin >> inch;
	height = (feet * 12 + inch) * 0.0254;
	cout << "Enter your weight:";
	int weight;
	cin >> weight;
	double bmi;
	bmi = weight / (height*height);
	cout << "your BMI is: " << bmi << endl;*/
	
	//problem 3
	/*cout << "Enter a latitude in degrees, minutes, and second:\n";
	const int dm = 60;
	const int ds = 360;
	
	int degree;
	int minute;
	int second;
	double degrees;
   
	cout << "First, enter the degrees: ";
        cin >> degree;
	cout << "Second, enter the minutes: ";
        cin >> minute;
	cout << "Finally, enter the seconds:";
        cin >> second;
 	degrees = (double)degree+ (double)minute / dm + (double)second / ds;/*
 	cout << degree << " dgrees, " << minute << " minutes, " << second << " seconds = " << degrees << " degrees\n";*/

	//problem 4
	/*cout << "Enter the number of seconds: ";
	const int ds = 3600 * 24;
	const int ms = 60;
	const int hs = 3600;
	int second;
	cin >> second; 
	int day;
	int hour;
	int minute;
	int second1;
	day = second / ds;
	hour = (second - day * ds) / hs;
	minute = (second - day * ds) % hs / ms;
	second1 = (second - day * ds) % hs % ms;
	cout << second << " seconds = " 
		 << day << " days," 
		 << hour << " hours," 
		 << minute << " minutes,"
		 << second1 << " seconds\n";*/

	//problem 5
	/*cout << "Enter the world's population:";
	long long wo;
	cin >> wo;
	cout << "Enter the polulation of US: ";
	long long us;
	cin >> us;
	double per;
	per = (double)us / (double)wo * 100;
	cout << "the population of the US is " << per << "% of the world polulation";*/

	//problem 6
	/*cout << "Enter the distance:";
	long dis;
	cin >> dis;
	cout << "Enter the oil: ";
	long oil;
	cin >> oil;
	double per;
	per = (double)oil / (double)dis * 100;
	cout << "the oil consumption is " << per << " L / km";*/

	//problem 7
	/*cout << "Enter the oil consumption: ";
	double con;
	cin >> con;
	double mpg;
	mpg = con / 12.14 * 19;
	cout << "the mpg is: " << mpg << " mpg" << endl;*/

	return 0; 	
}

猜你喜欢

转载自blog.csdn.net/yukinoai/article/details/79477136