C++Primer Plus(第6版)--学習雑記(第5章)


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

cout.setf(ios_base::boolalpha)関数呼び出しには、0と1ではなくtrueとfalseを表示するタグが設定されています.
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()関数で、2つの文字列アドレスをパラメータとして受け入れます.これは、パラメータがポインタ、文字列定数、または文字配列名であってもよいことを意味します.2つの文字列が同じ場合、関数はゼロを返します.1番目の文字列が2番目の文字列の前にアルファベット順に並べられている場合は、負の数値が返されます.そうでない場合は、整数値が返されます.
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("
2
"
); }

これにより、2つの出力の間に時間差が生じ、また、上記のコードを変更してwaitを10000000に変更し、ループをスキップするコンパイラもあります.より良い方法はシステムクロックにこの操作を完了させることです
#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
"
; clock_t start = clock(); while(clock()-startcout<<"done \a
"
; return 0; }

入力された数字は、そのキラキラした下線が現れる回数+1シンボル定数:CLOCKS_PER_SECは、毎秒含まれるシステム時間の単位数に等しいので、システム時間をこの値で割ると秒数に等しい.ctimeはclock_tはclock()としてタイプの別名を返します.これは、変数をclock_として宣言できることを意味します.tタイプ.
7.C++タイプの別名を作成するには、2つの方法があります.1つは、プリプロセッサを使用することです.
#define BYTE char

すべてのBYTEをcharで置き換え、BYTEはcharの別名で2つ目はキーワード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.2 D配列:
#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

"
; for(int city = 0;citycout<":\t"; for(int year=0;yearcout<"\t"; cout<return 0; }

12.この言葉の意味:int x=(1024);カンマ演算子、値は右の値、024は8進数の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<<":
"
; 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:
"
; 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):
"
; cin>>word; while(strcmp(word,"done")) { sum++; cin>>word; } cout<<"You entered a total of "<" words.
"; return 0; }
#include 
#include 

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