C++ 入門指南 V2.00 - 單元 8 範例及練習程式碼
cpp-08 C++教學, C++入門指南
while_demo.cpp
#include <iostream> using namespace std; int main(void) { cout << endl; int i = 10; // 設定控制變數 // 迴圈工作區 while (i > 0) { cout << i << endl; i--; // 調整控制變數值 } cout << endl; return 0; } /* 檔名: while_demo.cpp 作者: Kaiching Chang 時間: 2014-5 */
for_demo.cpp
#include <iostream> using namespace std; int main(void) { cout << endl; for (int i = 10; i > 0; i--) { cout << i << endl; } cout << endl; return 0; } /* 檔名: for_demo.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0801.cpp
#include <iostream> using namespace std; int main(void) { int sum = 0; for (int i = 0; i <= 1000; i++) { sum += i; } cout << endl; cout << "1 + 2 + 3 + ... + 1000 = " << sum << endl; cout << endl << endl; return 0; } /* 檔名: exercise0801.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0802.cpp
#include <iostream> using namespace std; int main(void) { int sum = 0; int end; cin >> end; for (int i = 0; i <= end; i++) { sum += i; } cout << endl; cout << "1 + 2 + 3 + ... + " << end << " = " << sum << endl; cout << endl << endl; return 0; } /* 檔名: exercise0802.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0803.cpp
#include <iostream> #include <string> using namespace std; int main(void) { char c = '*'; cout << endl; for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { cout << c; } cout << endl; } cout << endl << endl; return 0; } /* 檔名: exercise0803.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0804.cpp
#include <iostream> #include <string> using namespace std; int main(void) { char c = '*'; cout << endl; for (int i = 0; i < 5; i++) { for (int j = i; j < 5; j++) { cout << c; } cout << endl; } cout << endl << endl; return 0; } /* 檔名: exercise0804.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0805.cpp
#include <iostream> #include <string> using namespace std; int main(void) { char c = '*'; int base; cin >> base; cout << endl; for (int i = 0; i < base; i++) { for (int j = i; j < base; j++) { cout << c; } cout << endl; } cout << endl << endl; return 0; } /* 檔名: exercise0805.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0806.cpp
#include <iostream> using namespace std; int main(void) { cout << endl; for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { cout << i * j << ", "; } cout << endl; } cout << endl << endl; return 0; } /* 檔名: exercise0806.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0807.cpp
#include <iostream> using namespace std; int main(void) { char c; cout << endl; while (true) { cin >> c; if (c == 'e') { break; } cout << c; } cout << endl << endl; return 0; } /* 檔名: exercise0807.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0808.cpp
#include <iostream> using namespace std; int main(void) { int result = 1; for (int i = 1; i <= 10; i++) { result *= i; } cout << endl; cout << "1 * 2 * ... * 10 = " << result << endl; cout << endl; return 0; } /* 檔名: exercise0808.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0809.cpp
#include <iostream> using namespace std; int main(void) { int result= 1; int input; cin >> input; for (int i = 1; i <= input; i++) { result *= i; } cout << endl; cout << "1 * 2 * ... * " << input << " = " << result << endl; cout << endl; return 0; } /* 檔名: exercise0809.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0810.cpp
#include <iostream> using namespace std; int main(void) { int f0 = 0; int f1 = 1; int f2; for (int i = 3; i <= 20; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } cout << endl; cout << "f(20) = " << f2 << endl; cout << endl; return 0; } /* 檔名: exercise0810.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise0811.cpp
#include <iostream> using namespace std; int main(void) { int f0 = 0; int f1 = 1; int f2; int input; cin >> input; if (input == 1) { f2 = f0; } else if (input == 2) { f2 = f1; } else { for (int i = 2; i <= input; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } } cout << endl; cout << "f(" << input << ") = " << f2 << endl; cout << endl; return 0; } /* 檔名: exercise0811.cpp 作者: Kaiching Chang 時間: 2014-5 */
the end
沒有留言:
張貼留言