--- title: 程式設計(一) tags: 程式設計(一), 共同筆記 --- # 程式設計(一) >想練code的話,自學比較快:smirk:(X >被打,QQ: >常用編譯器:Dev-C++,codeblocks,vscode...... >vscode唯一選擇(x >我猜上學期最難的應該是指標和遞迴了? >[name=Maox Li] >原文書1: C programming,A Modern Approach,2/e,2008 >![](https://cf-assets2.tenlong.com.tw/products/images/000/042/182/original/41_bFKtFHjL.jpg?1525627843) >>Author: K.N.King >>Publisher: W.W.Norton >原文書2: The C Programming language,2/e,1998 >![](https://cf-assets2.tenlong.com.tw/products/images/000/070/035/original/41gHB8KelXL.jpg?1525610697) >>Author: Brian Kernighan,Dennis Ritchie >>Publisher: Prentice Hall ## 教授相關資訊 - 教授名稱: 蔡孟勳 - Email: tsaimh@csie.ncku.edu.tw - Office: 資訊新館 65B01 ## 日程 - 9/10 L.1 C語言介紹 - 9/17 L.2 C的基本原理 - 9/24 L.3 Formatted Input(scanf)/Output(printf) L.4 表達式(運算式)(+,-,*,/,%......) - 10/1 中秋節放假 - 10/8 L.5 陳述式(語法)(Selection Statements) - 10/15 L.6 迴圈(loop) - 10/22 L.7 資料型態(int,float,char......) - 10/29 L.8 陣列(arrays) - 11/5 L.9 函式(funtion) - 11/12 期中考 Middle-term Exam(地點會在考試前公布,6pm-9pm) - 11/19 L.10 Program Organization - 11/26 L.11 指標(pointer) / L.12 指標和陣列(pointer and arrays) - 12/3 L.13 字串(strings) - 12/10 L.14 預處理(編譯?,巨集)(preprocessor) - 12/17 L.15 開發大型程式(Writing Large Programs) - 12/24 L.16 結構,聯合,列舉(Structures, Unions, and Enumerations) - 12/31 L.17 指標的進階用法(Advanced Uses of Pointers) - 1/7 期末考 - 1/12 公布成績 ## 配分 Assignment 20% Exams(on-site exam) 80% Midterm(11/12) 35% Final(1/7) 45% Bonus(see course webpage on Moodle) ## Moodle http://moodle.ncku.edu.tw/course/view.php?id=9218 ## 自己的程式自己學,以下乾貨 ### Template code: ```c= //C #include <stdio.h> int main(){ printf("Hello! World!"); return 0; } ``` ```cpp= //C++ #include <iostream> using namespace std; int main(){ cout<<"Hello! World!"; return 0; } ``` :::warning 我們學校的judge好像是用gcc編譯,所以不能使用C++來解題目QAQ ::: ### 以下範例為邪教,不要跟別人過不去 ```c= #include "stdio.h" int main(){ printf("Hello! World!"); return 0; } ``` ```c= #include "stdio.h" int main(){printf("Hello! World!");return 0;} ``` ```c= #include "stdio.h" int main() { printf("Hello! World!"); return 0; } ``` :::info 最後一個括號的放法請自行決定,放在()後面跟放在新一行都有人用 例如這次的參考書籍 C programming,A Modern Approach,2/e 用的就是放在新一行的格式 ::: ### 變數 使用方法 ```c= int num; //宣告num這個變數,但他的初始值是未知 int num=0; //宣告一個叫做num的變數的值為0 num=1; //將num設成1 num++; //將num+1 num+=1; //同上 num=num+1; //同上 ``` 錯誤示範 ```c= num=0; //沒有宣告變數,如果宣告在底下,編譯也會失敗 9=num+1; //左值必須為變數,不能這樣子算出num=8 int num=0 //陳述式結束後要加;號 int 數字=0; //C不支援UTF-8編碼,除了英文外都不行 int int=0;//保留字不能被宣告,字符也不能宣告但#define可以,但可以宣告成num01等 ``` ### C的常見資料型態 ```c= int ``` >範圍:-2147483648~2147483647 = $-2^{31}$~$2^{31}-1$ ```c= unsigned int ``` >範圍:0~4294967295 = $0$~$2^{32}-1$ ```c= long long int ``` >範圍:-9223372036854775808~9223372036854775807 = $-2^{63}$~$2^{63}-1$ ```c= float ``` >有效位數:6~7位(整數+小數點位數的綜合) ```c= double ``` >有效位數:15~16位(整數+小數點位數的綜合) ```c= char ``` >範圍:-128~127 = $-2^{7}$~$2^7-1$ >儲存著Ascii規範裡0~127的字符編碼 >字元'0'為第48號編碼,'A'為65,'a'為97 等 >如果要將數字的字元轉成整數,可以這麼做:b=(int)a-48 or b=(int)a-'0' ### 表達式 #### 邏輯運算子 ```c= = ``` >等於(賦值) ex:a=b 當使用判斷式時判斷a=b時會使用a==b,不等於時為a!=b ```c= ! ``` >邏輯運算NOT ```c= && ``` >邏輯運算AND ```c= || ``` >邏輯運算OR ```c= + ``` >加法 ex:a=a+b; a++則為a+1,++a是在判斷式判斷之前先+1 ```c= - ``` >減法 ex:a=a-b; ```c= * ``` >乘法 ex:a=a*b; ```c= / ``` >除法 ex:a=a/b; ```c= % ``` >a對b取餘數 ex:a=a%b,當為float時請使用fmod()函式,取2餘數時能使用a&1 #### 位元運算子 ```c= & ``` >位元運算AND ex: >int a = 92; $\quad$in binary: 00000000000000000000000001011100 >int b = 101;$\quad$ in binary: 00000000000000000000000001100101 >int c= a & b;$\quad$ result: 00000000000000000000000001000100, or 68 in decimal. ```c= | ``` >位元運算OR ex: >int a = 92; $\quad$in binary: 00000000000000000000000001011100 >int b = 101;$\quad$ in binary: 00000000000000000000000001100101 >int c= a & b;$\quad$ result: 00000000000000000000000001111101, or 125 in decimal. ```c= ^ ``` >位元運算XOR ex: >int x = 12;$\quad$binary: 1100 >int y = 10;$\quad$binary: 1010 >int z = x ^ y;$\quad$binary: 0110, or decimal 6 :::info 比較常用在位元反轉的部分,也能用在bool的0,1反轉 ::: ### 判斷式 ```c= if(){ } ``` >當滿足判斷式()裡的條件時,執行{}裡的陳述式 ```c= else if(){ } ``` >類似if但會排在if的順位底下,執行if後不執行else if ```c= else{ } ``` >順位為else if底下 #### EX: ```c= #include "stdio.h" int main(){ int a=0,b=0; scanf("%d %d",&a,&b); if(a==b){ printf("a=b"); } else if(a>b){ printf("a>b"); } else{ printf("a<b"); } return 0; } ``` :::info 當內含物只有一行時,可以不打{} 請換行= = ::: ### 迴圈loop