C++ primer plus 第三章程序清单

本文深入探讨了C++中的各种基本数据类型及其操作方法,包括整数类型的范围限制、十六进制与八进制表示、字符类型的输入输出、浮点数的精度问题等,并通过具体示例展示了算术运算、取模运算以及类型转换的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// CC_3.cpp: 定义控制台应用程序的入口点。
//

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

int CC_3_1_limits();
int CC_3_2_exceed();
int CC_3_3_hexoct();
int CC_3_4_hexoct2();
int CC_3_5_chartype();
int CC_3_6_morechar();
int CC_3_7_bondini();
int CC_3_8_floatnum();
int CC_3_9_fitadd();
int CC_3_10_arith();								//略
int CC_3_11_divide();
int CC_3_12_modulus();
int CC_3_13_assign();
int CC_3_14_typecast();

using namespace std;

int main()
{
	//CC_3_1_limits();
	//CC_3_2_exceed();
	//CC_3_3_hexoct();
	//CC_3_4_hexoct2();
	//CC_3_5_chartype();
	//CC_3_6_morechar();
	//CC_3_7_bondini();
	//CC_3_8_floatnum();
	//CC_3_9_fitadd();
	//CC_3_10_arith();
	//CC_3_11_divide();
	//CC_3_12_modulus();
	//CC_3_13_assign();
	CC_3_14_typecast();

	cin.get();
	return 0;
}

int CC_3_1_limits()
{
	int n_int_1 = INT_MAX;
	short n_short_1 = SHRT_MAX;
	long n_long_1 = LONG_MAX;
	long long n_llong_1 = LLONG_MAX;


	cout << "int is " << sizeof(int) << " bytes." << endl;				//可以对变量名或者类型名使用 sizeof 运算符
	cout << "short is " << sizeof(short) << " bytes." << endl;			//在对类型名使用sizeof时,应将名称放入括号中
	cout << "long is " << sizeof n_long_1 << " bytes." << endl;			//在对变量名使用sizeof时,括号是可选的
	cout << "long long is " << sizeof(n_llong_1) << " bytes." << endl;
	cout << endl;

	cout << "Maximum values:" << endl
		<< "int: " << n_int_1 << endl
		<< "short: " << n_short_1 << endl
		<< "long: " << n_short_1 << endl
		<< "long long: " << n_llong_1 << endl << endl;

	cout << "Minium int value = " << INT_MIN << endl
		<< "Bits per byte = " << CHAR_BIT << endl;
	return 0;
}
int CC_3_2_exceed()
{
#define ZERO 0
	short sam_2 = SHRT_MAX;
	unsigned short sue_2 = sam_2;
	cout << "Sam has " << sam_2 << " dollars and Sue has " << sue_2 << " dollars desposited." << endl
		<< "add $1 to each account." << endl << "Now ";
	sam_2 = sam_2 + 1;
	sue_2 = sue_2 + 1;
	cout << "Sam has " << sam_2 << " dollars and Sue has " << sue_2 << " dollars desposited.\nPoor Sam!" << endl;
	sam_2 = sue_2 = ZERO;
	cout << "Sam has " << sam_2 << " dollars and Sue has " << sue_2 << " dollars desposited." << endl
		<< "Take $1 from each account." << endl << "Now ";
	sam_2 = sam_2 - 1;
	sue_2 = sue_2 - 1;
	cout << "Sam has " << sam_2 << " dollars and Sue has " << sue_2 << " dollars desposited." << endl << "lucky Sue!" << endl;

	return 0;
}
int CC_3_3_hexoct()
{
	int chest_3 = 42;
	int waist_3 = 0x42;
	int inseam_3 = 042;

	cout << "Monsieur cuts a striking figure!\n";
	cout << "Chest = " << chest_3 << " (42 in decimal)\n"
		<< "waist = " << waist_3 << " (0x42 in hex)\n"
		<< "inseam = " << inseam_3 << " (042 in octal)\n";
	return 0;
}
int CC_3_4_hexoct2()
{
	int num_4 = 42;
	cout << "Chest = " << num_4 << " (decimal for 42)\n"
		<< hex << "waist = " << num_4 << " (hex for 42)\n"
		<< oct << "inseam = " << num_4 << " (octal for 42)\n";
	return 0;
}
int CC_3_5_chartype()
{
	char ch_5;
	cout << "Enter a character: " << endl;
	cin >> ch_5;
	cout << "Hola! ";
	cout << "Thank you for the " << ch_5 << " character." << endl;
	return 0;
}
int CC_3_6_morechar()
{
	char ch_6 = 'M';
	int i_6 = ch_6;
	cout << "The ASCII code for " << ch_6 << " is: " << i_6 << endl;

	cout << "Add one to the character code: " << endl;
	ch_6 = ch_6 + 1;
	i_6 = ch_6;
	cout << "The ASCII code for " << ch_6 << " is: " << i_6 << endl;

	cout << "Displaying char ch_6 using cout.put(): ";
	cout.put(ch_6);
	cout.put('!');
	cout << endl << "Done." << endl;
	return 0;
}
int CC_3_7_bondini()
{
	cout << "\aOpreation \"HyperHype\" is now activated!\n";
	cout << "Enter your agent code:______\b\b\b\b\b\b";
	long code_7;
	cin >> code_7;
	cout << "\aYou entered " << code_7 << "...\n";
	cout << "\aCode verified! Proceed with Plan Z3!\n";
	return 0;
}
int CC_3_8_floatnum()
{
	cout.setf(ios_base::fixed, ios_base::floatfield);
	float tub_8 = 10.0 / 3.0;
	double mint_8 = 10.0 / 3.0;
	const float million_8 = 1.0e6;							// const限定符:声明后不允许修改
	
	cout << "tub_8 = " << tub_8 << ", a million tubs = " << million_8 * tub_8;
	cout << ",\nand ten million tubs = " << 10 * tub_8 * million_8 << endl;
	cout << "mint_8 = " << mint_8 << " and a million mints = " << million_8 * mint_8 << endl;
	return 0;
}	
int CC_3_9_fitadd()
{
	float a_9 = 2.34e+22f;
	float b_9 = a_9 + 1.0f;

	cout << "a_9 = " << a_9 << endl;
	cout << "b_9 - a_9 = " << b_9 - a_9 << endl;
	return 0;
}
int CC_3_10_arith()
{
	cout << "略" << endl;
	return 0;
}						
int CC_3_11_divide()
{
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Integer division: 9/5 = " << 9 / 5 << endl;
	cout << "Float-point division: 9.0/5.0 = " << 9.0 / 5.0 << endl;
	cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl;
	cout << "Double constants: 1e7/9.0 = " << 1e7 / 9.0 << endl;			//浮点常量在默认情况下位double型
	cout << "Mixed division: 1e7f/9.0 = " << 1e7f / 9.0 << endl;
	cout << "Float constants 1e7f/9.0f = " << 1e7f / 9.0f << endl;
	return 0;
}
int CC_3_12_modulus()
{
	const int Lbs_per_stn_12 = 14;
	int lbs_12;

	cout << "Enter your weight in pounds: ";
	cin >> lbs_12;
	int stone_12 = lbs_12 / Lbs_per_stn_12;
	int pounds_12 = lbs_12 % Lbs_per_stn_12;
	cout << lbs_12 << " pounds are " << stone_12 << " stone, " << pounds_12 << " pound(s)." << endl;
	return 0;
}
int CC_3_13_assign()
{
	cout.setf(ios_base::fixed, ios_base::floatfield);
	float tree_13 = 3;
	int guess_13(3.9832);
	int debt_13 = 7.2e12;
	cout << "tree_13 = " << tree_13 << endl;
	cout << "guess_13 = " << guess_13 << endl;
	cout << "debt_13 = " << debt_13 << endl;
	return 0;
}
/*以{}方式初始化时进行的转换(C++11)
int CC_3_13_2()
{
	const int code_13_2 = 66;
	int x_13_2 = 66;
	char c1_13_2 = { 31325 };
	char c2_13_2 = { 66 };
	char c3_13_2 = { code_13_2 };
	char c4_13_2 = { x_13_2 };
	x_13_2 = 31325;
	char c5_13_2 = x_13_2;

	return 0;
}
*/
int CC_3_14_typecast()
{
	int auks_14, bats_14, coots_14;
	auks_14 = 19.99 + 11.99;
	bats_14 = (int)19.99 + (int)19.99;
	coots_14 = int(19.99) + int(19.99);
	cout << "auks_14 = " << auks_14 << " ,bats_14 = " << bats_14;
	cout << " , coots_14 = " << coots_14 << endl;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值