Lesson N.9 of July 24, 2014¶
C and C++ string
File example14.cc
:open:
1/*
2
3 Inheritance and usage of it
4
5*/
6
7#include <iostream>
8#include <iomanip>
9#include <vector>
10#include <cmath>
11
12#include <string.h> // for strlen
13#include <string> // C++ string
14
15using namespace std ;
16
17/*
18
19 print character inserted by number
20
21*/
22
23int
24main() {
25 cout << "Digit 0\n" ;
26 cout << "Digit \x30\n" ;
27
28 cout << "Letter ~\n" ;
29 cout << "Letter \x7E\n" ;
30
31 char const * a_C_string = "I am a string" ; // pointer to a constant piece of memory
32 for ( int i = 0 ; i <= strlen(a_C_string) ; ++i )
33 cout << i << "-th character is: "
34 << a_C_string[i]
35 << " hex: " << hex << int(a_C_string[i]) << dec // reset to decimal
36 << '\n' ;
37
38 string a_Cpp_string = "I am a string" ;
39
40 cout << "C++ string: " << a_Cpp_string << '\n' ;
41 cout << "length: " << a_Cpp_string.length() << '\n' ;
42 sort( a_Cpp_string.begin(), a_Cpp_string.end()) ;
43 cout << "Sorted: " << a_Cpp_string << '\n' ;
44 reverse( a_Cpp_string.begin(), a_Cpp_string.end()) ;
45 cout << "Reversed: " << a_Cpp_string << '\n' ;
46
47 string str1 = "I am string1" ;
48 string str2 = "I am string2" ;
49 string str = str1 + str2 ;
50
51 cout << "Concatenated: " << str << '\n' ;
52 cout << "str[1] " << str[1] << '\n' ;
53 cout << "str[5] " << str[5] << '\n' ;
54 cout << "str[3] " << str[3] << '\n' ;
55
56 return 0 ;
57}
Divisors of a number
File example15.cc
:open:
1/*
2
3 Build a tree with the divisors of a number
4
5*/
6
7#include <iostream>
8#include <iomanip>
9#include <vector>
10#include <cmath>
11
12#include <string> // C++ string
13
14using namespace std ;
15
16class Node {
17 int number ;
18 vector<Node*> childs ;
19
20 void free() ;
21
22public:
23
24 Node() : number(1) {
25 cout << "Enter to the contructor of Node\n" ;
26 }
27
28 Node( int n ) : number(1) {
29 cout << "Enter to the contructor of Node(" << n << ")\n" ;
30 build( n ) ;
31 }
32
33 ~Node() ;
34
35 void build( int n ) ;
36 void print( ostream &, int level = 0 ) ;
37 void to_dag( ostream &, int level = 0 ) ;
38 void to_tree( ostream &, int level = 0 ) ;
39
40} ;
41
42void
43Node::free() {
44 cout << "Enter to the destructor of Node (" << number << ")\n" ;
45 for ( vector<Node*>::iterator it = childs.begin() ; it != childs.end() ; ++it )
46 delete *it ; // *it is a pointer to the corresponding child Node
47 // call the destroyer for the Node *it
48 childs.clear() ;
49}
50
51Node::~Node() {
52 free() ;
53}
54
55void
56Node::build( int n ) {
57 //cout << "Enter build for number " << n << "\n" ;
58 if ( number != 1 ) free() ;
59 number = n ;
60 // search for the divisors
61 for ( int i = 2 ; i < n ; ++i ) {
62 if ( (n % i) == 0 ) { // found a divisor
63 //cout << "found divisor: " << i << " for number " << number << '\n' ;
64 Node * pChild = new Node(i) ; // allocate the new divisor `i`
65 //pChild->build(i) ; // compute the divisor of i
66 childs.push_back(pChild) ; // add to the list of child od `number`
67 }
68 }
69 //cout << "Exit build for number " << n << "\n" ;
70}
71
72void
73Node::print( ostream & stream, int level ) {
74 stream << std::string(level*4, ' ') ;
75 stream << number << '\n' ;
76 for ( int i = 0 ; i < childs.size() ; ++i )
77 childs[i]->print( stream, level + 1 ) ;
78}
79
80void
81Node::to_dag( ostream & stream, int level ) {
82 if ( level == 0 ) stream << "digraph divisor_of_" << number << " {\n" ;
83 for ( int i = 0 ; i < childs.size() ; ++i ) {
84 stream << " " << number << " -> " << childs[i]->number << ";\n" ;
85 childs[i]->to_dag( stream, level + 1 ) ;
86 }
87 if ( level == 0 ) stream << "}\n" ;
88}
89
90
91int
92main() {
93 Node n(12) ;
94 n.to_dag(cout) ;
95
96 //n.build(234) ;
97 //n.to_dag(cout) ;
98
99 return 0 ;
100}
Convert integer to letters
File num_to_string.hh
:open:
1#ifndef NUM_TO_STRING_HH
2#define NUM_TO_STRING_HH
3
4#include <string> // C++ string
5#include <vector> // C++ string
6
7class NumToString {
8 std::vector<std::string> first20 ;
9 std::vector<std::string> tens ;
10
11 std::string to_string_less_than_1000( int ) const ;
12
13public:
14 NumToString() ;
15 ~NumToString() ;
16 std::string to_string( int ) const ;
17} ;
18
19#endif
File num_to_string.cc
:open:
1#include "num_to_string.hh"
2
3using namespace std ;
4
5NumToString::NumToString() {
6 first20.clear() ;
7 first20.reserve(20) ;
8 first20.push_back("") ;
9 first20.push_back("one") ;
10 first20.push_back("two") ;
11 first20.push_back("three") ;
12 first20.push_back("four") ;
13 first20.push_back("five") ;
14 first20.push_back("six") ;
15 first20.push_back("seven") ;
16 first20.push_back("eight") ;
17 first20.push_back("nine") ;
18 first20.push_back("ten") ;
19 first20.push_back("eleven") ;
20 first20.push_back("twelve") ;
21 first20.push_back("thirteen") ;
22 first20.push_back("fourteen") ;
23 first20.push_back("fifteen") ;
24 first20.push_back("sixteen") ;
25 first20.push_back("seventeen") ;
26 first20.push_back("eighteen") ;
27 first20.push_back("nineteen") ;
28
29 tens.clear() ;
30 tens.reserve(10) ;
31 tens.push_back("zero") ;
32 tens.push_back("ten") ;
33 tens.push_back("twenty") ;
34 tens.push_back("thirty") ;
35 tens.push_back("forty") ;
36 tens.push_back("fifty") ;
37 tens.push_back("sixty") ;
38 tens.push_back("seventy") ;
39 tens.push_back("eighty") ;
40 tens.push_back("ninety") ;
41}
42
43NumToString::~NumToString() {
44
45}
46
47string
48NumToString::to_string_less_than_1000( int num ) const {
49
50 string res = "" ;
51 // convert the first two digit
52 int tmp = num%100 ;
53 if ( tmp < 20 ) {
54 res = first20[tmp] ;
55 } else {
56 res = tens[tmp/10] ;
57 if ( (tmp%10) != 0 ) res += "-" + first20[tmp%10] ;
58 }
59
60 // convert hundreds
61 num /= 100 ;
62 tmp = num%10 ;
63 if ( tmp > 0 ) {
64 if ( res.length() > 0 ) res = first20[tmp]+"-hundred-"+res ;
65 else res = first20[tmp]+"-hundred" ;
66 }
67 return res ;
68
69}
70
71
72string
73NumToString::to_string( int num ) const {
74 string res = to_string_less_than_1000( num ) ;
75
76 // convert thousand
77 string res1 = to_string_less_than_1000( num/1000 ) ;
78 if ( res1.length() > 0 ) {
79 if ( res.length() > 0 ) res = res1+"-thousand-"+res ;
80 else res = res1+"-thousand" ;
81 }
82
83 // convert millions
84 res1 = to_string_less_than_1000( num/1000000 ) ;
85 if ( res1.length() > 0 ) {
86 if ( res.length() > 0 ) res = res1+"-million-"+res ;
87 else res = res1+"-million" ;
88 }
89
90 // convert billion
91 res1 = to_string_less_than_1000( num/1000000000 ) ;
92 if ( res1.length() > 0 ) {
93 if ( res.length() > 0 ) res = res1+"-billion-"+res ;
94 else res = res1+"-billion" ;
95 }
96
97 return res ;
98}
:open:
1#include "num_to_string.hh"
2#include <iostream>
3
4using namespace std ;
5
6int
7main() {
8 NumToString nts ;
9
10 for ( int i = 1000000 ; i < 1000000+333 ; ++i )
11 cout << i << " --> " << nts.to_string(i) << '\n' ;
12
13}
All the files in one zip file