C++primer Plus课本代码(第三章)

3.1 limits.cpp

#include <iostream>
#include <climits>

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;

	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 << "Maximun 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 << "Minimun int value = " << INT_MIN << endl;
	cout << "Bits per bytes = " << CHAR_BIT << endl;

	return 0;
}

3.2 exceed.cpp

#include <iostream>
#include <climits>

#define ZERO 0;

int main()
{
	using namespace std;
	short sam = SHRT_MAX;
	unsigned short sue = sam;
	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

#include <iostream>

using namespace std;

int main()
{
	int chest = 42;
	int waist = 0x42;
	int inseam = 042;

	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

#include <iostream>

using namespace std;

int main()
{
	int chest = 42;
	int waist = 42;
	int inseam = 42;

	cout << "Monsieur cuts a striking figure!" << endl;
	cout << "chest = " << chest << "{decimal for 42}" << endl;
	cout << hex;
	cout << "waist = " << waist << "{hexadecimal for 42}" << endl;
	cout << oct;
	cout << "inseam = " << inseam << "{octal for 42}" << endl;

	return 0;
}

3.5 chartype.cpp

#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

#include <iostream>

using namespace std;

int main()
{
	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;

	cout << "Displaying char ch using cout.put(ch): ";
	cout.put(ch);

	cout.put('!');
	cout << endl << "Done" << endl;

	return 0;
}

3.7 bondini.cpp

#include <iostream>

using namespace std;

int main()
{
	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

#include<iostream>
	
	using namespace std;
	
	int main()
	{
		cout.setf(ios_base::fixed, ios_base::floatfield);
	
		float tub = 10.0 / 3.0;
		double mint = 10.0 / 3.0;
		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

#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

#include <iostream>

using namespace std;

int main()
{
	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

#include <iostream>

using namespace std;

int main()
{
	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 << "Mixed 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

#include <iostream>

using namespace std;

int main()
{
	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

#include <iostream>

int main()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);

	float tree = 3;
	int guess(3.9832);
	int debt = 7.2E12;

	cout << "tree = " << tree << endl;
	cout << "guess = " << guess << endl;
	cout << "debt = " << debt << endl;

	return 0;
}

3.14 typecast.cpp

#include <iostream>

int main()
{
	using namespace std;
	int auks, bats, coots;

	auks = 19.99 + 11.99;

	bats = (int)19.99 + (int)11.99;
	coots = int(19.99) + int(11.99);
	cout << "auks = " << auks << ", bats = " << bats;
	cout << ", coots = " << coots << endl;

	char ch = 'Z';
	cout << "The code for " << ch << " is ";
	cout << int(ch) << endl;
	cout << "Yes, the code is ";
	cout << static_cast<int>(ch) << endl;

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满城枪声皆无你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值