C++ Primer Plus学习笔记之分支语句和逻辑运算符

2022-12-17 程序员资讯 0 731
¥ 2.88B

包年VIP免费升级包年VIP

开通VIP尊享优惠特权
立即下载 升级会员

C++ Primer Plus学习笔记之分支语句和逻辑运算符

序言

对个人真的自学程式设计最有效率的形式是写作专精的书刊,透过写作工具书能构筑更为管理体系化的科学知识管理体系。 始终年来都很想深入细致自学呵呵C++,将其做为他们的主力部队合作开发词汇。那时为的是顺利完成他们这始终年来的愿望,预备认真自学《C++ Primer Plus》。 为的是提升自学工作效率,在自学的桑利县透过正式发布自学讲义的形式,稳步历史记录他们自学C++的操作过程。

一、if句子

当C++流程要下定决心与否继续执行某一操作形式时一般来说采用if句子来同时实现优先选择。if有三种文件格式:ifif else

if句子的句法与while相近:

if(test-condition) statement

假如test-condition(试验前提)为true,则流程将继续执行statement(句子),前者既能是两条句子,也能是句子块。

if试验前提也将被强制性切换为bool值,因而0将被切换为false,一阶为true

if句子让流程下定决心与否继续执行特定的句子或句子块,而if else句子则让流程下定决心继续执行两条句子或句子块中的哪两条,这种句子对于优先选择其中一种操作形式很有用。

if else句子的通用文件格式如下:

if(test-condition) statement1else statement2

C++对if else进行扩展提供了两个以上优先选择的功能:

if(test-condition1) statement1else if(test-condition2) statement2else 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(!

!操作形式符将它后面的表达式的真值取反。

!操作形式符的优先级高于所有的关系操作形式符和算术操作形式符。

形式论操作形式符的另一个种表示形式:

C++ Primer Plus学习笔记之分支语句和逻辑运算符

三、字符函数库cctype

C++从C词汇继承了一个与字符相关的、非常方便的函数软件包,它能简化诸如确定字符与否为大写字母、数字、标点符号等工作,这些函数的原型是在头文件cctype(老式的凤格中为ctype.h)中定义的。

cctype中的字符函数:

C++ Primer Plus学习笔记之分支语句和逻辑运算符

四、?:操作形式符

C++有一个常被用来代替if else句子的操作形式符,这个操作形式符被称为前提运算符(?:),它是C++中唯一一个需要3个操作形式数的操作形式符。该操作形式符的通用文件格式如下:

expression1 ? expression2 :expression 3

假如expression1true,则整个前提表达式的值为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要是一个结果为整数值的表达式。另外,每个标签都要是整数常量表达式。最常见的标签是intchar常量,也能是枚举量。假如integer-expression不与任何标签匹配,则流程将跳到标签为default的那一行。Deault标签是可选的,假如被省略,而又没有匹配的标签,则流程将跳到switch后面的句子处继续执行。

C++中的case标签只是行标签,而不是选项之间的界线。也是说,流程跳到switch中特定代码行后,将依次继续执行之后的所有句子,除非有明确的其他指示,流程不会在继续执行到下一个case处自动停止,要让流程继续执行完一组特定句子后停止,要采用break句子。这将导致流程跳到switch后面的句子处继续执行。

六、breakcontinue句子

breakcontinue句子都使流程能够跳过部分代码。能在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/Oint main(){ using namespace std; char automobile[50]; int year; double a_price; double d_price; ofstream outFile; // create object for output outFile.open(“e:\\1.txt”); // associate with a file 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;// display information on screen with cout cout << fixed;//按浮点型进行输出。 cout.precision(2);//控制浮点精度为2,即保留小数点位数 cout.setf(ios_base::showpoint);//采用默认的浮点文件格式时,上述句子还将导致末尾的0被显示出来。 cout << “Make and model: ” << automobile << endl; cout << “Year: ” << year << endl; cout << “Was asking $” << a_price << endl; cout << “Now asking $” << d_price << endl;// now do exact same things using outFile instead of cout 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(); // 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 << “Enter name of data file: “; cin.getline(filename, SIZE); inFile.open(filename); // associate inFile with a file if (!inFile.is_open()) // failed to open file { cout << “Could not open the file ” << filename << endl; cout << “Program terminating.\n”; // 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 << “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(); // finished with the file // cin.get(); return 0;}
举报/反馈

资源下载此资源下载价格为2.88B,包年VIP免费,请先
2405474279

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务