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

xiaoxiao2021-02-27  258

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

short至少是16位

int至少和short一样长

long至少是32位

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

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

[cpp]  view plain  copy std::cout << "int size: " << sizeof(int) << std::endl;//整形,4个字节   std::cout << "short size: " << sizeof(short) << std::endl;//短整形,2个字节   std::cout << "long size: " << sizeof(long) << std::endl;//长整形,4个字节,跟int一样,怪不得从未用过   std::cout << "long long size: " << sizeof(long long) << std::endl;//long long,8个字节      std::cout << "int min:" << INT_MIN << std::endl;   std::cout << "int max:" << INT_MAX << std::endl;   std::cout << "short min:" << SHRT_MIN << std::endl;   std::cout << "short max: " << SHRT_MAX << std::endl;   std::cout << "long min:" << LONG_MIN << std::endl;   std::cout << "long max:" << LONG_MAX << std::endl;   std::cout << "long long min:" << LLONG_MIN << std::endl;   std::cout << "long long max:" << LLONG_MAX << std::endl;   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 int a = 0423;   int b = 00023;   int c = 0xa2;   书上写的好像不是很详细,其实第一位是0,第二位0-7,就是8进制。可以带多个0.

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

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

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

3.6复习题

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

3.7 编程练习

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

转载请注明原文地址: https://www.6miu.com/read-7255.html

最新回复(0)