1
#include <iostream> void show(const char *str, int n = 1); int main() { show("hello world!"); show("你好世界!",5); system("pause"); return 0; } void show(const char *str, int n) { if (n == 0) return; else { std::cout << str << std::endl; show(str, n - 1); } }
2
#include <iostream> #include <cstring> using namespace std; struct CandyBar { char name[40]; double weight; int heat; }; void input(CandyBar &pa, const char *st = "Millennium Munch", double a = 2.85, int b = 350); void show(const CandyBar &pa); int main() { CandyBar cbar; input(cbar); show(cbar); system("pause"); return 0; } void input(CandyBar &pa, const char *st, double a, int b) { strcpy_s(pa.name, 40, st); pa.weight = a; pa.heat = b; } void show(const CandyBar &pa) { cout << pa.name << " " << pa.weight << " " << pa.heat << endl; }
3
#include <iostream> #include <cctype> #include <string> using namespace std; void Uppercase(string &st); int main() { string str; cout << "请输入字符串[q退出]: "; while (getline(cin, str) && (str != "q")) { Uppercase(str); cout << str << endl; cout << "请输入字符串[q退出]: "; } cout << "再见.\n"; system("pause"); return 0; } void Uppercase(string &st) { for (int i = 0; i < int(st.size()); i++) { st[i] = toupper(st[i]); } }
4
#include <iostream> #include <cstring> using namespace std; struct stringy { char *str; int ct; }; void set(stringy &pa, const char *st); void show(const stringy &pa, int n = 1); void show(const char *st, int n = 1); int main() { stringy beany; char testing[] = "Reality isn't what it used to be."; set(beany, testing); show(beany); show(beany, 2); testing[0] = 'D'; testing[1] = 'u'; show(testing); show(testing, 3); show("Done!"); delete[]beany.str; system("pause"); return 0; } void show(const char *st, int n) { for (int i = 0; i < n; i++) cout << st << endl; } void show(const stringy &pa, int n) { for (int i = 0; i < n; i++) cout << pa.str << endl << pa.ct << endl; } void set(stringy &pa, const char *st) { pa.str = new char[strlen(st) + 1]; strcpy_s(pa.str, strlen(st) + 1, st); pa.ct = strlen(st); }
5
#include <iostream> template <typename T> T max5(const T *ar); int main() { using namespace std; int num[5] = { 5,7,8,2,3 }; double number[5] = { 2.5,8.3,1.6,7.2,9.1 }; cout << max5(num) << endl; cout << max5(number) << endl; system("pause"); return 0; } template <typename T> T max5(const T *ar) { T max = 0; for (int i = 0; i < 5; i++) { if (max < ar[i]) max = ar[i]; } return max; }
6
#include <iostream> #include <cstring> template <typename T> T maxn(const T *ar, int n); const char *maxn(const char *ar[], int n); int main() { using namespace std; int m[6] = { 5,3,7,8,6,1 }; double n[4] = { 4.2,5.1,6.5,7.3 }; const char *str[] = { "hello word!","Hi.","Thank you.","China.","Good." }; cout << maxn(m, 6) << endl; cout << maxn(n, 4) << endl; cout << maxn(str, 5) << endl; system("pause"); return 0; } const char *maxn(const char *ar[], int n) { const char *max = NULL; int m = 0; for (int i = 0; i < n; i++) { if (m < int(strlen(ar[i]))) { m = strlen(ar[i]); max = ar[i]; } } return max; } template <typename T> T maxn(const T *ar, int n) { T max = 0; for (int i = 0; i < n; i++) { if (max < ar[i]) max = ar[i]; } return max; }
7
#include <iostream> template <typename T> T ShowArray(T arr[], int n); template <typename T> T ShowArray(T * arr[], int n); struct debts { char name[50]; double amount; }; int main() { using namespace std; int things[6] = { 13, 31, 103, 301, 310, 130 }; struct debts mr_E[3] = { { "Ima Wolfe", 2400.0 }, { "Ura Foxe", 1300.0 }, { "Iby Stout", 1800.0 } }; double * pd[3]; for (int i = 0; i < 3; i++) pd[i] = &mr_E[i].amount; cout << "Listing Mr. E's counts of things:\n"; cout << ShowArray(things, 6) << endl; cout << "Listing Mr. E's debts:\n"; cout << ShowArray(pd, 3) << endl; system("pause"); return 0; } template <typename T> T ShowArray(T arr[], int n) { using namespace std; T sum = 0; cout << "template A\n"; for (int i = 0; i < n; i++) sum += arr[i]; return sum; } template <typename T> T ShowArray(T * arr[], int n) { using namespace std; T sum = 0; cout << "template B\n"; for (int i = 0; i < n; i++) sum += *arr[i]; return sum; }