C++ Primer Plus(第六版)—— 第三章 处理数据 笔记和答案


1.数据类型的宽度,可能受到不同语言和不同操作系统的影响。C++只做了最小宽度的限制。

short至少是16位

int至少和short一样长

long至少是32位

long long至少是64位,至少和long一样长

检测多少字节的代码。还是直接运行的好。猜测再多都是会错的。我的是64位windows7,Visual Studio 2013.

[cpp]  view plain  copy
  1. std::cout << "int size: " << sizeof(int) << std::endl;//整形,4个字节  
  2. std::cout << "short size: " << sizeof(short) << std::endl;//短整形,2个字节  
  3. std::cout << "long size: " << sizeof(long) << std::endl;//长整形,4个字节,跟int一样,怪不得从未用过  
  4. std::cout << "long long size: " << sizeof(long long) << std::endl;//long long,8个字节  
  5.   
  6. std::cout << "int min:" << INT_MIN << std::endl;  
  7. std::cout << "int max:" << INT_MAX << std::endl;  
  8. std::cout << "short min:" << SHRT_MIN << std::endl;  
  9. std::cout << "short max: " << SHRT_MAX << std::endl;  
  10. std::cout << "long min:" << LONG_MIN << std::endl;  
  11. std::cout << "long max:" << LONG_MAX << std::endl;  
  12. std::cout << "long long min:" << LLONG_MIN << std::endl;  
  13. std::cout << "long long max:" << LLONG_MAX << std::endl;  
  14. std::cout << "char bits" << CHAR_BIT << std::endl;//8位  
其实,直接看climits文件就好了。


2.int的长度为计算机处理效率最高的长度,也叫自然长度。一般使用int。

3.如果第一位是1-9,基数是10. 例如:123456.

如果第一位是0,第二位是1-7,则基数是8,例如024,等于十进制的20.(有效值是0-7)

如果第一位是0,第二位是x或者X,则基数是16,例如0x16,等于十进制的22.(有效值是0-9,a-f)

[cpp]  view plain  copy
  1. int a = 0423;  
  2. int b = 00023;  
  3. int c = 0xa2;  
书上写的好像不是很详细,其实第一位是0,第二位0-7,就是8进制。可以带多个0.

无论字面值是10进制、8进制还是16进制,都是以2进制保存的


3.浮点型,相关参数在float.h 

[cpp]  view plain  copy
  1. std::cout << "float size:" << sizeof(float) << std::endl;//单精度,4个字节  
  2. std::cout << "double size:" << sizeof(double) << std::endl;//双精度,8个字节  
  3. std::cout << "long double size:" << sizeoflong double) << std::endl;//长浮点型,8个字节  

3.6复习题

[cpp]  view plain  copy
  1. //3.6 复习题  
  2.     //  1.在不同场合下提供不同的数据类型,根据精确度,取值范围,存储方式来考虑吧。在旧时代的计算机中对数据类型限制很大。  
  3.     //而在现在日常使用的软件中,对这方面要求都不是很大限制了。否则也不会出现弱类型的语言了。  
  4.     //  2.  
  5.     short a = 80;  
  6.     unsigned b = 42110;  
  7.     long long c = 3000000000;  
  8.     //  3.C++没有提供自动防止越界的功能,超过了就变负了,真是阳极生阴,阴阳倒转,哈哈。  
  9.     //可以使用climits头文件来检测,不过我觉得这个检测有个鸟用,这个头文件是用来看的,不是用来算的。  
  10.     //  4.33L是long,33是int  
  11.     //  5.对于使用ASCII码的系统来说是等价的,如果不支持的话,就不知道了。而且一个是int,一个是char。  
  12.     //  6.  
  13.     char c88 = 88;  
  14.     std::cout << c88 << std::endl;//直接输出  
  15.     std::cout.put(c88);//put函数  
  16.     std::cout << char(88) << std::endl;//新构造一个char  
  17.     std::cout << (char)88 << std::endl;//强制转换  
  18.     //  7.这个问题取决于两个类型的长度。按照我电脑的情况,long是4字节,double是8字节,long long是8字节。  
  19.     //long最大值是21亿多,10位数,longlong是19位数。double只有13个有效数字。所以long是不会爆的。longlong就有可能爆了。  
  20.     //  8.a74,b4,c0,d4.5, e3.  
  21.     //  9.  
  22.     double _9a = 1.5;  
  23.     double _9b = 1.6;  
  24.     int _9c = int(_9a) + int(_9b);//2  
  25.     int _9d = int(_9a + _9b);//3  
  26.     //  10.a int, b float, c char, d char32_t, e double  

3.7 编程练习

[cpp]  view plain  copy
  1. //1 
  2. const int CHANGE_FT_TO_IN = 12;//1英尺=12英寸  
  3. const double CHANGE_IN_TO_M = 0.0254;//1英寸 = 0.00254米  
  4. const double CHANGE_KG_TO_LB = 2.2;//1千克 = 2.2磅  
  5. const double CHANGE_DEGREE_TO_MINUTE = 60;//一度为60分  
  6. const double CHANGE_MINUTE_TO_SECOND = 60;//1分60秒  
  7. void _3_7_1()  
  8. {  
  9.     int heightCm;  
  10.     std::cout << "Please input your height(in):______\b\b\b\b\b\b";//\b表示将光标退一格,输入的时候就会覆盖掉_  
  11.     std::cin >> heightCm;  
  12.     std::cout << "Your height:" << heightCm / CHANGE_FT_TO_IN << "m " << heightCm % CHANGE_FT_TO_IN << "cm" << std::endl;  
  13. }  
[cpp]  view plain  copy
  1. //2  
  2. void _3_7_2()  
  3. {  
  4.     int ft = 0;//英尺数  
  5.     int in = 0;//英寸数  
  6.     int lb = 0;//磅数  
  7.     std::cout << "Please input your height(x ft and y in):"<<std::endl;  
  8.     std::cout << "ft:______\b\b\b\b\b\b";  
  9.     std::cin >> ft;  
  10.     std::cout << "in:______\b\b\b\b\b\b";  
  11.     std::cin >> in;  
  12.     std::cout << "Please input your weight(lb):______\b\b\b\b\b\b";   
  13.     std::cin >> lb;  
  14.     double kg = lb / CHANGE_KG_TO_LB;  
  15.     double m = (ft * CHANGE_FT_TO_IN + in) * CHANGE_IN_TO_M;  
  16.     double bmi = kg / (m*m);  
  17.     std::cout << "BMI:" << bmi << std::endl;  
  18. }  
[cpp]  view plain  copy
  1. //3  
  2. void _3_7_3()  
  3. {  
  4.     int degree = 0;//度  
  5.     int minute = 0;//分  
  6.     int second = 0;//秒  
  7.     std::cout << "Enter a latitude in degrees, minutes, and seconds:" << std::endl;  
  8.     std::cout << "First: enter the degrees:";  
  9.     std::cin >> degree;  
  10.     std::cout << "Next, enter the minutes of arc:";  
  11.     std::cin >> minute;  
  12.     std::cout << "Finally, enter the seconds of arc:";  
  13.     std::cin >> second;   
  14.     double totalDegree = degree + minute / CHANGE_DEGREE_TO_MINUTE + second / CHANGE_MINUTE_TO_SECOND / CHANGE_DEGREE_TO_MINUTE;  
  15.     std::cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = " << totalDegree <<" degrees" << std::endl;  
  16. }  
[cpp]  view plain  copy
  1. //4  
  2. void _3_7_4()  
  3. {  
  4.     int totalSecond = 0;  
  5.     std::cout << "Enter the number of seconds:";  
  6.     std::cin >> totalSecond;  
  7.     int num = totalSecond;  
  8.     int seconds = num % 60;//秒数  
  9.     num /= 60;//总的分钟数  
  10.     int minutes = num % 60;//分钟数  
  11.     num /= 60;//总的小时数  
  12.     int hours = num % 24;//小时数  
  13.     num /= 24;//总天数  
  14.     std::cout << totalSecond << " seconds = " << num << " days, " << hours << " hours, "  
  15.         << minutes << " minutes, " << seconds << " seconds.\n";  
  16. }  
[cpp]  view plain  copy
  1. //5  
  2. void _3_7_5()  
  3. {  
  4.     double totalNum = 0;  
  5.     double num = 0;  
  6.     while (true)  
  7.     {  
  8.         std::cout << "Enter the world's population:";  
  9.         std::cin >> totalNum;  
  10.         if (totalNum > 0)  
  11.         {  
  12.             break;  
  13.         }  
  14.     }  
  15.     std::cout << "Enter the population of the US:";  
  16.     std::cin >> num;  
  17.     std::cout << "The population of the US is " << num / totalNum * 100   
  18.         <<"% of the world population";  
  19. }  
[cpp]  view plain  copy
  1. //6  
  2. void _3_7_6()  
  3. {  
  4.     double km = 0;  
  5.     double l = 0;  
  6.     std::cout << "Enter the km:";  
  7.     std::cin >> km;  
  8.     std::cout << "Enter the l:";  
  9.     std::cin >> l;  
  10.     if (l <= 0)  
  11.     {  
  12.         std::cout << "Enter error";   
  13.     }  
  14.     std::cout << km / l <<"km/L";   
  15. }  
[cpp]  view plain  copy
  1. //7  
  2. void _3_7_7()  
  3. {  
  4.     double km = 0;  
  5.     double l = 0;  
  6.     std::cout << "Enter the km:";  
  7.     std::cin >> km;  
  8.     std::cout << "Enter the l:";  
  9.     std::cin >> l;  
  10.     if (l <= 0)  
  11.     {  
  12.         std::cout << "Enter error";  
  13.     }  
  14.     std::cout << km / l << "km/L\n";  
  15.     double mi = km * 0.6214;  
  16.     double gal = l / 3.875;  
  17.     std::cout << gal / mi << "gal/mi\n";  
  18. }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值