c++primer plus 第六版 第六章重点内容总结 以及编程题答案

本文深入探讨C++中的控制结构,包括if语句的使用技巧、逻辑表达式的构成、字符函数库cctype的应用、三元运算符的替代使用、switch语句的详细解析、循环中的break与continue用法、文件的输入输出操作以及错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.if中的trick

当char型字符使用cout输出时,并且在使用运算符之后再cout的时候,一定要强制转化回char类型。否则打印出的是数字(字符的编码),if else中的两种操作如果需要多条语句需要用大括号将它们括起来。当if中的测试表达式是判断相等与否时,如variable==value形式,可以调换顺序变为value==variable的形式,避免与赋值运算符混淆。

2.逻辑表达式

逻辑表达式提供或||,与&&,非!来组合和修改测试表达式。如5==5||5==9为真,因为||运算符优先级比关系运算符低,5==5是真那么表达式为真。且c++规定||运算符是个顺序点,也就说先修改其左侧的值,再对右侧的值进行判定(c++11说运算符左侧的子表达式先于右侧的子表达式)。如i++<6||i=j,若i=10,则在对i和j比较时,i已经是11.另外如果左侧表达式为真那么c++将不会去判定右侧的表达式。&&关系运算符也比关系运算符低,也是顺序点,因此将首先判定左侧,并且在右侧被判定之前产生所有的副作用。如果左侧为false则整个表达式必定为false,因此不必再判定右侧。

并且在&&情况下,取值范围测试表达式的每一个部分都得使用&&将其组合起来。

if(age>17&&age<35)对的,但是不要if(17<age<35)这个表达式是有效的c++语法,<运算符从左到右结合,因此上述表达式意义是

if((17<age)<35),17<age要么为真(1),要么为假(0),因此整个表达式的值总为真!

!非运算符是将其后的表达式的真值取反。即如果expression为true或者非0,则!expression为false。if(!(x>5))其实就是if(x<=5)。注意!运算符优先级高于所有关系运算符和算术运算符,因此在对表达式求反的时候应用括号括起来。逻辑与运算符&&优先级大于逻辑or运算符。总是先计算&&的部分,然后将其结果去计算||的部分,所以若选择先计算||那么应用括号区分优先级。

3.字符函数库cctype

#include<cctype>中isalpha()检查字符是否为字母字符,使用isdigits()来测试字符是否为数字字符,使用isspace()来测试字符是否为空白,包括换行符,空格和制表符,使用ispunct()来测试字符是否为标点符号。if else if可以有多个else if的语句。这些函数返回是int而非bool。是的话返回一个非0值。

4.?:运算符

条件运算符?:用来替 代else运算符,是c++中唯一一个需要三个操作数的运算符。

expression 1? expression2:expression3若1为true,整个表达式的值为expression2,否则为3的值。它可以生成一个表达式因此是一个值可以将其赋给变量或将其放在一个更大的表示式中。如int c=a>b?a:b;相当于int c;if(a>b)c=a;elee c=b;

5.switch

switch(){case1:statemenst

             ...

             default:statements

}其中程序根据输入标签(最常见int或者char型常量)跳到switch中特定代码行后将依次执行之后的所有语句,要使得执行完某条语句停止那么需要break语句。

使用枚举量作为标签时,switch语句将读入int值,与枚举量标签进行比较时将枚举量提升为int。另外在while的循环测试条件中也会将枚举量提升为int类型。switch并不是为了处理范围而设计的,switch语句中的每一个case标签都必须是一个单独的值。另外这个值必须是整数(包括char),因此switch无法处理浮点测试。另外case值还必须是常量。如果选项涉及取值范围,浮点测试或者两个变量的比较,则应该使用if else语句。

7.break和continue

可以在switch语句或者任何循环中使用break语句,使得程序跳到switch循环后面的语句处执行(跳出循环)。cotinue语句用于循环中,让程序跳过循环体中余下的代码开新的一轮循环。注意的是continue语句不会跳过循环的更新表达式,在for循环中,continue语句使程序直接跳到更新表达式处然后跳到测试表达式处。对于while循环来讲,continue将使程序直接跳到测试表达式处,因此while循环体中位于continue之后的更新表达式都将被跳过。

8.读取数字的循环

当使用cin读取数字时却得到一个非数字输入时,会发生类型不匹配,导致n的值保持不变,不匹配的输入将被留在输入队列中,cin对象中的一个错误标记被设置。对cin 的方法的调用将返回false,cin对象中的一个错误标记被设置。因此可以使用非数字的输入来结束读取数字的循环。非数字输入设置错误标记意味着必须重置该标记程序才能继续读取。cin.clear()方法重置错误输入标记以及文件尾(EOF条件),还可以用cin.get()方法删除留在输入队列中的错误输入。

9.简单文件输入输出

首先查看I/O的概念,假设有如下输入38.5 19.2当使用char ch;cin>>ch;时,第一个字符是3(字符编码)存储在变量ch中。这里存储的数值3其实是字符3的编码。当采用int n;cin将不断读取直到遇到非数字字符。38的二进制编码存储在变量中。若采用double x;cin将不断读取直到遇到第一个不属于浮点数的字符。即38.5的二进制编码复制在变量x中,空格是输入队列中的下一个字符。若采用char word[50];cin将不断读取直到遇到空白为止,它读取3,8,。,5使得空格成为成为输入队列中的下一个字符。cin将这四个字符的字符编码存储到word数组中,并在结尾中加上空字符。若采用char word【50】;cin.getline(word,50);cin将不断读取直到遇到换行符。所有字符都被存储到数组word中并在末尾加上一个空字符。换行符被丢弃。输入队列的下一个字符是下一行中的第一个字符。

写入到文本文件:包含头文件fstream,声明一个用于输出的ofstream类,声明一个对象,且将ofstream对象与文件关联起来,其中之一的方法是使用open()。ofstream outfile;ofstream fout;outfile.open("fish.txt");char filename[50];cin>>filename;fout.open(filename);其中open接受一个c风格字符串还可以是一个字面字符串,也可以是存在数组中的字符串。使用这种对象的方法,double wt =125.8;outfile<<wt;char line[81]="objects are closer than they appear";fout<<line<<endl;

使用文件输出的步骤:包含头文件fstream;创建一个ofstream对象;将该ofstream对象同一个文件关联起来;就像使用cout那样该ofstream;注意cout<<fixed  用一般的方式输出浮点型,例如C++程序在控制台显示的时候大一点的数,显示的时候使用了科学计数法,使用该命令即可像一般的方式显示cout.precision(2) 设置精确度为2,并返回上一次的设置。cout.set(iOS_base::showpoint)  //显示浮点数小数点后面的零。默认情况下,open将首先截断文件,即将其长度截短到0,丢掉原有内容然后将新的输出加该文件中。
 

10.读取文本文件

与以上极为类似,必须包含头文件fstream,其中定义了一个处理输入的ifstream类,需要声明一个或者多个ifstream对象,并将其与文件关联起来,方法之一是open()。ifstream infile;ifstream fin;infile.open("bowling.txt");char filename[50];cin>>filename;fin.open(filename);open()仍然接受c风格字符串作为参数,字面字符串,存储在数组中的字符串。

double wt;infile>>wt;从wt中读取数字char line[81];fin.getline(line,81);读取文本的一行。其中infile.is_open()是检查文件是否被成功打开,有时也可以用good()代替。并且可以采用exit()结束函数,定义在头文件cstdlib中,还定义了一个用于同操作系统通信的参数值EXIT_FAILURE。infile.eof()判断是否到达文件尾eof,infile.fail()检查eof和类型不匹配。

#include<iostream>
#include<cctype>
#include<fstream>
#include<cstdlib>
using namespace std;
//+++++++++++++++++++1++++++++++++++++++++++++++++++++++++++++++
int main()
{
cout<<"enter your num";
char ch;
cin.get(ch);
while(ch!='@')
{
   if(isdigit(ch))
      cin.get(ch);
   else if(islower(ch))
     { 
      //cout<<toupper(ch); return int!!!!
      ch=toupper(ch);
      cout<<ch;
     }
   else if(isupper(ch))
      {
      //cout<<tolower(ch); return int!!!!
      ch=tolower(ch);
      cout<<ch;
      }
   else
      cout<<ch;
cin.get(ch);
}
return 0;
}
//++++++++++++++++++2+++++++++++++++++++++++++++++++++++++++++++
int main()
{
double average;
double donation[11];
int i=0;
int count=0;
double sum=0.0;
while((i<10)&&(cin>>donation[i]))
{
sum=sum+donation[i];
i++;
}
for(int j=0;j<i;j++)
{
if(donation[j]>0)
   count++;
}
cout<<sum/i<<endl;
cout<<count<<endl;
}
//++++++++++++++++++++++++3+++++++++++++++++++++++++++++++++++++++
int main()
{
char choice;
cout<<"Please enter one of following choices:"<<endl;
do
{
cin.get(choice);
switch(choice)
{
case 'c':cout<<"a map is a carnivore";break;
case 'p':cout<<"a map is a pianist";break;
case 't':cout<<"a map is a tree";break;
case 'g':cout<<"a map is a game";break;
default :cout<<"Please enter a c,p,t or g";
}
}while(!(choice=='c'||choice=='p'||choice=='t'||choice=='g'));
return 0;
}
//++++++++++++++++++++++++4++++++++++++++++++++++++++++++++++++++++
const int strsize=20;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void preference(bop *,int);
int main()
{
bop pts[5]=
{
   {"wimp wacho","title1","yin1",0},
   {"raki rhodes","title2","yin2",1},
   {"celia laiter","title3","yin3",2},
   {"hoppy hipman","title4","yin4",1},
   {"pat hand","title5","yin5",0}
};
char ch;
cout<<" a. display by name"<<"    "<<" b. display by title"<<endl;
cout<<" c. display by bopname"<<"    "<<" d.display by preference"<<endl;
cout<<" q. quit"<<endl;
cout<<"enter your choice"<<endl;
while(cin.get(ch))
{
   if(ch=='q')
   {    cout<<"bye!";
       break;
   }
   switch(ch)
   {
   case 'a': cout<<pts[0].fullname<<"\n"<<pts[1].fullname<<"\n"<<pts[2].fullname<<"\n"<<pts[3].fullname<<"\n"<<pts[4].fullname<<endl;break;
   case 'b': cout<<pts[0].title<<"\n"<<pts[1].title<<"\n"<<pts[2].title<<"\n"<<pts[3].title<<pts[4].title<<endl;break;
   case 'c': cout<<pts[0].bopname<<"\n"<<pts[1].bopname<<"\n"<<pts[2].bopname<<"\n"<<pts[3].bopname<<"\n"<<pts[4].bopname<<endl;break;
   case 'd': preference(pts,5);break;
   default : break;
   }
}
return 0;
}
void preference(bop *array,int size)
{
for(int i=0;i<size;i++)
{
   if(array[i].preference==0)
      {cout<<array[i].fullname<<endl;}
   else if(array[i].preference==1)
      {cout<<array[i].title<<endl;}
   else if(array[i].preference==2)
      {cout<<array[i].bopname<<endl;}
}
}
//+++++++++++++++++++++++++5++++++++++++++++++++++++++++++++++++++++++++
int main()
{
unsigned int salary;
while(cin>>salary)
{
   if(salary<=5000)
      cout<<"no taxes"<<endl;
   else if(salary>5000&&salary<=15000)
      cout<<(salary-5000)*0.1<<endl;
   else if(salary>15000&&salary<=35000)
      cout<<10000*0.1+(salary-15000)*0.15<<endl;
   else if(salary>35000)
      cout<<10000*0.1+20000*0.15+(salary-35000)*0.2<<endl;
}
return 0;
}
//++++++++++++++++++++++++6++++++++++++++++++++++++++++++++++++++++++++++
int num;
int temp=0;
struct donator
{
string name;
double money;
};
int main()
{
cout<<"enter your numbers"<<endl;
cin>>num;
cin.get();
donator *pt = new donator[num];
for(int i=0;i<num;i++)
{
cout<<"enter your name"<<endl;
getline(cin,pt[i].name);
cout<<"enter your money"<<endl;
cin>>pt[i].money;
cin.get();
}
cout<<"Grand patrons"<<endl;
for(int j=0;j<num;j++)
{
   if(pt[j].money>10000)
   {
   cout<<pt[j].name<<"  "<<pt[j].money<<endl;
   temp++;
   }
   
}
if(temp==0)
   cout<<"none"<<endl;
temp=0;
cout<<"patrons"<<endl;
for(int k=0;k<num;k++)
{
   if(pt[k].money<=10000)
   {
   cout<<pt[k].name<<"  "<<pt[k].money<<endl;
   temp++;
   }

}
if(temp==0)
   cout<<"none"<<endl;
return 0;
}
//++++++++++++++++++++++++7+++++++++++++++++++++++++++++++++++++++
int main()
{
string word;
int count1=0;
int count2=0;
int count3=0;
while(cin>>word)
{
   if(word.size()==1&&word[0]=='q')
   {
      break;
   }
   if(isalpha(word[0]))
   {
      if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u')
      {
      count1++;
      }
      
      else
      count2++;
   }

   else
   count3++;

}
cout<<"yuanyin has "<<count1<<endl;
cout<<"fuyin has "<<count2<<endl;
cout<<"others has "<<count3<<endl;
return 0;
}
//+++++++++++++++++++++++++8+++++++++++++++++++++++++++++++++++++++++++++
const int size=20;
int main()
{
char filename[size];
ifstream infile;
cin.getline(filename,size);
infile.open(filename);
if(!infile.is_open())
   {
   cout<<"could not open the file "<<endl;
   exit(EXIT_FAILURE);
   }
char ch;
int num=0;
while(!infile.eof())
   {
   infile.get(ch);
   num++;
   }
infile.close();
cout<<"all are "<<num<<endl;
return 0;
}
//++++++++++++++++++++++++++++9+++++++++++++++++++++++++++++++++++++++++++++
struct donator
{
string name;
double money;
};
int main()
{
//string filename="ceshi.txt";
string read_str;
int temp=0;
int money;
int num;
ifstream infile;
infile.open("ceshi.txt");
if(!infile.is_open())
   cout<<"could not open file" <<endl;
infile>>num;
infile.get();
donator *pt = new donator[num];
while(!infile.eof())
{
   for(int i=0;i<num;i++)
   {
   getline(infile,pt[i].name);
   infile>>pt[i].money;
   infile.get();
   }
}

cout<<"Grand patrons"<<endl;
for(int j=0;j<num;j++)
{
   if(pt[j].money>10000)
   {
   cout<<pt[j].name<<"  "<<pt[j].money<<endl;
   temp++;
   }
   
}
if(temp==0)
   cout<<"none"<<endl;
temp=0;
cout<<"patrons"<<endl;
for(int k=0;k<num;k++)
{
   if(pt[k].money<=10000)
   {
   cout<<pt[k].name<<"  "<<pt[k].money<<endl;
   temp++;
   }

}
if(temp==0)
   cout<<"none"<<endl;
return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值