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

1.

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

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

2.
对long long的应用:

#include<iostream>
using namespace std;
const int ArSize = 16;
int main()
{
    long long factorials[ArSize];
    factorials[1] = factorials[0] = 1LL;
    for(int i=2;i<ArSize;i++)
        factorials[i] = i*factorials[i-1];
    for(int i=0;i<ArSize;i++)   
            cout<<i<<"! = "<<factorials[i]<<endl;
} 

3.
倒序输出字符串

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

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

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

用strcmp的方法:

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

用string的方法:

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

6.
编写延时循环

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

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

#include<iostream>
#include<ctime>
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()-start<delay)
    ;
    cout<<"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<iostream>
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cout<<"Enter character; enter # to quit:"<<endl;
    cin>>ch;
    while(ch!='#')
    {
        cout<<ch;
        ++count;
        cin>>ch;
    }
    cout<<endl<<count<<" characters read"<<endl;
    return 0;
}

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

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

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

9.
函数重载

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

#include<iostream>
using namespace std;
int main()
{
    char ch;
    int count = 0;
    cout<<"Enter character; enter # to quit:"<<endl;
    cin.get(ch);
    while(cin.fail()==false)
    {
        cout<<ch;
        ++count;
        cin.get(ch);
    }
    cout<<endl<<count<<" characters read"<<endl;
    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<iostream>
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;city<Cities;++city)
    {
        cout<<cities[city]<<":\t";
        for(int year=0;year<Years;++year)
        cout<<maxtemps[year][city]<<"\t";
        cout<<endl;
    }
    return 0;
}

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

13.

#include <iostream>
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<<years[i]<<"的每个月销售量:"<<endl;
            for(int j=0;j<MONTHS;j++)
            {
                    cout<<"请输入"<<months[j]<<"的销售量:";
                    cin>>sales[i][j];
                    temp+=sales[i][j];
            } 
            year_sale[i]=temp;
            sum+=year_sale[i];
    }
    for(int i=0;i<3;i++)
    cout<<years[i]<<"的销售量为:"<<year_sale[i]<<endl;
    cout<<"这三年的总销售量为:"<<sum<<endl;
    return 0;
} 

14.

#include <iostream>
#include <string>
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;i<num;++i)
    {
            cout<<"Car #"<<i+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;i<num;++i)
    cout<<ps[i].year<<" "<<ps[i].name<<endl;
    delete [] ps;
    return 0;
} 

15.

#include <iostream>
#include <cstring>

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 "<<sum<<" words.\n";
    return 0;
}
#include <iostream>
#include <string>

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 "<<sum<<" words.\n";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值