C++ 速查手冊 V1.00 - 單元 4 範例




u04_1.cpp


#include <iostream>

int main() {
   if (1) {
      std::cout << "Hello, world!" 
                << std::endl;
   }
    
   return 0;
}
 
/* Kaiching Chang
   u04_1.cpp
   2014-02 */

u04_2.cpp


#include <iostream>
#include <cmath>
 
int main() {
   int a = -99;
   int b = abs(a);
    
   std::cout << "a: " 
             << a 
             << std::endl;
   std::cout << "abs(a): " 
             << b 
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u04_2.cpp
   2014-02 */

u0401_1.cpp


#include <iostream>
 
int main() {
   int a, b, c;

   a = 99;
   b = 36;
    
   c = a + b;
   std::cout << "a + b = " 
             << c 
             << std::endl;
    
   c = a - b;
   std::cout << "a - b = " 
             << c 
             << std::endl;
    
   c = a * b;
   std::cout << "a * b = " 
             << c 
             << std::endl;

   c = a / b;
   std::cout << "a / b = " 
             << c 
             << std::endl;
    
   c = a % b;
   std::cout << "a % b = " 
             << c 
             << std::endl;
    
   return 0;
}
 
/* Kaiching Chang
   u0401_1.cpp
   2014-02 */

u0401_2.cpp


#include <iostream>
 
int main() {
   double a, b, c;

   a = 9.9;
   b = 3.6;
    
   c = a + b;
   std::cout << "a + b = " 
             << c 
             << std::endl;
    
   c = a - b;
   std::cout << "a - b = " 
             << c 
             << std::endl;
    
   c = a * b;
   std::cout << "a * b = " 
             << c 
             << std::endl;

   c = a / b;
   std::cout << "a / b = " 
             << c 
             << std::endl;
    
   return 0;
}
 
/* Kaiching Chang
   u0401_2.cpp
   2014-02 */

u0401_3.cpp


#include <iostream>
 
int main() {
   char t = 'T';
     
   std::cout << "before: " 
             << t 
             << std::endl;
     
   t = t + 32;
   std::cout << "after : " 
             << t 
             << std::endl;
     
   return 0;
}
 
/* Kaiching Chang
   u0401_3.cpp
   2014-02 */

u0402_1.cpp


#include <iostream>
 
int main() {
   int a = 1;
    
   std::cout << a++ 
             << std::endl;
   std::cout << ++a 
             << std::endl;
   std::cout << a++ 
             << std::endl;
   std::cout << a 
             << std::endl; 
     
   return 0;
}
 
/* Kaiching Chang
   u0402_1.cpp
   2014-02 */

u0402_2.cpp


#include <iostream>

int main() {
   int a = 1;

   std::cout << a-- 
             << std::endl;
   std::cout << --a 
             << std::endl;
   std::cout << a-- 
             << std::endl;
   std::cout << a 
             << std::endl;

   return 0;
}

/* Kaiching Chang
   u0402_2.cpp
   2014-02 */

u0402_3.cpp


#include <iostream>

int main() {
   double a = 1.2;

   std::cout << a++ 
             << std::endl;
   std::cout << ++a 
             << std::endl;
   std::cout << a++ 
             << std::endl;
   std::cout << a 
             << std::endl; 
     
   return 0;
}

/* Kaiching Chang
   u0402_3.cpp
   2014-02 */

u0402_4.cpp


#include <iostream>

int main() {
   double a = 1.2;

   std::cout << a-- 
             << std::endl;
   std::cout << --a 
             << std::endl;
   std::cout << a-- 
             << std::endl;
   std::cout << a 
             << std::endl; 

   return 0;
}

/* Kaiching Chang
   u0402_4.cpp
   2014-02 */

u0402_5.cpp


#include <iostream>

int main() {
   char var = 'a';
    
   while (var <= 'z') {
      std::cout << var++;
   }
   std::cout << std::endl;
    
   var -= 1;
   while (var >= 'a') {
      std::cout << var--;
   }
   std::cout << std::endl;
    
   return 0;
}

/* Kaiching Chang
   u0402_5.cpp
   2014-02 */

u0403_1.cpp


#include <iostream>
 
int main() {
   int a = 12;
   int b = 22;
    
   if (a < b) {
      std::cout << "a < b" << std::endl;
   }
    
   if (a <= b) {
      std::cout << "a <= b" << std::endl;
   }
    
   if (a > b) {
      std::cout << "a > b" << std::endl;
   }
    
   if (a >= b) {
      std::cout << "a >= b" << std::endl;
   }
    
   if (a == b) {
      std::cout << "a == b" << std::endl;
   }

   if (a != b) {
      std::cout << "a != b" << std::endl;
   }

   return 0;
}
 
/* Kaiching Chang
   u0403_1.cpp
   2014-02 */

u0403_2.cpp


#include <iostream>
 
int main() {
    double a = 22.0;
    double b = 22.0;
    
    if (a < b) {
        std::cout << "a < b" 
                  << std::endl;
    }
    
    if (a <= b) {
        std::cout << "a <= b" 
                  << std::endl;
    }
    
    if (a > b) {
        std::cout << "a > b" 
                  << std::endl;
    }
    
    if (a >= b) {
        std::cout << "a >= b" 
                  << std::endl;
    }
    
    if (a == b) {
        std::cout << "a == b" 
                  << std::endl;
    }

    if (a != b) {
        std::cout << "a != b" 
                  << std::endl;
    }

    return 0;
}
 
/* Kaiching Chang
   u0403_2.cpp
   2014-02 */

u0403_3.cpp


#include <iostream>
 
int main() {
   char a = 't';
   char b = 'r' + 2;
 
   if (a < b) {
      std::cout << "a < b" 
                << std::endl;
   }
    
   if (a <= b) {
      std::cout << "a <= b" 
                << std::endl;
   }

   if (a > b) {
      std::cout << "a > b" 
                << std::endl;
   }

   if (a >= b) {
      std::cout << "a >= b" 
                << std::endl;
   }

   if (a == b) {
      std::cout << "a == b" 
                << std::endl;
   }

   if (a != b) {
      std::cout << "a != b" 
                << std::endl;
   }

   return 0;
}
 
/* Kaiching Chang
   u0403_3.cpp
   2014-02 */

u0403_4.cpp


#include <iostream>

int main() {
   bool a = true;
   bool b = false;

   if (a < b) {
      std::cout << "a < b" 
                << std::endl;
   }

   if (a <= b) {
      std::cout << "a <= b" 
                << std::endl;
   }

   if (a > b) {
      std::cout << "a > b" 
                << std::endl;
   }

   if (a >= b) {
      std::cout << "a >= b" 
                << std::endl;
   }

   if (a == b) {
      std::cout << "a == b" 
                << std::endl;
   }

   if (a != b) {
      std::cout << "a != b" 
                << std::endl;
   }

   return 0;
}

/* Kaiching Chang
   u0403_4.cpp
   2014-02 */

u0404_1.cpp


#include <iostream>
 
int main() {
   bool a = false;
   int b = 0;
   double c = 0.0;

   std::cout << a 
             << std::endl;
   std::cout << !a 
             << std::endl;
   std::cout << b 
             << std::endl;
   std::cout << !b 
             << std::endl;
   std::cout << c 
             << std::endl;
   std::cout << !c 
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0404_1.cpp
   2014-02 */

u0404_2.cpp


#include <iostream>
 
int main() {
   bool a = false;
   bool b = true;
   int c = 1;
   int d = 0;
    
   std::cout << (a && b) 
             << std::endl;
   std::cout << (a && a) 
             << std::endl;
   std::cout << (b && b) 
             << std::endl;
   std::cout << (c && c) 
             << std::endl;
   std::cout << (c && d) 
             << std::endl;
   std::cout << (d && d) 
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0404_2.cpp
   2014-02 */

u0404_3.cpp


#include <iostream>
 
int main() {
   bool a = false;
   bool b = true;
   int c = 1;
   int d = 0;
    
   std::cout << (a || b) 
             << std::endl;
   std::cout << (a || a) 
             << std::endl;
   std::cout << (b || b) 
             << std::endl;
   std::cout << (c || c) 
             << std::endl;
   std::cout << (c || d) 
             << std::endl;
   std::cout << (d || d) 
             << std::endl;

    return 0;
}
 
/* Kaiching Chang
   u0404_3.cpp
   2014-02 */

u0405.cpp


#include <iostream>

int main() {
   int a = 192;
   int b = 64;

   std::cout << (~a)
             << std::endl;
   std::cout << (b << 2)
             << std::endl;
   std::cout << (b >> 2)
             << std::endl;
   std::cout << (a & b)
             << std::endl;
   std::cout << (a | b)
             << std::endl;
   std::cout << (a ^ b)
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0405.cpp
   2014-02 */

u0406.cpp


#include <iostream>

int main() {
   std::cout << "bool : "
             << sizeof(bool)
             << std::endl;
   std::cout << "char : "
             << sizeof(char)
             << std::endl;
   std::cout << "int : "
             << sizeof(int)
             << std::endl;
   std::cout << "double : "
             << sizeof(double)
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0406.cpp
   2014-02 */

u0407.cpp


#include <iostream>

int main() {
   std::cout << "bool : "
             << typeid(bool).name()
             << std::endl;
   std::cout << "short : "
             << typeid(short).name()
             << std::endl;
   std::cout << "int : "
             << typeid(int).name()
             << std::endl;
   std::cout << "double : "
             << typeid(double).name()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0407.cpp
   2014-02 */

u0408.cpp


#include <iostream>

struct Demo1 {};

struct Demo2 {
   int f2;
   float f1;
   char c;
};

int main() {
   std::cout << "Demo1: "
             << alignof(Demo1)
             << std::endl;
   std::cout << "pointer: "
             << alignof(int*)
             << std::endl;
   std::cout << "char: "
             << alignof(char)
             << std::endl;
   std::cout << "Demo2: "
             << alignof(Demo2)
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0408.cpp
   2014-02 */

u0409_1.cpp


#include <iostream>
 
int main() {
   int a = 4;
   int b = 3;

   std::cout << (a += b)
             << std::endl;
   std::cout << (a -= b)
             << std::endl;
   std::cout << (a *= b)
             << std::endl;
   std::cout << (a /= b)
             << std::endl;
   std::cout << (a %= b)
             << std::endl;

   return 0;
}
 
/* Kaiching Chang
   u0409_1.cpp
   2014-02 */

u0409_2.cpp


#include <iostream>

int main() {
   int a = 181;
   int b = 192;

   std::cout << (a <<= 2)
             << std::endl;
   std::cout << (a >>= 2)
             << std::endl;
   std::cout << (a &= b)
             << std::endl;
   std::cout << (a |= b)
             << std::endl;
   std::cout << (a ^= b)
             << std::endl;

   return 0;
}

/* Kaiching Chang
   u0409_2.cpp
   2014-02 */

u0410_1.cpp


#include <iostream>

int main() {
   int a = 22;
   int b = 34;

   std::cout << "a = "
             << a
             << std::endl;
   std::cout << "b = "
             << b
             << std::endl;
   std::cout << (a > b ? a : b)
             << " is bigger"
             << std::endl;

   return 0;
}

/* Kaiching Chang
   u0410_1.cpp
   2014-02 */

u0410_2.cpp


#include <iostream>

int main() {
   int sex1 = 0;
   int sex2 = 1;
    
   std::cout << (sex1 ? "She" : "He")
             << " is here."
             << std::endl;
   std::cout << (sex2 ? "She" : "He")
             << " is running."
             << std::endl;

   return 0;
}

/* Kaiching Chang
   u0410_2.cpp
   2014-02 */

u0410_3.cpp


#include <iostream>

int main() {
   int a = 22;
   int b = 34;
   int max;

   if (a > b) {
      max = a;
   }
   else {
      max = b;
   }
    
   std::cout << "max = " 
             << max 
             << std::endl;

   return 0;
}

/* Kaiching Chang
   u0410_3.cpp
   2014-02 */

the end

沒有留言: