C++ Primer Plus(第六版)--学习杂记(第五章)

1.

#include
using namespace std;
int main()
{
    int x;
    cout<<"The expression x = 100 has the value ";
    cout<<(x = 100)<cout<<"Now x = "<cout<<"The expression x < 3 has the value ";
    cout<<(x < 3)<cout<<"The expression x > 3 has the value ";
    cout<<(x > 3)<cout.setf(ios_base::boolalpha);
    cout<<"The expression x < 3 has the value ";
    cout<<(x < 3)<cout<<"The expression x > 3 has the value ";
    cout<<(x > 3)<

cout.setf(ios_base::boolalpha)函数调用设置了一个标记,该标记命令cout显示true和false,而不是0和1

2.
对long long的应用:

#include
using namespace std;
const int ArSize = 16;
int main()
{
    long long factorials[ArSize];
    factorials[1] = factorials[0] = 1LL;
    for(int i=2;i1];
    for(int i=0;icout<"! = "<

3.
倒序输出字符串

#include
#include
using namespace std;
int main()
{
    cout<<"Enter a word: ";
    string word;
    cin>>word;
    for(int i = word.size()-1;i>=0;i--)
    cout<cout<

4.
前缀运算符是从右到左
++pt,pt递增后再将 作用于它
*p++后缀运算符++的优先级更高,移到右值

5.
strcmp( )函数,该函数接受两个字符串地址作为参数。这意味着参数可以是指针、字符串常量或字符数组名。如果两个字符串相同,该函数将返回零;如果第一个字符串按字母排序排在第二个字符串之前,则返回一个负数值,否则,返回整数值

用strcmp的方法:

#include
#include
using namespace std;
int main()
{
    char word[5]="?ate";
    for(char ch ='a';strcmp(word,"mate");ch++)
    {
        cout<0]=ch;
    }
    cout<<"After loop ends, word is "<return 0;
}

用string的方法:

#include
#include
using namespace std;
int main()
{
    string word="?ate";
    for(char ch ='a';word!="mate";ch++)
    {
        cout<0]=ch;
    }
    cout<<"After loop ends, word is "<return 0;
}

6.
编写延时循环

#include
#include
using namespace std;
int main()
{
    printf("1");
    long long wait=0;
    while(wait<1000000000)
    wait++;
    printf("\n2\n");
}

这能让两个输出之间有了个时间差,另外,有些编译器可能修改上述代码,将wait改成1000000000,从而跳过该循环。
更好的办法是让系统时钟来完成这个操作

#include
#include
using namespace std;
int main()
{
    cout<<"Enter the delay time, in seconds: ";
    float secs;
    cin>>secs;
    clock_t delay = secs * CLOCKS_PER_SEC;
    cout<<"starting\a\n";
    clock_t start = clock();
    while(clock()-startcout<<"done \a\n";
    return 0;
}

输入的数字就是那个闪闪的下划线出现的次数+1
符号常量:CLOCKS_PER_SEC,等于每秒钟包含的系统时间单位数,因此系统时间除以这个值就等于秒数。
ctime将clock_t作为clock( )返回类型的别名,这意味着可以将变量声明为clock_t类型。

7.
C++为类型建立别名有两种方式:
一种是使用预处理器:

#define BYTE char

用char替换掉所有的BYTE,BYTE是char的别名
第二种是使用关键字typedef,

typedef char byte;
//byte作为char的别名
typedef char * byte;
//byte作为char指针的别名

用typedef会更好

8.
使用最原始的cin

#include
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cout<<"Enter character; enter # to quit:"<cin>>ch;
    while(ch!='#')
    {
        cout<cin>>ch;
    }
    cout<" characters read"<return 0;
}

cin在读取char值时,与读取其他基本类型一样,cin将忽略空格和换行符。因此输入中的空格没有被回显,也没有被包括在计数内。发送给cin的输入被缓冲,这意味着只有在用户按下回车键后,他输入的内容才会被发送给程序。这就是在运行该程序时,可以在#后面输入字符的原因,按下回车键,整个字符序列发送给程序,但程序在遇到#字符后将结束对输入的处理

#include
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cout<<"Enter character; enter # to quit:"<cin.get(ch);
    while(ch!='#')
    {
        cout<cin.get(ch);
    }
    cout<" characters read"<return 0;
}

这个程序能计数每个字符,包括空格

9.
函数重载

10.
检查文件尾(EOF),C++输入工具和操作系统协同工作,来检测文件尾并将这种信息告诉程序。

#include
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cout<<"Enter character; enter # to quit:"<cin.get(ch);
    while(cin.fail()==false)
    {
        cout<cin.get(ch);
    }
    cout<" characters read"<return 0;
}

ctrl+z+enter
(1)
cin.get(ch);
while(cin.fail()==false)
(2)
ch=cin.get();
while(ch!=EOF)
(3)
while((ch=cin.get())!=EOF)

11.
二维数组:

#include
using namespace std;
const int Cities = 5;
const int Years = 4;
int main()
{
    const char * cities[Cities]=
    {
        "G city",
        "A city",
        "N city",
        "S city",
        "T city"
    };
    int maxtemps[Years][Cities]=
    {
        {96,100,87,101,105},
        {96,100,87,101,104},
        {96,100,87,101,107},
        {96,100,87,101,108},
    };
    cout<<"max ....... for\n\n";
    for(int city = 0;citycout<":\t";
        for(int year=0;yearcout<"\t";
        cout<return 0;
}

12.
这句话的意思:
int x = (1,024);
逗号运算符,值为右边的值,024是八进制的20,因此x=20;

13.

#include 
const int MONTHS = 12;
const char* months[MONTHS]={"January","February","March","April","May","June","July","August","September","October","November","December"};
const char* years[3]={"第一年","第二年","第三年"};
int main()
{
    using namespace std;
    int year_sale[3],sum=0,sales[3][MONTHS];
    for(int i=0;i<3;i++)
    {
            int temp=0;
            cout<"的每个月销售量:"<for(int j=0;jcout<<"请输入"<"的销售量:";
                    cin>>sales[i][j];
                    temp+=sales[i][j];
            } 
            year_sale[i]=temp;
            sum+=year_sale[i];
    }
    for(int i=0;i<3;i++)
    cout<"的销售量为:"<cout<<"这三年的总销售量为:"<return 0;
} 

14.

#include 
#include 
using namespace std;
struct car{
       string name;
       int year;
};
int main()
{
    cout<<"How many cars do you wish to catalog? ";
    int num;
    (cin>>num).get();
    car* ps=new car[num];
    for(int i=0;icout<<"Car #"<1<<":\n";
            cout<<"Please enter the make: ";
            getline(cin,ps[i].name);
            cout<<"Please enter the year made: ";
            (cin>>ps[i].year).get();
    }
    cout<<"Here is your collection:\n";
    for(int i=0;icout<" "<delete [] ps;
    return 0;
} 

15.

#include 
#include 

int main()
{
    using namespace std;

    char word[20];
    int sum=0;
    cout<<"Enter words (to stop,type the word done):\n";
    cin>>word;
    while(strcmp(word,"done"))
    {
           sum++;
           cin>>word;
    }
    cout<<"You entered a total of "<" words.\n";
    return 0;
}
#include 
#include 

int main()
{
    using namespace std;
    string word;
    int sum=0;
    cout<<"Enter words (to stop, type the word done):\n";
    cin>>word;
    while(word!="done")
    {
         sum++;
         cin>>word;
    }
    cout<<"You entered a total of "<" words.\n";
    return 0;
}

你可能感兴趣的:(C++,Primer,Plus,(第六版),学习笔记)