|
前言
个人觉得学习编程最有效的方法是阅读专业的书籍,通过阅读专业书籍可以构建更加系统化的知识体系。 一直以来都很想深入学习一下C++,将其作为自己的主力开发语言。现在为了完成自己这一直以来的心愿,准备认真学习《C++ Primer Plus》。 为了提高学习效率,在学习的过程中将通过发布学习笔记的方式,持续记录自己学习C++的过程。
一、if语句
当C++程序必须决定是否执行某个操作时通常使用if语句来实现选择。if有两种格式:if和if else。
if语句的语法与while相似:
if(test-condition)
statement
如果test-condition(测试条件)为true,则程序将执行statement(语句),后者既可以是一条语句,也可以是语句块。
if测试条件也将被强制转换为bool值,因此0将被转换为false,非零为true。
if语句让程序决定是否执行特定的语句或语句块,而if else语句则让程序决定执行两条语句或语句块中的哪一条,这种语句对于选择其中一种操作很有用。
if else语句的通用格式如下:
if(test-condition)
statement1
else
statement2
C++对if else进行扩展提供了两个以上选择的功能:
if(test-condition1)
statement1
else if(test-condition2)
statement2
else
statement3
二、逻辑表达式
C++据供了3种逻辑运算符,来组合或修改已有的表达式。这些运算符分别是逻辑OR(||)、逻辑AND(&&)和逻辑NOT(!)。
1、逻辑OR(||)
当两个条件中有一个或全部满是禁个要求时、可以用单词or来指明这种情况。
C++可以采用逻辑OR运算符(||),将两个表达式组合在一起。如果原来表达式中的任何一个或全部都为true(或非零),则得到的表达式的值为true;否则,表达式的值为false。
C++规定,运算符是个顺序点(sequence point)。也是说,先修改左侧的值,再对右侧的值进行判定(C++11的说法是,运算符左边的子表达式先于右边的子表达式)。另外,如果左侧的表达式为true,C++将不会去判定右侧的表达式,因为只要一个表达式为true,则整个逻辑表达式为true。
2、逻辑AND(&&)
逻辑AND运算符(&&),也是将两个表达式组合成一个表达式。仅当原来的两个表达式都为true时得到的表达式的值才为true。
和||运算符一样,&&运算符也是顺序点,因此将首先判定左侧,并且在右侧判定之前产生所有的副作用。如果左侧为false,则整个逻辑表达式必定为false,在这种情况下,C++将不会再对右侧进行判定。
逻辑AND运算符的优先级高于逻辑OR运算符。
3、逻辑NOT(!)
!运算符将它后面的表达式的真值取反。
!运算符的优先级高于所有的关系运算符和算术运算符。
逻辑运算符的另一个种表示方式:
运算符 | 另一种表示方式 | && | and | || | or | ! | not | 三、字符函数库cctype
C++从C语言继承了一个与字符相关的、非常方便的函数软件包,它可以简化诸如确定字符是否为大写字母、数字、标点符号等工作,这些函数的原型是在头文件cctype(老式的凤格中为ctype.h)中定义的。
cctype中的字符函数:
函数名称 | 返回值 | isalnum() | 如果参数是字母数字,即字母或数字,该函数返回true | isalpha() | 如果参数是字母,该函数返回true | iscntrl() | 如果参数是控制字符,该函数返回true | isdigit() | 如果参数是数字(0~9),该函数返回true | isgraph() | 如果参数是除空格之外的打印字符,该函数返回true | islower() | 如果参数是小写字母,该函数返回true | isprint() | 如果参数是打印字符(包括空格),该函数返回true | ispunct() | 如果参数是标点符号,该函数返回true | isspace() | 如果参数是标准空白字符,如空格、进纸、换行符、回车、水平制表符或垂直制表符,该函数返回true | isupper() | 如果参数是大写字母,该函数返回true | isxdigit() | 如果参数是十六进制数字,即0~9、a~f或A~F,该函数返回true | tolower() | 如果参数是大写字符,则返回其小写,否则返回该参数 | toupper() | 如果参数是小写字符,则返回其大写,否则返回该参数 | 四、?:运算符
C++有一个常被用来代替if else语句的运算符,这个运算符被称为条件运算符(?:),它是C++中唯一一个需要3个操作数的运算符。该运算符的通用格式如下:
expression1 ? expression2 :expression 3
如果expression1为true,则整个条件表达式的值为expression2的值;否则,整个表达式的值为expression3的值。
五、switch语句
假设用户需要从多个选项中选择一个,虽然可以通过扩展if else序列来处理这种情况,但C++的switch语句能够更容易地从大型列表中进行选择。下面是switch语句的通用格式:
switch (integer-expression)
{
case label1 : statement(s)
case label1 : statement(s)
...
default : statement(s)
}
C++的switch语句就像指路牌,告诉计算机接下来应执行哪行代码。执行到switch语句时,程序将跳到使用integer-expression的值标记的那一行。
integer-expression必须是一个结果为整数值的表达式。另外,每个标签都必须是整数常量表达式。最常见的标签是int或char常量,也可以是枚举量。如果integer-expression不与任何标签匹配,则程序将跳到标签为default的那一行。Deault标签是可选的,如果被省略,而又没有匹配的标签,则程序将跳到switch后面的语句处执行。
C++中的case标签只是行标签,而不是选项之间的界线。也是说,程序跳到switch中特定代码行后,将依次执行之后的所有语句,除非有明确的其他指示,程序不会在执行到下一个case处自动停止,要让程序执行完一组特定语句后停止,必须使用break语句。这将导致程序跳到switch后面的语句处执行。
六、break和continue语句
break和continue语句都使程序能够跳过部分代码。可以在switch语句或任何循环中使用break语句使程序跳到switch或循环后面的语句处执行。continue语句用于循环中,让程序跳过循环体中余下的代码,并开始新一轮循环。
八、简单文件输入/输出
文本I/O的概念:使用cin进行输入时,程序将输入视为一系列的字节,其中每个字节都被解释为字符编码。不管目标数据类型是什么,输入一开始都是字符数据——文本数据。然后,cin对象负责将文本转换为其他类型。为说明这是如何完成的,来看一些处理同一个输入行的代码。
本文讨论的文件I/O相当于控制台I/O,因此仅适用于文本文件。
// outfile.cpp -- writing to a file
#include <iostream>
#include <fstream> // for file I/O
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; // create object for output
outFile.open(&#34;e:\\1.txt&#34;); // associate with a file
cout << &#34;Enter the make and model of automobile: &#34;;
cin.getline(automobile, 50);
cout << &#34;Enter the model year: &#34;;
cin >> year;
cout << &#34;Enter the original asking price: &#34;;
cin >> a_price;
d_price = 0.913 * a_price;
// display information on screen with cout
cout << fixed;//按浮点型进行输出。
cout.precision(2);//控制浮点精度为2,即保留小数点位数
cout.setf(ios_base::showpoint);//使用默认的浮点格式时,上述语句还将导致末尾的0被显示出来。
cout << &#34;Make and model: &#34; << automobile << endl;
cout << &#34;Year: &#34; << year << endl;
cout << &#34;Was asking $&#34; << a_price << endl;
cout << &#34;Now asking $&#34; << d_price << endl;
// now do exact same things using outFile instead of cout
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << &#34;Make and model: &#34; << automobile << endl;
outFile << &#34;Year: &#34; << year << endl;
outFile << &#34;Was asking $&#34; << a_price << endl;
outFile << &#34;Now asking $&#34; << d_price << endl;
outFile.close(); // done with file
// cin.get();
// cin.get();
return 0;
}
读取文件使用ifstream:
// sumafile.cpp -- functions with an array argument
#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; // object for handling file input
cout << &#34;Enter name of data file: &#34;;
cin.getline(filename, SIZE);
inFile.open(filename); // associate inFile with a file
if (!inFile.is_open()) // failed to open file
{
cout << &#34;Could not open the file &#34; << filename << endl;
cout << &#34;Program terminating.\n&#34;;
// cin.get(); // keep window open
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0; // number of items read
inFile >> value; // get first value
while (inFile.good()) // while input good and not at EOF
{
++count; // one more item read
sum += value; // calculate running total
inFile >> value; // get next value
}
if (inFile.eof())
cout << &#34;End of file reached.\n&#34;;
else if (inFile.fail())
cout << &#34;Input terminated by data mismatch.\n&#34;;
else
cout << &#34;Input terminated for unknown reason.\n&#34;;
if (count == 0)
cout << &#34;No data processed.\n&#34;;
else
{
cout << &#34;Items read: &#34; << count << endl;
cout << &#34;Sum: &#34; << sum << endl;
cout << &#34;Average: &#34; << sum / count << endl;
}
inFile.close(); // finished with the file
// cin.get();
return 0;
} |
|