您的位置:首页 > 编程语言 > C语言/C++

c++primer第五版课后练习答案(第三章)

2014-10-20 21:50 393 查看
chapter1_3.2

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: