《C++ Primer Plus》第6章知识点总结
简单的知识不再进行赘述
逻辑表达式
逻辑OR运算符:||
5>3 || 5>10
如果||左边表达式为1,则程序不计算右边表达式
逻辑AND运算符:&&
5>3 && 5>10
如果&&左边表达式为0,则程序不计算右边表达式
逻辑NOT运算符:!
if(!(x>5)
!运算符将它后面的表达式的真值取反
字符函数库cctype
函数名称 | 返回值 |
isalnum() | 如果参数是字母数字,即字母或者数字,函数返回true |
isalpha() | 如果参数是字母,函数返回true |
iscntrl() | 如果参数是控制字符,函数返回true |
isdigit() | 如果参数是数字(0-9),函数返回true |
isgraph() | 如果参数是除空格之外的打印字符,函数返回true |
islower() | 如果参数是小写字母,函数返回true |
isprint() | 如果参数是打印字符(包括空格),函数返回true |
ispunct() | 如果参数是标点符号,函数返回true |
isspace() | 如果参数是标准空白字符,如空格、换行符、水平或垂直制表符,函数返回true |
isxdigit() | 如果参数是十六进制数字,即0-9、a-f、A-F,函数返回true |
isupper() | 如果参数是大写字母,函数返回true |
toupper() | 如果参数是小写字符,返回其大写,否则返回该参数 |
tolower() | 如果参数是大写字符,返回其小写,否则返回该参数 |
?: 运算符
expression1 ? expression2 : expression3
如果expression1为true,则整个表达式的值为expression2的值;否则,整个表达式的值为expression3的值
switch语句
switch(integer-expression)
{
case label1 : statement(s)
case label2 : statement(s)
...
default : statement(s)
}
break语句
可以在switch语句或任何循环中使用,使程序调到switch或循环后面的语句处执行
continue语句
用于循环中,让程序跳过循环体中余下的代码,并开始新一轮循环
简单文件输入/输出
与文件输出有关的几点
(1)必须包含头文件fstream
(2)头文件stream定义了一个用于处理输出的ofstream类
(3)需要声明一个或多个ofstream变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名规则
(4)必须指明名称空间std;例如,为引用元素ofstream,必须使用编译指令using或前缀std::
(5)需要将ofstream对象与文件关联起来。为此,方法之一是使用open()方法
(6)使用完文件后,应使用方法close()将其关闭
(7)可结合使用ofstream对象和运算符<<来输出各种类型的数据
简单来说,使用文件的输出的主要步骤如下
(1)包含头文件fstream
(2)创建一个ofstream对象
(3)将该ofstream对象同一个文件关联起来
(4)就像使用cout那样使用该ofstream对象
下面是一个例子
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile;
outFile.open("carinfo.txt");
cout<<"Enter the make the model of automobile: ";
cin.getline(automobile,50);
cout<<"Enter the model year: ";
cin>>year;
cout<<"Enter the original asking price: ";
cin>>a_price;
d_price=0.913*a_price;
cout<<fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout<<"Make and model: "<<automobile<<endl;
cout<<"Year: "<<year<<endl;
cout<<"Was asking $"<<a_price<<endl;
cout<<"Now asking $"<<d_price<<endl;
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile<<"Make and model: "<<automobile<<endl;
outFile<<"Year: "<<year<<endl;
outFile<<"Was asking $"<<a_price<<endl;
outFile<<"Now asking $"<<d_price<<endl;
outFile.close();
return 0;
}
与文件输入相关的几点
(1)必须包含头文件fstream
(2)头文件stream定义了一个用于处理输入的ifstream类
(3)需要声明一个或多个ifstream变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名规则
(4)必须指明名称空间std;例如,为引用元素ifstream,必须使用编译指令using或前缀std::
(5)需要将ifstream对象与文件关联起来。为此,方法之一是使用open()方法
(6)使用完文件后,应使用方法close()将其关闭
(7)可结合使用ifstream对象和运算符>>来输入各种类型的数据
(8)可以使用ifstream对象和get()来读取一个字符,使用ifstream对象和getline()来读取一行字符
(9)可以结合使用ifstream和eof()、fail()等方法来判断输入是否成功
(10)ifstream对象本身被用作测试条件时,如果最后一个读取操作成功,它将被转换为布尔值true,否则被转换为false
下面是一个例子
#include <iostream>
#include <fstream> //file I/O support
#include <cstdlib> //support for exit()
const int SIZE=60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout<<"Enter name of data file: ";
cin.getline(filename,SIZE);
inFile.open(filename);
if(!inFile.is_open())
{
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum=0.0;
int count=0;
inFile>>value;
while(inFile.good())
{
++count;
sum+=value;
inFile>>value;
}
if(inFile.eof())
cout<<"End of file reached.\n";
else if(inFile.fail())
cout<<"Input terminated by data mismatch.\n";
else
cout<<"Input terminated for unknown reason.\n";
if(count==0)
cout<<"No data processed.\n";
else
{
cout<<"Items read: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<sum/count<<endl;
}
inFile.close();
return 0;
}
运行这个程序,首先必须在可执行文件所属的文件夹中创建一个包含数字的文本文件