蔣立元
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Coding Style 2020, jeeeerrrpop --- 每個人都有自己寫Code的風格習慣。 --- neoj-2333 訊息處理 ```cpp #include <iostream> int cnt[26]; int main() { int n; std::cin >> n; for(int i = 0; i < n; i++) { char c; std::cin >> c; if(c >= 'a' && c <= 'z') { std::cout << c; cnt[c - 'a']++; } if(c >= 'A' && c <= 'Z') { std::cout << char(c - ('A'- 'a')); cnt[c - 'A']++; } } std::cout << std::endl; for(int i = 0; i < 26; i++) { std::cout << cnt[i] << " \n"[i == 25]; } return 0; } ``` --- ```cpp #include <iostream> int cc[26]; int main(){int n; std::cin>>n; for(int i=0;i<n;i++) {char c; std::cin>>c; if(c>='a'&&c<='z'){ std::cout<<c; cc[c-'a']++; }if(c >= 'A'&&c<='Z'){ std::cout<<char(c-('A'-'a'));cc[c-'A']++; } } std::cout<<std::endl; for(int i=0;i<26;i++)std::cout<<cc[i]<<" \n"[i == 25]; return 0; } ``` --- - 任何人都可以寫得出電腦看得懂的程式,但要讓人看得懂,不是一件容易的事情! - 永遠不要相信未來的自己會懂你 --- 並沒有哪種 Coding Style 是絕對最完美的 (宗教戰爭) --- [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) [Google C++ Style Guide 中文版](http://www.slmt.tw/google-cpp-style-guide-zh-tw/index.html#連結) [C++ Core Guideline](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#main) [C/C++ Programming Style Guidelines](http://squall.cs.ntou.edu.tw/cpp/102spring/C%20&%20C%2B%2B%20Programming%20Style%20Guidlines.pdf) --- ## Overall Goal - 易讀 - 易改 - 一致 - 看起來舒服 --- 命名規則 ![](https://scontent-hkg4-1.xx.fbcdn.net/v/t1.0-9/91278647_1083933651970814_8218271684394745856_n.jpg?_nc_cat=101&_nc_sid=110474&_nc_ohc=uKIbK02CNzYAX87c_dd&_nc_ht=scontent-hkg4-1.xx&oh=f45585040cffa55643ea6770b0a848d5&oe=5EB760F5 =40%x) --- - i, j, k, l, m - a, aa, aaa, aaaa - a, b, c, d, e, f..... - tmp1, tmp2, tmp3, tmp4 - _, __, ___, ____ --- 變數命名 - 描述性 - 慣例 - 一致 - 格調 --- ## 程式碼命名方式 ```cpp void f(int a[], int n) { int s = 0; for(int i = 0; i < n; i++) { s += a[i]; } return s; } ``` --- TL;DR camelCase PascalCase snake_case --- - 小駝峰(lower camel case) 第一個單字的首字母小寫,其餘單字首字母大寫 - 大駝峰(upper camel case,Pascal Case) 首字母也大寫 - snake_case 用 _ 來分隔單字 - [匈牙利命名法](https://zh.wikipedia.org/wiki/匈牙利命名法) --- ```cpp void CalcArraySum(int array[], int array_size) { int arraySum = 0; for(int i = 0; i < array_size; i++) { arraySum += array[i]; } return arraySum; } // 使用哪個命名規則也要一致 ``` --- Google says.... --- ## 版面􏰀與樣式編排 ![](https://i.pinimg.com/originals/ef/2c/66/ef2c66815c9878138c12505f5c821269.png =60%x) --- ![](https://i.imgur.com/l9X8HY9.png) [source](https://www.ncyu.edu.tw/files/site_content/cc/coding%20style.pdf) --- ![](https://i.imgur.com/T7RA7cm.png =70%x) [source](https://www.ncyu.edu.tw/files/site_content/cc/coding%20style.pdf) --- ```cpp if () { } else { } ``` ```cpp if(){ } else{ } ``` --- ![](https://cdn.ebaumsworld.com/mediaFiles/picture/516050/85968924.png =70%x) --- ## 適時加入空白 ```cpp //for(int i=0;i<n;i++) for(int i = 0; i < n; i++) //int c=(a+b)/2 int c = (a + b) / 2; //cin>>a>>b>>c; cin >> a >> b >> c; // function(int a,int b,intc) function(int a, int b, int c) ``` --- ## Tab vs space ```cpp meow meow ``` --- 各有優缺點 重要的是要一致以避免轉換平台的時候排版亂掉 ![](https://res.cloudinary.com/practicaldev/image/fetch/s--H8LtA_7W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://i.imgur.com/vOWAAUK.png) --- ## 檔案名稱 - 有意義 - 避免空白、特殊字元 - 避免中文 --- ## 註解 - 適時加上註解有助於理解 - 避免非ASCII的文字 --- ## 宣告變數 - 要注意變數的scope - 在靠近使用那個變數的地方宣告他 - 避免第10行宣告、第100行才用 - 要初始化變數 (避免WA) --- ```cpp void CalcArraySum(int array[], int array_size) { int arraySum; for(int i = 0; i < array_size; i++) { arraySum += array[i]; } return arraySum; } ``` --- ```cpp for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) { int tmp = i; } } ``` --- ## 函數 對於有特別意義或是重複的操作,可以寫成function,以維持易讀性。 --- ```cpp if(x >= 1 && x <= n && y >= 1 && y <= n) { ...... } // bool InGrid(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= n; } ...... if(InGrid(x, y)) { ...... } ``` --- [The International Obfuscated C Code Contest](https://zh.wikipedia.org/wiki/国际C语言混乱代码大赛)

    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