1 / 40

Chapter 1 用 VC++ 撰寫程式

Chapter 1 用 VC++ 撰寫程式. Text book: Ivor Horton. In this Chapter. The components of VC++ What is project? How to build a project? How to create and edit a program? How to compile, link and exectute a C++ program? How to create a basic program of Windows?. DOS vs. Windows. Direct or not

Download Presentation

Chapter 1 用 VC++ 撰寫程式

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1 用VC++撰寫程式 Text book: Ivor Horton

  2. In this Chapter • The components of VC++ • What is project? How to build a project? • How to create and edit a program? • How to compile, link and exectute a C++ program? • How to create a basic program of Windows?

  3. DOS vs. Windows • Direct or not • Event-driven or not

  4. Figure (Text book page 9)

  5. Visual C++6.0開發環境介紹 • 整合性的開發環境(IDE,Integrated Development Environment) • Editor • Debug • Link • Execution

  6. Visual C++6.0所提供的介面主要分為以下幾個區域 功能表 工作區 程式編寫區 輸出區 變數視窗 變數監看視窗

  7. 工作區:

  8. 輸出區: 選擇不同的頁籤觀看輸出結果。

  9. 工具列選項如下所示 輸出結果、工作區是否顯示 設定相關工具列是否顯示 自訂工具列內容

  10. 在Visual C++6.0建立MS-DOS程式 2.選擇「Projects」頁籤 4.輸入專案名稱 5.選擇專案存放路徑 3.選擇「Win32 Console Application」專案格式 軟體並執行「File/New」指令,開啟此畫面。

  11. 1.此對話框詢問建立的Console Application為何種型態。在此選擇第一個「空的專案」。

  12. 此對話框顯示我們所建立的專案相關資訊。 按下此按鈕完成Console Application建立。

  13. 專案建立之後,至少還必須建立一個原始檔,建立原始檔的方式如下:專案建立之後,至少還必須建立一個原始檔,建立原始檔的方式如下: 1.執行「File/New」指令,開啟此畫面 4.輸入原始碼檔案完整名稱 2.選擇「Files」頁籤 3.選擇「C++ Source File」檔案格式 5.按下此按鈕完成原始檔建立

  14. File • .dsp: 專案基本定義 • .dsw: 儲存有關專案工作區的詳細資訊 • .opt: 儲存專案工作區的設定, 包括專案工作區的外觀 • .ncb: 瀏覽資訊

  15. #include <iostream> using namespace std; int main() { cout << "Some people think programming Windows" << endl << "is like nailing jello to the ceiling..." << endl << "easy with the right kind of nails." << endl << endl; return 0; }

  16. 不輸入任何程式碼的情況下,建立一個視窗程式基本架構 開啟Visual C++6.0,執行「File/New」指令

  17. 1.選擇Project頁籤 3.輸入專案名稱 2.選擇MFC AppWizard [exe] 選項 4.按下次按鈕,進入下一畫面

  18. 選擇視窗樣式。分別為單文件、多文件及對話框等。選擇視窗樣式。分別為單文件、多文件及對話框等。 按此按鈕,進入下一步驟。 其它設定畫面

  19. 此專案的相關資訊 按下此鈕,完成專案建立

  20. 執行專案。 專案類別名稱。

  21. 多文件視窗程式建立完成

  22. Chapter 2 資料、變數和運算 • C++程式架構 • 名稱空間(Namespace) • C++的變數 • 定義變數(Variables)和常數(Constants) • 基本輸入輸出(輸出到螢幕、由鍵盤輸入) • 算術運算 • 轉型運算元(Casting Operand) • 變數範圍(Variable Scope)

  23. 程式註解(comments) • 適當的程式註解是撰寫程式的良好習慣。在C++語言中提供兩種程式註解方式,分別為單行及多行註解

  24. //單行註解。使用兩個「/」符號代表,註解範圍由註解符號後至該行末端為止。//單行註解。使用兩個「/」符號代表,註解範圍由註解符號後至該行末端為止。 /*多行註解。 使用「/*」及「*/」符號代表, 註解範圍由第一個符號至第二個符號之間 */

  25. C++的程式架構 • 在C++語言中,只要是在DOS下執行的程式,在程式內必須要有一個main()函式,而且只能有一個

  26. main()函式的格式 回傳值main( 傳入的參數) { //程式敘述 return 傳回值;//此程式將執行權交還作業系統 }

  27. int main() { input_name(); sort_name(); output_name(); return 0; } void input_name() { … return 0; } Ex about function() void sort_name() { … return 0; } void output_name() { … return 0; }

  28. // EX2_01.CPP // A Simple Example of a Program #include <iostream> using namespace std; int main() { int apples, oranges; // Declare two integer variables int fruit; // ...then another one apples = 5; oranges = 6; // Set initial values fruit = apples + oranges; // Get the total fruit cout << endl; // Start output on a new line cout << "Oranges are not the only fruit... " << endl << "- and we have " << fruit << " fruits in all."; cout << endl; // Start output on a new line return 0; // Exit the program }

  29. Ex2_01說明 • #include<iostream> • 標頭檔Header file • 前置處理程式Preprocessor directives • 假指令 • using namespace std; • Standard library

  30. 何謂「標頭檔」 • 最簡單的解釋,就是:「已完成之程式庫中的函式及資料結構,以一個文字檔將他們的格式宣告,以供其他程式引用及使用」。 • 在C語言中是以假指令#include來實施標頭檔的引入,有以下兩種格式: • #include <iostream> • #include "stdafx.h"

  31. 在程式碼中常會引入多個表頭檔,當程式中引用的函式名稱不只存在於一個表頭檔時,我們可以透過「using」這個假指令來告知編譯器,若程式碼中省略函式的名稱空間時,以using這個假指令宣告的名稱空間為主

  32. 假設我們不宣告使用的名稱空間,則使用的方式如下:假設我們不宣告使用的名稱空間,則使用的方式如下: #include<iostream> int main(void){ std::cout<<"未宣告使用的表頭檔"<<std::endl; return 0; }

  33. 變數 • 意義: 是程式設計師用來告訴編譯器,於記憶體中配置一塊記憶體空間,用來儲存資料;而變數名稱只是一種識別代號 • 變數宣告時必須同時定義其資料型態。變數其資料型態的定義關係到變數佔用的記憶體容量及能儲存資料的長度

  34. 變數命名規則 • 變數命名:第一個字元必須是底線或是大小寫英文字母 • A~Z • a~z • _ • 名稱內字元可使用英文字母、阿拉伯數字及底線等字元 • 名稱長度限制最長可達255個字元 • (31個以內為佳) • 避免使用底線開頭ex:_this

  35. 變數命名規則 (cont.) • 名稱中不可有空白字元 • 變數名稱不可與C++語言內定的關鍵字相同

  36. Example • Price • Discount • pShape • Value_ • COUNT

  37. Example • 8_Ball • 7_Up • 6_pack • HASH! • Mary-Ann • Mary Ann

  38. 變數命名補充 • republican and Republican • Keywords (see Appendix A)

  39. 變數宣告的格式 資料型態變數名稱; 如下變數宣告 int i; //宣告一個整數變數i char j[10]; //宣告一個字元陣列變數j float k; //宣告一個浮點數變數k

  40. C++的資料型態 • 整數變數 • 浮點數變數 • 邏輯變數 • 特殊集合的變數 • 自定資料型態

More Related