C++/Language/Operator

Материал из C\C++ эксперт
Перейти к: навигация, поиск

arithmetic assignment operators

<source lang="cpp">

  1. include <iostream>

using namespace std;

int main() {

  int ans = 27;   
 
  ans += 10;              //same as: ans = ans + 10;   
  cout << ans << ", ";   
  ans -= 7;               //same as: ans = ans - 7;   
  cout << ans << ", ";   
  ans *= 2;               //same as: ans = ans * 2;   
  cout << ans << ", ";   
  ans /= 3;               //same as: ans = ans / 3;   
  cout << ans << ", ";   
  ans %= 3;               //same as: ans = ans % 3;   
  cout << ans << endl;   
  return 0;   

}


 </source>


Compound assignments

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; int main() {

  float x, y;
  cout << "Please enter a starting value:" << endl;
  cin >> x;
  cout << "Please enter the increment value:"  << endl;
  cin >> y;
  x += y;
  cout << "Multiplication. Enter a factor:"  << endl;
  cin >> y;
  
  cout << x * y;
  
  cout << "Division. Divisor: "  << endl;
  cin >> y;
  cout << x / y;
  cout << "And this is "
       << "your current number without digits after the decimal point: "
       << fixed << setprecision(0)
       << x << endl;
  return 0;

}


 </source>


conditional Operator "?"

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num;
  cout << "Enter a whole number: ";
  cin >> num;
  cout << "The number is " << ( num % 2 == 0 ? "even" : 
          "odd") << endl;
  return 0;

}


 </source>


Create an XOR using the C++ logical operators.

<source lang="cpp">

  1. include <iostream>
  2. include <cmath>

using namespace std;

int main() {

 bool p, q; 

 p = true; 
 q = true; 

 cout << p << " XOR " << q << " is " << 
   ( (p || q) && !(p && q) ) << endl; 
  

 p = false; 
 q = true; 

 cout << p << " XOR " << q << " is " << 
   ( (p || q) && !(p && q) ) << endl; 
  

 p = true; 
 q = false; 

 cout << p << " XOR " << q << " is " << 
   ( (p || q) && !(p && q) ) << endl; 
  

 p = false; 
 q = false; 

 cout << p << " XOR " << q << " is " << 
   ( (p || q) && !(p && q) ) << endl; 
  

 return 0; 

}


 </source>


Define operator a custom class: =, +

<source lang="cpp">

  1. include <iostream>

using namespace std; class myClass { public:

 int count;
 myClass operator=(myClass obj);
 friend myClass operator+(myClass ob, int i);
 friend myClass operator+(int i, myClass ob);

}; myClass myClass::operator=(myClass obj) {

 count = obj.count;
 return *this;

} // ob + int. myClass operator+(myClass ob, int i) {

 myClass temp;
 temp.count = ob.count + i;
 return temp;

} // int + ob. myClass operator+(int i, myClass ob) {

 myClass temp;
 temp.count = ob.count + i;
 return temp;

} int main() {

 myClass myObject;
 myObject.count = 10;
 cout << myObject.count << " "; 
 myObject = 10 + myObject;      
 cout << myObject.count << " "; 
 myObject = myObject + 12;      
 cout << myObject.count;
 return 0;

}

 </source>


Demonstrates built-in arithmetic operators

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

 cout << "7 + 3 = " << 7 + 3 << endl;
 cout << "7 - 3 = " << 7 - 3 << endl;
 cout << "7 * 3 = " << 7 * 3 << endl;
 cout << "7 / 3 = " << 7 / 3 << endl;
 cout << "7.0 / 3.0 = " << 7.0 / 3.0 << endl;
 cout << "7 % 3 = " << 7 % 3 << endl;
 cout << "7 + 3 * 5 = " << 7 + 3 * 5 << endl;
 cout << "(7 + 3) * 5 = " << (7 + 3) * 5 << endl;
 return 0;

}


 </source>


demonstrates remainder operator

<source lang="cpp">

  1. include <iostream>

using namespace std;

int main(){

  cout <<  6 % 8 << endl    
       <<  7 % 8 << endl    
       <<  8 % 8 << endl    
       <<  9 % 8 << endl    
       << 10 % 8 << endl;   
  return 0;   

}


 </source>


Demonstrate the modulus operator.

<source lang="cpp">


  1. include <iostream>

using namespace std;

int main() {

 int x, y; 

 x = 10; 
 y = 3; 
 cout << x << " / " << y << " is " << x / y <<  
      " with a remainder of " << x % y << endl;  

 x = 1; 
 y = 2; 
 cout << x << " / " << y << " is " << x / y << endl << 
         x << " % " << y << " is " << x % y;  

 return 0; 

}


 </source>


Demonstrate the relational and logical operators.

<source lang="cpp">

  1. include <iostream>

using namespace std;

int main() {

 int i, j; 
 bool b1, b2; 

 i = 10; 
 j = 11; 
 if(i < j) 
    cout << "i < j\n"; 
 if(i <= j) 
    cout << "i <= j\n"; 
 if(i != j) 
    cout << "i != j\n"; 
 if(i == j) 
    cout << "this won"t execute\n"; 
 if(i >= j) 
    cout << "this won"t execute\n"; 
 if(i > j) 
    cout << "this won"t execute\n"; 

 b1 = true; 
 b2 = false; 
 if(b1 && b2) 
    cout << "this won"t execute\n"; 
 if(!(b1 && b2)) 
    cout << "!(b1 && b2) is true\n"; 
 if(b1 || b2) 
    cout << "b1 || b2 is true\n"; 

 return 0; 

}


 </source>


Dot (.) operator and operator operator

<source lang="cpp">

  1. include <iostream>

using namespace std; class MyClass { public:

 MyClass(int i) { 
    val=i; 
 }
 int val;
 int doubleValue() { 
    return val+val; 
 }

}; int main() {

 int MyClass::*data;                 
 int (MyClass::*func)();             
 
 MyClass myObject1(1), myObject2(2); 
 MyClass *p1, *p2;
 
 p1 = &myObject1;                    
 p2 = &myObject2;
 cout << (p1->*func)() << " ";
 cout << (p2->*func)() << "\n";
 cout << p1->*data << " " << p2->*data << "\n";
 return 0;

}


 </source>


increment operator

<source lang="cpp">

  1. include <iostream>

using namespace std;

int main(){

  int count = 10;   
 
  cout << "count=" << count << endl;    
  cout << "count=" << ++count << endl;  
  cout << "count=" << count << endl;    
  cout << "count=" << count++ << endl;     
  cout << "count=" << count << endl;    
  return 0;   

}


 </source>


Logical Not operator, combined with the logical And operator

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int age;
  cout << "Enter your age: ";
  cin >> age;
  if (!(age > 12 && age < 65))
     cout << "Admission is free";
  else
     cout << "You have to pay";
  return 0;

}


 </source>


Logical Or operator

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int age;
  cout << "Enter your age: ";
  cin >> age;
  if (age <= 12 || age >= 65)
     cout << "Admission is free";
  else
     cout << "You have to pay";
  return 0;

}


 </source>


Match mask with bit operator

<source lang="cpp">

  1. include <iostream>

using namespace std; int mystery( unsigned ); int main() {

  unsigned x;
  cout << "Enter an integer: ";
  cin >> x;
  cout << "The result is " << mystery( x ) << endl;
  return 0;

} int mystery( unsigned bits ) {

  unsigned mask = 1 << 15, total = 0;
  for ( int i = 0; i < 16; i++, bits <<= 1 )
     if ( ( bits & mask ) == mask ) 
        ++total;
  return total % 2 == 0 ? 1 : 0;

}


 </source>


Operator in C++

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

  double x, y;
  cout << "Enter two floating-point values: ";
  cin >> x ;
  cin >> y;
  cout << "The average of the two numbers is: " << (x + y)/2.0 << endl;
  return 0;

}


 </source>


Printing an unsigned integer in bits

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; void displayBits( unsigned ); int main() {

  unsigned x;
  cout << "Enter an unsigned integer: ";
  cin >> x;
  displayBits( x );
  return 0;

} void displayBits( unsigned value ) {

  unsigned c, displayMask = 1 << 15;
  cout << setw( 7 ) << value << " = ";
  for ( c = 1; c <= 16; c++ ) {
     cout << ( value & displayMask ? "1" : "0" );
     value <<= 1;
     if ( c % 8 == 0 ) 
        cout << " ";
  }

}


 </source>


using a bit field

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; struct BitCard {

  unsigned face : 4;
  unsigned suit : 2;
  unsigned color : 1;

}; void fillDeck( BitCard * ); void deal( BitCard * ); int main() {

  BitCard deck[ 52 ];
  fillDeck( deck );
  deal( deck );
  return 0;

} void fillDeck( BitCard *wDeck ) {

  for ( int i = 0; i <= 51; i++ ) {
     wDeck[ i ].face = i % 13;
     wDeck[ i ].suit = i / 13;
     wDeck[ i ].color = i / 26;
  }

} // Output cards in two column format. Cards 0-25 subscripted // with k1 (column 1). Cards 26-51 subscripted k2 in (column 2.) void deal( BitCard *wDeck ) {

  for ( int k1 = 0, k2 = k1 + 26; k1 <= 25; k1++, k2++ ) {
     cout << "Card:" << setw( 3 ) << wDeck[ k1 ].face 
          << "  Suit:" << setw( 2 ) << wDeck[ k1 ].suit 
          << "  Color:" << setw( 2 ) << wDeck[ k1 ].color 
          << "   " << "Card:" << setw( 3 ) << wDeck[ k2 ].face
          << "  Suit:" << setw( 2 ) << wDeck[ k2 ].suit 
          << "  Color:" << setw( 2 ) << wDeck[ k2 ].color 
          << endl;
  }

}


 </source>


Using the bitwise AND, bitwise inclusive OR, bitwise exclusive OR, and bitwise complement operators.

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; void displayBits( unsigned ); int main() {

  unsigned number1, number2, mask, setBits;
  number1 = 65535;
  mask = 1;
  cout << "The result of combining the following\n";
  displayBits( number1 );
  displayBits( mask );
  cout << "using the bitwise AND operator & is\n";
  displayBits( number1 & mask );
  number1 = 15;
  setBits = 241;
  cout << "\nThe result of combining the following\n";
  displayBits( number1 );
  displayBits( setBits );
  cout << "using the bitwise inclusive OR operator | is\n";
  displayBits( number1 | setBits );
  return 0;

} void displayBits( unsigned value ){

  unsigned c, displayMask = 1 << 15;
  cout << setw( 7 ) << value << " = ";
  for ( c = 1; c <= 16; c++ ) {
     cout << ( value & displayMask ? "1" : "0" );
     value <<= 1;
     if ( c % 8 == 0 )
        cout << " ";
  }

}


 </source>


Using the bitwise shift operators

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; void displayBits( unsigned ); int main() {

  unsigned number1 = 960;
  cout << "The result of left shifting\n";
  displayBits( number1 );
  cout << "8 bit positions using the left "
       << "shift operator is\n";
  displayBits( number1 << 8 );
  cout << "\nThe result of right shifting\n";
  displayBits( number1 );
  cout << "8 bit positions using the right "
       << "shift operator is\n";
  displayBits( number1 >> 8 );
  return 0;

} void displayBits( unsigned value ) {

  unsigned c, displayMask = 1 << 15;
  cout << setw( 7 ) << value << " = ";
  for ( c = 1; c <= 16; c++ ) {
     cout << ( value & displayMask ? "1" : "0" );
     value <<= 1;
     if ( c % 8 == 0 )
        cout << " ";
  }

}


 </source>