C++ primer 第三章习题(一)
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using std::vector;
using std::cout;
using std::endl;
using std::cin;
using std::string;
string s;
int main()
{
//3.4
string s2;
cout<<s<<endl;
cout<<s2<<endl;
//3.5
string str;
while(getline(cin,str))
{
cout<<str;
}
string str1;
while(cin>>str1)
{
cout<<str1<<endl;
}
//3.7
string s1,s2;
cin>>s1>>s2;
if(s1!=s2)
{
cout<<((s1>s2)?s1:s2)<<endl;
cout<<((s1.size()>s2.size())?s1:s2);
}
//3.8
string str;
string result;
while(cin>>str)
{
result=result+str+"-";
}
cout<<result;
//3.10
string str;
cin>>str;
for(string::size_type i=0;i<str.size();++i)
{
if(!ispunct(str[i]))
{
cout<<str[i];
}
}
//3.13
vector<int> vec;
int num(0);
while(cin>>num)
{
vec.push_back(num);
}
for(vector<int>::size_type i=0;i<vec.size();i=i+2) //相邻的
{
if(vec.size()%2!=0)
{
vec.push_back(0);
}
cout<<vec[i]+vec[i+1]<<endl;
}
for(vector<int>::size_type i=0;i<vec.size()/2;++i)//头尾的
{
if(vec.size()%2==0)
{
cout<<vec[i]+vec[vec.size()-i-1]<<endl;
}
else
{
cout<<vec[i]+vec[vec.size()-i-1]<<endl;
if(i==(vec.size()/2-1))
{
cout<<vec[i+1];
system("pause");
return 0;
}
}
}
//3.14 ---参考了其他
string str;
vector<string> vec;
while(cin>>str){
vec.push_back(str);}
vector<string>::size_type cnt=0;
for(vector<string>::iterator i=vec.begin();i!=vec.end();++i)
{
for(string::size_type item =0;item!=(*i).size();++item)
{
if(islower((*i)[item]))
{
(*i)[item]=toupper((*i)[item]);
}
}
cout<<(*i)<<" ";
++cnt;
if(cnt%8==0)
{
cout<<endl;
}
}
system("pause");
}