Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual C++重點複習.

Similar presentations


Presentation on theme: "Visual C++重點複習."— Presentation transcript:

1 Visual C++重點複習

2 C++ 程式結構 #include <iostream.h> void main( ) { int a;
cin >> a; cout << ”a= ” << a << endl; } 前置處理區 宣告main函數 變數宣告區 程式敘述區

3 數學運算 #include <iostream.h> void main( ) { int a=1; a = a + 5;
cout << ”a= ” << a << endl; } a = a+3; a = a+1;

4 if-else敘述

5 if-else敘述範例

6 計數型迴圈for for ( 起始運算式; 條件運算式; 更新運算式 ) { 多敘述區; } 範例
for(int i=1; i<=9; i++) cout << i << ' ';

7 計數型迴圈for (續) for ( 起始運算式; 條件運算式; 更新運算式 ) { 多敘述區; }

8 計數型迴圈for (續) 範例二 for (int i=0; i<10; i++) //宣告並使用i變數
cout << 2*i << endl; for (int i=0; i<10; i++) //重複宣告並使用i變數 cout << i*i << endl; 範例三 int i = 0; //宣告並起始變數i for (i=0; i<10; i++) //使用i變數 for (i=0; i<10; i++) //重複使用i變數

9 Ex 1 寫一個C++程式,計算 1到100之間,所有不是2也不是3的倍數的數字的總和。 例如:1, 5, 7, 11, 13, 17, 19, … 上載位置

10 函數 定義: 函數(function)是執行一項特殊工作之敘述的集合。 例如: 使用者也可以建立自定的函數 main( )函數
sin( )、sqrt( )、rand( ) 使用者也可以建立自定的函數

11 宣告函數 傳回型態 函數名稱 (參數列) { //函數本體 return 傳回值; } 範例
int total(int n) //使用者函數 { int i, sum=0; for(i=1; i<=n; i++) sum += i; return sum; }

12 實例 #include <iostream.h> int total(int n) { int sum = 0;
for(int i=1; i<=n; i++) sum += i; return sum; } void main() cout << " = " << total(100) << endl << endl; cout << " = " << total(200) << endl << endl; cout << " = " << total(300) << endl;

13 宣告函數原型 傳回型態 函數名稱 (參數型態0, 參數型態1, 參數型態2, …); 宣告函數原型(function prototype)敘述與函數標題敘述相同,它提供函數的基本資訊給編譯程式,包括傳遞給函數的參數型態與函數傳回的資料型態。 宣告函數原型必須出現在呼叫敘述之前,通常是放在程式的前置處理區。

14 宣告函數原型實例 #include <iostream.h> int total(int); void main() {
cout << " = " << total(100) << endl << endl; cout << " = " << total(200) << endl << endl; cout << " = " << total(300) << endl; } int total(int n) int sum = 0; for(int i=1; i<=n; i++) sum += i; return sum;

15 Ex 2 請使用函數寫一個C++程式計算奇數和
定義一個 oddTotal ( n )函數,接收到參數n的值後,會計算從1到n之間所有奇數的和。 在main函數中,分別以100, 200, 300的值為參數,呼叫oddTotal去計算並印出1到100, 200, 300的奇數和。


Download ppt "Visual C++重點複習."

Similar presentations


Ads by Google