C++ primer plus(第六版)学习笔记、习题答案(2)

来源:互联网 发布:增值税发票识别软件 编辑:程序博客网 时间:2024/06/05 23:56

第三章 处理数据

    这几天事情比较多,耽误了,可能后面的时间会花在英语上比较多一些,马上又有模式识别的考试了大哭


    这两天主要把第三章看完了,感觉看数据类型挺无趣的,但看完后,发现收获还是挺多的。


第一部分:学习笔记

1.变量名

1)使用字母字符,数字,下划线。

2)名称的第一个字符不能是数字。

3)区别大写小写字母。

4)不能使用C++关键字。

5)一“_ _”,“_”和大写字母大头的名称被保留给实现使用。

2.命名方案

1)用下划线_把单词放开,或者首字母大写。

2)用str大头表示字符串,b打头表示布尔值,c打头表示单个字符。

3.整型

1)short至少是16位,int至少与short一样长,long至少32位,且至少与int一样长,long long至少是64位,且至少与long一样长。

2)使用sizeof和#include climits可以查询自己机器上的各个字符字长和最大最小值:

// limits.cpp -- some integer limits#include <iostream>#include <climits>              // use limits.h for older systemsint main(){    using namespace std;    int n_int = INT_MAX;        // initialize n_int to max int value    short n_short = SHRT_MAX;   // symbols defined in climits file    long n_long = LONG_MAX;long long n_llong = LLONG_MAX;    // sizeof operator yields size of type or of variable    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 << "Maximum 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 << "Minimum int value = " << INT_MIN << endl;    cout << "Bits per byte = " << CHAR_BIT << endl;// cin.get();    return 0;}


3)在声明变量时对它进行初始化,可以避免以后忘记。

4)一种c++11初始化方式

int emus{7}和int emus = 7 一样int rocs = {};//表示初始化为零,以后要多用这种方式

4选择整型类型

1)如果知道变量可能表示的整数值大于16位的最大值,则使用long

2)仅当大型数据数组时,才有必要使用short

5.整型字面值

1)整型字面值是显式的书写的常量

2)以0开头表示八进制,0x表示16进制

3)不管是八进制还是16都是以二进制存储在计算机中,所以想输出他们需要“dec,hex,oct”:

// hexoct2.cpp -- display values in hex and octal#include <iostream>using namespace std;int main(){    using namespace std;    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;      // manipulator for changing number base    cout << "waist = " << waist << " (hexadecimal for 42)" << endl;    cout << oct;      // manipulator for changing number base    cout << "inseam = " << inseam << " (octal for 42)" << endl;    // cin.get();    return 0; }

6.确定常量类型

1)L表示long,U表示无符号数,LL表示long long

2)cin 和cout 的行为都是由变量类型引导的,类型是整型输入整型,字符性输入字符,如:

// morechar.cpp -- the char type and int type contrasted#include <iostream>int main(){    using namespace std;    char ch = 'M';       // assign ASCII code for M to ch    int i = ch;          // store same code in an int    cout << "The ASCII code for " << ch << " is " << i << endl;    cout << "Add one to the character code:" << endl;    ch = ch + 1;          // change character code in ch    i = ch;               // save new character code in i    cout << "The ASCII code for " << ch << " is " << i << endl;    // using the cout.put() member function to display a char    cout << "Displaying char ch using cout.put(ch): ";    cout.put(ch);    // using cout.put() to display a char constant    cout.put('!');    cout << endl << "Done" << endl;// cin.get();    return 0;}
3)cout.put()也可一显示字符

4)显示数字时,使用endl,显示字符时实现“\n”

5) cin,cout适合char流,不适合wchar_t(char的扩展)类型,使用wcin和wcout

whar_t bob = L"P";//L表示宽字符常量wcout << L"tall" << endl;
6)c++11新增char16_t和char32_t:如
char16_t ch1 = u'q';//u表示char16_tchar32_t ch2 = U'jdkafj';//U表示char32_t
6)常量初始化后其值固定,编译器将不允许修改其值

6)使用const少使用#define

const type name = value;

7.浮点数

1)浮点数的由来:计算机将这样的值分为有效数字和缩放因子。

2)E6表示10的6次方,中间不能有空格

3)float至少32位,double至少48位,且不少于float,long long 至少和的double一样多

4)cout只能输出小数点后6位

5)

ios_base::fixed 和 ios_base::floatfield
让浮点数不以E表示法输出,使用定点表示法

6)float:f,long double : L

7)分析代码:

int x = 66;char c4 = {x};
不允许,{}中必须是一个常量;我是这样理解的

8)新的强制转化,以后就用它了

typeName(value)

第二部分:习题答案

3_1

//test 3_1//2014/12/6#include<iostream>const int c = 12;using namespace std;int main(){int len;cout << "Please Enter your length __ \b\b\b";cin >> len;cout << "your tall is " << len << " 英尺" << endl;cout << "your tall is " << c * len << "英寸" << endl;cin.get();cin.get();return 0;}

3_2

//test 3_2//2014/12/6#include<iostream>const int c_tall1 = 12;const double c_tall2 = 0.0254;const double c_weight = 2.2;using namespace std;int main(){int len1,len2,weight;cout << "Please Enter your length(英尺) __ \b\b\b";cin >> len1;cout << endl;cout << "Please Enter your length(英寸) __ \b\b\b";cin >> len2;cout << endl;cout << "Please Enter your weight(磅) __ \b\b\b";cin >> weight;cout << endl;cout << "your BMI is " << weight * c_weight / ((len1 * c_tall1 + len2) * c_tall2) << endl;cin.get();cin.get();return 0;}


3_3

//test 3_3//2014/12/6#include<iostream>const int c_min = 60;const int c_sec = 60;using namespace std;int main(){int deg, min, sec;cout << "Enter a latitude in degrees, minutes, and seconds\n";cout << "First,enter the degrees: ";cin >> deg;cout << endl;cout << "Next,enter the minutes: ";cin >> min;cout << endl;cout << "Finally,enter the seconds: ";cin >> sec;cout << endl;cout << deg << "degrees," << min << "minutes" << sec << "seconds = " << deg + 1.0/60 * min + 1.0/3600 * sec;cin.get();cin.get();return 0;}


3_4

//test 3_1//2014/12/6#include<iostream>const int c_hour = 24;const int c_min = 60;const int c_sec = 60;using namespace std;int main(){long sec,day,hour,min;cout << "Enter the number of seconds: ";cin >> sec;//cout << endl;day = sec /(c_hour * c_min * c_sec);hour = sec / (c_min * c_sec);min = sec / c_sec;cout << sec << " seconds = " << day << " days  " <<  hour - day * c_hour << " hours  " << min - hour * c_min << " minutes" << sec - min * c_sec << " seconds" ;cin.get();cin.get();return 0;}

3_5

//test 3_1//2014/12/6#include<iostream>using namespace std;int main(){long long P_word;long long P_us;cout << "Enter the word's population: ";cin >> P_word;cout << "Enter the population of the US:";cin >> P_us;double per = double(P_us) / double(P_word);cout << "The population of the us is " << per * 100 << "%" << "of the world poputation";cin.get();cin.get();return 0;}
3_6

这一题太儿戏了

3_7

。。。
题目类型都差不多,练习了所学的就可以了

































0 0