07. 分支语句

if语句

  1. if

    #include <iostream>
    
    int main()
    {
     using namespace std;
     
     char str1;
     int kongge = 0;
     int zongji = 0;
     cout << "请输入一个字符串:\n"<< endl;
     cin.get(str1);
     while (str1 != '.')  // 输入点来停止字符串输入 
     {
         if (str1 == ' ')
         {
             ++kongge;
         }
         ++zongji;
         cin.get(str1);
     }
     cout << "字符串中总共有"<< kongge << "个空格\n";
     cout << "字符串总共"<< zongji << "个字符";
     return 0;
     } 
    
  2. if...else

    #include <iostream>
    int main()
    {
     using namespace std;
     char str1;
     cout << "输入一个字符串:\n";
     cin.get(str1);
     while (str1 !='.')
     {
         if (str1 == '\n'){
             cout << str1;
         } else{
             cout << ++str1;  // 让字符后移一个 
         }
         cin.get(str1);
     }
     return 0;
     } 
    
  3. if...else if...else

    #include <iostream>
    const int TAG = 6;
    int main()
    {
     using namespace std;
     int n;
     cout << "输入一个数字:\n";
     do
     {
         cin >> n;
         if (n < TAG)
         {
             cout << "小了\n";
         }
         else if (n> TAG)
         {
             cout << "大了\n";
         } 
         else{
             cout << "猜对了";
         }
     } while (n!= TAG);
     return 0;
     } 
    

逻辑表达式

  1. 逻辑或(OR)

    1. 运算符:||
  2. 逻辑与(AND)

    1. 运算符:&&

    2. 使用&&来设置取值范围

      #include <iostream>
      const char * a[4] = 
      {
        "yes",
        "no",
        "ok",
        "haha"
      };
      int main()
      {
        using namespace std;
        int i;
        cout << "输入一个数字:";
        cin >> i;
        int j;
        
        if (i> 18 && i < 35){
            j = 0;
        } else if(j>=35 && i <=50){
            j = 1;
        } else{
            j = 3;
        }
        cout << "结果是:" << a[j];
        return 0;
        
      }
      
  3. 逻辑非(NOT)

    1. 运算符:!
  4. 优先级

    1. 与和或运算符的优先级都低于关系运算符
    2. 非运算符的优先级高于所有的关系运算符和算数运算符,所以要对表达式求反,就需要使用括号

cctype库

  1. 例子

    #include <iostream>
    #include <cctype>
    
    int main()
    {
     using namespace std;
     cout << "输入一个字符串,输入@号表示停止\n";
     
     char str1;
     
     int spaces = 0;
     int digits = 0;
     int chars = 0;
     int others = 0;
     
     cin >> str1; 
     while (str1 != '@' )
     {
         if (isalpha(str1)){
             chars++;
         } else if (isspace(str1)){
             spaces++;
         } else if(isdigit(str1)){
             digits++;
         }else{
             others++;
         }
         cin >> str1;
     }
     cout << "空格数量是:"<<spaces << endl;
     cout << "数字数量是:"<<digits << endl;
     cout << "字母数量是:"<<chars << endl;
     cout << "其他字符数量是:"<<others << endl;
     
     return 0;
     } 
    

?:运算符

  1. c++中唯一的一个三元运算符

  2. 语法格式:表达式1?表达式2:表达式3

    • 当表达式1是true的时候,则取表达式2的值,否则就取表达式3的值
  3. 例子

    #include <iostream>
    int main()
    {
     using namespace std;
     int a,b;
     cout << "输入两个整数:"<<endl;
     cin >> a>> b;
     int c = a > b ? a : b;   // 判断如果a大于b,c的值就是a,否则就是b 
     
     cout << "较大的那个数字是:"<<c << endl;
     return 0;
    }
    

switch语句

  1. 语法格式

    switch (integet-expression)
    {
        case label1:statement
        case label2:statement
        ...    
        default :statement
            
    }
    
  2. 例子

    #include <iostream>
    using namespace std;
    void showmenu();
    void report();
    void comfort();
    int main()
    {
     showmenu();
     int choice;
     cin >> choice;
     while (choice !=5)
     {
         switch(choice){
             case 1:cout <<"金枪鱼\n";
             break;
             
             case 2:report();
             break;
             
             case 3:cout<<"炒饭\n";
             break;
             
             case 4:comfort();
             break;
             
             default :cout <<"请点菜单上有的\n"; 
         }
         showmenu();
         cin >> choice;
     }
     cout << "再见\n";
     return 0;
    }
    
    void showmenu()
    {
     cout << "请输入1,2,3,4,5\n";
    }
    
    void report()
    {
     cout << "这周的牛肉贼好吃.\n";
    }
    
    void comfort()
    {
     cout << "这周的三文鱼不太行.\n"; 
     } 
    
  3. 将枚举量用作标签

    • 例子

      #include <iostream>
      enum {
         red,orange,yellow,green,blue,white,black
      };
      
      int main()
      {
         
         using namespace std;
         cout << "输入颜色编码:";  // 这里只能输入0-6.因为枚举对应的值就是0-6 
         int code;
         cin >> code;
         while (code >= red && code <= black){
             switch(code)
             {
                 case red : cout << "红色\n";
                 break;
                 
                 case orange:cout << "橙色\n";
                 break;
                 
                 
                 case yellow:cout << "黄色\n";
                 break;
                 
                 
                 case green:cout << "绿色\n";
                 break;
                 
                 
                 case blue:cout << "蓝色\n";
                 break;
                 
                 
                 case white:cout << "白色\n";
                 break;
                 
                 
                 case black:cout << "黑色\n";
                 break;
             }
             cout << "输入颜色编码:"; 
             cin >> code;
         }
         
         cout << "退出";
         return 0;
      }
      

break和continue

  1. 在for循环中,continue语句使程序直接跳到更新表达式的位置,然后跳到测试表达式位置
  2. 在while循环中,continue跳到测试表达式的位置,所以continue之后的更新表达式都会被跳过

文件的I/O

  1. 写入文件

    1. 例子

      #include <iostream>
      #include <fstream>
      
      int main()
      {
        using namespace std;
        
        char autoindex[50];
        int year;
        double a_price;
        double b_price;
        
          
        ofstream outFile;  // 创建一个用于输出的对象
        outFile.open("E:\\CppDemo\\test.txt");
        
        cout << "输入内容:";
        cin.getline(autoindex,50);
        
        cout << "输入年份:";
        cin >> year;
        
        cout << "输入原价:";
        cin >> a_price;
        
        b_price = 0.93*a_price;
        
        cout << fixed;
        cout.precision(2);
        cout.setf(ios_base::showpoint);
        
          // 使用outFile来输出对象
        outFile<<fixed;
        outFile.precision(2);
        outFile.setf(ios_base::showpoint);
        outFile << "autoindex的值是:"<<autoindex<< endl;
        outFile << "年份:"<<year<< endl;
        outFile << "原价是:"<<a_price<< endl;
        outFile << "折扣价是:"<<b_price<< endl;
        
        outFile.close(); 
        
        return 0;
       } 
      
  2. 读取文件

    1. 例子

      #include <iostream>
      #include <fstream>
      #include <cstdlib>
      const int SIZE = 60;
      int main()
      {
        
        using namespace std;
        
        char filename[SIZE];
        ifstream inFile;
        
        cout << "输入文件名字:";
        cin.getline(filename,SIZE);  // 输入完整路径 
        inFile.open(filename);
        
        if(!inFile.is_open())// 如果文件打不开 
        {  
            cout << "打不开文件"<< endl;
            cout << "程序结束" << endl;
            exit (EXIT_FAILURE); 
        }
        
        double value;
        double sum = 0.0;
        int count = 0;
        
        inFile >> value;
        while (inFile.good())
        {
            ++count;
            sum+=value;
            inFile >> value;
        }
        if (inFile.eof())
        {
            cout << "到达文件终点(end of file)"; 
        } else if(inFile.fail())
        {
            cout << "读取失败了";
        } else{
            cout << "未知错误";
        }
        if (count == 0)
        {
            cout <<"没有数据";
        }else{
            cout << "读取的项目数量:"<< count << endl;
            cout <<"总数:"<< sum << endl;
            cout << "平均数是"<< sum/count << endl;
        }
        inFile.close();
        return 0;
      }
      
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容