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++程式 由功能表點選【File/New】
點選Project,選取『Win32 Console Application』,並且設定Project Name Console-mode程式是一個以字元為主的應用程式,主要在DOS視窗中執行 接著選取『An Empty Project』選項 完成設定後,按下工具列的『New Text File』,出現編輯視窗 編輯程式前,請先【存檔】,這樣編輯程式時才會有“關鍵字高亮度顯示”等功能,以方便編輯 存檔時請自行設定檔案的副檔名(.cpp)

3 執行第一個C++程式 程式經過編譯之後,若沒有問題(可由build status視窗中看到訊息),便可連結並建立執行檔,執行程式。
和.cpp檔案同一目錄中,會產生一Debug目錄,程式的執行檔便放在此目錄中,可在DOS視窗中來執行此程式。

4 程式範例 // Test.cpp // 程式的目的是在顯示器上顯示一行文字
// #include <iostream.h> // 程式主體部份 int main() { cout << "Hello, 您好!" // 主要的敘述 << endl; return 0; }

5 程式範例 // 要求使用者輸入兩個整數,並把計算結果輸出(練習使用cout, cin) #include <iostream>
int main() { int x; // 第一個輸入整數 int y; // 第二個輸入整數 int Sum; // 兩個整數的和 cout << "請輸入第一個整數\n"; cin >> x; cout << "請輸入第二個整數\n"; cin >> y; Sum = x + y; cout << "這兩個整數的和是: " << Sum << endl; return 0; }

6 程式範例 // A program to check the size of data types
#include <iostream> main() { cout << " Size of int is : " << sizeof(int) << " bytes" << endl; cout << " Size of short is : " << sizeof(short) << " bytes" << endl; cout << " Size of unsigned is: " << sizeof(unsigned) << " bytes" << endl; cout << " Size of long is : " << sizeof(long) << " bytes" << endl; cout << " Size of float is : " << sizeof(float) << " bytes" << endl; cout << " Size of double is : " << sizeof(double) << " bytes" << endl; cout << " Size of char is : " << sizeof(char) << " bytes" << endl; cout<< " Size of 3.8 is : " << sizeof(3.8) << " bytes" << endl; cout<< " Size of a is : " << sizeof(3.8 + a) << " bytes" << endl; cout<< " Size of 3.8f is : " << sizeof(3.8f) << " bytes" << endl; cout<< " Size of a is : " << sizeof(a) << " bytes" << endl; cout<< " Size of 3.8f + a is: " << sizeof(3.8f + a) << " bytes" << endl; }

7 // x的3次方計算 #include <cmath> #include <iostream> main() { float x ; cout << "請輸入一個數值:\n "; cin >> x; cout << "它的3次方是: " << pow(x,3) << endl ; }

8 常用的數學函數 名稱 功能 輸出值的資料型態 abs(x) 絕對值 int fabs(x) 與x相同 exp(x) double
pow(x,y) 次方 sqrt(x) 開根號 log(x) 取對數 log10(x) 以10為底取對數 Double ceil(x) >=x的最小整數 floor(x) <=x的最大整數

9 C++的資料型態 基本資料型態 整數 int, short, long 浮點數 float, double, long double
字元 char 邏輯值 bool (bool b1,b2,b3; b1=true; b2=false; b3=1) 衍生資料型態 與位址相關的資料型態 指標 參照 有結構的資料型態 string enum aray struct union class

10 函數 函數的語法有三個部分: 函數的宣告 建立函數的prototype,以告知編譯器本程式將使用的函數名稱,以及進出這個函數的資料之型態和數量 函數的定義 函數的呼叫

11 範例程式 //-------函數C2F的定義---------- float C2F(float C) { float F;
#include <iomanip> // 函數C2F的宣告 float C2F(float); int main() { float CTemp; cout << " 攝氏 華氏 " << endl ; cout << " " << endl ; for ( int i = 1 ; i <= 10 ; i++ ) CTemp = 10.0*i; cout << setw(5) << CTemp << " " << setw(5) << C2F(CTemp) << endl ; } return 0; // 函數C2F的定義 float C2F(float C) { float F; F = C*9.0/ ; return F; }

12 指標 指標的定義 float* ptf; //定義指標ptf, 較常用 float *ptf; //定義指標ptf,較少用
如果要同時定義兩個指標 float *p1,*p2; //p1,p2都是指標 float* p1,p2; //p1是指標, p2是float變數


Download ppt "開始使用Visual C++."

Similar presentations


Ads by Google