zoukankan      html  css  js  c++  java
  • C++ Primer Plus 笔记第六章

    if语句:

       if ( test_condition )

        statement

    逻辑表达式:

       C++提供了3种逻辑操作符,来组合或修改已有的表达式:

        逻辑OR ( || )、逻辑AND ( && )、逻辑NOT ( ! )

    逻辑OR操作符:||

      表达式中任何一个或全部都为true(或非零),得到表达式值为true;

       i++ < 6 || i == j:

        ||操作符是一个顺序点,设 i 值为10,则 i 和 j 进行比较时值为 11;

        当左侧为 true 时,则C++不会去判断右侧的表达式;

        另外:冒号和逗号也是顺序点

    逻辑 AND 操作符:&&

       仅当两个表达式都为 true 时,得到的表达式的值才为 true;

       和 || 一样,&& 也是顺序点;

       用 && 来设置取值范围:

         if (age > 17 && age < 35)

    逻辑 NOT 操作符:!

       !将操作符后面的表达式的值取反;

    逻辑操作符细节:

       C++逻辑操作符OR和逻辑操作符AND操作符的优先级低于关系操作符;

       !操作符高于所有个关系操作符和算数操作符;

       逻辑 AND 操作符的优先级高于逻辑 OR 操作符;

       C++确保程序从左向右进行计算逻辑表达式,并在知道答案后立刻停止

    字符函数库cctype:

      C++从C语言继承了一个与字符相关的、非常方便的函数软件包,可以简化诸如确定字符是否为大写字母、数字、标点符号等工作;

       使用函数比使用 AND 和 OR 操作符更方便:( 包含头文件<cctype> )

    1 // 使用 AND 和 OR 操作符 测试 ch 是否是字母字符
    2 if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
    3 
    4 // 使用 isalpha() 
    5 if ( isalpha(ch) )

      cctype 中的字符函数:

    函数名 返回值
    isalnum() 如果参数是字母数字,即字母或数字,返回值为true
    isalpha() 如果参数是字母,返回值是true
    isblank() 如果参数是空格或水平制表符,该函数返回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  :  expression3 

       如果 expression1 的值为 true ,则整个条件表达式的值为 expression2;否则,整个表达式的值为 expression3

       eg :  int c = a > b ? a : b;    // c 等于 a 、b 中的较大的一个

    switch 语句:

      C++ 的 switch 语句就像指路盘,告诉计算机接下来应该执行那些代码;

       switch ( integer-expression )

       {

        case label1 :  statement (s)

        case label2 :  statement (s)

        .  .  .

        default  :  statement (s)

       }

       执行到 switch 语句时,程序将跳到使用 integer-expression 的值标记的那一行;

       每个标签必须是整数常量表达式:  int  char  枚举; 

       如果不与任何标签匹配,则程序跳到 default 行;

       要让程序完成一组特定的语句后停止,必须使用 break 语句

    switch 和 if else:

       switch 语句和 if else 语句都允许程序从选项中进行选择。相比之下 if else 可以处理取值范围,更通用

    break 和 continue:

       break:在 switch 语句和任何循环中使用 break 语句,使程序跳到 switch 或循环后面的语句执行;

       continue:用于循环中,让程序跳过循环体中余下的代码,并开始新一轮的循环

    读取数字的循环:

          int n;

         cin >> n;

       如果用户输入的是一个单词,而不是数字,将发生4种情况:

        1. n 的值保持不变;

        2. 不匹配的输入将被留在输入队列中;

        3. cin 对象中的一个错误标记被设置;

        4. 对 cin 方法的调用返回 false (如果被转换为 bool 类型)

       当用户输入不是数字时,程序发现输入错误时,应采取 3 个步骤

        1. 重置 cin 以接受新的输入; // cin.clear() ,清除错误标记,是的 cin 可以继续接受输入

        2. 删除错误输入;     //  while ( cin.get() != ' ') { continue; } 将输入队列里的不匹配信息删除

        3. 提示用户再输入     

       例程:

     1 #include <iostream>
     2 using namespace std;
     3 const int Max = 5;
     4 
     5 int main()
     6 {
     7     int golf[Max];
     8     cout << "Please enter your golf scores.
    ";
     9     cout << "You must enter " << Max << " rounds.
    ";
    10     int i;
    11     for (i = 0; i < Max; i++)
    12     {
    13         cout << "round #" << i + 1 << ":  ";
    14         while (!(cin >> golf[i]))//判断输入错误内容,当输入不为数字时,没有内容添加到数组 golf[i] 
                 式cin >> golf[i]返回false,取反后为 true,进入循环执行错误处理
    15 { 16 cin.clear();          // 重置输入,如果省略,程序将拒绝继续读取输入 17 while (cin.get() != ' ')  // 读取行尾之前的所有输入,从而删除这一行中的错误输入 18 continue; 19 cout << "Please enter a number: "; 20 } 21 } 22 23 double total = 0; 24 for (int j = 0; j < i; j++) 25 total += golf[j]; 26 27 cout << total / Max << " = average score " 28 << Max << " rounds "; 29 30 return 0; 31 }

    简单文件输入输出:

       C++使得将读取读取键盘输入和在屏幕上显示输出(控制台输入输出)的技巧用于文件的输入输出(文件I/O);

    写入到文本文件中:

       将cout用于控制台输出的基本事实:

        1. 必须包含头文件 iostream;

        2. 头文件 iostream 定义了一个用于处理输出的ostream类;

        3. 头文件 iostream 声明了一个名为 cout 的ostream 变量(对象);

        4. 必须指定名称空间 std;

        5. 可以结合 使用 cout 和操作符 >> 来显示各种类型数据

       文件输出与 cout 上述相似:

        1. 必须包含头文件 fstream;

        2. 头文件 fstream 定义了一个用于处理输出的 ofstream 类;

        3. 需要声明一个或多个 ofstream 变量(对象);

        4. 必须指定名称空间 std;

        5. 需要将 ofstream 对象与文件关联起来,经常使用 open();

        6. 使用完文件后,应使用 close() 方法将其关闭;

        7. 可以结合使用 ofstream 对象和操作符 >> 来输出各种类型的数据

    文件输出主要步骤:

        1. 包含头文件 fstream;

        2. 创建一个 ofstream 对象;

        3. 将该 ofstream 对象同一个文件关联起来;

        4. 像 cout 那样使用该 ostream 对象

       例程1:

     1 #include<iostream>
     2 #include<fstream>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     char automobile[50];
     9     int year;
    10     double a_price;
    11     double d_price;
    12 
    13     ofstream outFile;            // 声明 ofstream 对象 outFile
    14     outFile.open("carinfo.txt");      // 将对象 outFile 与文件carinfo.txt 关联
    15                            将输出内容添加到carinfo.txt中;
    16     cout << "Enter the make and model of automobile: ";
    17     cin.getline(automobile, 50);
    18 
    19     cout << "Enter the model year: ";
    20     cin >> year;
    21 
    22     cout << "Enter the original asking price: ";
    23     cin >> a_price;
    24     d_price = 0.913*a_price;
    25 
    26     // display information on screen with cout
    27     cout << fixed;
    28     cout.precision(2);
    29     cout.setf(ios_base::showpoint);
    30     cout << "Make and model: " << automobile << endl;
    31     cout << "Year: " << year << endl;
    32     cout << "Was asking $" << a_price << endl;
    33     cout << "Now asking $" << d_price << endl;
    34 
    35     // now do exact same things using outFile instead of cout
    36     outFile << fixed;
    37     outFile.precision(2);
    38     outFile.setf(ios_base::showpoint);
    39     outFile << "Make and model: " << automobile << endl;
    40     outFile << "Year: " << year << endl;
    41     outFile << "Was asking $" << a_price << endl;
    42     outFile << "Now asking $" << d_price << endl;
    43 
    44     outFile.close();
    45     return 0;
    46 
    47 }

    读取文本文件:

      将 cin 用于控制台输入的基本事实:

        1. 必须包含头文件 iostream;

        2. 头文件 iostream 定义了一个用于处理输入的 istream 类;

        3. 头文件声明了一个名为 cin 的变量(对象);

        4. 必须指明名称空间 std;

        5. 可以结合使用 cin 和操作符 << 来读取各种类型的数据;

        6. 可以使用 cin 和 get() 方法来读取一个字符,使用 cin 和 getline() 来读取一行字符;

        7. 可以结合使用 cin 和 eof()、fail() 方法来判断输入是否成功

        8. 对象 cin 本身被用作测试条件时,如果最后一个读取操作成功,它将被转换为 true,否则转换成false

       文件输出与 cin 上述相似:

        1. 必须包含头文件 fstream;

        2. 头文件 fstream 定义了一个用于处理 ifstream 类;

        3. 需要声明一个或多个 ifstream 变量(对象);

        4. 必须指明名称空间 std;

        5. 需要将 ifstream 对象与文件关联起来。方法一使用 open();

        6. 使用完文件后,应使用 close() 方法将其关闭;

        7. 可以结合使用 ifstream 对象和操作符 << 来读取各种类型的数据;

        8. 可以使用 ifstream 对象和 get() 方法来读取一个字符,使用 ifstream 对象和 getline() 来读取一行字符;

        9. 可以结合使用 ifstream 和 eof()、fail() 等方法来判断输入是否成功;

        10. ifstream 对象本身被用作测试条件时,如果最后一个读取操作成功,它将被转换成布尔值 true,否则被转换成 false 

       例程2:

     1 #include<iostream>
     2 #include<fstream>
     3 #include<cstdlib>
     4 const int SIZE = 60;
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     char filename[SIZE];
    10     ifstream inFile;
    11 
    12     cout << "Enter name of data file: ";
    13     cin.getline(filename, SIZE);
    14     inFile.open(filename);                // 将对象 inFile与文件关联起来
    15 
    16     if (!inFile.is_open())
    17     {
    18         cout << "Could not open the file " << filename << endl;
    19         cout << "Program terminating.
     ";
    20         exit(EXIT_FAILURE);
    21     }
    22     double value;
    23     double sum = 0.0;
    24     int count = 0;
    25     /*
    26     inFile >> value;
    27     while (inFile.good())
    28     {
    29         ++count;
    30         sum += value;
    31         inFile >> value;
    32     }   
    33     */
    34     while (inFile >> value)  //用 ifstream 对象本身做测试条件,如果最后一个读取操作成功则为 true 
    35     {
    36         ++count;
    37         sum += value;
    38     }
    39     if (inFile.eof())
    40         cout << "End of file reached.
    ";
    41     else if (inFile.fail())
    42         cout << "Input terminated by data mismatch.
    ";
    43     else
    44         cout << "Input terminated for unknown reason.
    ";
    45     if (count == 0)
    46         cout << "No data processed.
    ";
    47     else
    48     {
    49         cout << "Items read: " << count << endl;
    50         cout << "Sum: " << sum << endl;
    51         cout << "Average: " << sum / count << endl;
    52     }
    53 
    54     inFile.close();
    55     return 0;
    56 
    57 }

    思考题:

      1. !!x 与 x 是否相同?

       答: 不一定,如果 x 是 10 则 !!x 是 1 ,如果 x 是 bool 变量则 !!x = x

      2. 创建一个表达式,其值为变量的绝对值

        答: (x > 0)? x : -x;

      

     

      

       

  • 相关阅读:
    软件工程第一次作业
    邮件服务器的搭建
    将博客搬至CSDN
    古典密码加密解密之多表代换
    Nginx + Tomcat 负载均衡配置详解
    openstack i版搭建教程
    IIS、apache、tomcat服务器虚拟主机配置
    SMTP邮件发送命令
    hadoop集群安装
    信息管理系统(java)
  • 原文地址:https://www.cnblogs.com/kidycharon/p/9677586.html
Copyright © 2011-2022 走看看