# 基礎程式設計參考程式碼 ## 基礎程式設計 ### Exercise 1 ```cpp= #include <iostream> using namespace std; int main() { cout << "Hello world!"; return 0 } ``` ### Exercise 1 without namespace ```cpp= #include <iostream> int main() { std::cout << "Hello world!"; return 0; } ``` ### Example - 換行 ```cpp= #include <iostream> using namespace std; int main() { cout << "Hello world!\n"; cout << "I am learning C++"; return 0; } ``` ### Example - 註解 (1/2) ```cpp= #include <iostream> int main() { std::cout << "Hello world!"; // 列印出 Hello World return 0; } ``` ### Example - 註解 (2/2) ```cpp= #include <iostream> using namespace std; int main() { /* The code below will print the sentence to the screen, and it is amazing */ cout << "I want to become a maker!"; return 0; } ``` ### Example - 先存入一個數值 ```cpp= #include <iostream> using namespace std; int main() { int myNum = 15; // 宣告myNum整數變數並把15存進去 cout << myNum; // 列印出存入myNum中的數值 return 0; } ``` ### Example - 先宣告再存入 ```cpp= #include <iostream> using namespace std; int main() { int myNum; myNum = 15; cout << myNum; return 0; } ``` ### Example - 指派新數值 ```cpp= #include <iostream> using namespace std; int main() { int myNum = 15; myNum = 10; cout << myNum; return 0; } ``` ### Exercise 2 ```cpp= #include <iostream> using namespace std; int main() { int myAge = 35 cout << "I am " << myAge << "years old."; return 0; } ``` ### Example - cin 輸入 ```cpp= #include <iostream> using namespace std; int main() { int num; cout << "Type a number"; cin >> num; cout << "Your number is: " << num; return 0; } ``` ### Example - 運算子 ```cpp= #include <iostream> using namespace std; int main() { int count = 1 + 2; //右邊先做運算再存入左邊的變數 cout << count; //列印出數字 return 0; } ``` ### Exercise 3 ```cpp= #include <iostream> using namespace std; int main() { int count = 3; // 宣告整數變數count為3 count = count + 1; // 右邊先做運算再存入左邊變數,會取代原有的數值 cout << count; return 0; } ``` ### Example - count 增加方式比較 ```cpp= #include <iostream> using namespace std; int main() { int count = 3; count += 1; cout << count; return 0; } ``` ```cpp= #include <iostream> using namespace std; int main() { int count = 3; count ++; cout << count; return 0; } ``` ### Exercise 4 ```cpp #include <iostream> using namespace std; int main() { int a = 5; int b = 2; cout << "The remainder is : " << a % b; return 0; } ``` ### Exercise 5 ```cpp= #include <iostream> using namespace std; int main() { int a = 1.02; float b = 1.02; cout << a << "\n" << b; return 0; } ``` ### Example - 運算精准度 ```cpp= #include <iostream> using namespace std; int main() { float num1 = 19.0; int num2 = 5; cout << num1 / num2; return 0; } ``` ```cpp= #include <iostream> using namespace std; int main() { int num1 = 19; int num2 = 5; cout << num1 / num2; return 0; } ``` ### Exercise 6 **提示:** 1. 先宣告PI = 3.14159 2. 再宣告一個變數radius 3. 由使用者輸入radius的值 4. 做運算 <!-- :::spoiler **解答** ```cpp= #include <iostream> using namespace std; int main() { double PI = 3.14159; // 宣告一個雙浮點數變數PI為3.14159 double radius; // 宣告一個雙浮點數變數radius cout << "Please enter a radius: "; // 輸入一個數字後按enter cin >> radius; // 從鍵盤上抓取你所輸入的數字存入radius這個變數 cout << "The area of this circle = "; cout << (PI*radius*radius); // 列印出運算出的結果 return 0; } ``` ::: --> ### Example - 不好看的格式 ```cpp= #include <iostream> using namespace std; int main ( ) { cout << "Learning C++ is fun"; return 0; } ``` ### Example - 好看的格式 ```cpp= #include <iostream> using namespace sd; int main() { cout << "Learning C++ is fun"; return 0; } ``` ### Example - 關係型 ```cpp= #include <iostream> using namespace std; int main() { int a = 3; int b = 5; int c; c = (a>b); cout << c <<"\n"; c = (a<b); cout << c <<"\n";; return 0; } ``` ### Example - 邏輯型 ```cpp= #include <iostream> using namespace std; int main() { int a = 3; int b = 5; int c = 7; int d; d = (a>c||b>c); cout << d <<"\n"; d = (a>b||b>c); cout << d <<"\n"; d = (c>b&&b>a); cout << d <<"\n" d = (a<b&&b>c); cout << d <<"\n"; } ``` ### Example - if ```cpp= #include <iostream> using namespace std; int main() { cout << "Enter a number:"; int number; cin >> number; if(number>=10) { cout << number; } return 0; } ``` ### Example - if else ```cpp= #include <iostream> using namespace std; int main() { cout << "Enter your score:"; int score; cin >> score; if(score>=60) { cout << "Pass" << endl; } else { cout << "Fail" << endl; } return 0; } ``` ### Exercise 7 <!-- :::spoiler **解答** ```cpp= #include <iostream> using namespace std; int main() { float score; cout << "Enter your score"; cin >> score; cout << "You get level "; if(score >= 90.0) { cout << "A"; } else if(score >= 80.0) { cout << "B"; } else if(score >= 70.0) { cout << "C"; } else { cout << "F"; } return 0; } ``` ::: --> ### Example - break in loop(for) ```cpp= #include <iostream> using namespace std; int main() { int count = 0; for(int i = 10; i <= 40; i += 2) { count++; if(i % 8 == 0) { break; } cout << i << " "; } cout << "\n" << count; return 0; } ``` ### Example - break in loop(while) ```cpp= #include <iostream> using namespace std; int main() { int count = 0; while(i<=40) { count++; if(i % 8 == 0) { break; } cout << i << " "; i += 2; } cout << "\n" << count; return 0; } ``` ### Example - continue in loop(while) ```cpp= #include <iostream> using namespace std; int main() { int i = 0; while(i < 20) { i = i + 2; if(i == 10) { continue; } cout << i << " "; } return 0; } ``` ### Example - continue in loop(for) ```cpp= #include <iostream> using namespace std; int main() { int i = 0; for(int i = 0; i < 20; i += 2) { if(i == 10) { continue; } cout << i << " "; } return 0; } ``` ### Exercise 8 <!-- :::spoiler **解答** ```cpp= #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) { cout << i << " "; } cout << "\n"; for(int i = 10; i >= 1; i--) { cout << i << " "; } return 0; } ``` ::: --> ### Example - 雙層迴圈 ```cpp= #include <iostream> using namespace std; int main() { int i = 0; for(int i = 1; i <= 9; i ++) { for(int j = 1; j <= 9; j ++) { cout << i*j << " "; } cout << "i = " << i << " "; cout << endl; } return 0; } ``` ### Exercise 9 <!-- :::spoiler **解答** ```cpp= #include <iostream> using namespace std; int main() { cout << "Enter the height: "; int height; cin >> height; for(int i = 1; i <= height; i++) { for(int j = 1; j <=i; j++) { cout << j << " "; } cout << "\n"; } return 0; } ``` ::: --> ### Exercise 10 :::spoiler **Hint** ```cpp= #include <iostream> using namespace std; int main() { cout << "Enter the size of rectangle: "; int a; cin >> a; for(int i = 1; i <= a; i++) { if(/*Finish yourself */){ for(int j = 1; j <= a; j++) { cout << /*Finish yourself */; } } else { for(int j = 1; j <= a; j++) { if(/*Finish yourself */) { cout << /*Finish yourself */; } else { cout << /*Finish yourself */; } } } cout << "\n"; } return 0; } ``` ::: <!-- :::spoiler **解答** ```cpp= #include <iostream> using namespace std; int main() { cout << "Enter the size of rectangle: "; int a; cin >> a; for(int i = 1; i <= a; i++) { if(i == 1 || i == a){ for(int j = 1; j <= a; j++) { cout << "*"; } } else { for(int j = 1; j <= a; j++) { if(j == 1 || j == a) { cout << "*"; } else { cout << " "; } } } cout << "\n"; } return 0; } ``` ::: --> ### Example - switch(1) ```cpp= #include <iostream> using namespace std; int main() { int n = 1; switch(n) { case 1: cout << "可樂"; break; case 2: cout << "雪碧"; break; case 3: cout << "紅茶"; break; default: cout << "無效"; } } ``` ### Example - switch(2) ```cpp= #include <iostream> using namespace std; int main() { int n = 1; switch(n) { case 1: cout << "可樂"; case 2: cout << "雪碧"; case 3: cout << "紅茶"; default: cout << "無效"; } } ``` ### Exercise 11 <!-- :::spoiler **解答** ```cpp= #include <iostream> using namespace std; int main() { int x; cout << "輸入分數"; cin >> x; x /= 10; switch(x) { case 10: cout << "Excellent"; break; case 9: cout << "Excellent"; break; case 8: cout << "perfect"; break; case 7: cout << "good"; break; default: cout << "OK"; } } ``` ::: --> ### Example - Basic function ```cpp= #include <iostream> #include<string> using namespace std; int change(int a) { cout << a; a++; cout << a; return a; } int main() { int x = 1, ans; cout << x; ans = change(x); cout << ans; } ``` ### Example - 全域變數與區域變數 ```cpp= #include <iostream> #include<string> using namespace std; int change(int x) { cout << x; x++; cout << x; return x; } int main() { int x = 1, ans; cout << x; ans = change(x); cout << ans; } ``` ### Exercise 12 <!-- :::spoiler **解答** ```cpp= #include <iostream> #include <string> using namespace std; int larger(int x, int y) { if(x > y) { retrun x; } return y; } int main() { int x, y, ans; cout << "Enter:" << endl; cin >> x >> y; ans = larger(x, y); cout << ans; } ``` ::: --> ### Exercise 13 :::spoiler **Hint** ```cpp= #include <iostream> #include <string> using namespace std; void reverse(int x) { for(int i = 0; i < 4; i++) { ***** ***** } } int main() { int x,; cin >> x; reverse(x); } ``` ::: <!-- :::spoiler **解答** ```cpp= #include <iostream> #include <string> using namespace std; void reverse(int x) { for(int i = 0; i < 4; i++) { cout << x % 10; x /= 10; } } int main() { int x,; cin >> x; reverse(x); } ``` ::: -->
×
Sign in
Email
Password
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