本站電子書

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

C++ 入門指南 V2.00 - 單元 9 範例及練習程式碼



big_demo.cpp


#include <iostream>
 
using namespace std;

// 宣告 big() 的函數原型
int big(int, int);

// main() 由作業系統呼叫,回傳整數給作業系統 
int main(void) {
   cout << endl;
   // 呼叫 big() 並且印出回傳值
   cout << big(22, 11) << " is bigger!";
   cout << endl << endl;

   return 0;
}

// 函數 big() 的實際定義
int big(int a, int b) {
   // 判斷兩個參數的大小
   if (a > b) {
      // a 大於 b ,回傳 a
      return a;
   }
   else {
      // a 小於 b ,回傳 b
      return b;
   }
}
 
/* 檔名: big_demo.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */

print_demo.cpp


#include <iostream>
 
using namespace std;

// 宣告函數原型 
void printInt(int a);
void printNewline(void);
 
int main(void) {
   int i = 10; // 設定倒數的初值
     
   printNewline(); // 印出空白一行
   while (i > 0) {
      printInt(i); // 印出數字
      i--; // 倒數
   }
   printNewline(); // 印出空白一行
     
   return 0;
}

// 印出參數 
void printInt(int a) {
   cout << a << endl;
} 

// 印出新行 
void printNewline(void) {
   cout << endl;
} 
   
/* 檔名: print_demo.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0901.cpp


#include <iostream>
 
using namespace std;

int sum(int, int);
 
int main(void) {
   cout << endl;
   cout << "11 + 22 = " << sum(11, 22) << endl;    
   cout << "35 + 66 = " << sum(35, 66) << endl;
   cout << "29 + 18 = " << sum(29, 18) << endl;
   cout << endl << endl;
    
   return 0;
}

int sum(int a, int b) {
   return a + b;
}
 
/* 檔名: exercise0901.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0902.cpp


#include <iostream>
 
using namespace std;

int sum(int, int);
 
int main(void) {
   int a, b;
    
   cout << "請輸入第一個整數:";
   cin >> a;
   cout << "請輸入第二個整數:";
   cin >> b;
    
   cout << endl;
   cout << "兩者的和是 " << sum(a, b) << endl;    
   cout << endl << endl;
    
   return 0;
}

int sum(int a, int b) {
   return a + b;
}
 
/* 檔名: exercise0902.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0903.cpp


#include <iostream>
 
using namespace std;

int sum(int);
 
int main(void) {
   cout << endl;
   cout << "1 + 2 + ...  12 = " << sum(12) << endl;    
   cout << "1 + 2 + ...  51 = " << sum(51) << endl;
   cout << "1 + 2 + ... 100 = " << sum(100) << endl;
   cout << endl << endl;
    
   return 0;
}

int sum(int p) {
   int result = 0;
    
   for (int i = 1; i <= p; i++) {
      result += i;
   }
    
   return result;
}
 
/* 檔名: exercise0903.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0904.cpp


#include <iostream>
 
using namespace std;

int sum(int);
 
int main(void) {
   int a;
    
   cout << "請輸入整數:";
   cin >> a;
    
   cout << endl;
   cout << "1 + 2 + ...  + " << a << " = " << sum(a) << endl;    
   cout << endl << endl;
    
   return 0;
}

int sum(int p) {
   int result = 0;
    
   for (int i = 1; i <= p; i++) {
      result += i;
   }
    
   return result;
}
 
/* 檔名: exercise0904.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0905.cpp


#include <iostream>
 
using namespace std;

int factorial(int);
 
int main(void) {
   cout << endl;
   cout << "10! = " << factorial(10) << endl;    
   cout << "11! = " << factorial(11) << endl;
   cout << "20! = " << factorial(20) << endl;
   cout << endl << endl;
    
   return 0;
}

int factorial(int p) {
   int result = 1;
    
   for (int i = 1; i <= p; i++) {
      result *= i;
   }
   
   return result;
}
 
/* 檔名: exercise0905.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0906.cpp


#include <iostream>
 
using namespace std;

int factorial(int);
 
int main(void) {
   cout << endl;
   cout << "10! = " << factorial(10) << endl;    
   cout << "11! = " << factorial(11) << endl;
   cout << "20! = " << factorial(20) << endl;
   cout << endl << endl;
    
   return 0;
}

int factorial(int p) {
   if (p == 1) {
      return p;
   }
   else {
      return p * factorial(p - 1);
   }
}
 
/* 檔名: exercise0906.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0907.cpp


#include <iostream>
 
using namespace std;

int factorial(int);
 
int main(void) {
   int a;
    
   cout << "請輸入整數:";
   cin >> a;
    
   cout << endl;
   cout << a << "! = " << factorial(a) << endl;    
   cout << endl << endl;
    
   return 0;
}

int factorial(int p) {
   if (p == 1) {
      return p;
   }
   else {
      return p * factorial(p - 1);
   }
}
 
/* 檔名: exercise0907.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0908.cpp


#include <iostream>
 
using namespace std;

int fibonacci(int);
 
int main(void) {
   cout << endl;
   cout << "f(20) = " << fibonacci(20) << endl;    
   cout << "f(30) = " << fibonacci(30) << endl;
   cout << "f(40) = " << fibonacci(40) << endl;
   cout << endl << endl;
    
   return 0;
}

int fibonacci(int n) {
   int f0 = 0;
   int f1 = 1;
   int f2;
    
   if (n == 0) {
      return f0;
   }
   else if (n == 1) {
      return f1;
   }
   else {
      for (int i = 2; i <= n; i++) {
         f2 = f0 + f1;
         f0 = f1;
         f1 = f2;
      }
       
      return f2;
   }
}
 
/* 檔名: exercise0908.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0909.cpp


#include <iostream>
 
using namespace std;

int fibonacci(int);
 
int main(void) {
   cout << endl;
   cout << "f(20) = " << fibonacci(20) << endl;    
   cout << "f(30) = " << fibonacci(30) << endl;
   cout << "f(40) = " << fibonacci(40) << endl;
   cout << endl << endl;
    
   return 0;
}

int fibonacci(int n) {
   if (n == 0) {
      return 0;
   }
   else if (n== 1) {
      return 1;
   }
   else {
      return fibonacci(n - 2) + fibonacci(n - 1);
   }
}
 
/* 檔名: exercise0909.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise0910.cpp


#include <iostream>
 
using namespace std;

int fibonacci(int);
 
int main(void) {
   int a;
    
   cout << "請輸入整數:";
   cin >> a;
    
   cout << endl;
   cout << "f(" << a << ") = " << fibonacci(a) << endl;    
   cout << endl << endl;
    
   return 0;
}

int fibonacci(int n) {
   if (n == 0) {
      return 0;
   }
   else if (n== 1) {
      return 1;
   }
   else {
      return fibonacci(n - 2) + fibonacci(n - 1);
   }
}
 
/* 檔名: exercise0910.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

the end

沒有留言: