C ++ Primer Plus Chapter VI

This chapter first describes if

Then introduced if else

Then introduced if else if else

Then introduced if, else if

These are not written. . . . Python are about the same thing, figured,

6.2 logical expression

The corresponding OR (||)

AND 对应(&&)

AND to note is that a relevant range of values, as follows:

// test ranges each section using an AND operator to combine two complete relational expression:

if (age > 17 && age < 35) // OK

// Do not use mathematical symbols be expressed as:

if (17 < age < 35)

// compiler will not catch this error because he is effective c ++ syntax, but this is the real meaning of the following operations:

if ((17 < age) < 35)

// only judge the value front, no matter how kind the value front, always less than 35. . . So the result is always to true
the NOT correspond (!)

The python's just not the same, just! Nothing more

6.2.5 Details logic operations

! About priority to note is that the operator is higher than all the relational operators and arithmetic operators, so as follows:

!(x > 5)
!x > 5

Clearly this second // first determine x, can be converted to true, false, or it is a 0, so the second always to false!
It is noted above that the logical operators AND OR, as follows:

age >30 && age < 45 ||weight > 300 //将被解释为
(age >30 && age < 45) ||weight > 300

(age >30 || age < 45) && weight > 300  //如果先用OR很明显,必须要括括号

6.3 character library cctype

This is a function package

Record what method, easy to use

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()  //如果参数是小写字符,则返回大写,否则返回该参数

6.4:? Operator

In fact, if else python also has a similar,

5 > 3 ? 10 : 12  //如果5>3是true,那么值是10,否则就是12

6.5 switch

This is a big collection, and then where he wanted to perform on the implementation of the statement where behind

    switch(choice)
    {
       case 1: cout<< "text";
               break;
       case 2: cout<< "text";
               break;
       case 3: cout<< "text";
               break;
    }

// 但是如果case 1这种后边没有跟break,它会继续执行下边一条,知道遇到break
break   continue就跟python一样了,不写了

6.8 Simple file input / output

6.8.2 written to a text file

Note that although the header file iostream provides a good object called cout ostream predefined, but you must declare your own ofsream objects, give it a name and associate it with the file, the following demonstrates how to declare such objects.

ofstream outFile;
ofstream fount;
outFile.open("fish.txt");
char filename[50];
cin >> filename;
fout.open(filename);

Note that the method open () accepts a C- style string as a parameter, which may be a literal string, or a string stored in the array.

The following demonstrates how to use such objects:

double wt = 125.8;
outFile << wt;
char line[81] = "Object are closer than they appear.";
fount << line << endl;

Importantly, after declaring a ofstream object and associate it with a file, you can use it just like that cout, cout can be used for all operations and methods (such as <<, endl and setf ()) can be used ofstream objects (e.g. outFile and fout in the foregoing example)

In short, the main steps to use the output file as follows:

1. include the header file fstream

2. Create a ofstream

3. The ofstream objects associated with a file

4. As with cout like to use the ofstream objects

The following presentations:

// 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("carinfo.txt");            // associate with a file

	cout << "Enter the make and model of automobile:";
	cin.getline(automobile, 50);
	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);
	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;

// now do exact 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
	return 0;
}
6.8.3 

Read a text file

Almost all the same, outFile changed inFile;

ifstream inFile;
ifstream fin;
inFile.open("bowling.txt");

Note that the close () method does not need to use the file name as an argument, because outFile already. Was associated with a particular file if you forget to close the file, the program terminates normally it will automatically shut down.

Warning: open an existing file to accept output, by default it will truncate its length is zero, so the original content will be lost.

to sum up:

After using the program guide to select different statements of operations, procedures and coding will be more interesting (if it can also cause programmers' interest?) C ++ provides an if statement, if else statements and switch statements to manage selection, the program has an if statement execute a statement or condition statement block. That is, if certain conditions are met, the program statement or statements that perform particular block, is else run the program statement or statements one of the two blocks of statements, if else may be added after this statement. To provide a range of options, switch statements, guide the execution of a series of options.

C ++ also provides help decision-making operator. Section V discusses the relational expression, the expression of two such values ​​are compared, and if else statements IS relational expression is generally used as a test condition by using logical operators (&&, || ,!), can be combined or modify relational expression, to create a more detailed test, conditional operator (? :) offers a choice of one of two values ​​concise way.

cctype character library provides a convenient and powerful tool that can be used to analyze character input.

For file i / o, the select statement cycle and make a very useful tool, i file / o console i / o very similar statement ifstream and ofstream objects and associate them with the file, it can be like edge use cin and cout as the use of these objects.

Using loops and decision-making statements, you can write an interesting, intelligent, and powerful program

Guess you like

Origin blog.csdn.net/u013693952/article/details/90726912