现在的位置: 首页 > 综合 > 正文

c++primer学习笔记(6.0)语句

2013年10月10日 ⁄ 综合 ⁄ 共 5542字 ⁄ 字号 评论关闭

6.1简单语句

空语句 ;单个分号   //无关的空语句并非是无害的

6.2声明语句

对象或类的定义或声明

6.3复合语句(块)

块标志了一个作用域

while循环,块并不是以分号结束的


6.4语句作用域

在条件表达式中定义的变量必须初始化,该条件检验的就是初始化对象的值。

6.5if语句

if(a>b){
/*
*/
}
else if(a=b)
/*   */
else 
/*    */

注意悬垂else问题

编程风格建议:在if后总是使用花括号


6.6switch语句

每个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双字符的个数

#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.7while语句

在循环中定义的变量在每次循环中都要经历创建和撤销的过程。

习题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   键按下作出检查,其检查的方式有两种:阻塞式以及非阻塞式。

阻塞式检查方式指的是只有在回车键按下之后才对此前是否有   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按下。还有一个特点就是:如果输入缓冲区中有可读的数据则不会检测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.8for循环语句

for(int i=0k=0;i<=10;++I)   ok

for(int i=1,long u=1.221;i<=10;i++)  erro 错误,初始化只可以初始化一种类型的


6.9do 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.10break语句

break语句用于结束最近的whiledo whilefor,或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.11continue语句

仅仅导致循环语句的档次迭代结束

习题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.12goto语句

不主张使用goto语句

goto语句不能跨越变量定义语句向前跳转,如果确实需要,则定义要放到块语句里面。


6.13try块和异常处理

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; 
}

标准异常

标准库异常类定义在以下四个头文件中

1exception头文件:定义了最常见的标准异常类,其类名为exception

   只通知异常的产生,但不会提供更多的信息

2stdexcept头文件定义了以下几种常见异常类

   函数                                  功能或作用

exception       最常见的问题

runtime_error     运行时错误:仅在运行时才能检测到的问题

range_error    运行时错误:生成的结果超出了有意义的值域范围

overflow_error   运行时错误:计算上溢

underflow_error   运行时错误:计算下溢

 

logic_error  逻辑错误:可在运行前检测到的问题

domain_error      逻辑错误:参数的结果值不存在

invalid_argument        逻辑错误:不合适的参数

length_error  逻辑错误:试图生成一个超出该类型最大长度的对象

out_of_range   逻辑错误:使用一个超出有效范围的值

 

3new头文件定义了bad_alloc异常类型,提供因无法分配内存而由new抛出的异常

4type_info头文件定义了bad_cast异常类型(要使用type_info必须包含typeinfo头文件)

抱歉!评论已关闭.