6.4
#include#include #include #include #include #include using namespace std; int fact(int val) { if (val < 0) return -1; int ret = 1; for (int i = 1; i != val + 1; ++i) ret *= i; return ret; } int main() { int num; cout << "請輸入一個整數:"; cin >> num; cout << num << "的階乘是:" << fact(num) << endl; return 0; }
6.5
#include#include #include #include #include #include using namespace std; double myABS(double val) { if (val < 0) return val*-1; else return val; } double myABS2(double val) { return abs(val); } int main() { double num; cout << "請輸入一個數:"; cin >> num; cout << num << "的絕對值是:" << myABS(num) << endl; cout << num << "的絕對值是:" << myABS2(num) << endl; return 0; }
6.6
#include#include #include #include #include #include using namespace std; double myADD(double val1, double val2) { double result = val1 + val2; static unsigned iCnt = 0; ++iCnt; cout << "該函數已經累計執行了" << iCnt << "次" << endl; return result; } int main() { double num1, num2; cout << "請輸入兩個整數:"; while (cin >> num1 >> num2) { cout << num1 << "與" << num2 << "的求和結果是:" << myADD(num1, num2) << endl; } return 0; }
6.7
這裡寫代碼片
6.8
#ifndef CHAPTER6_H_INCLUDED #define CHAPTER6_H_INCLUDED int fact(int); double myABS(double); #endif
6.9
#include"Chapter6.h" #includeusing namespace std; double myABS(double val) { if (val < 0) return val*-1; else return val; } double myABS2(double val) { return abs(val); } int fact(int val) { if (val < 0) return -1; int ret = 1; for (int i = 1; i != val + 1; ++i) ret *= i; return ret; } #include #include #include #include #include #include #include"Chapter6.h" using namespace std; int main() { int num; cout << "請輸入一個整數:"; cin >> num; cout << num << "的階乘是:" << fact(num) << endl; return 0; }
6.10
#include#include #include #include #include #include using namespace std; void mySWAP(int *p, int *q) { int tmp = *p; *p = *q; *q = tmp; } int main() { int a = 5, b = 10; int *r = &a, *s = &b; cout << "交換前:a=" << a << ",b=" << b << endl; mySWAP(r, s); cout << "交換後:a=" << a << ",b=" << b << endl; return 0; }
6.11
#include#include #include #include #include #include using namespace std; void reset(int &i) { i = 0; } int main() { int num = 10; cout << "重置前:num=" << num << endl; reset(num); cout << "重置後:num=" << num << endl; return 0; }
6.12
#include#include #include #include #include #include using namespace std; void mySWAP(int &i, int &j) { int tmp = i; i = j; j = tmp; } int main() { int a = 5, b = 10; int *r = &a, *s = &b; cout << "交換前:a=" << a << ",b=" << b << endl; mySWAP(a,b); cout << "交換後:a=" << a << ",b=" << b << endl; return 0; }
6.13
#include#include #include #include #include #include using namespace std; void a(int); void b(int&); int main() { int s = 0, t = 10; a(s); cout << s << endl; b(t); cout << t << endl; return 0; } void a(int i) { ++i; cout << i << endl; } void b(int& j) { ++j; cout << j << endl; }
6.17
#include#include #include #include #include #include using namespace std; bool HasUpper(const string& str) { for (auto c : str) { if (isupper(c)) return true; return false; } } void ChangeToLower(string& str) { for (auto &c : str) { c = tolower(c); } } int main() { cout << "請輸入一個字符串:" << endl; string str; cin >> str; if (HasUpper(str)) { ChangeToLower(str); cout << "轉換後的字符串是:" << str << endl; } else { cout << "該字符串不含大寫字母,無須轉換" << endl; } return 0; }
6.21
#include#include #include #include #include #include using namespace std; int myCompare(const int val, const int *p) { return (val > *p) ? val : *p; } int main() { srand((unsigned)time(NULL)); int a[10]; for (auto &i : a) i = rand() % 100; cout << "請輸入一個數:"; int j; cin >> j; cout << "您輸入的數與數組首元素中交大的是:" << myCompare(j, a) << endl; cout << "數組的全部元素是:" << endl; for (auto i : a) cout << i << " "; cout << endl; return 0; }
6.22
#include#include #include #include #include #include using namespace std; void SwapPointer1(int *p, int *q) { int *temp = p; p = q; q = temp; } void SwapPointer2(int *p, int *q) { int temp = *p; *p = *q; *q = temp; } void SwapPointer3(int *&p, int *&q) { int *temp = p; p = q; q = temp; } int main() { int a = 5, b = 10; int *p = &a, *q = &b; cout << "交換前:" << endl; cout << "p的值是:" << p << ",q的值是:" << q << endl; cout << "p所指的值是:" << *p << ",q所指的值是:" << *q << endl; SwapPointer1(p, q); cout << "交換後:" << endl; cout << "p的值是:" << p << ",q的值是:" << q << endl; cout << "p所指的值是:" << *p << ",q所指的值是:" << *q << endl; a = 5, b = 10; cout << "交換前:" << endl; cout << "p的值是:" << p << ",q的值是:" << q << endl; cout << "p所指的值是:" << *p << ",q所指的值是:" << *q << endl; SwapPointer2(p, q); cout << "交換後:" << endl; cout << "p的值是:" << p << ",q的值是:" << q << endl; cout << "p所指的值是:" << *p << ",q所指的值是:" << *q << endl; a = 5, b = 10; cout << "交換前:" << endl; cout << "p的值是:" << p << ",q的值是:" << q << endl; cout << "p所指的值是:" << *p << ",q所指的值是:" << *q << endl; SwapPointer3(p, q); cout << "交換後:" << endl; cout << "p的值是:" << p << ",q的值是:" << q << endl; cout << "p所指的值是:" << *p << ",q所指的值是:" << *q << endl; return 0; }
6.23
#include#include #include #include #include #include using namespace std; void print1(const int *p) { cout << *p << endl; } void print2(const int *p, const int sz) { int i = 0; while (i != sz) { cout << *p++ << endl; ++i; } } void print3(const int *b, const int *e) { for (auto q = b; q != e; ++q) cout << *q << endl; } int main() { int i = 0; int j[2] = { 0, 1 }; print1(&i); print1(j); print2(&i, 1); print2(j, sizeof(j) / sizeof(int)); auto a = begin(j); auto b = end(j); print3(a, b); return 0; }
6.25
#include#include #include #include #include #include using namespace std; int main(int argc, char **argv) { string str; for (int i = 0; i != argc; i++) str += argv[i]; cout << str << endl; return 0; }
6.26
#include#include #include #include #include #include using namespace std; int main(int argc, char **argv) { for (int i = 0; i != argc; i++) { cout << "argc[" << i << "]:" << argv[i] << endl; } return 0; }
6.27
#include#include #include #include #include #include using namespace std; int iCount(initializer_list il) { int count = 0; for (auto val : il) count += val; return count; } int main() { cout << "1,6,9的和是:" << iCount({ 1, 6, 9 }) << endl; cout << "4,5,9,18的和是:" << iCount({ 4, 5, 9, 18 }) << endl; cout << "10,10,10,10,10,10,10,10,10的和是:" << iCount({ 10, 10, 10, 10, 10, 10, 10, 10, 10 }) << endl; return 0; }
#include#include #include #include #include #include using namespace std; void print(vector v, int index) { unsigned sz = v.size(); if (!v.empty() && index < sz) { cout << v[index] << endl; print(v, index + 1); } } int main() { vector v = { 1, 3, 5, 7, 9, 11, 13, 15 }; print(v, 0); return 0; }
6.33
#pragma warning(disable:4996) #include#include #include #include #include #include #include #include #include using namespace std; void print(vector vInt, unsigned index) { unsigned sz = vInt.size(); if (!vInt.empty() && index < sz) { cout << vInt[index] << endl; print(vInt, index + 1); } } int main() { vector v = { 1, 3, 5, 7, 9, 11, 13, 15 }; print(v, 0); return 0; }
6.38
#pragma warning(disable:4996) #include#include #include #include #include #include #include #include #include using namespace std; string make_plural(size_t ctr, const string &word, const string &ending = "s") { return (ctr > 1) ? word + ending : word; } int main() { cout << "success的單數形式:" << make_plural(1, "success", "es") << endl; cout << "success的復數形式:" << make_plural(2, "success", "es") << endl; cout << "failure的單數形式:" << make_plural(1, "failure") << endl; cout << "failure的單數形式:" << make_plural(2, "failure") << endl; return 0; }
6.47
#pragma warning(disable:4996) #include#include #include #include #include #include #include #include #include using namespace std; void print(vector vInt, unsigned index) { unsigned sz = vInt.size(); #ifndef NDEBUG cout << "vector對象的大小:" << sz << endl; #endif if (!vInt.empty() && index < sz) { cout << vInt[index] << endl; print(vInt, index + 1); } } int main() { vector v = { 1, 3, 5, 7, 9, 11, 13, 15 }; print(v, 0); return 0; }
6.51
#pragma warning(disable:4996) #include#include #include #include #include #include #include #include #include using namespace std; void f() { cout << "該函數無須參數" << endl; } void f(int) { cout << "該函數有一個整型參數" << endl; } void f(int, int) { cout << "該函數有兩個整型參數" << endl; } void f(double a, double b = 3.14) { cout << "該函數有兩個雙精度浮點型參數" << endl; } int main() { //二義性f(2.56, 42); f(42); f(42, 0); f(2.56, 3.14); }
6.55
#pragma warning(disable:4996) #include#include #include #include #include #include #include #include #include using namespace std; int func1(int a, int b) { return a + b; } int func2(int a, int b) { return a - b; } int func3(int a, int b) { return a*b; } int func4(int a, int b) { return a / b; } int main() { decltype(func1)* p1 = func1, *p2 = func2, *p3 = func3, *p4 = func4; vector vF = { p1, p2, p3, p4 }; return 0; }
6.56
#pragma warning(disable:4996) #include#include #include #include #include #include #include #include #include using namespace std; int func1(int a, int b) { return a + b; } int func2(int a, int b) { return a - b; } int func3(int a, int b) { return a*b; } int func4(int a, int b) { return a / b; } void Compute(int a, int b, int(*p)(int, int)) { cout << p(a, b) << endl; } int main() { int i = 5, j = 10; decltype(func1)* p1 = func1, *p2 = func2, *p3 = func3, *p4 = func4; vector vF = { p1, p2, p3, p4 }; for (auto p : vF) { Compute(i, j, p); } return 0; }