C++Primer第三章(第五版) 课后习题 程序题 源代码

    xiaoxiao2025-06-07  17

    3.1

    #include<iostream> using std::cout; using std::endl; int main() { int sum = 0; int i = 50; while (i <= 100) { sum += i; i++; } cout << "50到100之间整数之和为" << sum << endl; return 0; } #include<iostream> using std::cout; using std::endl; int main() { int i = 10; while (i >= 0) { cout << i << " "; i--; } cout << endl; return 0; } #include<iostream> using std::cin; using std::cout; using std::endl; int main() { cout << "请输入两个数"; cout << endl; int v1, v2; cin >> v1 >> v2; if (v1 > v2) while (v1 >= v2){ cout << v1 << " "; v1--; } else while (v1 <= v2){ cout << v1 << " "; v1++; } cout << endl; return 0; } #include<iostream> #include<string> using std::cin; using std::cout; using std::endl; using std::istream; using std::ostream; using std::string; class Sales_data{ friend istream& operator>>(istream&, Sales_data&); friend ostream& operator<<(ostream&, const Sales_data&); friend bool operator<(const Sales_data&, const Sales_data&); friend bool operator==(const Sales_data&, const Sales_data&); public: Sales_data() = default; Sales_data(const string &book) :bookNo(book){} Sales_data(istream &is){ is >> *this; } public: Sales_data& operator+=(const Sales_data&); string isbn()const{ return bookNo; } private: string bookNo; unsigned units_sold = 0; double sellingprice = 0.0; double saleprice = 0.0; double discount = 0.0; }; inline bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) { return lhs.isbn() == rhs.isbn(); } Sales_data operator+(const Sales_data&, const Sales_data&); inline bool operator==(const Sales_data &lhs, const Sales_data &rhs) { return lhs.units_sold == rhs.units_sold && lhs.sellingprice == rhs.sellingprice && lhs.saleprice == rhs.saleprice&& lhs.isbn() == rhs.isbn(); } inline bool operator !=(const Sales_data &lhs, const Sales_data& rhs) { return !(lhs == rhs); } Sales_data& Sales_data::operator+=(const Sales_data& rhs) { units_sold += rhs.units_sold; saleprice = (rhs.saleprice*rhs.units_sold + saleprice*units_sold) / (rhs.units_sold + units_sold); if (sellingprice != 0) discount = saleprice / sellingprice; return *this; } Sales_data operator +(const Sales_data&lhs, const Sales_data& rhs) { Sales_data ret(lhs); ret += rhs; return ret; } std::istream& operator>>(std::istream&in, Sales_data&s) { in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice; if (in&&s.sellingprice != 0) s.discount = s.saleprice / s.sellingprice; else s = Sales_data(); return in; } std::ostream& operator<<(std::ostream&out, const Sales_data& s) { out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " << s.discount; return out; } int main() { Sales_data book; cout << "请输入销售记录:" << endl; while (cin >> book) { cout << "ISBN、售出本数、原始价格、实售价格、折扣为" << book << endl; } Sales_data trans1, trans2; cout << "请输入两条ISBN相同的销售记录:" << endl; cin >> trans1 >> trans2; if (compareIsbn(trans1, trans2)) cout << "汇总信息:ISBN、售出本数、实售价格、折扣为" << trans1 + trans2 << endl; else cout << "两条销售记录的ISBN不同" << endl; Sales_data total, trans; cout << "请输入几条ISBN相同的销售记录:" << endl; if (cin >> total) { while (cin >> trans) if (compareIsbn(total, trans)) total = total + trans; else { cout << "当前书籍ISBN不同" << endl; break; } cout << "有效汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为" << total << endl; } else { cout << "没有数据" << endl; return -1; } int num = 1; cout << "请输入若干销售记录:" << endl; if (cin >> trans1) { while (cin >> trans2) if (compareIsbn(trans1, trans2)) num++; else { cout << trans1.isbn() << "共有" << num << "条销售记录" << endl; trans1 = trans2; num = 1; } cout << trans1.isbn() << "共有" << num << "条销售记录" << endl; } else { cout << "没有数据" << endl; return -1; } return 0; }

    3.2

    #include<iostream> #include<string> using namespace std; int main() { string line; cout << "请输入您的字符串,可以包含空格:" << endl; while (getline(cin, line)) cout << line << endl; return 0; } #include<iostream> #include<string> using namespace std; int main() { string word; cout << "请输入您的单词,不可以包含空格:" << endl; while (cin >> word) cout << word << endl; return 0; }

    3.3

    #include<iostream> #include<string> using namespace std; int main() { string word, line; cout << "请选择读取字符串的方式:1表示逐词读取,2表示整行读取" << endl; char ch; cin >> ch; if (ch == '1') { cout << "请输入字符串: Welcome to C++ family! " << endl; cin >> word; cout << "系统读取的有效字符串是:" << endl; cout << word << endl; return 0; } cin.clear(); cin.sync(); if (ch == '2') { cout << "请输入字符串: Welcome to C++ family! " << endl; getline(cin, line); cout << "系统读取的有效字符串是:" << endl; cout << line << endl; return 0; } cout << "您的输入有误!"; return -1; }

    3.4

    #include<iostream> #include<string> using namespace std; int main() { string s1, s2; cout << "请输入两个字符串:" << endl; cin >> s1 >> s2; if (s1 == s2) cout << "两个字符串相等" << endl; else if (s1 > s2) cout << s1 << "大于" << s2 << endl; else cout << s2 << "大于" << s1 << endl; return 0; } #include<iostream> #include<string> using namespace std; int main() { string s1, s2; cout << "请输入两个字符串:" << endl; cin >> s1 >> s2; auto len1 = s1.size(); auto len2 = s2.size(); if (len1 == len2) cout << s1 << "和" << s2 << "的长度都是" << len1 << endl; else if (len1 > len2) cout << s1 << "比" << s2 << "的长度多" << len1 - len2 << endl; else cout << s1 << "比" << s2 << "的长度小" << len2 - len1 << endl; return 0; }

    3.5

    #include<iostream> #include<string> using namespace std; int main() { char cont = 'y'; string s, result; cout << "请输入第一个字符串:" << endl; while (cin >> s) { result += s; cout << "是否继续(y or n)?" << endl; cin >> cont; if (cont == 'y' || cont == 'Y') { cout << "请输入下一个字符串:" << endl; } else break; } cout << "拼接后的字符串是:" << result << endl; return 0; } #include<iostream> #include<string> using namespace std; int main() { char cont = 'y'; string s, result; cout << "请输入第一个字符串:" << endl; while (cin >> s) { if (!result.size()) { result += s; } else { result = result + " " + s; } cout << "是否继续(y or n)?" << endl; cin >> cont; if (cont == 'y' || cont == 'Y') { cout << "请输入下一个字符串:" << endl; } else break; } cout << "拼接后的字符串是:" << result << endl; return 0; }

    3.6

    #include<iostream> #include<string> using namespace std; int main() { string s; cout << "请输入一个字符串,可以包含空格:" << endl; getline(cin, s); for (auto &c : s) { c = 'X'; } cout << s << endl; return 0; }

    3.7

    #include<iostream> #include<string> using namespace std; int main() { string s; cout << "请输入一个字符串,可以包含空格:" << endl; getline(cin, s); for (char &c : s) { c = 'X'; } cout << s << endl; return 0; }

    3.8

    #include<iostream> #include<string> using namespace std; int main() { string s; cout << "请输入一个字符串,可以包含空格:" << endl; getline(cin, s); int i = 0; while (s[i] != '\0') { s[i] = 'X'; ++i; } cout << s << endl; return 0; } #include<iostream> #include<string> using namespace std; int main() { string s; cout << "请输入一个字符串,可以包含空格:" << endl; getline(cin, s); for (unsigned int i = 0; i < s.size(); i++) { s[i] = 'X'; } cout << s << endl; return 0; }

    3.10

    #include<iostream> #include<string> #include<cctype> using namespace std; int main() { string s; cout << "请输入一个字符串,最好含有某些标点符号:" << endl; getline(cin, s); for (auto c:s) { if (!ispunct(c)) cout << c; } cout <<endl; return 0; } #include<iostream> #include<string> #include<cctype> using namespace std; int main() { string s,result; cout << "请输入一个字符串,最好含有某些标点符号:" << endl; getline(cin, s); for (decltype(s.size())i = 0; i < s.size(); i++) { if (!ispunct(s[i])) result += s[i]; } cout << result << endl; return 0; }

    3.11

    #include<iostream> #include<string> using namespace std; int main() { const string s = "Keep out!"; for (auto &c : s) { c = 'X'; } return 0; }

    3.14

    #include<iostream> #include<vector> using namespace std; int main() { vector<int> vInt; int i; char cont='y'; while(cin>>i) { vInt.push_back(i); cout<<"您要继续(y or n)?"<<endl; cin>>cont; if(cont!='y'&&cont!='y') break; } for(auto mem : vInt ) cout << mem<<" "; return 0; }

    3.15

    #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<string> vString; string s; char cont = 'y'; while (cin >> s) { vString.push_back(s); cout << "您要继续吗(y or n)?" << endl; cin >> cont; if (cont != 'y'&&cont != 'Y') break; } for (auto mem : vString) cout << mem << " "; cout << endl; return 0; }

    3.16

    #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<int> v1; vector<int> v2(10); vector<int> v3(10, 42); vector<int> v4{ 10 }; vector<int> v5{ 10, 42 }; vector<string> v6{ 10 }; vector<string> v7{ 10, "hi" }; cout << "v1的元素个数是:" << v1.size() << endl; if (v1.size() > 0) { cout << "v1的元素分别是:" << endl; for (auto e : v1) { cout << e << " "; cout << endl; } } cout << "v2的元素个数是:" << v1.size() << endl; if (v2.size() > 0) { cout << "v2的元素分别是:" << endl; for (auto e : v2) { cout << e << " "; cout << endl; } } cout << "v3的元素个数是:" << v1.size() << endl; if (v3.size() > 0) { cout << "v3的元素分别是:" << endl; for (auto e : v3) { cout << e << " "; cout << endl; } } cout << "v4的元素个数是:" << v1.size() << endl; if (v4.size() > 0) { cout << "v4的元素分别是:" << endl; for (auto e : v4) { cout << e << " "; cout << endl; } } cout << "v5的元素个数是:" << v5.size() << endl; if (v5.size() > 0) { cout << "v5的元素分别是:" << endl; for (auto e : v5) { cout << e << " "; cout << endl; } } cout << "v6的元素个数是:" << v6.size() << endl; if (v6.size() > 0) { cout << "v6的元素分别是:" << endl; for (auto e : v6) { cout << e << " "; cout << endl; } } cout << "v7的元素个数是:" << v7.size() << endl; if (v7.size() > 0) { cout << "v7的元素分别是:" << endl; for (auto e : v7) { cout << e << " "; cout << endl; } } return 0; }

    3.17

    #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<string> vString; string s; char cont = 'y'; cout << "请输入第一个词: " << endl; while (cin >> s) { vString.push_back(s); cout << "您要继续吗(y or n)?" << endl; cin >> cont; if (cont != 'y'&&cont != 'Y') break; cout << "请输入下一个词:" << endl; } cout << "转换后的结果是: " << endl; for (auto &mem : vString) { for (auto &c : mem) { c = toupper(c); } cout << mem << endl; } return 0; }

    3.20

    #include<iostream> #include<vector> using namespace std; int main() { vector<int> vInt; int iVal; cout << "请输入一组数字:" << endl; while (cin >> iVal) vInt.push_back(iVal); if (vInt.size() == 0) { cout << "没有任何元素" << endl; return -1; } cout << "相邻两项的和依次是:" << endl; for (decltype(vInt.size())i = 0; i < vInt.size() - 1; i += 2) { cout << vInt[i] + vInt[i + 1] << " "; if ((i + 2) % 10 == 0) cout << endl; } if (vInt.size() % 2 != 0) cout << vInt[vInt.size() - 1]; return 0; } #include<iostream> #include<vector> using namespace std; int main() { vector<int> vInt; int iVal; cout << "请输入一组数字:" << endl; while (cin >> iVal) vInt.push_back(iVal); if (vInt.size() == 0) { cout << "没有任何元素" << endl; return -1; } cout << "首位两项的和依次是:" << endl; for (decltype(vInt.size())i = 0; i < vInt.size() / 2; i++) { cout << vInt[i] + vInt[vInt.size() - i - 1] << " "; if ((i + 1) % 5 == 0) cout << endl; } if (vInt.size() % 2 != 0) { cout << vInt[vInt.size() / 2]; } return 0; }

    3.21

    #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<int> v1; vector<int> v2(10); vector<int> v3(10, 42); vector<int> v4{ 10 }; vector<int> v5{ 10, 42 }; vector<string> v6{ 10 }; vector<string> v7{ 10, "hi" }; cout << "v1的元素个数是:" << v1.size() << endl; if (v1.cbegin() != v1.cend()) { cout << "v1的元素分别是:" << endl; for (auto it = v1.cbegin(); it != v1.cend(); it++) { cout << *it << " "; cout << endl; } } cout << "v2的元素个数是:" << v2.size() << endl; if (v2.cbegin() != v2.cend()) { cout << "v2的元素分别是:" << endl; for (auto it = v2.cbegin(); it != v2.cend(); it++) { cout << *it << " "; cout << endl; } } cout << "v3的元素个数是:" << v3.size() << endl; if (v3.cbegin() != v3.cend()) { cout << "v3的元素分别是:" << endl; for (auto it = v3.cbegin(); it != v3.cend(); it++) { cout << *it << " "; cout << endl; } } cout << "v4的元素个数是:" << v4.size() << endl; if (v4.cbegin() != v4.cend()) { cout << "v1的元素分别是:" << endl; for (auto it = v4.cbegin(); it != v4.cend(); it++) { cout << *it << " "; cout << endl; } } cout << "v5的元素个数是:" << v5.size() << endl; if (v5.cbegin() != v5.cend()) { cout << "v5的元素分别是:" << endl; for (auto it = v5.cbegin(); it != v5.cend(); it++) { cout << *it << " "; cout << endl; } } cout << "v6的元素个数是:" << v6.size() << endl; if (v6.cbegin() != v6.cend()) { cout << "v6的元素分别是:" << endl; for (auto it = v6.cbegin(); it != v6.cend(); it++) { cout << *it << " "; cout << endl; } } cout << "v7的元素个数是:" << v7.size() << endl; if (v7.cbegin() != v7.cend()) { cout << "v7的元素分别是:" << endl; for (auto it = v7.cbegin(); it != v7.cend(); it++) { cout << *it << " "; cout << endl; } } return 0; }

    3.22

    #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<string> text; string s; while (getline(cin, s)) text.push_back(s); for (auto it = text.begin(); it != text.end() && !it->empty(); it++) { for (auto it2 = it->begin(); it2 != it->end(); it2++) *it2 = toupper(*it2); cout << *it << endl; } return 0; }

    3.23

    #include<iostream> #include<vector> #include<cstdlib> #include<ctime> using namespace std; int main() { vector<int> vInt; srand((unsigned)time(NULL)); for (int i = 0; i < 10; i++) { vInt.push_back(rand() % 1000); } cout << "随机生成的10个数字是:" << endl; for (auto it = vInt.cbegin(); it != vInt.cend(); it++) { cout << *it << " "; } cout << endl; cout << "翻倍后的10个数字是: " << endl; for (auto it = vInt.begin(); it != vInt.end(); it++) { *it *= 2; cout << *it << " "; } cout << endl; return 0; }

    3.24

    #include<iostream> #include<vector> #include<cstdlib> #include<ctime> using namespace std; int main() { vector<int> vInt; srand((unsigned)time(NULL)); for (int i = 0; i < 10; i++) { vInt.push_back(rand() % 1000); } cout << "随机生成的10个数字是:" << endl; for (auto it = vInt.cbegin(); it != vInt.cend(); it++) { cout << *it << " "; } cout << endl; cout << "翻倍后的10个数字是: " << endl; for (auto it = vInt.begin(); it != vInt.end(); it++) { *it *= 2; cout << *it << " "; } cout << endl; return 0; } #include<iostream> #include<vector> using namespace std; int main() { vector<int> vInt; int iVal; cout << "请输入一组数据:" << endl; while (cin >> iVal) vInt.push_back(iVal); if (vInt.cbegin() == vInt.cend()) { cout << "没有任何元素" << endl; return -1; } cout << "首尾两项的和依次是: " << endl; auto beg = vInt.begin(); auto end = vInt.end(); for (auto it = beg; it != beg + (end-beg)/2;it++) { cout << (*it + *(beg + (end - it) - 1)) << " "; if ((it - beg + 1) % 5 == 0) cout << endl; } if (vInt.size() % 2 != 0) cout << *(beg + (end - beg) / 2); return 0; } #include<iostream> #include<vector> using namespace std; int main() { vector<unsigned> vUS(11); auto it = vUS.begin(); int iVal; cout << "请输入一组成绩(0~100): " << endl; while (cin >> iVal) if (iVal < 101) ++*(it + iVal / 10); cout << "您总计输入了" << vUS.size() << "个成绩" << endl; cout << "各分数段的人数分布是(成绩从低到高):" << endl; for (it = vUS.begin(); it != vUS.end(); it++) { cout << *it << " "; } cout << endl; return 0; }

    3.31

    #include<iostream> using namespace std; int main() { const int sz = 10; int a[sz]; for (int i = 0; i < sz; i++) a[i] = i; for (auto val : a) cout << val << " "; cout << endl; return 0; }

    3.32

    #include<iostream> using namespace std; int main() { const int sz = 10; int a[sz], b[sz]; for (int i = 0; i < sz; i++) a[i] = i; for (int j = 0; j < sz; j++) b[j] = a[j]; for (auto val : b) cout << val << " "; cout << endl; return 0; } #include<iostream> #include<vector> using namespace std; int main() { const int sz = 10; vector<int> vInt, vInt2; for (int i = 0; i < sz; i++) vInt.push_back(i); for (int j = 0; j < sz; j++) vInt2.push_back(vInt[j]); for (auto val : vInt2) cout << val << " "; cout << endl; return 0; }

    3.35

    #include<iostream> using namespace std; int main() { const int sz = 10; int a[sz], i = 0; for (i = 0; i < 10; i++) a[i] = i; cout << "初始状态下数组的内容是:" << endl; for (auto val : a) { cout << val << " "; } cout << endl; int *p = begin(a); while (p != end(a)) { *p = 0; p++; } cout << "修改后的数组内容是:" << endl; for (auto val : a) cout << val << " "; cout << endl; return 0; }

    3.36

    #pragma warning(disable:4996) #include<iostream> #include<ctime> #include<cstdlib> using namespace std; int main() { const int sz = 5; int a[sz], b[sz], i; srand((unsigned)time(NULL)); for (i = 0; i < sz; i++) a[i] = rand() % 10; cout << "系统数据已经生成,请输入您猜测的5个数(0~9),可以重复:" << endl; int uVal; for (i = 0; i < sz; i++) { if (cin >> uVal) b[i] = uVal; } cout << "系统生成的数据是:" << endl; for (auto val : a) cout << val << " "; cout << endl; cout << "您猜测的数据是:" << endl; for (auto val : b) cout << val << " "; cout << endl; int *p = begin(a), *q = begin(b); while (p != end(a) && q != end(b)) { if (*p != *q) { cout << "您的猜测错误,两个数组不想等" << endl; return -1; } p++; q++; } cout << "恭喜您全都猜对了!" << endl; return 0; } #pragma warning(disable:4996) #include<iostream> #include<ctime> #include<cstdlib> #include<vector> using namespace std; int main() { const int sz = 5; int i; vector<int> a, b; srand((unsigned)time(NULL)); for (i = 0; i < sz; i++) a.push_back(rand() % 10); cout << "系统数据已经生成,请输入您猜测的5个数(0~9),可以重复:" << endl; int uVal; for (i = 0; i < sz; i++) { if (cin >> uVal) b.push_back(uVal); } cout << "系统生成的数据是:" << endl; for (auto val : a) cout << val << " "; cout << endl; cout << "您猜测的数据是:" << endl; for (auto val : b) cout << val << " "; cout << endl; auto it1 = a.cbegin(), it2 = b.cbegin(); while (it1!=a.cend()&&it2!=b.cend()) { if (*it1!=*it2) { cout << "您的猜测错误,两个数组不想等" << endl; return -1; } it1++; it2++; } cout << "恭喜您全都猜对了!" << endl; return 0; }

    3.39

    #include<iostream> #include<string> using namespace std; int main() { string str1, str2; cout << "请输入两个字符串:" << endl; cin >> str1 >> str2; if (str1 > str2) cout << "第一个字符串大于第二个字符串" << endl; else if (str1 < str2) cout << "第一个字符串小于第二个字符串" << endl; else cout << "两个字符串相等" << endl; return 0; } #include<iostream> #include<string> using namespace std; int main() { char str1[80], str2[80]; cout << "请输入两个字符串: " << endl; cin >> str1 >> str2; auto result = strcmp(str1, str2); switch (result) { case 1: cout << "第一个字符串大于第二个字符串" << endl; break; case -1: cout << "第一个字符串小于第二个字符串" << endl; break; case 0: cout << "两个字符串相等" << endl; break; default: cout << "未定义的结果" << endl; break; } return 0; }

    3.40

    #include<iostream> #include<cstring> using namespace std; int main() { char str1[] = "Welcome to "; char str2[] = "C++ family!"; int k1, k2; char *result=new char[strlen(str1)+strlen(str2)- 1]; strcpy(result, str1); strcat(result, str2); cout << "第一个字符串是:" << str1 << endl; cout << "第二个字符串是:" << str2 << endl; cout << "拼接后的字符串是:" << result << endl; return 0; }

    3.41

    #include<iostream> #include<vector> #include<ctime> #include<cstdlib> using namespace std; int main() { const int sz = 10; int a[sz]; srand((unsigned)time(NULL)); cout << "数组的内容是:" << endl; for (auto &val : a) { val = rand() % 100; cout << val << " "; } cout << endl; vector<int> vInt(begin(a), end(a)); cout << "vector的内容是:" << endl; for (auto val : vInt) { cout << val << " "; } cout << endl; return 0; }

    3.42

    #include<iostream> #include<vector> #include<ctime> #include<cstdlib> using namespace std; int main() { const int sz = 10; vector<int> vInt; srand((unsigned)time(NULL)); cout << "vector对象的内容是:" << endl; for (int i = 0; i != sz;i++) { vInt.push_back(rand() % 100); cout << vInt[i] << " "; } cout << endl; auto it = vInt.cbegin(); int a[vInt.size()]; cout << "数组的内容是:" << endl; for (auto &val :a) { val = *it; cout << val << " "; it++; } cout << endl; return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1299705.html
    最新回复(0)