Lesson of 23 July 2009¶
Use of the string class
File string-examples.cc
1/*
2 A simple program to test string class
3 */
4
5#include <iostream> // for I/O
6#include <iomanip> // for hex, dec, width
7#include <string> // string support
8
9using namespace std ;
10
11int
12main() {
13 string a, b, c("$$$"), d("pippo") ;
14
15 a = d ;
16
17 char const * pa = a . c_str() ; // get the pointer to the C-string of a
18 char const * pd = d . c_str() ; // get the pointer to the C-string of d
19
20 cout << "address of string a = " << hex << setw(10) << (unsigned long)pa << '\n' ;
21 cout << "address of string d = " << hex << setw(10) << (unsigned long)pd << '\n' ;
22 //
23 // ^ ^ ^
24 // use hexadecimal numer -----------+ | |
25 // set width for next output to 10 charater -+ |
26 // cast: convert type from pointer to unsigned integer ------+
27
28 a += " pluto" ;
29 cout << "a = '" << a << "'\n" ;
30 cout << "b = '" << b << "'\n" ;
31
32 pa = a . c_str() ; // get the pointer to the C-string of a
33 pd = d . c_str() ; // get the pointer to the C-string of d
34
35 cout << "address of string a = " << hex << setw(10) << (unsigned long)pa << dec << '\n' ;
36 cout << "address of string d = " << hex << setw(10) << (unsigned long)pd << dec << '\n' ;
37
38 // main operation on string
39
40 // concatenation, concatenate string a and c
41 b = a+c ;
42 cout << "a = '" << a << "'\n" ;
43 cout << "c = '" << c << "'\n" ;
44 cout << "a+c = '" << b << "'\n" ;
45
46 // find
47 int i = a . find("plut") ;
48 cout << "a . find(\"plut\") = " << i << "\n" ;
49
50 i = a . find("asx") ;
51 cout << "a . find(\"asx\") = " << i << "\n" ;
52
53 // comparison
54 a = "pippo" ;
55 b = "pluto" ;
56 c = "paperino" ;
57 d = "pippo" ;
58 cout << "a = '" << a << "'\n" ;
59 cout << "b = '" << b << "'\n" ;
60 cout << "c = '" << c << "'\n" ;
61 cout << "d = '" << d << "'\n" ;
62
63 cout << "'" << a <<"' < '" << b << "' = " ;
64 if ( a < b ) cout << "true\n" ;
65 else cout << "false\n" ;
66
67 cout << "'" << a <<"' > '" << c << "' = " ;
68 if ( a > c ) cout << "true\n" ;
69 else cout << "false\n" ;
70
71 cout << "'" << a <<"' == '" << d << "' = " ;
72 if ( a == d ) cout << "true\n" ;
73 else cout << "false\n" ;
74
75 cout << "'" << b <<"' >= '" << c << "' = " ;
76 if ( b >= c ) cout << "true\n" ;
77 else cout << "false\n" ;
78
79 cout << "'" << c <<"' >= '" << a << "' = " ;
80 if ( c >= a ) cout << "true\n" ;
81 else cout << "false\n" ;
82
83}
Use of the string class: convert a number to letter
File convert.cc
1/*
2 A simple program to convert a number to string.
3 For example
4
5 1234 => onethousandtwohundredthirtythree
6
7 */
8
9#include <iostream> // for I/O
10#include <string> // string support
11
12using namespace std ;
13
14string
15convert0to19( unsigned n ) {
16 // vector of strings for the first 20 numbers
17 // *(str[]) = vector of pointer to character
18 char *str[] = {
19 "", // 0 must be empty
20 "one", // 1
21 "two",
22 "three",
23 "four",
24 "five",
25 "six",
26 "seven",
27 "eight",
28 "nine",
29 "ten",
30 "eleven",
31 "twelve",
32 "thirteen",
33 "fourteen",
34 "fifteen",
35 "sixteen",
36 "seventeen",
37 "nineteen"
38 } ;
39 return str[ n % 20 ] ; // here the constructor string(str[n]) is used
40 // to build the return object
41}
42
43string
44convert0to99( unsigned n ) {
45 // vector of strings for the first 20 numbers
46 // *(str[]) = vector of pointer to character
47 char *str[] = {
48 "",
49 "ten",
50 "twenty",
51 "thirty",
52 "fourty",
53 "fifty",
54 "sixty",
55 "seventy",
56 "eighty",
57 "ninety",
58 } ;
59
60 if ( n < 20 ) return convert0to19(n) ;
61
62 return string( str[ (n/10) % 10 ] ) + convert0to19( n % 10 ) ;
63}
64
65string
66convert0to999( unsigned n ) {
67 // vector of strings for the first 20 numbers
68 // *(str[]) = vector of pointer to character
69 char *str[] = {
70 "",
71 "onehundred",
72 "twohundred",
73 "threehundred",
74 "fourhundred",
75 "fivehundred",
76 "sixhundred",
77 "sevenhundred",
78 "eighthundred",
79 "ninehundred",
80 } ;
81
82 if ( n < 100 ) return convert0to99(n) ;
83
84 return string( str[ (n/100) % 10 ] ) + convert0to99( n % 10 ) ;
85}
86
87string
88convert0to999999( unsigned n ) {
89 // vector of strings for the first 20 numbers
90 // *(str[]) = vector of pointer to character
91
92 if ( n < 1000 ) return convert0to999(n) ;
93
94 unsigned upper = n / 1000 ;
95 unsigned lower = n % 1000 ;
96
97 return convert0to999( upper ) + "thousand" + convert0to999( lower ) ;
98}
99
100string
101convert( unsigned n ) {
102 string res = convert0to999999( n ) ;
103 if ( res . length() > 0 ) return res ;
104 else return "zero" ; // call the contructor string("zero")
105}
106
107int
108main() {
109 cout << convert(10002) << "\n" ;
110 cout << convert(21222) << "\n" ;
111 cout << convert(1000) << "\n" ;
112 cout << convert(991234) << "\n" ;
113}
Euler numerical scheme, C-like implementation
File Euler.cc
1/*
2 A simple example to use class and numerical integrator.
3 Use Euler numerical method to "solve" a differential
4 equation
5
6 Y' = f(t,Y)
7 Y(0) = Y0
8
9 */
10
11#include <iostream> // for I/O
12#include <fstream> // for I/O
13#include <iomanip> // for hex, dec, width
14#include <string> // string support
15
16#include <algorithm>
17#include <vector>
18
19
20// define the problem
21//
22// x' = y
23// y' = -x
24// x(0) = 1
25// y(0) = 0
26//
27// Y = (x,y)^T f(t,Y) = ( y, -x ) ^T
28//
29void
30f( double t, double Y[], double Y1[] ) {
31 Y1[0] = Y[1] ;
32 Y1[1] = -Y[0] ;
33}
34
35// Euler solver (C-way)
36void
37Euler( void (*fun)( double t, double Y[], double Y1[] ), // function to be integrated
38 double const Y0[], // initial value
39 int const neq, // number of equation
40 int const N, // number of steps
41 double const h, // time step
42 double Y[] ) { // output vector
43
44 double * Fk = new double[neq] ;
45
46 // copy the initial condition
47 std::copy( Y0, Y0 + neq, Y ) ;
48 for ( int k=0 ; k < N ; ++k ) {
49 double * Yk = Y + neq * k ;
50 double * Ykp1 = Yk + neq ;
51 fun( k*h, Yk, Fk ) ;
52 // perform Euler step
53 for ( int i = 0 ; i < neq ; ++i )
54 Ykp1[i] = Yk[i] + h * Fk[i] ;
55 }
56
57 delete [] Fk ;
58}
59
60using namespace std ;
61
62int
63main() {
64 double Y[100][2] ;
65 double Y0[] = {1, 0} ;
66 Euler( f, Y0, 2, 100, 0.1, (double*)Y ) ;
67
68 ofstream file("out.txt") ;
69 file << "x\ty\n" ; // header of the output file
70 for ( int i=0 ; i < 100 ; ++i )
71 file << Y[i][0] << '\t' << Y[i][1] << '\n' ;
72 file . close() ;
73}
Euler numerical scheme, C++ implementation
File Euler1.cc
1/*
2 A simple example to use class and numerical integrator.
3 Use Euler numerical method to "solve" a differential
4 equation
5
6 Y' = f(t,Y)
7 Y(0) = Y0
8
9 better implementation using STL
10
11 */
12
13#include <iostream> // for I/O
14#include <fstream> // for I/O
15#include <iomanip> // for hex, dec, width
16#include <string> // string support
17
18#include <algorithm>
19#include <vector>
20
21using namespace std ;
22
23// define the problem
24//
25// x' = y
26// y' = -x
27// x(0) = 1
28// y(0) = 0
29//
30// Y = (x,y)^T f(t,Y) = ( y, -x ) ^T
31//
32void
33f( double t, vector<double> const & Y, vector<double> & Y1 ) {
34 Y1[0] = Y[1] ;
35 Y1[1] = -Y[0] ;
36}
37
38// Euler solver
39template <typename T, int neq>
40void
41Euler( void (*fun)( T t, vector<T> const &, vector<T> &), // function to be integrated
42 vector<T> const Y0, // initial value
43 int const N, // number of steps
44 T const h, // time step
45 vector<vector<T> > & Y ) { // output vector
46
47 std::vector<T> Ykp1(neq), Fk(neq) ;
48
49 Y . reserve( N ) ; // pre allocation for N elements
50
51 // copy the initial condition
52 std::copy( Y0 . begin(), Y0 . end(), Ykp1.begin() ) ;
53 Y . push_back( Ykp1 ) ;
54
55 for ( int k=0 ; k < N ; ++k ) {
56 fun( k*h, Y.back(), Fk ) ;
57 // perform Euler step
58 for ( int i = 0 ; i < neq ; ++i )
59 Ykp1[i] = Y.back()[i] + h * Fk[i] ;
60 Y . push_back( Ykp1 ) ; // copy the last computed value to the end
61 // of the output vector
62 }
63}
64
65int
66main() {
67 vector<vector<double> > Y ;
68 vector<double> Y0 ;
69 Y0 . push_back(1) ;
70 Y0 . push_back(0) ;
71
72 Euler<double,2>( f, Y0, 100, 0.1, Y ) ;
73
74 ofstream file("out.txt") ;
75 file << "x\ty\n" ; // header of the output file
76
77 for ( vector<vector<double> >::iterator i = Y.begin() ;
78 i != Y . end() ; ++i )
79 file << (*i)[0] << '\t' << (*i)[1] << '\n' ;
80 file . close() ;
81}