Tony041010
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: 2021CRC title: 變數與運算 slideOptions: transition: slide theme: --- # 變數與運算 ## 2021/09/17 電算社第一堂社課 --- ## 變數 ---- 朋友買了一件衣料,當他拿給我們看的時候, 一位對圍棋很感興趣的人說 :「啊,好像棋盤似的。」 我說:「我倒覺得有點像稿紙。」 一位很愛吃的同學說:「這明明就是綠豆糕。」 對圍棋很感興趣的同學又說 :「我覺得它像稿紙了。」 我又說:「嗯我也覺得它像棋盤了。」 ---- 是不是覺得很亂呢 如果我們用**變數**表示的話 ---- A:「啊,好像棋盤似的。」 B:「我倒覺得有點像稿紙。」 C:「這明明就是綠豆糕。」 A:「我又覺得它像稿紙了。」 B:「嗯我也覺得它像棋盤了。」 ---- ### 變數的意義 * 防止很長的程式碼一再地出現 ```cpp= int a = 5 , b = 7 , c = 11; int d = a + b + c; cout << d; ``` <br> * 把想要存的東西存下來 ```cpp= int a; cin >> a; // 輸入值到 a; ``` ---- #### 變數的宣告 ```cpp= int a; //宣告一個變數a,其資料型態為整數int std::string club; //宣告一個變數club,其資料型態為字串string ``` ---- #### 賦值 ```cpp= int a = 10; //賦予a一個整數值10 std::string club = "CRC"; //賦予club一個字串的值"CRC" a = 20; //也可以重新賦值 ``` ---- #### 變數命名規定 * 開頭不能用數字,例如 3A * 不能使用特殊符號,例如-、@、# * 不能使用保留字,會出事 例如if、class、enum、new等等 tags: 保留字就是在C++裡面已經有自己意義的字 ---- #### 變數具有區域性 ```cpp= #include<iostream> using namespace std; int a = 10;//全域變數 int main() { int b = 10;//區域變數 if(b > 5){ int c = 50;//區域變數 } //這裡不是c的有效範圍 //這裡是a和b的有效範圍 } //這裡不是b、c的有效範圍 //但是是a的有效範圍 ``` --- ## 資料 ---- ### 資料型態 - 整數(int , long) - 浮點數(float , double) - 布林值(bool) - 字元(char) - 字串(std::string) - 因為我們都會用 using namespace std; 所以下面就用 string 帶過 ---- ### 整數 ![](https://i.imgur.com/bsmbOaF.png) ---- ### 浮點數 ![](https://i.imgur.com/Q9Koj1L.png) **double比float精準** ---- ### 布林值 bool 只有兩個值, True or False 對應到數字則是 1 和 0 之後在學條件判斷時會用到 ---- ### 字元 char 表示方式 : 用單引號 ' ' 把字元包起來 Examples : 'A'、'B'、'c'、'5' 字元用**整數**的形式儲存 ASCII Code : 把數字對應到字元 [維基百科](https://zh.wikipedia.org/wiki/ASCII) ---- ### 跳脫字元 特殊的或是有功能的字元 ![](https://i.imgur.com/rFranZg.png =30%x) ---- ### 字串 string 由一個或多個字元組成的句子 表示方式 : 用雙引號包起來 Examples : "HSNU"、"CRC" ![](https://i.imgur.com/gclcNuu.png =75%x) 字串在 std 裡面,要使用時記得加 "std::" ---- ### 雙引號 or 單引號 一個字串是用 " " "CRC is the best" 一個字元是用 ' ' ```cpp= {'C' , '8' , ' ' , '7' , '6' , '3' , '\0'} //"C8 763" ``` 可以看到 一個空格 ' ' 和跳脫字元 '\0' or '\n'...... 都可以當作是一個字元 所以可以用' '包起來 --- ## 運算 ---- ### 名詞解釋 a + b \+ : 運算子 a、b : 運算元 ---- ### 優先度 * 優先度高的先算 * e.g. 先乘除後加減 * [C++ Operator Precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) * [Microsoft Doc](https://docs.microsoft.com/zh-tw/cpp/cpp/cpp-built-in-operators-precedence-and-associativity?view=msvc-160) ---- ### 算術運算 ![](https://i.imgur.com/IB8eP1d.png =70%x) ---- ### 小撇步 ```cpp= a = a + 1 ; a += 1 ; a++ ; //三行都代表 a = a + 1 //(直接把 a 的值 + 1) ``` 有 a -= 1, a \*= 1, a /= 1, a %= 1 相同的也有 a-\-,**不過沒有 a 和 a// 或 a%%** ---- 整數和整數做運算,輸出的也是整數 浮點數做運算,輸出的就是浮點數 ```cpp= cout << 4/3; //1 cout << 4.0/3; //1.33333 ``` ---- ### 條件運算 算完的結果會是一個布林值 不是True就是False ![](https://i.imgur.com/ezyop8a.png =50%x) ---- ### 邏輯運算 多條件的條件運算 得到的結果也是一個布林值 ![](https://i.imgur.com/qekUQvu.png) ---- Examples ```cpp= int a = 5 , b = 3 , c = 1; cout << (a > b); // 1 cout << (b > a); // 0 cout << (a > b && c > b); // 0 cout << (a > b || c > b); // 1 cout << ((a > b) && !(c > b)); // 1 ``` --- ### 輸入與輸出 ---- 上一節課講了輸出但沒講輸入 因為輸入需要變數的概念 用cin取得輸入 用cout輸出 ---- ### 輸入 ```cpp= int N; cin >> N; //輸入 100 的話, N 就是 100 ``` 當然也可以一次輸入很多 ```cpp= int a , b; cin >> a >> b; ``` ---- ### 輸出 ```cpp= int a = 100; cout << a << '\n'; //輸出 100 ``` ---- ### 一起用 ```cpp= int N; cin >> N; cout << N + 10 << '\n'; ``` 注意 : cout 跟 cin 的箭頭方向不一樣 --- ### 小練習 輸入兩個數字a b 幫忙算算a跟b的總和吧 ---- 我是防雷頁 ---- ### 解答 ```cpp= #include<iostream> using namespace std; int main(){ int a , b; cin >> a >> b; cout << a + b; return 0; } ``` --- ### OJ練習 * [GreenJudge a005:矩形面積](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a005) * [GreenJudge a006:時間換算(一)](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a006) * [GreenJudge a008:溫度轉換](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a008) * [GreenJudge a009:團購力量大](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a009) * [GreenJudge a010:年齡推算](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a010)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully