C++ 邏輯運(yùn)算符

2018-03-24 14:54 更新

學(xué)習(xí)C++ - C++邏輯運(yùn)算符

運(yùn)算符是邏輯或,寫成||;邏輯與,寫&&和邏輯非,寫成!。

邏輯或運(yùn)算符:||

OR可以指示兩種條件之一或兩者滿足要求。

邏輯或運(yùn)算符,寫入||,將兩個(gè)表達(dá)式組合成一個(gè)。

如果一個(gè)或兩個(gè)原件表達(dá)式為true或非零,則生成的表達(dá)式的值為true。

否則,表達(dá)式的值為false。

這里有些例子:

5 == 5 || 5 == 9   // true because first expression is true 
5 > 3 || 5 > 10    // true because first expression is true 
5 > 8 || 5 < 10    // true because second expression is true 
5 < 8 || 5 > 2     // true because both expressions are true 
5 > 8 || 5 < 2     // false because both expressions are false 

因?yàn)閨|具有比關(guān)系運(yùn)算符低的優(yōu)先級,您不需要在這些表達(dá)式中使用括號。

下表總結(jié)了||運(yùn)算符的工作原理。

expr1的值|| expr2

                        expr1 == true       expr1 == false 

  expr2 == true                true                 true 

  expr2 == false               true                 false 

C ++||運(yùn)算符支持快捷方式評估。

例如,考慮以下表達(dá)式:

i++ < 6 || i == j

假設(shè) i 最初的值為10。

如果表達(dá)式i ++<6是真的,C++將不會麻煩評估表達(dá)式 i== j,因?yàn)樗恍枰粋€(gè)真實(shí)的表達(dá)式來使整個(gè)邏輯表達(dá)式為真。


例子

以下代碼在if語句中使用||運(yùn)算符來檢查字符的大寫和小寫版本。


#include <iostream> 
using namespace std; 
int main() 
{ 
     cout << "Do you wish to continue? <y/n> "; 
     char ch; 
     cin >> ch; 
     if (ch == "y" || ch == "Y")             // y or Y 
          cout << "You were warned!\a\a\n"; 
     else if (ch == "n" || ch == "N")        // n or N 
          cout << "A wise choice ... bye\n"; 
     else 
         cout << "else"; 
     return 0; 
} 

上面的代碼生成以下結(jié)果。



邏輯與運(yùn)算符:&&

邏輯與運(yùn)算符&&將兩個(gè)表達(dá)式組合成一個(gè)。

如果兩個(gè)原始表達(dá)式都為true,則生成的表達(dá)式的值為true。

這里有些例子:

5 == 5 && 4 == 4   // true because both expressions are true 
5 > 3 && 5 > 10    // false because second expression is false 

下表總結(jié)了&&運(yùn)算符的工作原理。

expr1 && expr2的值

                       expr1 == true        expr1 == false 

  expr2 == true               true                 false 

  expr2 == false              false                false 

例2

以下代碼顯示如何使用&&amp;


#include <iostream> 
const int SIZE = 6; 
using namespace std; 
int main() 
{ 
     float naaq[SIZE]; 
     int i = 0; 
     float temp; 
     cout << "First value: "; 
     cin >> temp; 
     while (i < SIZE && temp >= 0) // 2 quitting criteria 
     { 
         naaq[i] = temp; 
         ++i; 
         if (i < SIZE)             // room left in the array, 
          { 
              cout << "Next value: "; 
              cin >> temp;            // so get next value 
          } 
     } 
     return 0; 
} 

上面的代碼生成以下結(jié)果。

邏輯非運(yùn)算符:!

!運(yùn)算符否定后面的表達(dá)式的真值。

如果expression為true,則!expressionis為false,反之亦然。

以下代碼顯示如何使用not運(yùn)算符。


#include <iostream>
#include <climits>
using namespace std;
bool is_int(double); 
int main()
{
    
    double num;

    cout << "Enter an integer value: ";
    cin >> num;
    while (!is_int(num))    // continue while num is not int-able
    {
        cout << "Out of range -- please try again: ";
        cin >> num;
    }
    int val = int (num);    // type cast
    cout << "You"ve entered the integer " << val << "\nBye\n";
    return 0;
}

bool is_int(double x)
{
    if (x <= INT_MAX && x >= INT_MIN)   // use climits values
        return true;
    else
        return false; 
}

上面的代碼生成以下結(jié)果。

字符函數(shù)

下面的代碼演示了cctype系列的一些函數(shù)。


#include <iostream>
#include <cctype>              // prototypes for character functions
int main(){
    using namespace std;
    cout << "Enter text for analysis, and type @ to terminate input.\n";
    char ch; 
    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int punct = 0;
    int others = 0;

    cin.get(ch);                // get first character
    while (ch != "@")            // test for sentinel
    {
        if(isalpha(ch))         // is it an alphabetic character?
            chars++;
        else if(isspace(ch))    // is it a whitespace character?
            whitespace++;
        else if(isdigit(ch))    // is it a digit?
            digits++;
        else if(ispunct(ch))    // is it punctuation?
            punct++;
        else
            others++;
        cin.get(ch);            // get next character
    }
    cout << chars << " letters, "
         << whitespace << " whitespace, "
         << digits << " digits, "
         << punct << " punctuations, "
         << others << " others.\n";
    return 0; 
}

上面的代碼生成以下結(jié)果。

測試字符的函數(shù)

下表總結(jié)了cctype包中提供的函數(shù)。

函數(shù)名稱返回值
isalnum()如果參數(shù)是字母數(shù)字(即字母或數(shù)字),則返回true。
isalpha()如果參數(shù)是字母,則返回true。
isblank()如果參數(shù)是空格或水平制表符,則返回true。
iscntrl()如果參數(shù)是控制字符,則返回true。
isdigit()如果參數(shù)是十進(jìn)制數(shù)字(0-9),則返回true。
isgraph()如果參數(shù)是除空格之外的任何打印字符,則返回true。
islower()如果參數(shù)是小寫字母,則返回true。
isprint()如果參數(shù)是任何打印字符,則返回true,包括空格。
ispunct()如果參數(shù)是標(biāo)點(diǎn)符號,則返回true。
isspace()如果參數(shù)是標(biāo)準(zhǔn)空格字符(即,空格,換頁,換行,回車,水平制表符,垂直制表符),則返回true。
isupper()如果參數(shù)是大寫字母,則返回true。
isxdigit()如果參數(shù)是十六進(jìn)制數(shù)字字符(即,0-9,a-f或A-F),則返回true。
tolower()如果參數(shù)是大寫字符,tolower()返回該字符的小寫版本;否則,它返回參數(shù)不變。
toupper()如果參數(shù)是小寫字符,則toupper()返回該字符的大寫版本;否則,它返回參數(shù)不變。
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號