本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

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




u08_1.cpp


#include <iostream>

void do_something() {
   std::cout << "hello"
             << std::endl;
}
 
int main() {
   do_something();
   return 0;
}
 
/* Kaiching Chang 
   u08_1.cpp
   2014-02 */

u08_2.cpp


#include <iostream>

double power(double x, int n) {
   double result = 1.0;

   if (n > 0) {
      for (int i = 0; i < n; i++) {
         result *= x;
      }
   }
   else {
      for (int i = 0; i < -n; i++) {
         result /= x;
      }
   }
    
   return result;
}

int main() {
   for (int i = -6; i < 7; i++) {
      std::cout << power(5.0, i)
                << std::endl;
   }
   
   return 0;
}
 
/* Kaiching Chang 
   u08_2.cpp
   2014-02 */

u0801_1.cpp


#include <iostream>

int main() {
   do_something("What's truth?");
   do_something("There is no spoon.");

   return 0;
}

void do_something(char* s) {
   std::cout << s
             << std::endl;
}
 
/* Kaiching Chang 
   u0801_1.cpp
   2014-02 */

u0801_2.cpp


#include <iostream>

void do_something(char*);

int main() {
   do_something("What's truth?");
   do_something("There is no spoon.");

   return 0;
}

void do_something(char* s) {
   std::cout << s
             << std::endl;
}
 
/* Kaiching Chang 
   u0801_2.cpp
   2014-02 */

u0802_1.cpp


#include <iostream>

void do_something(int* n_ptr) {
   *n_ptr = 10;
}

int main() {
   int a = 22;
   std::cout << a 
             << std::endl;

   do_something(&a);
   std::cout << a 
             << std::endl;

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

u0802_2.cpp


#include <iostream>

int do_something2
(int* n1_ptr, int* n2_ptr) {
   *n1_ptr *= 2;
   *n2_ptr *= 2;
   return *n1_ptr + *n2_ptr;
}

int main() {
   int a = 22;
   int b = 33;
   std::cout << "a + b: "
             << do_something2(&a, &b)
             << std::endl;
   std::cout << "a: "
             << a
             << std::endl;
   std::cout << "b: "
             << b
             << std::endl;
    
    return 0;
}
 
/* Kaiching Chang 
   u0802_2.cpp
   2014-02 */

u0803_1.cpp


#include <iostream>

void do_something(int& n_ref) {
    n_ref = 10;
}

int main() {
   int a = 22;
   std::cout << a
             << std::endl;

   do_something(a);
   std::cout << a
             << std::endl;

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

u0803_2.cpp


#include <iostream>

int do_something2
(int &n1_ref, int &n2_ref) {
   n1_ref *= 2;
   n2_ref *= 2;

   return n1_ref + n2_ref;
}

int main() {
   int a = 22;
   int b = 33;
   std::cout << "a + b: "
             << do_something2(a, b)
             << std::endl;
   std::cout << "a: "
             << a
             << std::endl;
   std::cout << "b: "
             << b
             << std::endl;

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

u0804_1.cpp


#include <iostream>

void do_something(int n = 22) {
   std::cout << n
             << std::endl;
}

int main() {
   do_something(11);
   do_something();
   do_something();

   return 0;
}

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

u0804_2.cpp


#include <iostream>

void do_something2
(int n1, int n2 = 5, int n3 = 0) {
   std::cout << n1 + n2 + n3
             << std::endl;
}

int main() {
   do_something2(6);
   do_something2(6, 9);
   do_something2(3, 4, 5);

   return 0;
}

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

u0805.cpp


#include <iostream>
#include <cstdarg>

int sum(int n_args, ...) {
   va_list ap;
   va_start(ap, n_args);
   int sum = va_arg(ap, int);
   for (int i = 2; i <= n_args; i++) {
       sum += va_arg(ap, int);
   }
   va_end(ap);

   return sum;
}

int main() {
   std::cout << sum(3, 11, 22, 33)
             << std::endl;
   std::cout << sum(3, 19, 20, 21)
             << std::endl;
   std::cout << sum(3, 3, 65, 101)
             << std::endl;

   return 0;
}

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

u0806.cpp


#include <iostream>

int main() {
   auto f = [](int i) {return i * i;};
   
   std::cout << f(11)
             << std::endl;
   std::cout << f(22)
             << std::endl;
   std::cout << f(16)
             << std::endl;

   return 0;
}

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

u0807.cpp


#include <iostream>

inline int max(int a, int b) {
    return a > b ? a : b;
}

int main() {
   std::cout << max(55, 22)
             << std::endl;
   std::cout << max(2, 214)
             << std::endl;

   return 0;
}

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

u0808.cpp


#include <iostream>

void do_something
(int n1, int n2, int n3) {
   std::cout << n1 + n2 + n3
             << std::endl;
}

void do_something
(double n1, double n2, double n3) {
   std::cout << n1 + n2 + n3
             << std::endl;
}

void do_something
(int n1) {
   std::cout << n1
             << std::endl;
}

int main() {
   do_something(10);
   do_something(0.2, 4.5, 6.1);
   do_something(3, 4, 5);

   return 0;
}

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

u0809.cpp


#include <iostream>

void fun1(void) {
   std::cout << "fun1"
             << std::endl;
}
 
int fun2(int n) {
   return 0;
}
 
int main() {
   void (*f1Ptr)(void) = fun1;
   int (*f2Ptr)(int) = fun2;

   f1Ptr();
   std::cout << f2Ptr(1)
             << std::endl;

   return 0;
}

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

the end

沒有留言: