C++Primer部分练习参考答案 第三章
编译环境:Microsoft Visual Studio 2019 备注:本文留作作者自用,如有错误敬请指出
3.2
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string line;
while (getline(cin, line))
cout << line << endl;
return 0;
}
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string line;
cin >> line;
cout << line << endl;
return 0;
}
3.4
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s1,s2;
cin >> s1>>s2;
if (s1 == s2)
cout << "两个字符串相等" << endl;
else if (s1 > s2)
cout << s1 << "和" << s2 << "中较大的是" << s1 << endl;
else
cout << s1 << "和" << s2 << "中较大的是" << s2 << endl;
return 0;
}
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s1,s2;
cin >> s1>>s2;
if (s1.size() == s2.size())
cout << "两个字符串一样长" << endl;
else if (s1.size() > s2.size())
cout << s1 << "和" << s2 << "中较长的是" << s1<<endl;
else
cout << s1 << "和" << s2 << "中较长的是" << s2<<endl;
return 0;
}
3.5
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str,ini;
if (cin >> ini)
{
while (cin >> str)
ini += str;
cout << ini << endl;
}
else
cout << "请输入至少一个字符串!" << endl;
return 0;
}
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str,ini;
if (cin >> ini)
{
while (cin >> str)
{
ini += ' ';
ini += str;
}
cout << ini << endl;
}
else
cout << "请输入至少一个字符串!" << endl;
return 0;
}
3.6
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str;
cin >> str;
for (auto& c : str)
c = 'X';
cout << str << endl;
return 0;
}
3.8
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str;
cin >> str;
decltype(str.size()) n = 0;
while (n < str.size())
{
str[n] = 'X';
n++;
}
cout << str << endl;
return 0;
}
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str;
cin >> str;
decltype(str.size()) n = 0;
for (;n < str.size();n++)
str[n] = 'X';
cout << str << endl;
return 0;
}
3.10
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str,op;
cin >> str;
decltype(str.size()) n = 0;
while (n < str.size())
{
if (!ispunct(str[n]))
op += str[n];
n++;
}
cout << op << endl;
return 0;
}
3.14
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int int1;
vector<int> int2;
while (cin >> int1)
int2.push_back(int1);
for (auto i : int2)
cout << i << endl;
return 0;
}
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
string s1;
vector<string> s2;
while (cin >> s1)
s2.push_back(s1);
for (auto c : s2)
cout << c << endl;
return 0;
}
3.17
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
string v1;
vector<string> v2;
while (cin >> v1)
v2.push_back(v1);
for (decltype(v2.size()) n = 0; n < v2.size(); n++)
for (auto& c : v2[n])
c=toupper(c);
for (auto c : v2)
cout << c << endl;
return 0;
}
3.20
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int v1;
vector<int> v;
while (cin >> v1)
v.push_back(v1);
for (int n = 0; n < v.size() - 1; n++)
cout << v[n] + v[n + 1]<<" ";
cout << endl;
return 0;
}
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int v1;
vector<int> v;
while (cin >> v1)
v.push_back(v1);
for (int n = 0; n < v.size(); n++)
cout << v[n] + v[v.size()-n -1]<<" ";
cout << endl;
return 0;
}
3.23
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> v(10);
for (auto it = v.begin(); it != v.end(); it++)
{
cin >> *it;
(*it) *= 2;
}
for (auto it = v.begin(); it != v.end(); it++)
cout<< *it<<" ";
return 0;
}
3.24
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int v1;
vector<int> v;
while (cin >> v1)
v.push_back(v1);
for (auto it=v.cbegin();it!=v.cend()-1; it++)
cout << *it+*(it +1)<< " ";
cout << endl;
return 0;
}
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int v1;
vector<int> v;
while (cin >> v1)
v.push_back(v1);
for (auto it = v.cbegin(), iu = v.cend(); it != v.cend(); it++,iu--)
cout << *it + *(iu-1)<< " ";
cout << endl;
return 0;
}
3.25
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<unsigned> scores(11,0);
unsigned grade;
auto it = scores.begin();
while (cin >> grade)
{
if (grade <= 100)
++* (it+ grade/10);
it = scores.begin();
}
for (auto i : scores)
cout << i << " ";
return 0;
}
3.31
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int a[10];
for (int n = 0; n <= 9; n++)
a[n] = n;
for (auto i : a)
cout << i << " ";
return 0;
}
3.32
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int a[10],b[10];
for (int n = 0; n <= 9; n++)
a[n] = n;
for (int n = 0; n <= 9; n++)
b[n] = a[n];
for (auto i : b)
cout << i << " ";
return 0;
}
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> a(10), b(10);
for (int n = 0; n <= 9; n++)
a[n] = n;
for (int n = 0; n <= 9; n++)
b[n] = a[n];
for (auto i : b)
cout << i << " ";
return 0;
}
3.35
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int a[5];
int* p = a;
for (int n = 0; n < 5; n++)
{
*p = 0;
p++;
}
for (int n = 0; n < 5; n++)
cout << a[n]<<" ";
return 0;
}
3.36
#include<iostream>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
using std::begin;
using std::end;
int main()
{
int a[5] = { 1,2,3,4,5 };
int b[4] = { 1,2,3,4};
int* p1 = begin(a),*p2=end(a),*p3=begin(b),*p4=end(b);
for (;p1!=p2&&p3!=p4; p1++,p3++)
if (*p1==*p3)
{
if ((p1 == p2 - 1) && (p3 == p4 - 1))
{
cout << "两个数组相等" << endl;
return 0;
}
}
cout << "两个数组不相等!" << endl;
return 0;
}
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> a= {1,2,3};
vector<int> b = { 1,2,3,4};
auto p1 =a.begin(),p2=a.end(),p3=b.begin(),p4=b.end();
for (;p1!=p2 && p3!=p4; p1++,p3++)
if (*p1==*p3)
{
if ((p1 == p2 - 1) && (p3 == p4 - 1))
{
cout << "两个vector相等" << endl;
return 0;
}
}
cout << "两个vector不相等!" << endl;
return 0;
}
3.39
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s1="a string", s2="another string";
if (s1 == s2)
cout << s1 << "=" << s2 << endl;
else if(s1<s2)
cout << s1 << "<" << s2 << endl;
else
cout << s1 << ">" << s2 << endl;
return 0;
}
#include<iostream>
#include<cstring>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char s1[]="a string", s2[]="another string";
int a = strcmp(s1, s2);
if (a==0)
cout << s1 << "=" << s2 << endl;
else if(a<0)
cout << s1 << "<" << s2 << endl;
else
cout << s1 << ">" << s2 << endl;
return 0;
}
3.40
#include<iostream>
#include<cstring>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char s1[]="a string", s2[]=" and another string";
char s3[100];
strcpy_s(s3,s1);
strcat_s(s3, s2);
cout << s3;
return 0;
}
3.41
#include<iostream>
#include<vector>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::begin;
using std::end;
int main()
{
int a[] = { 1,2,3,4,5 };
vector<int> v(begin(a), end(a));
for (auto i : v)
cout << i << endl;
return 0;
}
3.42
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int a[5] ;
vector<int> v = {1,2,3,4,5};
for (int n = 0; n<5; n++)
a[n] = v[n];
for (int &i : a)
cout << i<< " ";
return 0;
}
3.43
#include<iostream>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int ia[3][4] = {
{0,1,2,3} ,
{4,5,6,7} ,
{8,9,10,11}
};
for (int(&row)[4] : ia)
{
for (int col : row)
cout << col << " ";
cout << endl;
}
return 0;
}
#include<iostream>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int ia[3][4] = {
{0,1,2,3} ,
{4,5,6,7} ,
{8,9,10,11}
};
for (int i=0;i<3;i++)
{
for (int j=0;j<4;j++)
cout << ia[i][j] << " ";
cout << endl;
}
return 0;
}
#include<iostream>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int ia[3][4] = {
{0,1,2,3} ,
{4,5,6,7} ,
{8,9,10,11}
};
for (int (*p)[4]=ia;p!=ia+3;p++)
{
for (int *q=*p;q!=*p+4;q++)
cout << *q << " ";
cout << endl;
}
return 0;
}
3.44
#include<iostream>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int ia[3][4] = {
{0,1,2,3} ,
{4,5,6,7} ,
{8,9,10,11}
};
typedef int int_array[4];
for (int_array *p=ia;p!=ia+3;p++)
{
for (int *q=*p;q!=*p+4;q++)
cout << *q << " ";
cout << endl;
}
return 0;
}
3.45
#include<iostream>
#include<iterator>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int ia[3][4] = {
{0,1,2,3} ,
{4,5,6,7} ,
{8,9,10,11}
};
for (auto p=ia;p!=ia+3;p++)
{
for (auto q=*p;q!=*p+4;q++)
cout << *q << " ";
cout << endl;
}
return 0;
}
|