1 / 20

C++ 程式設計入門

C++ 程式設計入門. 變數與運算子 作者:黃建庭. 變數. 變數 變數是程式放資料的空間, 每個變數 佔有 特定的 記憶體空間 ,由編譯器分配。 程式將資料進行運算,就是對變數與記憶體進行運算。 範例: y=x+12. 變數的命名. 1. 開頭不能為數字。 2. 只能使用大小寫英文字母、數字與底線 (_) 所組成。 3. 不能使用保留字 ( 如 if) 。 4. 變數大小寫有差別。 5. 使用 有意義的單字命名,如 score 命名成績變數。. 宣告變數的方式. (a) 宣告變數 資料型別 變數 ; 例如: int a; (b) 宣告變數並初始化

may-gibson
Download Presentation

C++ 程式設計入門

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. C++程式設計入門 變數與運算子 作者:黃建庭

  2. 變數 • 變數 • 變數是程式放資料的空間,每個變數佔有特定的記憶體空間,由編譯器分配。 • 程式將資料進行運算,就是對變數與記憶體進行運算。 • 範例:y=x+12

  3. 變數的命名 • 1.開頭不能為數字。 • 2.只能使用大小寫英文字母、數字與底線(_)所組成。 • 3.不能使用保留字(如if)。 • 4.變數大小寫有差別。 • 5.使用有意義的單字命名,如score命名成績變數。

  4. 宣告變數的方式 • (a)宣告變數 • 資料型別 變數; • 例如:int a; • (b)宣告變數並初始化 • 資料型別 變數=初始值; • 例如:int a=10; • (c)宣告多個變數 • 以逗點隔開 • 例如: int a,b,c;

  5. 資料型別 • C語言的資料型別分成整數、浮點數與字元等。 • 整數與浮點數(小數點) • 資料型別有空間限制,超過空間範圍後,數值就無法正確表示 • 字元是任何英文字母、數字與半形標點符號。

  6. 整數 • 整數分為short、int、long與long long。

  7. 浮點數 • 浮點數分為單精度與倍精度浮點數

  8. 字元與字串 • 大小寫英文字母、數字、英文標點符號。 • 字串 格式代碼 %s

  9. C語言輸入與輸出 • #include <stdio.h> • 輸出printf。

  10. C語言輸入與輸出

  11. C語言特殊控制符號 • \n換行字元 • \0表示字串的結束 • \t相當於按「tab」鍵 • \a發出嗶一聲

  12. C++輸入與輸出 • #include <iostream> • using namespace std;

  13. C++輸入與輸出

  14. 跳脫字元 • C語言中要讓特殊字元失去原本的意義要在字元前面新增「\」,注意斜線方向。 • 例如要輸出「”」,要 cout << “ \” ”; • 例如要輸出「\」,要 cout << “ \\ ”;

  15. 指定運算子 • 指定運算子 • 將等號右邊計算結果儲存到等號左邊。

  16. 算術運算子 • 負責數值的運算,如加、減、乘、除與求餘數

  17. 範例-華氏轉攝氏 #include <iostream> using namespace std; int main() { double F,C; cin >> F; C=(F-32)*5/9; cout << "攝氏為" << C << endl; system("pause"); }

  18. 關係運算子

  19. 邏輯運算子 • 邏輯運算子有三個且(&&)、或(||)、非(!)

  20. 範例-判斷成績是否大於等於60小於80 #include <iostream> using namespace std; int main() { int score; cin >> score; cout << “成績>=60且<80結果為" << ((score >=60)&& (score<80)) << endl; system("pause"); }

More Related