- 博客(126)
- 收藏
- 关注
原创 7.6 7.7
#include#includeusing namespace std;struct Sales_data{ string const &isbn() const{ return bookNo; }; Sales_data &combine(const Sales_data&); string bookNo; unsigned units_sold = 0; d
2016-09-27 19:33:28 411
原创 7.4 7.5
#includeclass Person{private: string strName; string strAddress;public: string getName() const{ return strName; }//常量成员函数 string getAddress() const { return getAddress; }};
2016-09-27 16:51:11 418
原创 7.1
#include#includeusing namespace std;struct Sales_data{ std::string bookNo; unsigned units_sold = 0; double revenue = 0.0;};int main(){ Sales_data total; if (cin >> total.bookNo >> total.
2016-09-24 09:05:15 338
原创 6.44
#include#includeusing namespace std;inline bool is_shorter(const string &lft, const string &rht){ return lft.size() < rht.size();}int main(){ cout << is_shorter("pezy", "mooophy") << endl;
2016-09-24 08:38:49 428
原创 6.42
#include#include#includeusing namespace std;string make_plural(size_t ctr, const string &word, const string &ending = "s"){ return (ctr > 1) ? word + ending : word;}int main(){ cout << ma
2016-09-24 08:35:52 385
原创 6.33
#include#includeusing namespace std;void print(vector::iterator first, vector::iterator last){ if (first != last) { cout << *first << " "; print(++first, last); }}int main(){ vectorv
2016-09-23 14:55:30 420
原创 6.27
#include#includeusing namespace std;int sum(initializer_list const &il){ int sum = 0; for (auto i : il) sum += i; return sum;}int main(){ auto il = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; cout
2016-09-23 14:43:32 284
原创 6.22
#include#includeusing namespace std;void swap(int *&lft, int *&rht){ auto tmp = lft; lft = rht; rht = tmp;}int main(){ int i = 42, j = 99; auto lft = &i; auto rht = &j; swap(lft, rht);
2016-09-23 14:09:47 318
原创 6.21
#includeusing namespace std;int large_one(const int i, const int *p){ return (i > *p) ? i : *p;}int main(){ int i = 6; cout << large_one(8, &i) << endl; return 0;}
2016-09-23 14:04:34 309
原创 6.17
#include#includeusing namespace std;bool any_capital(string const &str)//不需要修改 设为常量引用{ for (auto ch : str) if (isupper(ch)) return true; return false;}void to_lowercase(string &str)//需要修改
2016-09-23 13:46:56 406
原创 6.12
#includeusing namespace std;void swap(int &lhs,int &rhs)//lhs=left hand side { int temp = lhs; lhs = rhs; rhs = temp;}int main(){ for (int left, right; cin >> left >> right;) { swap(left
2016-09-23 11:16:42 288
原创 6.10
#includeusing namespace std;void swap(int *lhs, int *rhs){ int tmp; tmp = *lhs; *lhs = *rhs; *rhs = tmp;}int main(){ for (int lft, rht; cin >> lft>>rht;) { swap(&lft, &rht); cout << l
2016-09-23 11:01:05 231
原创 6.8
int fact(int val);int func();templateT abs(T i){ return i > 0 ? i : -i;}
2016-09-23 10:35:34 272
原创 6.7
#includeusing namespace std;size_t count_add(int n){ static size_t ctr = 0;//函数体内如果在定义静态变量的同时进行了初始化,则以后程序不再进行初始化操作(出现在函数内部的基本类型的的静态变量初始化语句只有在第一次调用才执行)//而对自动变量赋初值是在函数调用时进行,每调用一次函数重新给一次初值,相当于执行一次
2016-09-23 10:32:44 264
原创 6.6
#includeusing namespace std;size_t count_add(int n){ static size_t ctr = 0;//函数体内如果在定义静态变量的同时进行了初始化,则以后程序不再进行初始化操作(出现在函数内部的基本类型的的静态变量初始化语句只有在第一次调用才执行)//而对自动变量赋初值是在函数调用时进行,每调用一次函数重新给一次初值,相当于执行一次
2016-09-23 10:31:06 353
原创 6.5
#includeusing namespace std;int abs(int i){ return i > 0 ? i : -i;}int main(){ for (int i; cin >> i; cout << abs(i) << endl); return 0;}
2016-09-23 10:13:47 276
转载 6.4
#includeusing namespace std;int face(int i){ return i > 1 ? i*face(i - 1) : 1;}int main(){ for (int i; cin >> i; cout << face(i) << endl); return 0;}
2016-09-23 10:12:51 478
原创 6.3
#includeusing namespace std;int face(int i){ return i > 1 ? i*face(i - 1) : 1;}int main(){ cout << face(5) << endl; return 0;}
2016-09-23 10:10:36 272
原创 5.21
#include#include#includeusing namespace std;int main(){ string read, tmp; int flag = 0; while (cin >> read) { if (isupper(read[0]) && read == tmp) { flag = 1; break; } else tmp
2016-09-23 10:02:23 207
原创 5.20
#include#include#includeusing namespace std;int main(){ string read, tmp; int flag = 0; while (cin >> read) { if (read == tmp) { flag = 1; break; } else tmp = read; } if (fla
2016-09-23 09:58:55 223
原创 5.19
#include#include#includeusing namespace std;int main(){ string s1, s2; cin >> s1 >> s2; do{ cout << (s1.size() < s2.size()? s1 : s2) << endl; } while (cin >> s1 >> s2); return 0;}
2016-09-22 19:38:50 225
原创 5.17
#include#includeusing namespace std;bool is_prefix(vector const &lhs, vector const &rhs)//prefix 前缀// C++ 常量指针或者常量引用作为函数参数传递的原因 1.当传递较大的数据结构作为参数时,用引用或者指针作为函数参数可以节省 一个复制的过程,提高效率。{ if (lhs.size(
2016-09-22 19:33:45 278
原创 5.14
#include#include#includeusing namespace std;int main(){ pairmax_duplicated; int count = 0; for (string str, prestr; cin >> str; prestr = str) { if (str == prestr) ++count; else count
2016-09-22 19:19:13 341
原创 5.12
#include#include#includeusing namespace std;int main(){ unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, spacecnt = 0, tabcnt = 0, newlinecnt = 0, ficnt = 0, flcnt = 0, ffcnt = 0; ch
2016-09-22 18:59:31 294
原创 5.11
#include#include#includeusing namespace std;int main(){ unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, spacecnt = 0, tabcnt = 0, newlinecnt = 0; char ch; while (cin >> ch) { sw
2016-09-22 16:57:35 266
原创 5.10
#include#include#includeusing namespace std;int main(){ unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0; char ch; while (cin >> ch) { switch (ch) { case'a': case'A': ++a
2016-09-22 16:52:35 289
原创 5.9
#include#include#includeusing namespace std;int main(){ unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0; char ch; while (cin >> ch) { if (ch == 'a')++acnt; else if (ch == 'e')+
2016-09-22 16:46:37 398
原创 5.6
#include#include#includeusing namespace std;int main(){ vector scores = { "F", "D", "C", "B", "A", "A++" }; int grade; while (cin >> grade) { string lettergrade = grade < 60 ? scores[0] :
2016-09-22 16:33:21 228
原创 5.5
#include#include#includeusing namespace std;int main(){ vector scores = { "F", "D", "C", "B", "A", "A++" }; for (int g; cin >> g;) { string letter; if (g < 60) cout << (letter = scores
2016-09-22 15:25:00 248
原创 4.22
#includeusing namespace std;int main(){ for (unsigned g; cin >> g;) { auto result = g > 90 ? "high pass" : g < 60 ? "fail" : g < 75 ? "low pass" : "pass"; cout << result << endl; if (g>90
2016-09-22 14:56:04 267
原创 4.21
#include#includeusing namespace std;int main(){ vector vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (auto i : vec) cout << ((i % 2 != 0) ? i *= 2 : i) << " "; cout << endl; return 0;}
2016-09-22 14:49:07 289
原创 3.45
#includeusing namespace std;int main(){ int arr[3][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } }; for (auto &p:arr) for (int q : p) cout << q << " "; cout << endl; for
2016-09-22 14:43:27 289
原创 3.44
#includeusing int_array = int[4];using namespace std;int main(){ int arr[3][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } }; for (const int(&row)[4] : arr) for (int col : ro
2016-09-22 14:37:35 301
原创 3.43
#includeusing namespace std;int main(){ int arr[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }; for (const int(&row)[4]:arr) for (int col : row) cout << col << " "; cout << endl;
2016-09-22 14:33:07 394
原创 3.42
#include#include#includeusing namespace std;int main(){ vector vec = { 0, 1, 2, 3, 4, 5 }; int arr[6]; for (int i = 0; i <= vec.size() - 1; i++) arr[i] = vec[i]; for (auto i : arr) cout
2016-09-22 09:39:12 467
原创 3.41
#include#include#includeusing namespace std;int main(){ int arr[5] = { 0, 1, 2, 3, 4 }; vector vec; for (auto i : arr) vec.push_back(i); for (auto i : vec) cout << i << " "; cout << en
2016-09-22 09:36:22 376
原创 3.40
#include#includeusing namespace std;const char cstr1[] = "Hello";const char cstr2[] = "world";int main(){ constexpr size_t new_size = strlen(cstr1) + strlen(" ") + strlen(cstr2) + 1; char cst
2016-09-22 09:16:39 452
原创 3.39
#includeusing namespace std;int main(){ string s1("moophy"), s2("pezy"); if (s1 == s2)//如果s1和s2所含的字符完全一样,则他们相等 cout << "same" << endl; else if (s1 > s2) cout pezy" << endl; else cout <<
2016-09-22 08:53:20 313
原创 3.36
#include#include#includeusing namespace std;bool compare(int *const pb1, int *const pe1, int *const pb2, int *const pe2){ if ((pe1 - pb2) != (pe2 - pb2)) return false; else { for (int *i
2016-09-22 08:38:50 335
原创 3.35
#includeusing namespace std;int main(){ const int size = 10; int arr[size]; for (auto ptr = arr; ptr != arr + size; ptr++)//指针和迭代器很像 可以执行迭代器运算 *ptr = 0; for (auto i : arr)//遍历数组也可以用范围for语句
2016-09-22 08:12:09 291
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人