C++ Primer (第五版)》
最近在补基础,在看《C++ Primer (第五版)》,先把 第三章Chap.3 自己写的代码题发上来。。不定期更新
3.4、3.5、3.6、3.10、3.14、3.17、3.20、3.23、3.24、3.25、3.32、3.35、3.36
3.4
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1, str2;
cin >> str1 >> str2;
cout << "str1: " << str1 << "\tstr2: " << str2 << endl;
string ret;
if(str1 != str2){
cout << (str1 > str2 ? str1 : str2) << endl;
}else{
cout << "str1 = str2" << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1, str2;
cin >> str1 >> str2;
cout << "str1: " << str1 << "\tstr2: " << str2 << endl;
string ret;
if(str1.size() != str2.size()){
cout << (str1.size() > str2.size() ? str1 : str2) << endl;
}else{
cout << "size of str1 = size of str2" << endl;
}
return 0;
}
3.5
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1, str2;
cout << "tips: Ctrl+Z to end." << endl;
while(cin >> str1){
str2 += str1;
}
cout << str2 << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1, str2;
cout << "tips: Ctrl+Z to end." << endl;
while(cin >> str1){
str2 = str2 + " " + str1;
}
cout << str2 << endl;
return 0;
}
3.6
#include <iostream>
#include <string>
using namespace std;
int main(){
string str("Hello World!!!");
for(auto &c : str){
c = 'X';
}
cout << str << endl;
return 0;
}
3.10
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1("Hello World!!!");
for(unsigned index = 0; index != str1.size(); index++){
if(!ispunct(str1[index])){
cout << str1[index];
}
}
return 0;
}
3.14
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> ivec;
int i;
while(cin >> i){
ivec.push_back(i);
}
return 0;
}
3.17
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
vector<string> str_vec;
string str;
while(cin >> str){
str_vec.push_back(str);
}
for(auto &s : str_vec){
for(char &c : s){
c = toupper(c);
}
cout << s << endl;
}
return 0;
}
3.20
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> i_vec;
int i, sum;
while(cin >> i){
i_vec.push_back(i);
}
for(auto i = 0; i != i_vec.size(); i++){
sum = i_vec[i] + i_vec[i+1];
cout << sum << " ";
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> i_vec;
int i, sum;
while(cin >> i){
i_vec.push_back(i);
}
for(auto i = 0; i != i_vec.size()/2; i++){
sum = i_vec[i] + i_vec[i_vec.size()-1 - i];
cout << sum << " ";
}
return 0;
}
3.23
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> i_vec;
int i, ret;
for(int count = 0; count < 10; count++){
cin >> i;
i_vec.push_back(i);
}
for(auto it = i_vec.begin(); it != i_vec.end(); it++){
*it *= 2;
cout << i << " ";
}
return 0;
}
3.24
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> i_vec;
int i, ret;
while(cin >> i){
i_vec.push_back(i);
}
for(auto it = i_vec.begin(); it != i_vec.end() - 1; it++){//end()-1使迭代器指向最后一个元素
ret = *it + *(it+1);
cout << ret << " ";
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> i_vec;
int i, ret;
while(cin >> i){
i_vec.push_back(i);
}
auto iter = i_vec.end();
for(auto it = i_vec.begin(); it != i_vec.begin() + (i_vec.end() - i_vec.begin())/2; it++){
ret = *it + *(--iter);
cout << ret << " ";
}
return 0;
}
3.25
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> score(11, 0);
vector<int>::iterator it = score.begin();
int grade;
while(cin >> grade){
if(grade <= 100){
++(*(it + grade/10));
}
}
for(auto i : score){
cout << i << " ";
}
return 0;
}
3.32
#include <iostream>
using namespace std;
int main(){
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int arr2[10];
for(int i = 0; i != 10; i++){
arr2[i] = arr[i];
}
for(int i : arr2){
cout << i << " ";
}
return 0;
}
3.35
#include <iostream>
using namespace std;
int main(){
int a[] = {1,2,3,4,5,6,7};
int *beg = begin(a), *last = end(a);
int *it = a;
for(; beg != last; beg++){
*it= 0;
it++;
}
return 0;
}
3.36
#include <iostream>
using namespace std;
int main(){
int a[] = {1,2,3,4,5,6,7,8,9};
int b[] = {1,2,3,4,5,6,7,8 };
int *iter_a = a, *iter_b = b;
int count_a = sizeof(a)/sizeof(a[0]);
int count_b = sizeof(b)/sizeof(b[0]);
int count = 0;
if(count_a == count_b){
for(int i = 0; i != count_a; i++){
if(iter_a[i] = iter_b[i]){
count++;
}
}
if(count_a == count){
cout << "eq" << endl;
}
}else{
cout << "neq" << endl;
}
return 0;
}