函数探幽
#include <iostream>
using namespace std;
void printStr ( const char * str, int n = 0 ) ;
int cnt = 0 ;
int main ( )
{
int len;
cout << "Please enter the length of the string: " ;
cin >> len;
char * str = new char [ len+ 1 ] ;
cout << "Please enter a string: " ;
cin >> str;
printStr ( str) ;
printStr ( str, 1 ) ;
delete [ ] str;
return 0 ;
}
void printStr ( const char * str, int n)
{
cnt++ ;
if ( n == 0 ) cout << str << endl;
else
{
for ( int i= 0 ; i< cnt; i++ ) cout << "Another version: " << str << endl;
}
}
#include <iostream>
using namespace std;
struct CandyBar
{
char * brand = "Millennium Munch" ;
double weight = 2.85 ;
int cal = 350 ;
} ;
void build ( CandyBar & cd, char * br, double wgh, int ca) ;
void show_CandyBar ( const CandyBar & cd) ;
int main ( )
{
CandyBar c1, c2;
build ( c1, "oneone" , 3.66 , 280 ) ;
show_CandyBar ( c1) ;
show_CandyBar ( c2) ;
return 0 ;
}
void build ( CandyBar & cd, char * br, double wgh, int ca)
{
cd. brand = br;
cd. weight = wgh;
cd. cal = ca;
}
void show_CandyBar ( const CandyBar & cd)
{
cout << "Brand: " << cd. brand << endl;
cout << "Weight: " << cd. weight << endl;
cout << "Calorie: " << cd. cal << endl;
}
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void strUpper ( string & str) ;
int main ( )
{
string str;
cout << "Enter a string (q to quit): " ;
getline ( cin, str) ;
while ( str != "q" )
{
strUpper ( str) ;
cout << str << endl;
cout << "Next string (q to quit): " ;
getline ( cin, str) ;
}
cout << "Bye." << endl;
return 0 ;
}
void strUpper ( string & str)
{
for ( int i= 0 ; i< str. size ( ) ; i++ ) str[ i] = toupper ( str[ i] ) ;
}
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char * str;
int ct;
} ;
void set ( stringy & stgy, char test[ ] ) ;
void show ( const stringy stgy, int n = 1 ) ;
void show ( const string str, int n = 1 ) ;
int main ( )
{
stringy beany;
char testing[ ] = "Reality isn't what it used to be." ;
set ( beany, testing) ;
show ( beany) ;
show ( beany, 2 ) ;
testing[ 0 ] = 'D' ;
testing[ 1 ] = 'u' ;
show ( testing) ;
show ( testing, 2 ) ;
return 0 ;
}
void set ( stringy & stgy, char test[ ] )
{
stgy. ct = strlen ( test) ;
char * tmp = new char [ stgy. ct] ;
strcpy ( tmp, test) ;
stgy. str = tmp;
}
void show ( const stringy stgy, int n)
{
for ( int i= 0 ; i< n; i++ )
cout << stgy. str << endl;
}
void show ( const string str, int n)
{
for ( int i= 0 ; i< n; i++ )
cout << str << endl;
}
#include <iostream>
using namespace std;
template < typename T>
T max5 ( T ar[ ] ) ;
int main ( )
{
double dar[ 5 ] = { 1.1 , 3.2 , 3.3 , 5.6 , 7.3 } ;
int iar[ 5 ] = { 1 , 2 , 3 , 4 , 5 } ;
cout << max5 ( dar) << endl;
cout << max5 ( iar) << endl;
return 0 ;
}
template < typename T>
T max5 ( T ar[ ] )
{
T ans = ar[ 0 ] ;
for ( int i= 0 ; i< 5 ; i++ )
{
if ( ar[ i] > ans) ans = ar[ i] ;
}
return ans;
}
#include <iostream>
#include <cstring>
using namespace std;
template < typename T> T maxn ( T ar[ ] , int len) ;
template < > char * maxn< char * > ( char * ar[ ] , int len) ;
int main ( )
{
int iar[ 6 ] = { 1 , 3 , 5 , 61 , 21 , 4 } ;
double dar[ 4 ] = { 3.1 , 15.3 , 2.9 , 3.2 } ;
char * ps[ 4 ] = { "abcde" , "abd" , "abdde" , "absc" } ;
cout << maxn ( iar, 6 ) << endl;
cout << maxn ( dar, 4 ) << endl;
cout << maxn ( ps, 4 ) << endl;
return 0 ;
}
template < typename T> T maxn ( T ar[ ] , int len)
{
T ans = ar[ 0 ] ;
for ( int i= 0 ; i< len; i++ )
{
if ( ar[ i] > ans) ans = ar[ i] ;
}
return ans;
}
template < > char * maxn< char * > ( char * ar[ ] , int len)
{
char * ans = ar[ 0 ] ;
for ( int i= len- 1 ; i>= 0 ; i-- )
if ( strlen ( ar[ i] ) >= strlen ( ans) ) ans = ar[ i] ;
return ans;
}
#include <iostream>
template < typename T>
T SumArray ( T arr[ ] , int n) ;
template < typename T>
T SumArray ( T * arr[ ] , int n) ;
struct debts
{
char name[ 50 ] ;
double amount;
} ;
int main ( )
{
using namespace std;
int things[ 6 ] = { 13 , 31 , 103 , 301 , 310 , 130 } ;
struct debts mr_E[ 3 ] =
{
{ "Ima Wolfe" , 2400.0 } ,
{ "Ura Foxe" , 1300.0 } ,
{ "Iby Stout" , 1800.0 }
} ;
double * pd[ 3 ] ;
for ( int i= 0 ; i< 3 ; i++ ) pd[ i] = & mr_E[ i] . amount;
cout << "The sum of Mr.E's things: " << endl;
cout << SumArray ( things, 6 ) << endl;
cout << "The sum of his debts: " << endl;
cout << SumArray ( pd, 3 ) << endl;
return 0 ;
}
template < typename T>
T SumArray ( T arr[ ] , int n)
{
using namespace std;
T ans = 0 ;
for ( int i= 0 ; i< n; i++ ) ans + = arr[ i] ;
return ans;
}
template < typename T>
T SumArray ( T* arr[ ] , int n)
{
using namespace std;
T ans = 0 ;
for ( int i= 0 ; i< n; i++ ) ans + = * arr[ i] ;
return ans;
}