chapter3 类型与变量

c++中基本类型分为两种
1.整形;bool,char(8位),short,int(32位),long
2.浮点型:float(32位),double(64位)

// floatnum.cpp -- floating-point types
#include <iostream>
int main()
{
    using namespace std; 
    cout.setf(ios_base::fixed, ios_base::floatfield); // 保留结尾的0
    float tub = 10.0 / 3.0;     // 精确到第六位
    double mint = 10.0 / 3.0;   // 精确到第十五位
    const float million = 1.0e6;	//上述变量成1000000以后看结果

    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;
	
	float a = 2.34E+22f;	//浮点型的优点:1.可以表示整数之间的值,2.有缩放因子,可表示的范围大很多
	float b = a + 1.0f;
	cout << "a = " << a << endl;	//			缺点:1.运算比int慢,精度将降低
	cout << "b - a = " << b - a << endl; //数字a加1,然后减去a竟然等于0
	// cin.get();	
	//类型转换
	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 ";    // 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值