<c++ primer plus>第六版
目录
6 分支语句和逻辑运算符
6.1 if语句
if (test-condition)
{
statement1
}
else if(test-condition)
{
statement2
}
else
{
statement3
}
实际上, 上述语句只是嵌套的if-else, 只是排版格式不同.
if (test-condition) {
statement1
}
else {
if(test-condition) {
statement2
}
else {
statement3
}
}
6.2 逻辑表达式
3种逻辑运算符: ||, &&, !.
对应的标识符: or, and, not.
逻辑运算符优先级低于关系运算符(>, <, ==).
6.3 字符函数库cctype
cctype对应老式的ctype.h.
主要用于判断字符: 是否为大写/数字/字母/标点等.
isalpha(ch) //字母
isdigits(ch) //数字
isalnum(ch) //字母或数字
ispunct(ch) //标点
isspace(ch) //空白
6.4 三目运算符?:
var = expression1 ? expression2 : expression3
//expr1为true 时, 表达式值为expr2.
//expr1为false时, 表达式值为expr3.
6.5 switch语句
switch(相对于if-else)更容易从大型列表中进行选择.
c++中的case标签只是行标签, 而不是选项之间的界线, 所以需要配合break使用.
switch的标签必须是单独的值, 不能是取值范围.
switch的标签必须是整数(包括char), 不能是浮点数等.
注意: 每个语句序列后都要加break, 否则程序将继续执行后续分支.
switch (integer-expression)
{
case label1: {statements; break;} //如果没有break, c++会执行后续所有语句
case label2: {statements; break;}
...
default: {statements; break;}
};
可以将枚举量用作标签:
#include<iostream>
// red到indigo分别代表0~6
enum {red, orange, yellow, green, blue, violet, indigo};
int main(){
using namespace std;
cout << "Enter color code(0~6): ";
int code;
cin >> code;
while(code>=red && code<=indigo)
{
switch(code)
{
case red : cout << "input red.\n" ; break ;
case orange : cout << "input orange.\n" ; break ;
case yellow : cout << "input yellow.\n" ; break ;
case green : cout << "input green.\n" ; break ;
case blue : cout << "input blue.\n" ; break ;
case violet : cout << "input violet.\n" ; break ;
case indigo : cout << "input indigo.\n" ; break ;
}
cout << "Enter color code(0~6): ";
cin >> code;
}
cout << "Bye\n";
return 0;
}
6.6 break和continue语句
break: 跳过整个循环, 执行循环块后面的语句.
continue: 跳过当前循环体中余下的代码, 进入下一轮循环.
大多数情况下, 不要使用goto.
6.7 读取数字的循环
6.8 简单的文件输入/输出
6.8.1 文本IO和文本文件
6.8.2 写入到文本文件中
文件输出准备工作:
- 必须包含头文件fstream.
- fstream定义了一个用于处理输出的ofstream类.
- 需要声明一个或多个ofstream对象.
- 需要使用命名空间std (using或std:
- 使用open()方法.
- 使用close()方法.
例:
#include <iostream>
#include <fstream> //文件操作要使用fstream
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; //定义一个ofstream对象
outFile.open("car_info.txt"); //使用outFile来写文件car_info.txt
cout << "Enter the make and 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;
}
6.8.2 读取文本内容
#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
int main()
{
using namespace std;
char filename[SIZE] = "scores.txt";
//cout << "Enter name of data file: ";
//cin.get(filename, SIZE);
ifstream inFile; //ifstream对象
inFile.open(filename); //filename和inFile关联
if (!inFile.is_open()) //检查是否打开
{
cout << "Could not open the file " << filename << ", program terminating." << endl;
exit(EXIT_FAILURE);
}
double value;
double sum=0.0;
int count = 0;
inFile >> value; //获取第1个值
while (inFile.good()) //输入文件完好且没有到文件结尾EOF
{
++count;
sum += value;
inFile >> value; //获取下一个值
}
if (inFile.eof()) //读取数据时遇到EOF, eof()方法会返回true.
cout << "End of file reached.\n";
else if (inFile.fail())//最后一次读取操作, 如果发生类型不匹配, fail()方法返回true.
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;
}