c++primer第五版课后练习答案(第三章)
2014-10-20 21:50
393 查看
chapter1_3.2
chapter1_3.7
chapter1_3.8
chapter1_3.10
chapter1_3.14
chapter1_3.15
chapter1_3.15
chapter1_3.17
chapter1_3.20
chapter1_3.22
chapter1_3.24
chapter1_3.25
#include "stdafx.h" #include "string" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string line; while (getline(cin, line)) { cout << line<<endl ; } return 0; } /* int _tmain(int argc, _TCHAR* argv[]) { string word; while (cin>>word) { cout << word<<endl; } return 0; }*/chapter1_3.6
#include "stdafx.h" #includ<pre name="code" class="cpp">#include "stdafx.h" #include "string" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string line; getline(cin, line); for (auto &c : line) { if (!ispunct(c)) { cout << c; } } cout << endl; return 0; }
chapter1_3.7
#include "stdafx.h" #include "iostream" #include "string" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string s("some string"); cout << s << endl; for (char &c : s) //为什么此处c必须是引用,才能改变字符的值 { if (c!=' ') c = 'X'; } cout << s << endl; return 0; }
//运行结果是同样可以实现改变字符功能
chapter1_3.8
#include "stdafx.h" #include "iostream" #include "string" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string s("some string"); cout << s << endl; decltype(s.size()) i = 0; while (i < s.size) { if (s[i] != ' ') { s[i] = 'X'; } i++; } /* for (decltype(s.size()) i = 0; i < s.size(); i++) { if (s[i] != ' ') { s[i] = 'X'; } } */ cout << s << endl; return 0; }
chapter1_3.10
#include "stdafx.h" #include "string" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string line; getline(cin, line); for (auto &c : line) { if (!ispunct(c)) { cout << c; } } cout << endl; return 0; }
chapter1_3.14
#include "stdafx.h" #include <vector> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> ivec; int i; while (cin >> i) { ivec.push_back(i); } return 0; }
chapter1_3.15
#include "stdafx.h" #include <vector> #include <iostream> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string word; vector<string> ivec; while (cin >> word) ivec.push_back(word); return 0; }
chapter1_3.15
#include "stdafx.h" #include <vector> #include <iostream> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> v1; //空vector对象,无元素; vector<int> v2(10); //10个元素,初始值为0; vector<int> v3(10, 42); //十个水元素,初始值均为42; vector<int> v4{ 10 }; //一个元素,值为10; vector<int> v5{ 10, 42 }; //两个元素,分别为10、42; vector<string> v6{ 10 }; //十个默认初始化的元素 vector<string> v7{ 10, "hi" };//十个初始化值为“hi”的元素 return 0; }
chapter1_3.17
#include "stdafx.h" #include <vector> #include <iostream> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string word; vector <string> ivec; while (cin>>word) { ivec.push_back(word); } for (vector<string>::size_type ix = 0; ix != ivec.size(); ++ix) { for (string::size_type index = 0; index != ivec[ix].size(); ++index) { if (islower(ivec[ix][index])) { ivec[ix][index] = toupper(ivec[ix][index]); } } } for (string::size_type i = 0; i<ivec.size();++i) { cout <<ivec[i]<<endl; } return 0; }
chapter1_3.20
#include "stdafx.h" #include <vector> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> ivec; int i; while (cin >> i) { ivec.push_back(i); } /* 输出每对相邻整数的和 for (int i = 0; i < ivec.size()-1; ) { cout << ivec[i] + ivec[i + 1]<<endl; i = i + 1; } */ //依次输出首尾整数的和 for (int i = 0, j = ivec.size() - 1; i<ivec.size()/2; i++,j--) { cout << ivec[i] + ivec[j] << endl; } return 0; }
chapter1_3.22
#include "stdafx.h" #include <iostream> #include <vector> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string text("somg string"); for (auto it = text.begin();it != text.end()&&!isspace(*it); ++it) { *it = toupper(*it); cout << *it; } return 0; }chapter1_3.23
#include "stdafx.h" #include <string> #include <iostream> #include <vector> #include <iomanip> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> ivec; int i,j=1; while (j <11) { cin >> i; ivec.push_back(i); j++; } for (auto it = ivec.begin(); it != ivec.end(); ++it) { *it = (*it)*2; } for (auto it = ivec.begin(); it != ivec.end(); ++it) { cout << setw(6)<<*it; } return 0; }
chapter1_3.24
#include "stdafx.h" #include <vector> #include <string> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> ivec; int i; while (cin >> i) { ivec.push_back(i); } for (auto it = ivec.begin(); it != ivec.end(); ++it) { if ((it + 1) != ivec.end()) cout << (*it) + (*it + 1) << endl; } auto p = ivec.begin(); auto q = ivec.end() - 1; for (int i=0;i<ivec.size()/2;++p,--q,i++) cout << *p + (*q) << endl; return 0; }
chapter1_3.25
#include "stdafx.h" #include <vector> #include <iostream> #include <iomanip> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<unsigned> ivec; unsigned grade; while (cin >> grade) { ivec.push_back(grade); } for (int n = 0; n < 11; n++) { int num = 0; for (auto is = ivec.begin(); is != ivec.end(); is++) { if ((*is) / 10 == n) num++; } cout << setw(4) << num; } cout << endl; return 0; }
相关文章推荐
- 模板元编程 第三章课后练习(非答案)
- c++primer第五版课后练习答案(第九章)
- C与指针课后答案与编程练习(第一章)
- C++primer plus第六版课后编程练习答案3.3
- C++primer plus第六版课后编程练习答案4.6
- C++primer plus第六版课后编程练习答案5.7
- C++primer plus第六版课后编程练习答案7.4
- c++primer第五版课后练习答案(第十章)
- C++ primer plus第六版课后编程练习答案:6.3
- C++primer第5版课后练习习题答案9.28
- objective-C第十五章课后练习答案
- C++primer plus第六版课后编程练习答案3.4
- C++primer plus第六版课后编程练习答案5.8
- C++primer plus第六版课后编程练习答案7.5
- C++primer第5版课后练习习题答案9.26
- c++primer第五版课后练习答案(第一章)
- C++primer plus第六版课后编程练习答案3.5
- C++primer plus第六版课后编程练习答案5.9
- C++primer plus第六版课后编程练习答案7.6
- C++ primer plus第六版课后编程练习答案:6.4