https://sandbox.cs50.io/ https://www.youtube.com/watch?v=e9Eds2Rc_x8
** clang ** source code -> complier -> machine code
int.c cough1 agree floats 溢位 mario 2
需引入參數 #include <cs50.h> #include <unistd.h> #include <stdio.h>
hello.c 1 2 3 4 5 6 7 #include <stdio.h> int main(void) { printf("hello world \n"); }
1 2 3 4 5 6 7 $ clang hello.c $ ./a.out $ clang -o hello hello.c ./hello rm a.out yes
string.c 1 2 3 4 5 6 7 #include <stdio.h> int main(void) { string answer = get_string("What's your name? \n"); printf("hello, %s\n", answer); }
1 clang -o string string.c
string.c step2. 1 2 3 4 5 6 7 8 9 10 #include <stdio.h> #include <cs50.h> int main(void) { string answer = get_string("What's your name? \n"); printf("hello, %s\n", answer); } // %s 意思是 string
錯誤 $ clang string string.c -lcs50 clang-7: error: no such file or directory: ‘string’
正確
1 clang -o string string.c -lcs50
int.c step3 1 2 3 4 5 6 7 8 9 10 #include <cs50.h> #include <stdio.h> int main(void) { int age = get_int("What's your age?\n"); int days = age * 365; printf("You are at least %i days old.\n", days); }
float.c step4 1 2 3 4 5 6 7 8 9 10 11 #include <cs50.h> #include <stdio.h> int main(void) { int price = get_float("What's the price? \n"); printf("Your total is %f. \n", price * 1.0625); }
printf("Your total is %.2f. \n", price * 1.0625);
小數點後兩位
agree step 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // Logical operators #include <cs50.h> #include <stdio.h> int main(void) { // Prompt user to agree char c = get_char("Do you agree?\n"); // Check whether agreed if(c == 'Y' || c == 'y') { printf("Agreed.\n"); } else if(c == 'N' || c == 'n') { printf("Not agreed.\n"); } } // Logical operators #include <cs50.h> #include <stdio.h> int main(void) { // Prompt user to agree char c = get_char("Do you agree?\n"); // Check whether agreed if(c == 'Y' || c == 'y') { printf("Agreed.\n"); } else if(c == 'N' || c == 'n') { printf("Not agreed.\n"); } }
cough1 step 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h> void cough(void); int main(void) { for(int i=0; i < 3; i++) { // printf('Cough!'); cough(); } } void cough(void) { printf("cough \n"); }
函數左側是輸出類型 void 是空 int 是自自 void cough(void) { printf(“cough \n”); }
int get_positive_int(void) { int n; do { n = get_int(“Positive Integer: “);
}
while(n < 1)
return n;
}
step7 mario 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <cs50.h> #include <stdio.h> int main(void) { int n; do { n = get_int("Width: "); } while (n < 1); for (int i =0; i < n; i++) { for(int j=0; j < n; j++) <!-- 聖誕樹 for(int j=0; j < i; j++) --> <!-- printf("#\n"); --> { printf("#"); } printf("\n"); } printf("\n"); }
1 2 3 4 5 6 7 8 9 10 11 12 $ make mario2 ./mario2 Width: 10 ########## ########## ########## ########## ########## ########## ########## ########## ##########
step 9 floats 溢位 // 有限的內存 計算機 不能計算超過特定區域的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <cs50.h> #include <stdio.h> int main(void) { float x = get_float("x: "); float y = get_float("y: "); // printf("x/y = %f\n", x / y); printf("x/y = %.50f\n", x / y); }
overflow step10 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <cs50.h> #include <unistd.h> #include <stdio.h> int main(void) { for (int i =1; ; i *=2) { printf("%i\n", i); sleep(1); } }
1 2 3 make overflow ./overflow