《Essential C++》第二章练习
练习2.1
-
#include <iostream>
-
using namespace std;
-
-
bool fibon_elem( int, int & );
-
-
int main()
-
{
-
int pos;
-
while(true)
-
{
-
cout<<"Please enter a position: ";
-
cin>>pos;
-
-
if( pos == -1 ) break;
-
-
int elem;
-
if ( fibon_elem( pos, elem ) )
-
{
-
cout<< "element # " << pos
-
<< " is " << elem << endl;
-
}
-
else
-
{
-
cout<< "Sorry, Could not calculate element # "
-
<< pos << endl;
-
}
-
-
}
-
-
return 0;
-
}
-
-
bool fibon_elem( int pos, int & elem)
-
{
-
if( pos <= 0 || pos > 1024 )
-
{
-
elem = 0;
-
return false;
-
}
-
-
elem = 1;
-
int n_2 = 1, n_1 = 1;
-
for( int ix = 3; ix<=pos; ++ix)
-
{
-
elem = n_2 + n_1;
-
n_2 = n_1; n_1 = elem;
-
}
-
-
return true;
-
}
练习2.2
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
using namespace std;
-
-
bool build_pentagonal_vector( int , vector<int> & );
-
-
void display_vector(const vector<int>, const string);
-
-
int main()
-
{
-
int size;
-
-
cout<<" Please input the vector's length: ";
-
cin>>size;
-
-
vector<int> ve;
-
-
if(build_pentagonal_vector(size, ve))
-
{
-
display_vector(ve, "int");
-
}
-
-
return 0;
-
}
-
-
bool build_pentagonal_vector( int size, vector<int> & ve )
-
{
-
-
if( size <= 0 || size > 1024 )
-
{
-
cout<<"The size is not legal ! "<<endl;
-
return false;
-
}
-
-
for(int t = 1; t <= size; t++)
-
{
-
ve.push_back(t*(3*t-1)/2);
-
}
-
-
return true;
-
}
-
-
void display_vector(vector<int> ve, string type)
-
{
-
vector<int>::iterator iter = ve.begin();
-
for( ; iter != ve.end(); iter++ )
-
{
-
cout << (*iter) << " ";
-
}
-
cout << endl;
-
}
练习2.3
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
using namespace std;
-
-
bool build_pentagonal_vector( int , vector<int> & );
-
-
void display_vector(const vector<int>, const string);
-
-
int main()
-
{
-
int size;
-
-
cout<<" Please input the vector's length: ";
-
cin>>size;
-
-
vector<int> ve;
-
-
if(build_pentagonal_vector(size, ve))
-
{
-
display_vector(ve, "int");
-
}
-
-
return 0;
-
}
-
-
inline bool check_size ( int size )
-
{
-
if( size <= 0 || size > 1024 )
-
{
-
cout<<"The size is not legal ! "<<endl;
-
return false;
-
}
-
return true;
-
}
-
-
bool build_pentagonal_vector( int size, vector<int> & ve )
-
{
-
if(!check_size(size)) return false;
-
-
for(int t = 1; t <= size; t++)
-
{
-
ve.push_back(t*(3*t-1)/2);
-
}
-
-
return true;
-
}
-
-
void display_vector(vector<int> ve, string type)
-
{
-
vector<int>::iterator iter = ve.begin();
-
for( ; iter != ve.end(); iter++ )
-
{
-
cout << (*iter) << " ";
-
}
-
cout << endl;
-
}
练习2.4
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
using namespace std;
-
-
const vector<int> * pentagonal_vector( int size );
-
int print_pos( const vector<int>, int );
-
-
int main()
-
{
-
int size;
-
-
cout<<"Please input the vector's size: ";
-
cin>>size;
-
-
const vector<int> *ve = pentagonal_vector(size);
-
-
int pos;
-
cout<<"Please input your pos: ";
-
cin>>pos;
-
cout<<"The position # " << pos <<" element is " << print_pos(*ve, pos) << endl;
-
-
return 0;
-
}
-
-
const vector<int> * pentagonal_vector( int size )
-
{
-
static vector<int> ve;
-
-
if( ve.size() < size )
-
{
-
for( int i=ve.size(); i<=size; i++ )
-
{
-
ve.push_back( i*(3*i-1)/2 );
-
}
-
}
-
-
return &ve;
-
}
-
-
int print_pos( const vector<int> ve, int pos )
-
{
-
if( pos < 0 || pos > ve.size() )
-
{
-
return -1;
-
}
-
else
-
{
-
return ve[pos];
-
}
-
}
练习2.5
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
using namespace std;
-
-
inline int max ( int a , int b )
-
{
-
return a>=b?a:b;
-
}
-
-
inline float max ( float a , float b )
-
{
-
return a>=b?a:b;
-
}
-
-
inline string max ( string a, string b )
-
{
-
return a>=b?a:b;
-
}
-
-
inline int max ( vector<int> v )
-
{
-
int m = v[0];
-
for(int i = 1; i<v.size(); i++)
-
{
-
if(v[i]>m) m = v[i];
-
}
-
return m;
-
}
-
-
inline float max ( vector<float> v )
-
{
-
float m = v[0];
-
for(int i = 1; i<v.size(); i++)
-
{
-
if(v[i]>m) m = v[i];
-
}
-
return m;
-
}
-
-
inline string max ( vector<string> v )
-
{
-
string m = v[0];
-
for(int i = 1; i<v.size(); i++)
-
{
-
if(v[i]>m) m = v[i];
-
}
-
return m;
-
}
-
-
inline int max ( int array[], int size )
-
{
-
int m = array[0];
-
for(int i = 1; i<size; i++)
-
{
-
if(array[i]>m) m = array[i];
-
}
-
return m;
-
}
-
-
inline float max ( float array[], int size )
-
{
-
float m = array[0];
-
for(int i = 1; i<size; i++)
-
{
-
if(array[i]>m) m = array[i];
-
}
-
return m;
-
}
-
-
inline string max ( string array[], int size )
-
{
-
string m = array[0];
-
for(int i = 1; i<size; i++)
-
{
-
if(array[i]>m) m = array[i];
-
}
-
return m;
-
}
-
-
int main()
-
{
-
int a = 3, b = 2;
-
cout<<max(a,b)<<endl;
-
-
float c = 0.1, d = 0.11;
-
cout<<max(c, d)<<endl;
-
-
string e = "hi", f = "gogo";
-
cout<<max(e,f)<<endl;
-
-
vector<int> vi;
-
vi.push_back(3);
-
vi.push_back(4);
-
vi.push_back(5);
-
vi.push_back(1);
-
vi.push_back(2);
-
cout<<max(vi)<<endl;
-
-
vector<float> vf;
-
vf.push_back(0);
-
vf.push_back(0.42);
-
vf.push_back(0.51);
-
vf.push_back(0.01);
-
vf.push_back(2.0);
-
cout<<max(vf)<<endl;
-
-
vector<string> vs;
-
vs.push_back("go");
-
vs.push_back("z");
-
vs.push_back("yy");
-
vs.push_back("a");
-
cout<<max(vs)<<endl;
-
-
int ai[] = { 3, 2, 5, 1, 3 };
-
cout<<max(ai, 5)<<endl;
-
-
float af[] = { 0.01, 0.02, 0.1, 0.2, 0 };
-
cout<<max(af, 5)<<endl;
-
-
string as[] = {"a", "b", "c", "d", "" };
-
cout<<max(as, 5)<<endl;
-
-
return 0;
-
}
练习2.6
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
using namespace std;
-
-
template<typename T>
-
inline T max ( T a , T b )
-
{
-
return a>=b?a:b;
-
}
-
-
template<typename T>
-
inline T max ( vector<T> v )
-
{
-
T m = v[0];
-
for(int i = 1; i<v.size(); i++)
-
{
-
if(v[i]>m) m = v[i];
-
}
-
return m;
-
}
-
-
template<typename T>
-
inline T max ( T array[], int size )
-
{
-
T m = array[0];
-
for(int i = 1; i<size; i++)
-
{
-
if(array[i]>m) m = array[i];
-
}
-
return m;
-
}
-
-
int main()
-
{
-
int a = 3, b = 2;
-
cout<<max(a,b)<<endl;
-
-
float c = 0.1, d = 0.11;
-
cout<<max(c, d)<<endl;
-
-
string e = "hi", f = "gogo";
-
cout<<max(e,f)<<endl;
-
-
vector<int> vi;
-
vi.push_back(3);
-
vi.push_back(4);
-
vi.push_back(5);
-
vi.push_back(1);
-
vi.push_back(2);
-
cout<<max(vi)<<endl;
-
-
vector<float> vf;
-
vf.push_back(0);
-
vf.push_back(0.42);
-
vf.push_back(0.51);
-
vf.push_back(0.01);
-
vf.push_back(2.0);
-
cout<<max(vf)<<endl;
-
-
vector<string> vs;
-
vs.push_back("go");
-
vs.push_back("z");
-
vs.push_back("yy");
-
vs.push_back("a");
-
cout<<max(vs)<<endl;
-
-
int ai[] = { 3, 2, 5, 1, 3 };
-
cout<<max(ai, 5)<<endl;
-
-
float af[] = { 0.01, 0.02, 0.1, 0.2, 0 };
-
cout<<max(af, 5)<<endl;
-
-
string as[] = {"a", "b", "c", "d", "" };
-
cout<<max(as, 5)<<endl;
-
-
return 0;
-
}