c++primer学習ノート(6.0)文


6.1簡単な文
空の文;単一セミコロン//関係のない空の文は無害ではありません
6.2宣言文
オブジェクトまたはクラスの定義または宣言
6.3複合文(ブロック)
ブロックは役割ドメインを示します
whileループのように、ブロックはセミコロンで終わるわけではありません.
6.4文の役割ドメイン
条件式で定義された変数は初期化する必要があります.この条件は、初期化オブジェクトの値を検証します.
6.5 if文
if(a>b){
/*
*/
}
else if(a=b)
/*   */
else 
/*    */

懸垂else問題に注意
プログラミングスタイルの推奨事項:if後に常にカッコを使用
6.6 switch文
各case記号の値は定数式でなければなりません.
const int val;
........
case val:    //ok
#include<iostream>
#define FAIL 0; 
#define TRUE 1;
using namespace std;
int main(){
char ch;
int acnt=0,bcnt=0,ccnt=0,cnt=0;
while(cin>>ch){
switch(ch){
  case 'a':
      ++acnt;
      break;
  case 'b':
      ++bcnt;
      break;
  case 'c':
      ++ccnt;
      break;
  default:
      ++cnt;  
}
}
    cout<<"the number of a is"<<endl<<acnt<<endl; 
cout<<"the number of b is"<<endl<<bcnt<<endl; 
cout<<"the number of c is"<<endl<<ccnt<<endl;
cout<<"the number of others is"<<endl<<cnt<<endl; 
return TRUE;
}

練習問題6.7
//          
#include <iostream>
#include <string>
using namespace std;
int main(){ 
   char ch;
   int cnt=0;
   while(cin>>ch){
   switch(ch){
     case 'a':   case 'A':
     case 'e':   case 'E':
 case 'i':	 case 'I': 
 case 'o':	 case 'O':
 case 'u':	 case 'U':
   ++cnt; 
  default:
     ;  
}
}
    cout<<"the number of vowel is"<<endl<<cnt<<endl; 
return 0; 
}

//母音統計プログラムを修正してff,fl,fiの2文字の個数を統計できるようにする
#include<iostream>
using namespace std;
int main(){
char currch,prech='\0';
int ffcnt=0,ficnt=0,flcnt=0;
while(cin>>currch){
if(prech=='f')
switch(currch){
case 'f':
  ++ffcnt;
  break;
case 'l':
  ++flcnt;
 case 'i':
   ++ficnt;
   break;   
}
prech=currch;
}
cout<<"The number of \'ff\' is "<<ffcnt<<endl;
cout<<"The number of \'fi\' is "<<ficnt<<endl;
cout<<"The number of \'fl\' is "<<flcnt<<endl;
} 

6.7 while文
ループで定義された変数は、ループのたびに作成および取り消しのプロセスを経験します.
練習問題6.12
#include<iostream>
#include<string>
using namespace std;
int main(){
int currcnt=1,maxcnt=1;  //       
    string prestr="\0",currstr="\0";
    string repstr;  //       string 
    cout<<"input some strings:"<<endl;
    while(cin>>currstr){
    	if(prestr==currstr)
          currcnt++;
        else{
        	if(currcnt>maxcnt){
        	maxcnt=currcnt;
        	repstr=prestr;
        }
        currcnt=1;   
        } 
 	    prestr=currstr;        
 }
    
    	//              
        if(currcnt>maxcnt){
        	maxcnt=currcnt;
        	repstr=currstr;
        }
        
        
    if(maxcnt>1){
        	cout<<"repeat word is "<<repstr<<",and the number of it is "<<maxcnt<<endl;
        }
        else
        cout<<"There is no repeat word!"<<endl;
        
    
    return 0;
} 

while(cin>>ch)がどのように終了するかの問題
 
入力ストリームが終了する条件は、^zの前に文字入力(リターンを除く)ができないことです.そうしないと、^zはストリーム終了の役割を果たしません.  
方法:車に戻った後、新行Ctrl+zで車に戻ります.データを入力するとCtrl+zが無効になります.理由:入力バッファは行バッファです.キーボードから一連の文字を入力して車に戻ると、これらの文字はまず入力バッファに格納されます.リターンキーを押すたびにcin.get()は、入力バッファに読み取り可能なデータがあるかどうかを検出します.cin.get()はまた、キーボードにストリーム終了フラグであるCtrl+ZまたはCtrl+Dキーが押されているかどうかをチェックします.チェックの仕方は、ブロック式と非ブロック式の2つです.
閉塞式検査方式とは、リターンキーが押された後にのみ、これに対してCtrl+Zコンビネーションキーが押されているか否かを検査し、非閉塞様式とは、Ctrl+Dが押された直後に応答する方式をいう.Ctrl+Dを押す前にキーボードから文字が入力されている場合、Ctrl+Dの役割は、これらの文字を入力バッファに送って読み取りのために使用することに相当し、このときCtrl+Dはフロー終了文字の役割を果たしません.Ctrl+Dを押す前にキーボード入力がない場合、Ctrl+Dはストリーム終了の信号です.
Windowsシステムでは一般的にブロック式検査Ctrl+Z、Unix/Linuxシステムでは非ブロック式検査Ctrl+Dが用いられる.ビルの所有者はWindowsシステムの下にあるので、ブロック式のCtrl+Zを使用してストリームの終了を識別します.
このようなブロック方式には、リターンを押してから、その前にCtrl+Zが押されたかどうかを検出することができるという特徴がある.もう1つの特徴は、入力バッファに読み取り可能なデータがある場合、Ctrl+Zは検出されないことです(読み出すデータがあるため、ストリームの末尾になったとは考えられません).もう一つ知っておくべきことは、Ctrl+Zは通常のASCIIコード値ではなく、つまり文字ではないので、他のキーボードから入力された文字と同じように入力バッファに格納されないことです.この点が分かったら、スレ主が提起した問題を説明することができます.
キーボードからabcd^zを入力して車を戻した後、Windowsシステムでは、車を返す役割で前のabcdなどの文字が入力バッファに送られる(注意:前述したように、^zは文字を生成しないので、入力バッファには格納されず、バッファに^zは存在しない).この時、cin.get()は、入力バッファにすでにデータが存在することを検出し(したがって^zの入力があるかどうかはチェックしない)、バッファから対応するデータを読み出す.すべての読み出しが完了すると、入力バッファが再び空になり、cin.get()は新しい入力を待つ.^zが押されているにもかかわらず、その前に他の入力文字(abcd)があるため、ストリームは終了しないことがわかります.
したがって、入力ストリームが終了する条件は、^zの前に文字入力(リターンを除く)ができないことです.そうしないと、^zはストリーム終了の役割を果たしません.
  
参考資料http://www.2cto.com/kf/201202/119312.html 
6.8 forループ文
for(int i=0,k=0;i<=10;++I)   ok
for(int i=1,long u=1.221;i<=10;i++)erroエラー、初期化は1つのタイプの
6.9 do while文
while文とは異なり、do while文は常にセミコロンで終了します.
練習問題6.18
#include<iostream>
#include<string> 
using namespace std;
string max(string &a,string &b);
int main(){
    string str1,str2,str3;
    cout<<"please input two strings:"<<endl;
    do{    
    cin>>str1>>str2;
    cout<<max(str1,str2)<<" is bigger."<<endl;
    cout<<"please input two strings:"<<endl;
    cout<<"continue?   y/n"<<endl;
    cin>>str3;    
    }while(str3[0]=='y');    //string  ==     ,           
} 
string max(string &a,string &b){
if(a<b)
return b;
else 
return a;
}

6.10 break文
break文は、最近のwhile、do while、for、またはswitch文を終了するために使用されます.
練習問題6.20
#include<iostream>
#include<string> 
using namespace std;
int main(){
string str1="\0",str2;
int cnt=1;  //count
cin>>str1;
while(cin>>str2){
if(str1==str2) {
cout<<"      :"<<str2<<endl;
++cnt;
break;
}
else
str1=str2;
}
if(cnt==1){
cout<<"      "<<endl;
return 0;
}
else
return 0;
} 

6.11 continue文
ループ文のレベル反復のみが終了します
練習問題6.21
#include<iostream>
#include<string> 
using namespace std;
int main(){
string str1="\0",str2;
int cnt=1;  //count
cin>>str1;
while(cin>>str2){
if(str2[0]<='Z'&&str2[0]>='A'&&str1==str2) {
cout<<"               :"<<str2<<endl;
++cnt;
break;
}
else
str1=str2;
}
if(cnt==1){
cout<<"                "<<endl;
return 0;
}
else
return 0;
} 

6.12 goto文
goto文の使用を主張しない
goto文は変数定義文を越えて前にジャンプすることはできません.必要であれば、定義をブロック文に配置します.
6.13 tryブロックと異常処理
throw式
if(!item1.same_isbn(item2))
  throw runtime_erro("Dsta must refer to same ISBN.");
throwはruntimeを使用しましたErrorタイプのオブジェクト、runtime_errorタイプは、stdexceptヘッダファイルで定義される標準ライブラリ異常クラスの一種です.
tryブロック
try{
program-statements
}catch(exception-specifier){
handler-statements
}catch(exception-specifier){
handler-statements
}//....
練習問題6.23
#include<iostream>
#include<bitset>
using namespace std;
int main(){
  bitset<100> bs;
  for(size_t ix=0;ix!=bs.size();++ix)
     bs[ix]=1;
  bs.to_ulong();
  return 0;   
}

練習問題6.34
#include<iostream>
#include<bitset>
#include<stdexcept>
using namespace std;
int main(){
  bitset<100> bs;
  for(size_t ix=0;ix!=bs.size();++ix)
     bs[ix]=1;
 try{
 	 bs.to_ulong();
 }catch (runtime_error err){
 	cout<<err.what()<<endl;
 }
  return 0; 
}

ひょうじゅんいじょう
標準ライブラリ例外クラスは、次の4つのヘッダファイルに定義されます.
1、exceptionヘッダファイル:最も一般的な標準異常クラスを定義し、そのクラス名はexceptionである.
例外の発生のみを通知しますが、これ以上の情報は提供されません.
2、stdexceptヘッダファイルは以下のいくつかのよくある異常クラスを定義した.
関数の機能または役割
Exceptionで最も一般的な問題
runtime_Errorランタイムエラー:ランタイムのみで検出できる問題
range_Errorランタイムエラー:生成された結果が有意義な値ドメイン範囲を超えています
overflow_error運転時エラー:オーバーフロー計算
underflow_Errorランタイムエラー:アンダーフローの計算
 
logic_Errorロジックエラー:実行前に検出できる問題
domain_error論理エラー:パラメータの結果値が存在しません
invalid_Argumentロジックエラー:不適切なパラメータ
length_errorロジックエラー:このタイプの最大長を超えるオブジェクトを生成しようとしました
out_of_range論理エラー:有効範囲外の値を使用
 
3、newヘッダファイルはbad_を定義したalloc例外タイプ、メモリが割り当てられないためnewから放出される例外を提供
4、type_infoヘッダファイルはbad_を定義しますCast例外タイプ(type_infoを使用するにはtypeinfoヘッダファイルを含む必要があります)