Commit c0b14c544131744ae980568edb9ae1eb9fd08613

Authored by Grzegorz Jabłoński
1 parent e02010c3

Reformatted code

Showing 84 changed files with 1811 additions and 1934 deletions

Too many changes to show.

To preserve performance only 84 of 279 files are displayed.

examples01-en/01-firstcpp/hellocpp.cpp
@@ -3,5 +3,5 @@ using namespace std; @@ -3,5 +3,5 @@ using namespace std;
3 3
4 int main() 4 int main()
5 { 5 {
6 - cout << "Hello from c++\n"; 6 + cout << "Hello from c++\n";
7 } 7 }
examples01-en/02-string/string.cpp
@@ -4,14 +4,13 @@ using namespace std; @@ -4,14 +4,13 @@ using namespace std;
4 4
5 string getword() 5 string getword()
6 { 6 {
7 - string s;  
8 - cin >> s;  
9 - return s; 7 + string s;
  8 + cin >> s;
  9 + return s;
10 } 10 }
11 11
12 -  
13 int main() 12 int main()
14 -{  
15 - cout << "Podaj slowo" << endl;  
16 - cout << "Podane slowo to:" << getword() << endl; 13 +{
  14 + cout << "Podaj slowo" << endl;
  15 + cout << "Podane slowo to:" << getword() << endl;
17 } 16 }
examples01-en/03-vector/vector.cpp
1 #include <iostream> 1 #include <iostream>
2 -#include <vector>  
3 #include <string> 2 #include <string>
  3 +#include <vector>
4 using namespace std; 4 using namespace std;
5 5
6 int main() 6 int main()
7 { 7 {
8 - string word;  
9 - vector<string> v;  
10 - while(cin)  
11 - {  
12 - if(cin>>word)  
13 - v.push_back(word);  
14 - }  
15 - for(unsigned int i=0;i<v.size();i++)  
16 - cout << v[i]<<" ";  
17 - cout << endl; 8 + string word;
  9 + vector<string> v;
  10 + while (cin) {
  11 + if (cin >> word)
  12 + v.push_back(word);
  13 + }
  14 + for (unsigned int i = 0; i < v.size(); i++)
  15 + cout << v[i] << " ";
  16 + cout << endl;
18 } 17 }
examples01-en/04-qt-1/hello.cpp 100755 → 100644
1 #include <QApplication> 1 #include <QApplication>
2 #include <QPushButton> 2 #include <QPushButton>
3 3
4 -int main(int argc, char *argv[]) 4 +int main(int argc, char* argv[])
5 { 5 {
6 QApplication app(argc, argv); 6 QApplication app(argc, argv);
7 7
examples01-en/05-qt-2/hello.cpp 100755 → 100644
1 #include <QApplication> 1 #include <QApplication>
2 #include <QPushButton> 2 #include <QPushButton>
3 3
4 -int main(int argc, char *argv[]) 4 +int main(int argc, char* argv[])
5 { 5 {
6 QApplication app(argc, argv); 6 QApplication app(argc, argv);
7 7
8 QPushButton hello("Hello world!"); 8 QPushButton hello("Hello world!");
9 hello.resize(100, 30); 9 hello.resize(100, 30);
10 hello.setFont(QFont("Arial", 18, QFont::Bold)); 10 hello.setFont(QFont("Arial", 18, QFont::Bold));
11 - hello.setGeometry(400, 200 , 180, 40); 11 + hello.setGeometry(400, 200, 180, 40);
12 12
13 hello.show(); 13 hello.show();
14 return app.exec(); 14 return app.exec();
examples01-en/06-qt-3/hello.cpp 100755 → 100644
1 #include <QApplication> 1 #include <QApplication>
2 -#include <QPushButton>  
3 #include <QDial> 2 #include <QDial>
  3 +#include <QPushButton>
4 4
5 -int main(int argc, char *argv[]) 5 +int main(int argc, char* argv[])
6 { 6 {
7 QApplication app(argc, argv); 7 QApplication app(argc, argv);
8 8
9 QPushButton hello("Hello world!"); 9 QPushButton hello("Hello world!");
10 hello.resize(100, 30); 10 hello.resize(100, 30);
11 hello.setFont(QFont("Arial", 18, QFont::Bold)); 11 hello.setFont(QFont("Arial", 18, QFont::Bold));
12 - hello.setGeometry(400, 200 , 180, 40); 12 + hello.setGeometry(400, 200, 180, 40);
13 13
14 QDial dial; 14 QDial dial;
15 dial.show(); 15 dial.show();
examples01-en/07-stackc/stack.c
  1 +#include "stack.h"
1 #include <assert.h> 2 #include <assert.h>
2 -#include <stdlib.h>  
3 #include <stdio.h> 3 #include <stdio.h>
4 -#include "stack.h" 4 +#include <stdlib.h>
5 5
6 void init(struct stack_handle* s) 6 void init(struct stack_handle* s)
7 { 7 {
8 - s->top=0; 8 + s->top = 0;
9 } 9 }
10 10
11 -void finalize(struct stack_handle* s)  
12 -{  
13 -} 11 +void finalize(struct stack_handle* s) {}
14 12
15 void clear(struct stack_handle* s) 13 void clear(struct stack_handle* s)
16 { 14 {
17 - s->top=0; 15 + s->top = 0;
18 } 16 }
19 17
20 -void push(struct stack_handle* s,int a) 18 +void push(struct stack_handle* s, int a)
21 { 19 {
22 - assert(s->top<STACKSIZE);  
23 - s->data[s->top++]=a; 20 + assert(s->top < STACKSIZE);
  21 + s->data[s->top++] = a;
24 } 22 }
25 23
26 int pop(struct stack_handle* s) 24 int pop(struct stack_handle* s)
27 { 25 {
28 - assert(s->top>0);  
29 - return s->data[--s->top]; 26 + assert(s->top > 0);
  27 + return s->data[--s->top];
30 } 28 }
31 -  
examples01-en/07-stackc/stack.h
1 #define STACKSIZE 20 1 #define STACKSIZE 20
2 2
3 -struct stack_handle  
4 -{  
5 -int top; /* index of first free slot on stack */  
6 -int data[STACKSIZE]; 3 +struct stack_handle {
  4 + int top; /* index of first free slot on stack */
  5 + int data[STACKSIZE];
7 }; 6 };
8 7
9 -void push(struct stack_handle* s,int a); 8 +void push(struct stack_handle* s, int a);
10 int pop(struct stack_handle* s); 9 int pop(struct stack_handle* s);
11 void clear(struct stack_handle* s); 10 void clear(struct stack_handle* s);
12 void init(struct stack_handle* s); 11 void init(struct stack_handle* s);
13 void finalize(struct stack_handle* s); 12 void finalize(struct stack_handle* s);
14 -  
examples01-en/07-stackc/teststack.c
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - struct stack_handle s1;  
7 - struct stack_handle s2;  
8 - init(&s1);  
9 - init(&s2);  
10 - push(&s1,1);  
11 - push(&s1,2);  
12 - push(&s1,3);  
13 - push(&s2,5);  
14 - push(&s2,6);  
15 - printf("%d %d\n",pop(&s1),pop(&s2));  
16 - printf("%d\n",pop(&s1));  
17 - finalize(&s1);  
18 - finalize(&s2);  
19 - return 0; 6 + struct stack_handle s1;
  7 + struct stack_handle s2;
  8 + init(&s1);
  9 + init(&s2);
  10 + push(&s1, 1);
  11 + push(&s1, 2);
  12 + push(&s1, 3);
  13 + push(&s2, 5);
  14 + push(&s2, 6);
  15 + printf("%d %d\n", pop(&s1), pop(&s2));
  16 + printf("%d\n", pop(&s1));
  17 + finalize(&s1);
  18 + finalize(&s2);
  19 + return 0;
20 } 20 }
examples01-en/08-stackcpp/stack.cpp
1 -#include <assert.h>  
2 -#include <stdlib.h>  
3 -#include <stdio.h>  
4 #include "stack.h" 1 #include "stack.h"
  2 +#include <assert.h>
5 #include <iostream> 3 #include <iostream>
  4 +#include <stdio.h>
  5 +#include <stdlib.h>
6 using namespace std; 6 using namespace std;
7 7
8 stack::stack() 8 stack::stack()
9 { 9 {
10 - this->top=0;  
11 - cout << "stack::stack() called\n";  
12 - 10 + this->top = 0;
  11 + cout << "stack::stack() called\n";
13 } 12 }
14 13
15 stack::~stack() 14 stack::~stack()
16 { 15 {
17 - cout << "stack::~stack() called\n"; 16 + cout << "stack::~stack() called\n";
18 } 17 }
19 18
20 void stack::clear() 19 void stack::clear()
21 { 20 {
22 - this->top=0; 21 + this->top = 0;
23 } 22 }
24 23
25 void stack::push(int a) 24 void stack::push(int a)
26 { 25 {
27 - assert(this->top<STACKSIZE);  
28 - this->data[this->top++]=a; 26 + assert(this->top < STACKSIZE);
  27 + this->data[this->top++] = a;
29 } 28 }
30 29
31 int stack::pop() 30 int stack::pop()
32 { 31 {
33 - assert(this->top>0);  
34 - return this->data[--this->top]; 32 + assert(this->top > 0);
  33 + return this->data[--this->top];
35 } 34 }
36 -  
examples01-en/08-stackcpp/stack.h
1 #define STACKSIZE 20 1 #define STACKSIZE 20
2 2
3 -class stack  
4 -{  
5 -public:  
6 -void push(int a);  
7 -int pop();  
8 -void clear();  
9 -stack();  
10 -~stack();  
11 -private:  
12 -int top;  
13 -int data[STACKSIZE];  
14 -}; 3 +class stack {
  4 + public:
  5 + void push(int a);
  6 + int pop();
  7 + void clear();
  8 + stack();
  9 + ~stack();
15 10
  11 + private:
  12 + int top;
  13 + int data[STACKSIZE];
  14 +};
examples01-en/08-stackcpp/teststack.cpp
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - stack s1;  
7 - stack s2;  
8 - s1.push(1);  
9 - s1.push(2);  
10 - s1.push(3);  
11 - s2.push(5);  
12 - s2.push(6);  
13 - {  
14 - stack s3;  
15 - }  
16 - printf("%d %d\n",s1.pop(),s2.pop());  
17 - printf("%d\n",s1.pop());  
18 - return 0; 6 + stack s1;
  7 + stack s2;
  8 + s1.push(1);
  9 + s1.push(2);
  10 + s1.push(3);
  11 + s2.push(5);
  12 + s2.push(6);
  13 + {
  14 + stack s3;
  15 + }
  16 + printf("%d %d\n", s1.pop(), s2.pop());
  17 + printf("%d\n", s1.pop());
  18 + return 0;
19 } 19 }
examples01-en/09-funoverload/overload.cpp
@@ -3,16 +3,16 @@ using namespace std; @@ -3,16 +3,16 @@ using namespace std;
3 3
4 void fun(char c) 4 void fun(char c)
5 { 5 {
6 - cout<<"fun(char) called"<<endl; 6 + cout << "fun(char) called" << endl;
7 } 7 }
8 8
9 void fun(int c) 9 void fun(int c)
10 { 10 {
11 - cout <<"fun(int) called"<<endl; 11 + cout << "fun(int) called" << endl;
12 } 12 }
13 13
14 int main() 14 int main()
15 { 15 {
16 - fun(10);  
17 - fun('0'); 16 + fun(10);
  17 + fun('0');
18 } 18 }
examples01-pl/01-kompilacja/hello.c
@@ -2,6 +2,6 @@ @@ -2,6 +2,6 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 - printf("Hello, world\n");  
6 - return 0; 5 + printf("Hello, world\n");
  6 + return 0;
7 } 7 }
examples01-pl/02-moduly/hello.c
1 -#include <stdio.h>  
2 #include "witaj.h" 1 #include "witaj.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - witaj();  
7 - return 0; 6 + witaj();
  7 + return 0;
8 } 8 }
examples01-pl/02-moduly/witaj.c
1 -#include <stdio.h>  
2 -#include <math.h>  
3 #include "witaj.h" 1 #include "witaj.h"
  2 +#include <math.h>
  3 +#include <stdio.h>
4 4
5 void witaj(void) 5 void witaj(void)
6 { 6 {
7 - printf ("Hello user %g\n",sqrt(16.0)); 7 + printf("Hello user %g\n", sqrt(16.0));
8 } 8 }
examples01-pl/03-cmdline/cmdline.c
@@ -2,10 +2,10 @@ @@ -2,10 +2,10 @@
2 2
3 int main(int argc, char** argv) 3 int main(int argc, char** argv)
4 { 4 {
5 -int i;  
6 -printf("argc==%d\n",argc);  
7 -for(i=0;i<argc;i++)  
8 - printf("%s ",argv[i]);  
9 -printf("\n");  
10 -return 0; 5 + int i;
  6 + printf("argc==%d\n", argc);
  7 + for (i = 0; i < argc; i++)
  8 + printf("%s ", argv[i]);
  9 + printf("\n");
  10 + return 0;
11 } 11 }
examples01-pl/04-malloc/malloc.c
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 3
4 -int  
5 -main () 4 +int main()
6 { 5 {
7 - double *a = NULL;  
8 - int n;  
9 - int i;  
10 - FILE *infile = fopen ("ala", "r");  
11 - if (fscanf (infile, "%d\n", &n) != 1) abort();  
12 - a = (double *) malloc (n * sizeof (double));  
13 - if(a==NULL) abort();  
14 - for (i = 0; i < n; i++)  
15 - if (1 != fscanf (infile, "%lf ", /*a+i */ &(a[i]))) abort();  
16 - for (i = 0; i < n; i++)  
17 - printf ("%f ", a[i]);  
18 - printf("\n");  
19 - free (a);  
20 - fclose (infile);  
21 - return 0; 6 + double* a = NULL;
  7 + int n;
  8 + int i;
  9 + FILE* infile = fopen("ala", "r");
  10 + if (fscanf(infile, "%d\n", &n) != 1)
  11 + abort();
  12 + a = (double*)malloc(n * sizeof(double));
  13 + if (a == NULL)
  14 + abort();
  15 + for (i = 0; i < n; i++)
  16 + if (1 != fscanf(infile, "%lf ", /*a+i */ &(a[i])))
  17 + abort();
  18 + for (i = 0; i < n; i++)
  19 + printf("%f ", a[i]);
  20 + printf("\n");
  21 + free(a);
  22 + fclose(infile);
  23 + return 0;
22 } 24 }
examples01-pl/05-2darray/2darray.c
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 3
4 -int  
5 -main () 4 +int main()
6 { 5 {
7 - double **a = NULL;  
8 - double **b = NULL;  
9 - int rows = 3;  
10 - int columns = 5;  
11 - int i;  
12 - a = (double **) malloc (rows * sizeof (double *));  
13 - for (i = 0; i < rows; i++)  
14 - a[i] = (double *) malloc (columns * sizeof (double));  
15 - b = (double **) malloc (rows * sizeof (double *));  
16 - b[0] = (double *) malloc (rows * columns * sizeof (double));  
17 - for (i = 1; i < rows; i++)  
18 - b[i] = b[i-1] + columns;  
19 - /* use a and b */  
20 - for (i = 0; i < rows; i++)  
21 - free (a[i]);  
22 - free (a);  
23 - free (b[0]);  
24 - free (b);  
25 - return 0; 6 + double** a = NULL;
  7 + double** b = NULL;
  8 + int rows = 3;
  9 + int columns = 5;
  10 + int i;
  11 + a = (double**)malloc(rows * sizeof(double*));
  12 + for (i = 0; i < rows; i++)
  13 + a[i] = (double*)malloc(columns * sizeof(double));
  14 + b = (double**)malloc(rows * sizeof(double*));
  15 + b[0] = (double*)malloc(rows * columns * sizeof(double));
  16 + for (i = 1; i < rows; i++)
  17 + b[i] = b[i - 1] + columns;
  18 + /* use a and b */
  19 + for (i = 0; i < rows; i++)
  20 + free(a[i]);
  21 + free(a);
  22 + free(b[0]);
  23 + free(b);
  24 + return 0;
26 } 25 }
examples01-pl/06-mymalloc/main.c
1 -#include <assert.h>  
2 #include "mymalloc.h" 1 #include "mymalloc.h"
  2 +#include <assert.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 -void* p1;  
7 -void* p2;  
8 -void* p3;  
9 -void* p4;  
10 -void* p5; 6 + void* p1;
  7 + void* p2;
  8 + void* p3;
  9 + void* p4;
  10 + void* p5;
11 11
12 -if(!mymalloc_init(10000000)) abort();  
13 -mymalloc_dump();  
14 -p1=mymalloc(100);  
15 -mymalloc_dump();  
16 -p2=mymalloc(200);  
17 -mymalloc_dump();  
18 -p3=mymalloc(300);  
19 -mymalloc_dump();  
20 -myfree(p2);  
21 -mymalloc_dump();  
22 -p2=mymalloc(150);  
23 -mymalloc_dump();  
24 -p2=myrealloc(p2,170);  
25 -mymalloc_dump();  
26 -p2=myrealloc(p2,300);  
27 -mymalloc_dump();  
28 -p4=mymalloc(10000);  
29 -mymalloc_dump();  
30 -p5=mymalloc(50000);  
31 -mymalloc_dump();  
32 -myfree(p4);  
33 -mymalloc_dump();  
34 -p4=mymalloc(7000);  
35 -mymalloc_dump();  
36 -myfree(p2);  
37 -mymalloc_dump();  
38 -myfree(p1);  
39 -mymalloc_dump();  
40 -myfree(p3);  
41 -mymalloc_dump();  
42 -myfree(p4);  
43 -mymalloc_dump();  
44 -myfree(p5);  
45 -mymalloc_dump();  
46 -return 0; 12 + if (!mymalloc_init(10000000))
  13 + abort();
  14 + mymalloc_dump();
  15 + p1 = mymalloc(100);
  16 + mymalloc_dump();
  17 + p2 = mymalloc(200);
  18 + mymalloc_dump();
  19 + p3 = mymalloc(300);
  20 + mymalloc_dump();
  21 + myfree(p2);
  22 + mymalloc_dump();
  23 + p2 = mymalloc(150);
  24 + mymalloc_dump();
  25 + p2 = myrealloc(p2, 170);
  26 + mymalloc_dump();
  27 + p2 = myrealloc(p2, 300);
  28 + mymalloc_dump();
  29 + p4 = mymalloc(10000);
  30 + mymalloc_dump();
  31 + p5 = mymalloc(50000);
  32 + mymalloc_dump();
  33 + myfree(p4);
  34 + mymalloc_dump();
  35 + p4 = mymalloc(7000);
  36 + mymalloc_dump();
  37 + myfree(p2);
  38 + mymalloc_dump();
  39 + myfree(p1);
  40 + mymalloc_dump();
  41 + myfree(p3);
  42 + mymalloc_dump();
  43 + myfree(p4);
  44 + mymalloc_dump();
  45 + myfree(p5);
  46 + mymalloc_dump();
  47 + return 0;
47 } 48 }
examples01-pl/06-mymalloc/mymalloc.c
1 -#include <unistd.h> 1 +#include "mymalloc.h"
2 #include <assert.h> 2 #include <assert.h>
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <string.h> 4 #include <string.h>
5 -#include "mymalloc.h" 5 +#include <unistd.h>
6 6
7 -typedef struct _blkdata  
8 -{  
9 - unsigned long size; /* rozmiar bloku wlaczajac naglowek */  
10 - unsigned int flags;  
11 -}  
12 -blkdata; 7 +typedef struct _blkdata {
  8 + unsigned long size; /* rozmiar bloku wlaczajac naglowek */
  9 + unsigned int flags;
  10 +} blkdata;
13 #define MM_OCCUPIED 1 11 #define MM_OCCUPIED 1
14 #define MM_LAST 2 12 #define MM_LAST 2
15 13
16 -static void *begin; 14 +static void* begin;
17 15
18 -int  
19 -mymalloc_init (unsigned long size) 16 +int mymalloc_init(unsigned long size)
20 { 17 {
21 - blkdata *b;  
22 - assert (size > 0);  
23 - begin = sbrk (size);  
24 - if (!begin)  
25 - return 0;  
26 - b = (blkdata *) begin;  
27 - b->size = size;  
28 - b->flags = MM_LAST;  
29 - return 1; 18 + blkdata* b;
  19 + assert(size > 0);
  20 + begin = sbrk(size);
  21 + if (!begin)
  22 + return 0;
  23 + b = (blkdata*)begin;
  24 + b->size = size;
  25 + b->flags = MM_LAST;
  26 + return 1;
30 }; 27 };
31 28
32 -void *  
33 -mymalloc (unsigned long size) 29 +void* mymalloc(unsigned long size)
34 { 30 {
35 - blkdata *b = (blkdata *) begin;  
36 - printf ("Request to allocate %lu bytes\n", size);  
37 - while (1)  
38 - {  
39 - if ((b->size > size + sizeof (blkdata)) && !(b->flags & MM_OCCUPIED))  
40 - {  
41 - unsigned long after = b->size - size - sizeof (blkdata);  
42 - if (after > sizeof (blkdata))  
43 - {  
44 - blkdata *na =  
45 - (blkdata *) ((void *) b + size + sizeof (blkdata));  
46 - na->flags = 0;  
47 - if (b->flags & MM_LAST)  
48 - na->flags |= MM_LAST;  
49 - na->size = after;  
50 - b->size = size + sizeof (blkdata);  
51 - b->flags = MM_OCCUPIED;  
52 - return ((void *) b) + sizeof (blkdata);  
53 - }  
54 - else /* wez caly blok */  
55 - {  
56 - b->flags |= MM_OCCUPIED;  
57 - return ((void *) b) + sizeof (blkdata);  
58 - }  
59 - };  
60 - if (b->flags & MM_LAST)  
61 - break;  
62 - b = (blkdata *) ((void *) b + b->size); 31 + blkdata* b = (blkdata*)begin;
  32 + printf("Request to allocate %lu bytes\n", size);
  33 + while (1) {
  34 + if ((b->size > size + sizeof(blkdata)) && !(b->flags & MM_OCCUPIED)) {
  35 + unsigned long after = b->size - size - sizeof(blkdata);
  36 + if (after > sizeof(blkdata)) {
  37 + blkdata* na = (blkdata*)((void*)b + size + sizeof(blkdata));
  38 + na->flags = 0;
  39 + if (b->flags & MM_LAST)
  40 + na->flags |= MM_LAST;
  41 + na->size = after;
  42 + b->size = size + sizeof(blkdata);
  43 + b->flags = MM_OCCUPIED;
  44 + return ((void*)b) + sizeof(blkdata);
  45 + }
  46 + else /* wez caly blok */
  47 + {
  48 + b->flags |= MM_OCCUPIED;
  49 + return ((void*)b) + sizeof(blkdata);
  50 + }
  51 + };
  52 + if (b->flags & MM_LAST)
  53 + break;
  54 + b = (blkdata*)((void*)b + b->size);
63 }; 55 };
64 - return NULL; 56 + return NULL;
65 } 57 }
66 58
67 -void  
68 -myfree (void *ptr) 59 +void myfree(void* ptr)
69 { 60 {
70 - blkdata *b;  
71 - blkdata *nextb = NULL;  
72 - blkdata *prevb = NULL;  
73 - if (ptr == NULL)  
74 - return;  
75 - b = (blkdata *) (ptr - sizeof (blkdata));  
76 - printf ("Request to free %lu bytes at the address %p\n", b->size - sizeof (blkdata), b);  
77 - if ((void *) b != begin)  
78 - {  
79 -/* znajdz poprzedni blok */  
80 - prevb = (blkdata *) begin;  
81 - while (1)  
82 - {  
83 - blkdata *next;  
84 - assert (!(prevb->flags & MM_LAST));  
85 - next = (blkdata *) ((void *) prevb + prevb->size);  
86 - if (next == b)  
87 - break;  
88 - prevb = next;  
89 - }; 61 + blkdata* b;
  62 + blkdata* nextb = NULL;
  63 + blkdata* prevb = NULL;
  64 + if (ptr == NULL)
  65 + return;
  66 + b = (blkdata*)(ptr - sizeof(blkdata));
  67 + printf("Request to free %lu bytes at the address %p\n",
  68 + b->size - sizeof(blkdata), b);
  69 + if ((void*)b != begin) {
  70 + /* znajdz poprzedni blok */
  71 + prevb = (blkdata*)begin;
  72 + while (1) {
  73 + blkdata* next;
  74 + assert(!(prevb->flags & MM_LAST));
  75 + next = (blkdata*)((void*)prevb + prevb->size);
  76 + if (next == b)
  77 + break;
  78 + prevb = next;
  79 + };
90 }; 80 };
91 -/* znajdz nastepny blok */  
92 - if (!(b->flags & MM_LAST))  
93 - nextb = (blkdata *) ((void *) b + b->size);  
94 -/* zaznacz blok jako wolny */  
95 - b->flags &= ~MM_OCCUPIED;  
96 -/* skonsoliduj z nastepnym */  
97 - if (nextb && !(nextb->flags & MM_OCCUPIED))  
98 - {  
99 - if (nextb->flags & MM_LAST)  
100 - b->flags = MM_LAST;  
101 - b->size += nextb->size; 81 + /* znajdz nastepny blok */
  82 + if (!(b->flags & MM_LAST))
  83 + nextb = (blkdata*)((void*)b + b->size);
  84 + /* zaznacz blok jako wolny */
  85 + b->flags &= ~MM_OCCUPIED;
  86 + /* skonsoliduj z nastepnym */
  87 + if (nextb && !(nextb->flags & MM_OCCUPIED)) {
  88 + if (nextb->flags & MM_LAST)
  89 + b->flags = MM_LAST;
  90 + b->size += nextb->size;
102 } 91 }
103 -/* skonsoliduj z poprzednim */  
104 - if (prevb && !(prevb->flags & MM_OCCUPIED))  
105 - {  
106 - if (b->flags & MM_LAST)  
107 - prevb->flags |= MM_LAST;  
108 - prevb->size += b->size; 92 + /* skonsoliduj z poprzednim */
  93 + if (prevb && !(prevb->flags & MM_OCCUPIED)) {
  94 + if (b->flags & MM_LAST)
  95 + prevb->flags |= MM_LAST;
  96 + prevb->size += b->size;
109 }; 97 };
110 } 98 }
111 99
112 -static void *  
113 -copyrealloc (void *ptr, unsigned long size, unsigned long oldsize) 100 +static void* copyrealloc(void* ptr, unsigned long size, unsigned long oldsize)
114 { 101 {
115 - void *blk = mymalloc (size);  
116 - if (blk == NULL)  
117 - return NULL;  
118 - memcpy (blk, ptr, oldsize);  
119 - myfree (ptr);  
120 - return blk; 102 + void* blk = mymalloc(size);
  103 + if (blk == NULL)
  104 + return NULL;
  105 + memcpy(blk, ptr, oldsize);
  106 + myfree(ptr);
  107 + return blk;
121 } 108 }
122 109
123 -void *  
124 -myrealloc (void *ptr, unsigned long size) 110 +void* myrealloc(void* ptr, unsigned long size)
125 { 111 {
126 - blkdata *b;  
127 - if (ptr == NULL)  
128 - return mymalloc (size);  
129 - b = (blkdata *) (ptr - sizeof (blkdata));  
130 - printf ("Request to reallocate %lu bytes to %lu bytes, old address=%p\n",  
131 - b->size - sizeof (blkdata), size, b);  
132 - if (size == 0)  
133 - {  
134 - myfree (ptr);  
135 - return NULL; 112 + blkdata* b;
  113 + if (ptr == NULL)
  114 + return mymalloc(size);
  115 + b = (blkdata*)(ptr - sizeof(blkdata));
  116 + printf("Request to reallocate %lu bytes to %lu bytes, old address=%p\n",
  117 + b->size - sizeof(blkdata), size, b);
  118 + if (size == 0) {
  119 + myfree(ptr);
  120 + return NULL;
136 }; 121 };
137 - if (size <= b->size - sizeof (blkdata)) /* zmniejszenie bloku */ 122 + if (size <= b->size - sizeof(blkdata)) /* zmniejszenie bloku */
138 { 123 {
139 - unsigned long after = b->size - size - sizeof (blkdata);  
140 - blkdata *nextb = NULL;  
141 - if (!(b->flags & MM_LAST))  
142 - nextb = (blkdata *) ((void *) b + b->size);  
143 - blkdata *na = ((void *) b) + size + sizeof (blkdata);  
144 - if (nextb && !(nextb->flags & MM_OCCUPIED))  
145 - /* skonsoliduj z nastepnym */  
146 - {  
147 - unsigned int flags = nextb->flags;  
148 - unsigned long size = after + nextb->size;  
149 - na->flags = flags;  
150 - na->size = size;  
151 - b->size = size + sizeof (blkdata);  
152 - }  
153 - else if (after > sizeof (blkdata))  
154 - {  
155 - na->flags = 0;  
156 - if (b->flags & MM_LAST)  
157 - na->flags |= MM_LAST;  
158 - na->size = after;  
159 - b->size = size + sizeof (blkdata);  
160 - b->flags = MM_OCCUPIED;  
161 - }  
162 - return ptr; 124 + unsigned long after = b->size - size - sizeof(blkdata);
  125 + blkdata* nextb = NULL;
  126 + if (!(b->flags & MM_LAST))
  127 + nextb = (blkdata*)((void*)b + b->size);
  128 + blkdata* na = ((void*)b) + size + sizeof(blkdata);
  129 + if (nextb && !(nextb->flags & MM_OCCUPIED))
  130 + /* skonsoliduj z nastepnym */
  131 + {
  132 + unsigned int flags = nextb->flags;
  133 + unsigned long size = after + nextb->size;
  134 + na->flags = flags;
  135 + na->size = size;
  136 + b->size = size + sizeof(blkdata);
  137 + }
  138 + else if (after > sizeof(blkdata)) {
  139 + na->flags = 0;
  140 + if (b->flags & MM_LAST)
  141 + na->flags |= MM_LAST;
  142 + na->size = after;
  143 + b->size = size + sizeof(blkdata);
  144 + b->flags = MM_OCCUPIED;
  145 + }
  146 + return ptr;
163 } 147 }
164 - else /* zwiekszenie bloku */ 148 + else /* zwiekszenie bloku */
165 { 149 {
166 - blkdata *next = (blkdata *) ((void *) b + b->size);  
167 - if (!(b->flags & MM_LAST) && !(next->flags & MM_OCCUPIED))  
168 - {  
169 - unsigned long totalsize = b->size + next->size;  
170 - unsigned long after = totalsize - size - sizeof (blkdata);  
171 - if (size + sizeof (blkdata) > totalsize) /* brak miejsca w nastepnym bloku */  
172 - return copyrealloc (ptr, size, b->size - sizeof (blkdata));  
173 - if (next->flags & MM_LAST)  
174 - b->flags |= MM_LAST;  
175 - if (after > sizeof (blkdata))  
176 - {  
177 - blkdata *na =  
178 - (blkdata *) ((void *) b + size + sizeof (blkdata));  
179 - na->flags = 0;  
180 - if (b->flags & MM_LAST)  
181 - na->flags |= MM_LAST;  
182 - na->size = after;  
183 - b->size = size + sizeof (blkdata);  
184 - b->flags = MM_OCCUPIED;  
185 - }  
186 - else  
187 - b->size += next->size;  
188 - return ptr;  
189 - }  
190 - else /* nastepny zajety albo biezacy ostatni */  
191 - return copyrealloc (ptr, size, b->size - sizeof (blkdata)); 150 + blkdata* next = (blkdata*)((void*)b + b->size);
  151 + if (!(b->flags & MM_LAST) && !(next->flags & MM_OCCUPIED)) {
  152 + unsigned long totalsize = b->size + next->size;
  153 + unsigned long after = totalsize - size - sizeof(blkdata);
  154 + if (size + sizeof(blkdata) >
  155 + totalsize) /* brak miejsca w nastepnym bloku */
  156 + return copyrealloc(ptr, size, b->size - sizeof(blkdata));
  157 + if (next->flags & MM_LAST)
  158 + b->flags |= MM_LAST;
  159 + if (after > sizeof(blkdata)) {
  160 + blkdata* na = (blkdata*)((void*)b + size + sizeof(blkdata));
  161 + na->flags = 0;
  162 + if (b->flags & MM_LAST)
  163 + na->flags |= MM_LAST;
  164 + na->size = after;
  165 + b->size = size + sizeof(blkdata);
  166 + b->flags = MM_OCCUPIED;
  167 + }
  168 + else
  169 + b->size += next->size;
  170 + return ptr;
  171 + }
  172 + else /* nastepny zajety albo biezacy ostatni */
  173 + return copyrealloc(ptr, size, b->size - sizeof(blkdata));
192 } 174 }
193 } 175 }
194 176
195 -void  
196 -mymalloc_dump () 177 +void mymalloc_dump()
197 { 178 {
198 - blkdata *b = (blkdata *) begin;  
199 - unsigned long totalsize = 0;  
200 - printf ("Mymalloc block list dump\n");  
201 - while (1)  
202 - {  
203 - printf ("Block at the address %p flags %c%c size %lu\n", b,  
204 - (b->flags & MM_OCCUPIED) ? 'O' : 'F',  
205 - (b->flags & MM_LAST) ? 'L' : ' ', b->size);  
206 - totalsize += b->size;  
207 - if (b->flags & MM_LAST)  
208 - break;  
209 - b = (blkdata *) ((void *) b + b->size); 179 + blkdata* b = (blkdata*)begin;
  180 + unsigned long totalsize = 0;
  181 + printf("Mymalloc block list dump\n");
  182 + while (1) {
  183 + printf("Block at the address %p flags %c%c size %lu\n", b,
  184 + (b->flags & MM_OCCUPIED) ? 'O' : 'F',
  185 + (b->flags & MM_LAST) ? 'L' : ' ', b->size);
  186 + totalsize += b->size;
  187 + if (b->flags & MM_LAST)
  188 + break;
  189 + b = (blkdata*)((void*)b + b->size);
210 }; 190 };
211 - printf ("Total size:%lu\n", totalsize); 191 + printf("Total size:%lu\n", totalsize);
212 } 192 }
examples01-pl/06-mymalloc/mymalloc.h
@@ -3,4 +3,3 @@ void* mymalloc(unsigned long size); @@ -3,4 +3,3 @@ void* mymalloc(unsigned long size);
3 void myfree(void* ptr); 3 void myfree(void* ptr);
4 void* myrealloc(void* ptr, unsigned long size); 4 void* myrealloc(void* ptr, unsigned long size);
5 void mymalloc_dump(void); 5 void mymalloc_dump(void);
6 -  
examples01-pl/07-badexample1/bad.c
@@ -2,8 +2,7 @@ @@ -2,8 +2,7 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 - int* a;  
6 - *a=10;  
7 - return 0; 5 + int* a;
  6 + *a = 10;
  7 + return 0;
8 } 8 }
9 -  
examples01-pl/07-badexample1/good.c
@@ -2,8 +2,8 @@ @@ -2,8 +2,8 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 - int b;  
6 - int* a=&b;  
7 - *a=10;  
8 - return 0; 5 + int b;
  6 + int* a = &b;
  7 + *a = 10;
  8 + return 0;
9 } 9 }
examples01-pl/08-badexample2/bad1.c
1 -#include <stdlib.h>  
2 #include <stdio.h> 1 #include <stdio.h>
  2 +#include <stdlib.h>
3 3
4 char* itoa(int a) 4 char* itoa(int a)
5 { 5 {
6 - char buf[20];  
7 - snprintf(buf,20,"%d", a);  
8 - return buf; 6 + char buf[20];
  7 + snprintf(buf, 20, "%d", a);
  8 + return buf;
9 } 9 }
10 10
11 int main() 11 int main()
12 { 12 {
13 - printf("%s %s\n",itoa(17), itoa(15));  
14 - return 0; 13 + printf("%s %s\n", itoa(17), itoa(15));
  14 + return 0;
15 } 15 }
16 -  
examples01-pl/08-badexample2/bad2.c
1 -#include <stdlib.h>  
2 #include <stdio.h> 1 #include <stdio.h>
  2 +#include <stdlib.h>
3 3
4 char* itoa(int a) 4 char* itoa(int a)
5 { 5 {
6 - static char buf[20];  
7 - snprintf(buf,20,"%d", a);  
8 - return buf; 6 + static char buf[20];
  7 + snprintf(buf, 20, "%d", a);
  8 + return buf;
9 } 9 }
10 10
11 int main() 11 int main()
12 { 12 {
13 - printf("%s %s\n",itoa(17), itoa(15));  
14 - return 0; 13 + printf("%s %s\n", itoa(17), itoa(15));
  14 + return 0;
15 } 15 }
16 -  
examples01-pl/08-badexample2/good.c
1 -#include <stdlib.h>  
2 #include <stdio.h> 1 #include <stdio.h>
  2 +#include <stdlib.h>
3 3
4 char* itoa(int a) 4 char* itoa(int a)
5 { 5 {
6 - char * buf = (char*)malloc(20);  
7 - snprintf(buf,20,"%d", a);  
8 - return buf; 6 + char* buf = (char*)malloc(20);
  7 + snprintf(buf, 20, "%d", a);
  8 + return buf;
9 } 9 }
10 10
11 int main() 11 int main()
12 { 12 {
13 - char *w1, *w2;  
14 - printf("%s %s\n",w1=itoa(17), w2=itoa(15));  
15 - free(w1);  
16 - free(w2);  
17 - return 0; 13 + char *w1, *w2;
  14 + printf("%s %s\n", w1 = itoa(17), w2 = itoa(15));
  15 + free(w1);
  16 + free(w2);
  17 + return 0;
18 } 18 }
19 -  
examples01-pl/09-valgrind/testvalgrind1.c
@@ -2,8 +2,7 @@ @@ -2,8 +2,7 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 - char* a=(char*)malloc(10);  
6 - a[10]='c';  
7 - return 0; 5 + char* a = (char*)malloc(10);
  6 + a[10] = 'c';
  7 + return 0;
8 } 8 }
9 -  
examples01-pl/09-valgrind/testvalgrind2.c
@@ -2,9 +2,8 @@ @@ -2,9 +2,8 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 - char a[10];  
6 - a[10]='c';  
7 - free(a);  
8 - return 0; 5 + char a[10];
  6 + a[10] = 'c';
  7 + free(a);
  8 + return 0;
9 } 9 }
10 -  
examples01-pl/10-firstcpp/hellocpp.cpp
@@ -3,5 +3,5 @@ using namespace std; @@ -3,5 +3,5 @@ using namespace std;
3 3
4 int main() 4 int main()
5 { 5 {
6 - cout << "Hello from c++\n"; 6 + cout << "Hello from c++\n";
7 } 7 }
examples01-pl/11-string/string.cpp
@@ -4,15 +4,13 @@ using namespace std; @@ -4,15 +4,13 @@ using namespace std;
4 4
5 string getword() 5 string getword()
6 { 6 {
7 - string s;  
8 - cin >> s;  
9 - return s; 7 + string s;
  8 + cin >> s;
  9 + return s;
10 } 10 }
11 11
12 -  
13 int main() 12 int main()
14 -{  
15 - cout << "Podaj slowo" << endl;  
16 - cout << "Podane slowo to:" << getword() << endl; 13 +{
  14 + cout << "Podaj slowo" << endl;
  15 + cout << "Podane slowo to:" << getword() << endl;
17 } 16 }
18 -  
examples01-pl/12-vector/vector.cpp
1 #include <iostream> 1 #include <iostream>
2 -#include <vector>  
3 #include <string> 2 #include <string>
  3 +#include <vector>
4 using namespace std; 4 using namespace std;
5 5
6 int main() 6 int main()
7 { 7 {
8 - string word;  
9 - vector<string> v;  
10 - while(cin)  
11 - {  
12 - if(cin>>word)  
13 - v.push_back(word);  
14 - }  
15 - for(unsigned int i=0;i<v.size();i++)  
16 - cout << v[i]<<" ";  
17 - cout << endl; 8 + string word;
  9 + vector<string> v;
  10 + while (cin) {
  11 + if (cin >> word)
  12 + v.push_back(word);
  13 + }
  14 + for (unsigned int i = 0; i < v.size(); i++)
  15 + cout << v[i] << " ";
  16 + cout << endl;
18 } 17 }
19 -  
examples01-pl/13-qt-1/hello.cpp 100755 → 100644
1 #include <QApplication> 1 #include <QApplication>
2 #include <QPushButton> 2 #include <QPushButton>
3 3
4 -int main(int argc, char *argv[]) 4 +int main(int argc, char* argv[])
5 { 5 {
6 QApplication app(argc, argv); 6 QApplication app(argc, argv);
7 7
examples01-pl/14-qt-2/hello.cpp 100755 → 100644
1 #include <QApplication> 1 #include <QApplication>
2 #include <QPushButton> 2 #include <QPushButton>
3 3
4 -int main(int argc, char *argv[]) 4 +int main(int argc, char* argv[])
5 { 5 {
6 QApplication app(argc, argv); 6 QApplication app(argc, argv);
7 7
8 QPushButton hello("Hello world!"); 8 QPushButton hello("Hello world!");
9 hello.resize(100, 30); 9 hello.resize(100, 30);
10 hello.setFont(QFont("Arial", 18, QFont::Bold)); 10 hello.setFont(QFont("Arial", 18, QFont::Bold));
11 - hello.setGeometry(400, 200 , 180, 40); 11 + hello.setGeometry(400, 200, 180, 40);
12 12
13 hello.show(); 13 hello.show();
14 return app.exec(); 14 return app.exec();
examples01-pl/15-qt-3/hello.cpp 100755 → 100644
1 #include <QApplication> 1 #include <QApplication>
2 -#include <QPushButton>  
3 #include <QDial> 2 #include <QDial>
  3 +#include <QPushButton>
4 4
5 -int main(int argc, char *argv[]) 5 +int main(int argc, char* argv[])
6 { 6 {
7 QApplication app(argc, argv); 7 QApplication app(argc, argv);
8 8
9 QPushButton hello("Hello world!"); 9 QPushButton hello("Hello world!");
10 hello.resize(100, 30); 10 hello.resize(100, 30);
11 hello.setFont(QFont("Arial", 18, QFont::Bold)); 11 hello.setFont(QFont("Arial", 18, QFont::Bold));
12 - hello.setGeometry(400, 200 , 180, 40); 12 + hello.setGeometry(400, 200, 180, 40);
13 13
14 QDial dial; 14 QDial dial;
15 dial.show(); 15 dial.show();
examples01-pl/16-stack1/stack.c
1 -#include <assert.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <assert.h>
3 3
4 #define STACKSIZE 20 4 #define STACKSIZE 20
5 static int top; /* pierwsze wolne miejsce na stosie */ 5 static int top; /* pierwsze wolne miejsce na stosie */
@@ -7,27 +7,24 @@ static int dane[STACKSIZE]; @@ -7,27 +7,24 @@ static int dane[STACKSIZE];
7 7
8 void init() 8 void init()
9 { 9 {
10 - top=0; 10 + top = 0;
11 } 11 }
12 12
13 -void finalize()  
14 -{  
15 -} 13 +void finalize() {}
16 14
17 void clear() 15 void clear()
18 { 16 {
19 - top=0; 17 + top = 0;
20 } 18 }
21 19
22 void push(int a) 20 void push(int a)
23 { 21 {
24 - assert(top<STACKSIZE);  
25 - dane[top++]=a; 22 + assert(top < STACKSIZE);
  23 + dane[top++] = a;
26 } 24 }
27 25
28 int pop() 26 int pop()
29 { 27 {
30 - assert(top>0);  
31 - return dane[--top]; 28 + assert(top > 0);
  29 + return dane[--top];
32 } 30 }
33 -  
examples01-pl/16-stack1/teststack.c
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - init();  
7 - push(1);  
8 - push(2);  
9 - push(3);  
10 - printf("%d %d\n",pop(),pop());  
11 - printf("%d\n",pop());  
12 - finalize();  
13 - return 0; 6 + init();
  7 + push(1);
  8 + push(2);
  9 + push(3);
  10 + printf("%d %d\n", pop(), pop());
  11 + printf("%d\n", pop());
  12 + finalize();
  13 + return 0;
14 } 14 }
examples01-pl/17-stack2/stack.c
  1 +#include "stack.h"
1 #include <assert.h> 2 #include <assert.h>
2 -#include <stdlib.h>  
3 #include <stdio.h> 3 #include <stdio.h>
4 -#include "stack.h" 4 +#include <stdlib.h>
5 5
6 static int top; /* pierwsze wolne miejsce na stosie */ 6 static int top; /* pierwsze wolne miejsce na stosie */
7 -static int *dane; 7 +static int* dane;
8 static int size; 8 static int size;
9 9
10 void init() 10 void init()
11 { 11 {
12 - top=0;  
13 - size=0;  
14 - dane=0; 12 + top = 0;
  13 + size = 0;
  14 + dane = 0;
15 } 15 }
16 16
17 void finalize() 17 void finalize()
18 { 18 {
19 - free(dane); 19 + free(dane);
20 } 20 }
21 21
22 void clear() 22 void clear()
23 { 23 {
24 - top=0; 24 + top = 0;
25 } 25 }
26 26
27 void push(int a) 27 void push(int a)
28 { 28 {
29 - if(top>=size)  
30 - {  
31 - int newsize=(size+1)*2;  
32 - int* ndane=(int*)realloc(dane,newsize*sizeof(int));  
33 - if(ndane)  
34 - dane=ndane;  
35 - else  
36 - {  
37 - free(dane);  
38 - abort();  
39 - }  
40 - fprintf(stderr,"Rozmiar stosu %d -> %d\n",size,newsize);  
41 - size=newsize;  
42 - }  
43 - dane[top++]=a; 29 + if (top >= size) {
  30 + int newsize = (size + 1) * 2;
  31 + int* ndane = (int*)realloc(dane, newsize * sizeof(int));
  32 + if (ndane)
  33 + dane = ndane;
  34 + else {
  35 + free(dane);
  36 + abort();
  37 + }
  38 + fprintf(stderr, "Rozmiar stosu %d -> %d\n", size, newsize);
  39 + size = newsize;
  40 + }
  41 + dane[top++] = a;
44 } 42 }
45 43
46 int pop() 44 int pop()
47 { 45 {
48 - assert(top>0);  
49 - return dane[--top]; 46 + assert(top > 0);
  47 + return dane[--top];
50 } 48 }
51 -  
examples01-pl/17-stack2/teststack.c
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - init();  
7 - push(1);  
8 - push(2);  
9 - push(3);  
10 - printf("%d %d\n",pop(),pop());  
11 - printf("%d\n",pop());  
12 - finalize();  
13 - return 0; 6 + init();
  7 + push(1);
  8 + push(2);
  9 + push(3);
  10 + printf("%d %d\n", pop(), pop());
  11 + printf("%d\n", pop());
  12 + finalize();
  13 + return 0;
14 } 14 }
examples01-pl/18-stack3/stack.c
  1 +#include "stack.h"
1 #include <assert.h> 2 #include <assert.h>
2 -#include <stdlib.h>  
3 #include <stdio.h> 3 #include <stdio.h>
4 -#include "stack.h" 4 +#include <stdlib.h>
5 5
6 #define STACKSIZE 20 6 #define STACKSIZE 20
7 7
8 -struct stack_handle  
9 -{  
10 -int top; /* pierwsze wolne miejsce na stosie */  
11 -int dane[STACKSIZE]; 8 +struct stack_handle {
  9 + int top; /* pierwsze wolne miejsce na stosie */
  10 + int dane[STACKSIZE];
12 } 11 }
13 12
14 -  
15 -struct stack_handle* init() 13 +struct stack_handle*
  14 +init()
16 { 15 {
17 - struct stack_handle* s=(struct stack_handle*)malloc(sizeof(struct stack_handle));  
18 - s->top=0;  
19 - return s; 16 + struct stack_handle* s =
  17 + (struct stack_handle*)malloc(sizeof(struct stack_handle));
  18 + s->top = 0;
  19 + return s;
20 } 20 }
21 21
22 void finalize(struct stack_handle* s) 22 void finalize(struct stack_handle* s)
23 { 23 {
24 - free(s); 24 + free(s);
25 } 25 }
26 26
27 void clear(struct stack_handle* s) 27 void clear(struct stack_handle* s)
28 { 28 {
29 - s->top=0; 29 + s->top = 0;
30 } 30 }
31 31
32 -void push(struct stack_handle* s,int a) 32 +void push(struct stack_handle* s, int a)
33 { 33 {
34 - assert(s->top<STACKSIZE);  
35 - s->dane[s->top++]=a; 34 + assert(s->top < STACKSIZE);
  35 + s->dane[s->top++] = a;
36 } 36 }
37 37
38 int pop(struct stack_handle* s) 38 int pop(struct stack_handle* s)
39 { 39 {
40 - assert(s->top>0);  
41 - return s->dane[--s->top]; 40 + assert(s->top > 0);
  41 + return s->dane[--s->top];
42 } 42 }
43 -  
examples01-pl/18-stack3/stack.h
1 struct stack_handle; 1 struct stack_handle;
2 2
3 -void push(struct stack_handle* s,int a); 3 +void push(struct stack_handle* s, int a);
4 int pop(struct stack_handle* s); 4 int pop(struct stack_handle* s);
5 void clear(struct stack_handle* s); 5 void clear(struct stack_handle* s);
6 struct stack_handle* init(void); 6 struct stack_handle* init(void);
examples01-pl/18-stack3/teststack.c
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - struct stack_handle * s1=init();  
7 - struct stack_handle * s2=init();  
8 - push(s1,1);  
9 - push(s1,2);  
10 - push(s1,3);  
11 - push(s2,5);  
12 - push(s2,6);  
13 - printf("%d %d\n",pop(s1),pop(s2));  
14 - printf("%d\n",pop(s1));  
15 - finalize(s1);  
16 - finalize(s2);  
17 - return 0; 6 + struct stack_handle* s1 = init();
  7 + struct stack_handle* s2 = init();
  8 + push(s1, 1);
  9 + push(s1, 2);
  10 + push(s1, 3);
  11 + push(s2, 5);
  12 + push(s2, 6);
  13 + printf("%d %d\n", pop(s1), pop(s2));
  14 + printf("%d\n", pop(s1));
  15 + finalize(s1);
  16 + finalize(s2);
  17 + return 0;
18 } 18 }
examples01-pl/19-stack4/stack.c
  1 +#include "stack.h"
1 #include <assert.h> 2 #include <assert.h>
2 -#include <stdlib.h>  
3 #include <stdio.h> 3 #include <stdio.h>
4 -#include "stack.h" 4 +#include <stdlib.h>
5 5
6 void init(struct stack_handle* s) 6 void init(struct stack_handle* s)
7 { 7 {
8 - s->top=0; 8 + s->top = 0;
9 } 9 }
10 10
11 -void finalize(struct stack_handle* s)  
12 -{  
13 -} 11 +void finalize(struct stack_handle* s) {}
14 12
15 void clear(struct stack_handle* s) 13 void clear(struct stack_handle* s)
16 { 14 {
17 - s->top=0; 15 + s->top = 0;
18 } 16 }
19 17
20 -void push(struct stack_handle* s,int a) 18 +void push(struct stack_handle* s, int a)
21 { 19 {
22 - assert(s->top<STACKSIZE);  
23 - s->dane[s->top++]=a; 20 + assert(s->top < STACKSIZE);
  21 + s->dane[s->top++] = a;
24 } 22 }
25 23
26 int pop(struct stack_handle* s) 24 int pop(struct stack_handle* s)
27 { 25 {
28 - assert(s->top>0);  
29 - return s->dane[--s->top]; 26 + assert(s->top > 0);
  27 + return s->dane[--s->top];
30 } 28 }
31 -  
examples01-pl/19-stack4/stack.h
1 #define STACKSIZE 20 1 #define STACKSIZE 20
2 2
3 -struct stack_handle  
4 -{  
5 -int top; /* pierwsze wolne miejsce na stosie */  
6 -int dane[STACKSIZE]; 3 +struct stack_handle {
  4 + int top; /* pierwsze wolne miejsce na stosie */
  5 + int dane[STACKSIZE];
7 }; 6 };
8 7
9 -void push(struct stack_handle* s,int a); 8 +void push(struct stack_handle* s, int a);
10 int pop(struct stack_handle* s); 9 int pop(struct stack_handle* s);
11 void clear(struct stack_handle* s); 10 void clear(struct stack_handle* s);
12 void init(struct stack_handle* s); 11 void init(struct stack_handle* s);
13 void finalize(struct stack_handle* s); 12 void finalize(struct stack_handle* s);
14 -  
examples01-pl/19-stack4/teststack.c
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - struct stack_handle s1;  
7 - struct stack_handle s2;  
8 - init(&s1);  
9 - init(&s2);  
10 - push(&s1,1);  
11 - push(&s1,2);  
12 - push(&s1,3);  
13 - push(&s2,5);  
14 - push(&s2,6);  
15 - printf("%d %d\n",pop(&s1),pop(&s2));  
16 - printf("%d\n",pop(&s1));  
17 - finalize(&s1);  
18 - finalize(&s2);  
19 - return 0; 6 + struct stack_handle s1;
  7 + struct stack_handle s2;
  8 + init(&s1);
  9 + init(&s2);
  10 + push(&s1, 1);
  11 + push(&s1, 2);
  12 + push(&s1, 3);
  13 + push(&s2, 5);
  14 + push(&s2, 6);
  15 + printf("%d %d\n", pop(&s1), pop(&s2));
  16 + printf("%d\n", pop(&s1));
  17 + finalize(&s1);
  18 + finalize(&s2);
  19 + return 0;
20 } 20 }
examples01-pl/20-stack5/stack.cpp
  1 +#include "stack.h"
1 #include <assert.h> 2 #include <assert.h>
2 -#include <stdlib.h>  
3 #include <stdio.h> 3 #include <stdio.h>
4 -#include "stack.h" 4 +#include <stdlib.h>
5 5
6 stack::stack() 6 stack::stack()
7 { 7 {
8 - this->top=0; 8 + this->top = 0;
9 } 9 }
10 10
11 -stack::~stack()  
12 -{  
13 -} 11 +stack::~stack() {}
14 12
15 void stack::clear() 13 void stack::clear()
16 { 14 {
17 - this->top=0; 15 + this->top = 0;
18 } 16 }
19 17
20 void stack::push(int a) 18 void stack::push(int a)
21 { 19 {
22 - assert(this->top<STACKSIZE);  
23 - this->dane[this->top++]=a; 20 + assert(this->top < STACKSIZE);
  21 + this->dane[this->top++] = a;
24 } 22 }
25 23
26 int stack::pop() 24 int stack::pop()
27 { 25 {
28 - assert(this->top>0);  
29 - return this->dane[--this->top]; 26 + assert(this->top > 0);
  27 + return this->dane[--this->top];
30 } 28 }
31 -  
examples01-pl/20-stack5/stack.h
1 #define STACKSIZE 20 1 #define STACKSIZE 20
2 2
3 -class stack  
4 -{  
5 -public:  
6 -void push(int a);  
7 -int pop();  
8 -void clear();  
9 -stack();  
10 -~stack();  
11 -private:  
12 -int top;  
13 -int dane[STACKSIZE];  
14 -}; 3 +class stack {
  4 + public:
  5 + void push(int a);
  6 + int pop();
  7 + void clear();
  8 + stack();
  9 + ~stack();
15 10
  11 + private:
  12 + int top;
  13 + int dane[STACKSIZE];
  14 +};
examples01-pl/20-stack5/teststack.cpp
1 -#include <stdio.h>  
2 #include "stack.h" 1 #include "stack.h"
  2 +#include <stdio.h>
3 3
4 int main() 4 int main()
5 { 5 {
6 - stack s1;  
7 - stack s2;  
8 - s1.push(1);  
9 - s1.push(2);  
10 - s1.push(3);  
11 - s2.push(5);  
12 - s2.push(6);  
13 - printf("%d %d\n",s1.pop(),s2.pop());  
14 - printf("%d\n",s1.pop());  
15 - return 0; 6 + stack s1;
  7 + stack s2;
  8 + s1.push(1);
  9 + s1.push(2);
  10 + s1.push(3);
  11 + s2.push(5);
  12 + s2.push(6);
  13 + printf("%d %d\n", s1.pop(), s2.pop());
  14 + printf("%d\n", s1.pop());
  15 + return 0;
16 } 16 }
examples02/01-date/DateType.cpp
1 #include "DateType.h" 1 #include "DateType.h"
2 2
3 /** Constructors **/ 3 /** Constructors **/
4 -DateType::DateType() {  
5 - Day = 1;  
6 - Month = 1;  
7 - Year = 1; 4 +DateType::DateType()
  5 +{
  6 + Day = 1;
  7 + Month = 1;
  8 + Year = 1;
8 } 9 }
9 -DateType::DateType(int newMonth, int newDay, int newYear) {  
10 - Day = newDay;  
11 - Month = newMonth;  
12 - Year = newYear; 10 +DateType::DateType(int newMonth, int newDay, int newYear)
  11 +{
  12 + Day = newDay;
  13 + Month = newMonth;
  14 + Year = newYear;
13 } 15 }
14 // returns Year 16 // returns Year
15 -int DateType::GetYear() const { return Year; } 17 +int DateType::GetYear() const
  18 +{
  19 + return Year;
  20 +}
16 // returns Month 21 // returns Month
17 -int DateType::GetMonth() const { return Month; } 22 +int DateType::GetMonth() const
  23 +{
  24 + return Month;
  25 +}
18 // returns Day 26 // returns Day
19 -int DateType::GetDay() const { return Day; } 27 +int DateType::GetDay() const
  28 +{
  29 + return Day;
  30 +}
20 31
21 -RelationType DateType::ComparedTo(DateType otherDate) {  
22 - if (Year < otherDate.Year)  
23 - return Precedes;  
24 - if (Year > otherDate.Year)  
25 - return Follows;  
26 - if (Month < otherDate.Month)  
27 - return Precedes;  
28 - if (Month > otherDate.Month)  
29 - return Follows;  
30 - if (Day < otherDate.Day)  
31 - return Precedes;  
32 - if (Day > otherDate.Day)  
33 - return Follows;  
34 - return Same; 32 +RelationType DateType::ComparedTo(DateType otherDate)
  33 +{
  34 + if (Year < otherDate.Year)
  35 + return Precedes;
  36 + if (Year > otherDate.Year)
  37 + return Follows;
  38 + if (Month < otherDate.Month)
  39 + return Precedes;
  40 + if (Month > otherDate.Month)
  41 + return Follows;
  42 + if (Day < otherDate.Day)
  43 + return Precedes;
  44 + if (Day > otherDate.Day)
  45 + return Follows;
  46 + return Same;
35 } 47 }
examples02/01-date/DateType.h
1 enum RelationType { Precedes, Same, Follows }; 1 enum RelationType { Precedes, Same, Follows };
2 2
3 class DateType { 3 class DateType {
4 -public:  
5 - // constructor  
6 - DateType();  
7 - DateType(int newMonth, int newDay, int newYear);  
8 - // accessor methods (get methods)  
9 - int GetYear() const; // returns Year  
10 - int GetMonth() const; // returns Month  
11 - int GetDay() const; // returns Day  
12 - RelationType ComparedTo(DateType dateA); 4 + public:
  5 + // constructor
  6 + DateType();
  7 + DateType(int newMonth, int newDay, int newYear);
  8 + // accessor methods (get methods)
  9 + int GetYear() const; // returns Year
  10 + int GetMonth() const; // returns Month
  11 + int GetDay() const; // returns Day
  12 + RelationType ComparedTo(DateType dateA);
13 13
14 -private:  
15 - int Year;  
16 - int Month;  
17 - int Day; 14 + private:
  15 + int Year;
  16 + int Month;
  17 + int Day;
18 }; 18 };
19 \ No newline at end of file 19 \ No newline at end of file
examples02/01-date/TestDateType.cpp
@@ -3,78 +3,82 @@ @@ -3,78 +3,82 @@
3 #include <iostream> 3 #include <iostream>
4 using namespace std; 4 using namespace std;
5 5
6 -void PrintMonth(int Month, ostream &Out) {  
7 - switch (Month) {  
8 - case 1:  
9 - Out << "January";  
10 - return;  
11 - case 2:  
12 - Out << "February";  
13 - return;  
14 - case 3:  
15 - Out << "March";  
16 - return;  
17 - case 4:  
18 - Out << "April";  
19 - return;  
20 - case 5:  
21 - Out << "May";  
22 - return;  
23 - case 6:  
24 - Out << "June";  
25 - return;  
26 - case 7:  
27 - Out << "July";  
28 - return;  
29 - case 8:  
30 - Out << "August";  
31 - return;  
32 - case 9:  
33 - Out << "September";  
34 - return;  
35 - case 10:  
36 - Out << "October";  
37 - return;  
38 - case 11:  
39 - Out << "November";  
40 - return;  
41 - case 12:  
42 - Out << "December";  
43 - return;  
44 - default:  
45 - Out << "Juvember";  
46 - } 6 +void PrintMonth(int Month, ostream& Out)
  7 +{
  8 + switch (Month) {
  9 + case 1:
  10 + Out << "January";
  11 + return;
  12 + case 2:
  13 + Out << "February";
  14 + return;
  15 + case 3:
  16 + Out << "March";
  17 + return;
  18 + case 4:
  19 + Out << "April";
  20 + return;
  21 + case 5:
  22 + Out << "May";
  23 + return;
  24 + case 6:
  25 + Out << "June";
  26 + return;
  27 + case 7:
  28 + Out << "July";
  29 + return;
  30 + case 8:
  31 + Out << "August";
  32 + return;
  33 + case 9:
  34 + Out << "September";
  35 + return;
  36 + case 10:
  37 + Out << "October";
  38 + return;
  39 + case 11:
  40 + Out << "November";
  41 + return;
  42 + case 12:
  43 + Out << "December";
  44 + return;
  45 + default:
  46 + Out << "Juvember";
  47 + }
47 } 48 }
48 49
49 -void PrintDate(DateType aDate, ostream &Out) {  
50 - PrintMonth(aDate.GetMonth(), Out);  
51 - Out << ' ' << aDate.GetDay() << ", " << setw(4) << aDate.GetYear() << endl; 50 +void PrintDate(DateType aDate, ostream& Out)
  51 +{
  52 + PrintMonth(aDate.GetMonth(), Out);
  53 + Out << ' ' << aDate.GetDay() << ", " << setw(4) << aDate.GetYear() << endl;
52 } 54 }
53 55
54 -RelationType ComparedTo(DateType dateA, DateType dateB) {  
55 - if (dateA.GetYear() < dateB.GetYear())  
56 - return Precedes;  
57 - if (dateA.GetYear() > dateB.GetYear())  
58 - return Follows;  
59 - if (dateA.GetMonth() < dateB.GetMonth())  
60 - return Precedes;  
61 - if (dateA.GetMonth() > dateB.GetMonth())  
62 - return Follows;  
63 - if (dateA.GetDay() < dateB.GetDay())  
64 - return Precedes;  
65 - if (dateA.GetDay() > dateB.GetDay())  
66 - return Follows;  
67 - return Same; 56 +RelationType ComparedTo(DateType dateA, DateType dateB)
  57 +{
  58 + if (dateA.GetYear() < dateB.GetYear())
  59 + return Precedes;
  60 + if (dateA.GetYear() > dateB.GetYear())
  61 + return Follows;
  62 + if (dateA.GetMonth() < dateB.GetMonth())
  63 + return Precedes;
  64 + if (dateA.GetMonth() > dateB.GetMonth())
  65 + return Follows;
  66 + if (dateA.GetDay() < dateB.GetDay())
  67 + return Precedes;
  68 + if (dateA.GetDay() > dateB.GetDay())
  69 + return Follows;
  70 + return Same;
68 } 71 }
69 72
70 -int main() {  
71 - DateType Tomorrow(1, 18, 2002), AnotherDay(10, 12, 1885);  
72 - if (ComparedTo(Tomorrow, AnotherDay) == Same) {  
73 - cout << "what do you think?" << endl;  
74 - } 73 +int main()
  74 +{
  75 + DateType Tomorrow(1, 18, 2002), AnotherDay(10, 12, 1885);
  76 + if (ComparedTo(Tomorrow, AnotherDay) == Same) {
  77 + cout << "what do you think?" << endl;
  78 + }
75 79
76 - if (Tomorrow.ComparedTo(AnotherDay) == Same)  
77 - cout << "Think about it, Scarlett!" << endl; 80 + if (Tomorrow.ComparedTo(AnotherDay) == Same)
  81 + cout << "Think about it, Scarlett!" << endl;
78 82
79 - PrintDate(Tomorrow, cout); 83 + PrintDate(Tomorrow, cout);
80 } 84 }
examples03/00-funptr/funptr.c
1 #include <stdio.h> 1 #include <stdio.h>
2 2
3 -int *  
4 -f1 (int *a, const int *b) 3 +int* f1(int* a, const int* b)
5 { 4 {
6 - *a += *b;  
7 - return a; 5 + *a += *b;
  6 + return a;
8 } 7 }
9 8
10 -int *  
11 -f2 (int *a, const int *b) 9 +int* f2(int* a, const int* b)
12 { 10 {
13 - *a -= *b;  
14 - return a; 11 + *a -= *b;
  12 + return a;
15 } 13 }
16 14
17 -int *(*fun (int s)) (int *, const int *) 15 +int* (*fun(int s))(int*, const int*)
18 { 16 {
19 - if (s == 1)  
20 - return f1;  
21 - else  
22 - return f2; 17 + if (s == 1)
  18 + return f1;
  19 + else
  20 + return f2;
23 } 21 }
24 22
25 -int  
26 -main () 23 +int main()
27 { 24 {
28 - int *(*(*fp2) (int)) (int *, const int *) = fun; 25 + int* (*(*fp2)(int))(int*, const int*) = fun;
29 26
30 - int a = 15;  
31 - int b = 32;  
32 - fp2 (1) (&a, &b);  
33 - fp2 (2) (&b, &a);  
34 - printf ("%d %d\n", a, b);  
35 - return 0; 27 + int a = 15;
  28 + int b = 32;
  29 + fp2(1)(&a, &b);
  30 + fp2(2)(&b, &a);
  31 + printf("%d %d\n", a, b);
  32 + return 0;
36 } 33 }
examples03/01-list/list.cpp
@@ -4,47 +4,46 @@ using namespace std; @@ -4,47 +4,46 @@ using namespace std;
4 4
5 list::list() 5 list::list()
6 { 6 {
7 - head = NULL;  
8 - current = NULL; 7 + head = NULL;
  8 + current = NULL;
9 } 9 }
10 10
11 list::~list() 11 list::~list()
12 { 12 {
13 - while(head)  
14 - {  
15 - node* t=head->next;  
16 - delete head;  
17 - head=t;  
18 - }; 13 + while (head) {
  14 + node* t = head->next;
  15 + delete head;
  16 + head = t;
  17 + };
19 } 18 }
20 19
21 void list::insert(int a) 20 void list::insert(int a)
22 { 21 {
23 - node* t=new node;  
24 - t->next=head;  
25 - head = t;  
26 - head->val = a; 22 + node* t = new node;
  23 + t->next = head;
  24 + head = t;
  25 + head->val = a;
27 } 26 }
28 27
29 void list::goToHead() 28 void list::goToHead()
30 { 29 {
31 - current=head; 30 + current = head;
32 } 31 }
33 32
34 int list::getCurrentData() 33 int list::getCurrentData()
35 { 34 {
36 - return current->val; 35 + return current->val;
37 } 36 }
38 37
39 void list::advance() 38 void list::advance()
40 { 39 {
41 - current=current->next; 40 + current = current->next;
42 } 41 }
43 42
44 bool list::moreData() 43 bool list::moreData()
45 { 44 {
46 - if(current)  
47 - return true;  
48 - else  
49 - return false; 45 + if (current)
  46 + return true;
  47 + else
  48 + return false;
50 } 49 }
examples03/01-list/list.h
1 -class list  
2 -{  
3 -private:  
4 - struct node  
5 - {  
6 - node *next;  
7 - int val;  
8 - };  
9 - node * head;  
10 - node *current;  
11 -public:  
12 - list ();  
13 - ~list ();  
14 - void insert (int a);  
15 - void goToHead ();  
16 - int getCurrentData ();  
17 - void advance ();  
18 - bool moreData (); 1 +class list {
  2 + private:
  3 + struct node {
  4 + node* next;
  5 + int val;
  6 + };
  7 + node* head;
  8 + node* current;
  9 +
  10 + public:
  11 + list();
  12 + ~list();
  13 + void insert(int a);
  14 + void goToHead();
  15 + int getCurrentData();
  16 + void advance();
  17 + bool moreData();
19 }; 18 };
examples03/01-list/testlist.cpp
@@ -4,17 +4,16 @@ using namespace std; @@ -4,17 +4,16 @@ using namespace std;
4 4
5 int main() 5 int main()
6 { 6 {
7 - list l;  
8 - l.insert(1);  
9 - l.insert(2);  
10 - l.insert(3);  
11 - l.goToHead();  
12 - while(l.moreData())  
13 - {  
14 - int val;  
15 - val=l.getCurrentData();  
16 - cout << val << " ";  
17 - l.advance();  
18 - }  
19 - cout << endl; 7 + list l;
  8 + l.insert(1);
  9 + l.insert(2);
  10 + l.insert(3);
  11 + l.goToHead();
  12 + while (l.moreData()) {
  13 + int val;
  14 + val = l.getCurrentData();
  15 + cout << val << " ";
  16 + l.advance();
  17 + }
  18 + cout << endl;
20 } 19 }
examples03/02-list/list.cpp
@@ -4,47 +4,46 @@ using namespace std; @@ -4,47 +4,46 @@ using namespace std;
4 4
5 list::list() 5 list::list()
6 { 6 {
7 - head = NULL;  
8 - current = NULL; 7 + head = NULL;
  8 + current = NULL;
9 } 9 }
10 10
11 list::~list() 11 list::~list()
12 { 12 {
13 - while(head)  
14 - {  
15 - node* t=head->next;  
16 - delete head;  
17 - head=t;  
18 - }; 13 + while (head) {
  14 + node* t = head->next;
  15 + delete head;
  16 + head = t;
  17 + };
19 } 18 }
20 19
21 void list::insert(int a) 20 void list::insert(int a)
22 { 21 {
23 - node* t=new node;  
24 - t->next=head;  
25 - head = t;  
26 - head->val = a; 22 + node* t = new node;
  23 + t->next = head;
  24 + head = t;
  25 + head->val = a;
27 } 26 }
28 27
29 void list::goToHead() 28 void list::goToHead()
30 { 29 {
31 - current=head; 30 + current = head;
32 } 31 }
33 32
34 int list::getCurrentData() 33 int list::getCurrentData()
35 { 34 {
36 - return current->val; 35 + return current->val;
37 } 36 }
38 37
39 void list::advance() 38 void list::advance()
40 { 39 {
41 - current=current->next; 40 + current = current->next;
42 } 41 }
43 42
44 bool list::moreData() 43 bool list::moreData()
45 { 44 {
46 - if(current)  
47 - return true;  
48 - else  
49 - return false; 45 + if (current)
  46 + return true;
  47 + else
  48 + return false;
50 } 49 }
examples03/02-list/list.h
1 -class list  
2 -{  
3 -private:  
4 - struct node  
5 - {  
6 - node *next;  
7 - int val;  
8 - };  
9 - node * head;  
10 - node *current;  
11 -public:  
12 - list ();  
13 - ~list ();  
14 - void insert (int a);  
15 - void goToHead ();  
16 - int getCurrentData ();  
17 - void advance ();  
18 - bool moreData (); 1 +class list {
  2 + private:
  3 + struct node {
  4 + node* next;
  5 + int val;
  6 + };
  7 + node* head;
  8 + node* current;
  9 +
  10 + public:
  11 + list();
  12 + ~list();
  13 + void insert(int a);
  14 + void goToHead();
  15 + int getCurrentData();
  16 + void advance();
  17 + bool moreData();
19 }; 18 };
examples03/02-list/testlist.cpp
@@ -2,33 +2,28 @@ @@ -2,33 +2,28 @@
2 using namespace std; 2 using namespace std;
3 #include "list.h" 3 #include "list.h"
4 4
5 -  
6 -void  
7 -PrintList (list & toPrint, ostream & Out) 5 +void PrintList(list& toPrint, ostream& Out)
8 { 6 {
9 - int nextValue;  
10 - Out << "Printing list contents: " << endl;  
11 - toPrint.goToHead ();  
12 - if (!toPrint.moreData ())  
13 - {  
14 - Out << "List is empty" << endl;  
15 - return; 7 + int nextValue;
  8 + Out << "Printing list contents: " << endl;
  9 + toPrint.goToHead();
  10 + if (!toPrint.moreData()) {
  11 + Out << "List is empty" << endl;
  12 + return;
16 } 13 }
17 - while (toPrint.moreData ())  
18 - {  
19 - nextValue = toPrint.getCurrentData ();  
20 - Out << nextValue << " ";  
21 - toPrint.advance (); 14 + while (toPrint.moreData()) {
  15 + nextValue = toPrint.getCurrentData();
  16 + Out << nextValue << " ";
  17 + toPrint.advance();
22 } 18 }
23 - Out << endl; 19 + Out << endl;
24 } 20 }
25 21
26 -int  
27 -main () 22 +int main()
28 { 23 {
29 - list l;  
30 - l.insert (1);  
31 - l.insert (2);  
32 - l.insert (3);  
33 - PrintList (l, cout); 24 + list l;
  25 + l.insert(1);
  26 + l.insert(2);
  27 + l.insert(3);
  28 + PrintList(l, cout);
34 } 29 }
examples03/03-list/list.cpp
@@ -4,47 +4,46 @@ using namespace std; @@ -4,47 +4,46 @@ using namespace std;
4 4
5 list::list() 5 list::list()
6 { 6 {
7 - head = NULL;  
8 - current = NULL; 7 + head = NULL;
  8 + current = NULL;
9 } 9 }
10 10
11 list::~list() 11 list::~list()
12 { 12 {
13 - while(head)  
14 - {  
15 - node* t=head->next;  
16 - delete head;  
17 - head=t;  
18 - }; 13 + while (head) {
  14 + node* t = head->next;
  15 + delete head;
  16 + head = t;
  17 + };
19 } 18 }
20 19
21 void list::insert(int a) 20 void list::insert(int a)
22 { 21 {
23 - node* t=new node;  
24 - t->next=head;  
25 - head = t;  
26 - head->val = a; 22 + node* t = new node;
  23 + t->next = head;
  24 + head = t;
  25 + head->val = a;
27 } 26 }
28 27
29 void list::goToHead() 28 void list::goToHead()
30 { 29 {
31 - current=head; 30 + current = head;
32 } 31 }
33 32
34 int list::getCurrentData() 33 int list::getCurrentData()
35 { 34 {
36 - return current->val; 35 + return current->val;
37 } 36 }
38 37
39 void list::advance() 38 void list::advance()
40 { 39 {
41 - current=current->next; 40 + current = current->next;
42 } 41 }
43 42
44 bool list::moreData() 43 bool list::moreData()
45 { 44 {
46 - if(current)  
47 - return true;  
48 - else  
49 - return false; 45 + if (current)
  46 + return true;
  47 + else
  48 + return false;
50 } 49 }
examples03/03-list/list.h
1 -class list  
2 -{  
3 -private:  
4 - struct node  
5 - {  
6 - node *next;  
7 - int val;  
8 - };  
9 - node * head;  
10 - node *current;  
11 -public:  
12 - list ();  
13 - ~list ();  
14 - void insert (int a);  
15 - void goToHead ();  
16 - int getCurrentData ();  
17 - void advance ();  
18 - bool moreData (); 1 +class list {
  2 + private:
  3 + struct node {
  4 + node* next;
  5 + int val;
  6 + };
  7 + node* head;
  8 + node* current;
  9 +
  10 + public:
  11 + list();
  12 + ~list();
  13 + void insert(int a);
  14 + void goToHead();
  15 + int getCurrentData();
  16 + void advance();
  17 + bool moreData();
19 }; 18 };
examples03/03-list/testlist.cpp
@@ -2,34 +2,29 @@ @@ -2,34 +2,29 @@
2 using namespace std; 2 using namespace std;
3 #include "list.h" 3 #include "list.h"
4 4
5 -  
6 -void  
7 -PrintList (list toPrint, ostream & Out) 5 +void PrintList(list toPrint, ostream& Out)
8 { 6 {
9 - int nextValue;  
10 - Out << "Printing list contents: " << endl;  
11 - toPrint.goToHead ();  
12 - if (!toPrint.moreData ())  
13 - {  
14 - Out << "List is empty" << endl;  
15 - return; 7 + int nextValue;
  8 + Out << "Printing list contents: " << endl;
  9 + toPrint.goToHead();
  10 + if (!toPrint.moreData()) {
  11 + Out << "List is empty" << endl;
  12 + return;
16 } 13 }
17 - while (toPrint.moreData ())  
18 - {  
19 - nextValue = toPrint.getCurrentData ();  
20 - Out << nextValue << " ";  
21 - toPrint.advance (); 14 + while (toPrint.moreData()) {
  15 + nextValue = toPrint.getCurrentData();
  16 + Out << nextValue << " ";
  17 + toPrint.advance();
22 } 18 }
23 - Out << endl; 19 + Out << endl;
24 } 20 }
25 21
26 -int  
27 -main () 22 +int main()
28 { 23 {
29 - list l;  
30 - l.insert (1);  
31 - l.insert (2);  
32 - l.insert (3);  
33 - PrintList (l, cout);  
34 - cout << endl; 24 + list l;
  25 + l.insert(1);
  26 + l.insert(2);
  27 + l.insert(3);
  28 + PrintList(l, cout);
  29 + cout << endl;
35 } 30 }
examples03/04-list/list.cpp
@@ -2,101 +2,92 @@ @@ -2,101 +2,92 @@
2 using namespace std; 2 using namespace std;
3 #include "list.h" 3 #include "list.h"
4 4
5 -list::list () 5 +list::list()
6 { 6 {
7 - head = NULL;  
8 - current = NULL; 7 + head = NULL;
  8 + current = NULL;
9 } 9 }
10 10
11 -list::~list () 11 +list::~list()
12 { 12 {
13 - while (head)  
14 - {  
15 - node *t = head->next;  
16 - delete head;  
17 - head = t; 13 + while (head) {
  14 + node* t = head->next;
  15 + delete head;
  16 + head = t;
18 }; 17 };
19 } 18 }
20 19
21 -void  
22 -list::insert (int a) 20 +void list::insert(int a)
23 { 21 {
24 - node *t = new node;  
25 - t->next = head;  
26 - head = t;  
27 - head->val = a; 22 + node* t = new node;
  23 + t->next = head;
  24 + head = t;
  25 + head->val = a;
28 } 26 }
29 27
30 -void  
31 -list::goToHead () 28 +void list::goToHead()
32 { 29 {
33 - current = head; 30 + current = head;
34 } 31 }
35 32
36 -int  
37 -list::getCurrentData () 33 +int list::getCurrentData()
38 { 34 {
39 - return current->val; 35 + return current->val;
40 } 36 }
41 37
42 -void  
43 -list::advance () 38 +void list::advance()
44 { 39 {
45 - current = current->next; 40 + current = current->next;
46 } 41 }
47 42
48 -bool  
49 -list::moreData () 43 +bool list::moreData()
50 { 44 {
51 - if (current)  
52 - return true;  
53 - else  
54 - return false; 45 + if (current)
  46 + return true;
  47 + else
  48 + return false;
55 } 49 }
56 50
57 -list::list (const list & l) 51 +list::list(const list& l)
58 { 52 {
59 - current=NULL;  
60 - node *src, **dst;  
61 - head = NULL;  
62 - src = l.head;  
63 - dst = &head;  
64 - while (src)  
65 - {  
66 - *dst = new node;  
67 - (*dst)->val = src->val;  
68 - (*dst)->next = NULL;  
69 - if(src==l.current)  
70 - current=*dst;  
71 - src = src->next;  
72 - dst = &((*dst)->next); 53 + current = NULL;
  54 + node *src, **dst;
  55 + head = NULL;
  56 + src = l.head;
  57 + dst = &head;
  58 + while (src) {
  59 + *dst = new node;
  60 + (*dst)->val = src->val;
  61 + (*dst)->next = NULL;
  62 + if (src == l.current)
  63 + current = *dst;
  64 + src = src->next;
  65 + dst = &((*dst)->next);
73 } 66 }
74 } 67 }
75 68
76 -list & list::operator= (const list & l) 69 +list& list::operator=(const list& l)
77 { 70 {
78 - if (&l == this)  
79 - return *this;  
80 - current=NULL;  
81 - while (head)  
82 - {  
83 - node *t = head->next;  
84 - delete head;  
85 - head = t; 71 + if (&l == this)
  72 + return *this;
  73 + current = NULL;
  74 + while (head) {
  75 + node* t = head->next;
  76 + delete head;
  77 + head = t;
86 }; 78 };
87 - node *src, **dst;  
88 - head = NULL;  
89 - src = l.head;  
90 - dst = &head;  
91 - while (src)  
92 - {  
93 - *dst = new node;  
94 - (*dst)->val = src->val;  
95 - (*dst)->next = NULL;  
96 - if(src==l.current)  
97 - current=*dst;  
98 - src = src->next;  
99 - dst = &((*dst)->next); 79 + node *src, **dst;
  80 + head = NULL;
  81 + src = l.head;
  82 + dst = &head;
  83 + while (src) {
  84 + *dst = new node;
  85 + (*dst)->val = src->val;
  86 + (*dst)->next = NULL;
  87 + if (src == l.current)
  88 + current = *dst;
  89 + src = src->next;
  90 + dst = &((*dst)->next);
100 } 91 }
101 - return *this; 92 + return *this;
102 } 93 }
examples03/04-list/list.h
1 -class list  
2 -{  
3 -private:  
4 - struct node  
5 - {  
6 - node *next;  
7 - int val;  
8 - };  
9 - node * head;  
10 - node *current;  
11 -public:  
12 - list ();  
13 - list (const list& l);  
14 - list& operator=(const list& l);  
15 - ~list ();  
16 - void insert (int a);  
17 - void goToHead ();  
18 - int getCurrentData ();  
19 - void advance ();  
20 - bool moreData (); 1 +class list {
  2 + private:
  3 + struct node {
  4 + node* next;
  5 + int val;
  6 + };
  7 + node* head;
  8 + node* current;
  9 +
  10 + public:
  11 + list();
  12 + list(const list& l);
  13 + list& operator=(const list& l);
  14 + ~list();
  15 + void insert(int a);
  16 + void goToHead();
  17 + int getCurrentData();
  18 + void advance();
  19 + bool moreData();
21 }; 20 };
examples03/04-list/testlist.cpp
@@ -2,36 +2,31 @@ @@ -2,36 +2,31 @@
2 using namespace std; 2 using namespace std;
3 #include "list.h" 3 #include "list.h"
4 4
5 -  
6 -void  
7 -PrintList (list toPrint, ostream & Out) 5 +void PrintList(list toPrint, ostream& Out)
8 { 6 {
9 - int nextValue;  
10 - Out << "Printing list contents: " << endl;  
11 - toPrint.goToHead ();  
12 - if (!toPrint.moreData ())  
13 - {  
14 - Out << "List is empty" << endl;  
15 - return; 7 + int nextValue;
  8 + Out << "Printing list contents: " << endl;
  9 + toPrint.goToHead();
  10 + if (!toPrint.moreData()) {
  11 + Out << "List is empty" << endl;
  12 + return;
16 } 13 }
17 - while (toPrint.moreData ())  
18 - {  
19 - nextValue = toPrint.getCurrentData ();  
20 - Out << nextValue << " ";  
21 - toPrint.advance (); 14 + while (toPrint.moreData()) {
  15 + nextValue = toPrint.getCurrentData();
  16 + Out << nextValue << " ";
  17 + toPrint.advance();
22 } 18 }
23 - Out << endl; 19 + Out << endl;
24 } 20 }
25 21
26 -int  
27 -main () 22 +int main()
28 { 23 {
29 - list l;  
30 - l.insert (1);  
31 - l.insert (2);  
32 - l.insert (3);  
33 - PrintList (l, cout);  
34 - list l2;  
35 - l2=l;  
36 - PrintList (l2,cout); 24 + list l;
  25 + l.insert(1);
  26 + l.insert(2);
  27 + l.insert(3);
  28 + PrintList(l, cout);
  29 + list l2;
  30 + l2 = l;
  31 + PrintList(l2, cout);
37 } 32 }
examples03/05-funptr/funptr.cpp
1 #include <iostream> 1 #include <iostream>
2 2
3 -int  
4 -addition (int a, int b) 3 +int addition(int a, int b)
5 { 4 {
6 - return (a + b); 5 + return (a + b);
7 } 6 }
8 7
9 -int  
10 -subtraction (int a, int b) 8 +int subtraction(int a, int b)
11 { 9 {
12 - return (a - b); 10 + return (a - b);
13 } 11 }
14 12
15 -int (*minus) (int, int) = subtraction;  
16 -int  
17 -operation (int x, int y, int (*functocall) (int, int)) 13 +int (*minus)(int, int) = subtraction;
  14 +int operation(int x, int y, int (*functocall)(int, int))
18 { 15 {
19 - int g;  
20 - g = (*functocall) (x, y);  
21 - return (g); 16 + int g;
  17 + g = (*functocall)(x, y);
  18 + return (g);
22 } 19 }
23 20
24 -int  
25 -main () 21 +int main()
26 { 22 {
27 - int m, n;  
28 - m = operation (7, 5, addition);  
29 - n = operation (20, m, minus);  
30 - std::cout << n << std::endl;  
31 - return 0; 23 + int m, n;
  24 + m = operation(7, 5, addition);
  25 + n = operation(20, m, minus);
  26 + std::cout << n << std::endl;
  27 + return 0;
32 } 28 }
examples03/06-complex/complex.h
@@ -5,75 +5,71 @@ @@ -5,75 +5,71 @@
5 5
6 using namespace std; 6 using namespace std;
7 7
8 -class Complex  
9 -{  
10 -private:  
11 - double Real, Imag;  
12 -public:  
13 - Complex ():Real (0), Imag (0)  
14 - {  
15 - }; 8 +class Complex {
  9 + private:
  10 + double Real, Imag;
16 11
17 - Complex (double co)  
18 - {  
19 - Real = co;  
20 - Imag = 0;  
21 - }; 12 + public:
  13 + Complex() : Real(0), Imag(0){};
22 14
23 - Complex (double Real, double Imag)  
24 - {  
25 - this->Real = Real;  
26 - this->Imag = Imag;  
27 - }; 15 + Complex(double co)
  16 + {
  17 + Real = co;
  18 + Imag = 0;
  19 + };
28 20
29 - Complex & operator= (const Complex & s)  
30 - {  
31 - Real = s.Real;  
32 - Imag = s.Imag;  
33 - return *this;  
34 - }; 21 + Complex(double Real, double Imag)
  22 + {
  23 + this->Real = Real;
  24 + this->Imag = Imag;
  25 + };
35 26
36 - Complex operator- () const  
37 - {  
38 - return Complex(-Real,-Imag);  
39 - }; 27 + Complex& operator=(const Complex& s)
  28 + {
  29 + Real = s.Real;
  30 + Imag = s.Imag;
  31 + return *this;
  32 + };
40 33
41 - Complex & operator= (double co)  
42 - {  
43 - Real = co;  
44 - Imag = 0;  
45 - return *this;  
46 - }; 34 + Complex operator-() const
  35 + {
  36 + return Complex(-Real, -Imag);
  37 + };
47 38
48 - Complex operator+ (const Complex& co) const  
49 - {  
50 - Complex n;  
51 - n.Real = this->Real + co.Real;  
52 - n.Imag = this->Imag + co.Imag;  
53 - return n;  
54 - }; 39 + Complex& operator=(double co)
  40 + {
  41 + Real = co;
  42 + Imag = 0;
  43 + return *this;
  44 + };
55 45
  46 + Complex operator+(const Complex& co) const
  47 + {
  48 + Complex n;
  49 + n.Real = this->Real + co.Real;
  50 + n.Imag = this->Imag + co.Imag;
  51 + return n;
  52 + };
56 53
57 - Complex & operator-= (Complex co)  
58 - {  
59 - Real -= co.Real;  
60 - Imag -= co.Imag;  
61 - return *this;  
62 - }; 54 + Complex& operator-=(Complex co)
  55 + {
  56 + Real -= co.Real;
  57 + Imag -= co.Imag;
  58 + return *this;
  59 + };
63 60
64 - friend Complex operator- (Complex, Complex);  
65 - friend ostream & operator << (ostream & s, const Complex & c)  
66 - {  
67 - s << "(" << c.Real << "," << c.Imag << ")";  
68 - return s;  
69 - }; 61 + friend Complex operator-(Complex, Complex);
  62 + friend ostream& operator<<(ostream& s, const Complex& c)
  63 + {
  64 + s << "(" << c.Real << "," << c.Imag << ")";
  65 + return s;
  66 + };
70 }; 67 };
71 68
72 -inline Complex  
73 -operator - (Complex s1, Complex s2) 69 +inline Complex operator-(Complex s1, Complex s2)
74 { 70 {
75 - Complex n (s1);  
76 - return n -= s2; 71 + Complex n(s1);
  72 + return n -= s2;
77 } 73 }
78 74
79 #endif /* __Complex_H__ */ 75 #endif /* __Complex_H__ */
examples03/06-complex/testcplx.cpp
@@ -2,18 +2,17 @@ @@ -2,18 +2,17 @@
2 using namespace std; 2 using namespace std;
3 #include "complex.h" 3 #include "complex.h"
4 4
5 -  
6 int main() 5 int main()
7 { 6 {
8 - Complex a(0.0,15),b(0,13),c;  
9 - c = 10;  
10 - cout << c <<endl;  
11 - c = -a;  
12 - cout << c <<endl;  
13 - c = a + b;  
14 - c = c - Complex(10);  
15 - cout << c <<endl;  
16 - c = 10 - a;  
17 - (c -=b) -=10;  
18 - cout << c <<endl; 7 + Complex a(0.0, 15), b(0, 13), c;
  8 + c = 10;
  9 + cout << c << endl;
  10 + c = -a;
  11 + cout << c << endl;
  12 + c = a + b;
  13 + c = c - Complex(10);
  14 + cout << c << endl;
  15 + c = 10 - a;
  16 + (c -= b) -= 10;
  17 + cout << c << endl;
19 } 18 }
examples03/07-vector/testvector.cpp
@@ -2,21 +2,18 @@ @@ -2,21 +2,18 @@
2 using namespace std; 2 using namespace std;
3 #include "vector.h" 3 #include "vector.h"
4 4
5 -void  
6 -printvector (vector v) 5 +void printvector(vector v)
7 { 6 {
8 - cout << v << endl; 7 + cout << v << endl;
9 } 8 }
10 9
11 -int  
12 -main () 10 +int main()
13 { 11 {
14 - vector a (10);  
15 - cout << a << endl;  
16 - a[0] = 15;  
17 - a[5] = 32;  
18 - vector b(10);  
19 - b=a;  
20 - printvector (b); 12 + vector a(10);
  13 + cout << a << endl;
  14 + a[0] = 15;
  15 + a[5] = 32;
  16 + vector b(10);
  17 + b = a;
  18 + printvector(b);
21 } 19 }
22 -  
examples03/07-vector/vector.h
1 #ifndef __VECTOR_H__ 1 #ifndef __VECTOR_H__
2 #define __VECTOR_H__ 2 #define __VECTOR_H__
3 3
4 -class vector  
5 -{  
6 - int *dane;  
7 - unsigned int size;  
8 -public:  
9 - vector (int s)  
10 - {  
11 - dane = new int[s];  
12 - size = s;  
13 - for (unsigned i = 0; i < size; i++)  
14 - dane[i] = 0;  
15 - }  
16 - ~vector ()  
17 - {  
18 - delete [] dane;  
19 - }  
20 - int& operator[] (unsigned int pos)  
21 - {  
22 - if (pos >= size)  
23 - abort ();  
24 - return dane[pos];  
25 - }  
26 - int operator[] (unsigned int pos) const  
27 - {  
28 - if (pos >= size)  
29 - abort ();  
30 - return dane[pos];  
31 - } 4 +class vector {
  5 + int* dane;
  6 + unsigned int size;
32 7
33 - vector (const vector & s)  
34 - {  
35 - dane = new int[s.size];  
36 - size = s.size;  
37 - for (unsigned i = 0; i < size; i++)  
38 - dane[i] = s.dane[i];  
39 - }  
40 - vector & operator= (const vector & s)  
41 - {  
42 - if (this == &s)  
43 - return *this;  
44 - delete [] dane;  
45 - dane = new int[s.size];  
46 - size = s.size;  
47 - for (unsigned i = 0; i < size; i++)  
48 - dane[i] = s.dane[i];  
49 - return *this;  
50 - }  
51 - friend ostream & operator<< (ostream & o, const vector & v)  
52 - {  
53 - o << '[';  
54 - for (unsigned i = 0; i < v.size;i++)  
55 - {  
56 - o << v[i];  
57 - if (i != v.size - 1)  
58 - o << ',';  
59 - };  
60 - o << ']';  
61 - return o;  
62 - } 8 + public:
  9 + vector(int s)
  10 + {
  11 + dane = new int[s];
  12 + size = s;
  13 + for (unsigned i = 0; i < size; i++)
  14 + dane[i] = 0;
  15 + }
  16 + ~vector()
  17 + {
  18 + delete[] dane;
  19 + }
  20 + int& operator[](unsigned int pos)
  21 + {
  22 + if (pos >= size)
  23 + abort();
  24 + return dane[pos];
  25 + }
  26 + int operator[](unsigned int pos) const
  27 + {
  28 + if (pos >= size)
  29 + abort();
  30 + return dane[pos];
  31 + }
  32 +
  33 + vector(const vector& s)
  34 + {
  35 + dane = new int[s.size];
  36 + size = s.size;
  37 + for (unsigned i = 0; i < size; i++)
  38 + dane[i] = s.dane[i];
  39 + }
  40 + vector& operator=(const vector& s)
  41 + {
  42 + if (this == &s)
  43 + return *this;
  44 + delete[] dane;
  45 + dane = new int[s.size];
  46 + size = s.size;
  47 + for (unsigned i = 0; i < size; i++)
  48 + dane[i] = s.dane[i];
  49 + return *this;
  50 + }
  51 + friend ostream& operator<<(ostream& o, const vector& v)
  52 + {
  53 + o << '[';
  54 + for (unsigned i = 0; i < v.size; i++) {
  55 + o << v[i];
  56 + if (i != v.size - 1)
  57 + o << ',';
  58 + };
  59 + o << ']';
  60 + return o;
  61 + }
63 }; 62 };
64 #endif /* __VECTOR_H__ */ 63 #endif /* __VECTOR_H__ */
examples04/00-string/mystring.h
1 #ifndef __MYSTRING_H__ 1 #ifndef __MYSTRING_H__
2 #define __MYSTRING_H__ 2 #define __MYSTRING_H__
3 3
4 -class mystring  
5 -{  
6 - char *dane;  
7 -public:  
8 - mystring ():dane (0)  
9 - {  
10 - };  
11 - ~mystring ()  
12 - {  
13 - delete[]dane;  
14 - };  
15 - mystring (const char *s)  
16 - {  
17 - if (s)  
18 - {  
19 - dane = new char[strlen (s) + 1];  
20 - strcpy (dane, s);  
21 - }  
22 - else  
23 - dane = 0;  
24 - };  
25 -  
26 - unsigned int length () const  
27 - {  
28 - if (!dane)  
29 - return 0;  
30 - else  
31 - return strlen (dane);  
32 - }  
33 -  
34 - mystring (const mystring & src)  
35 - {  
36 - if (src.dane)  
37 - {  
38 - dane = new char[strlen (src.dane) + 1];  
39 - strcpy (dane, src.dane);  
40 - }  
41 - else  
42 - dane = 0;  
43 - };  
44 - mystring & operator= (const mystring & src)  
45 - {  
46 - if (this != &src)  
47 - {  
48 - delete[]dane;  
49 - if (src.dane)  
50 - {  
51 - dane = new char[strlen (src.dane) + 1];  
52 - strcpy (dane, src.dane);  
53 - }  
54 - else  
55 - dane = 0;  
56 - };  
57 - return *this;  
58 - } 4 +class mystring {
  5 + char* dane;
59 6
60 - mystring & operator+= (const mystring & src)  
61 - {  
62 - char *newstr = new char[length () + src.length () + 1];  
63 - if (length ())  
64 - strcpy (newstr, dane);  
65 - else  
66 - newstr[0] = '\0';  
67 - if (src.length ())  
68 - strcat (newstr, src.dane);  
69 - delete[]dane;  
70 - dane = newstr;  
71 - return *this;  
72 - } 7 + public:
  8 + mystring() : dane(0){};
  9 + ~mystring()
  10 + {
  11 + delete[] dane;
  12 + };
  13 + mystring(const char* s)
  14 + {
  15 + if (s) {
  16 + dane = new char[strlen(s) + 1];
  17 + strcpy(dane, s);
  18 + }
  19 + else
  20 + dane = 0;
  21 + };
73 22
  23 + unsigned int length() const
  24 + {
  25 + if (!dane)
  26 + return 0;
  27 + else
  28 + return strlen(dane);
  29 + }
74 30
75 - char operator[] (unsigned int i) const  
76 - {  
77 - if (!dane)  
78 - abort ();  
79 - if (i >= strlen (dane))  
80 - abort ();  
81 - return dane[i];  
82 - }  
83 - char &operator[] (unsigned int i)  
84 - {  
85 - if (!dane)  
86 - abort ();  
87 - if (i >= strlen (dane))  
88 - abort ();  
89 - return dane[i];  
90 - } 31 + mystring(const mystring& src)
  32 + {
  33 + if (src.dane) {
  34 + dane = new char[strlen(src.dane) + 1];
  35 + strcpy(dane, src.dane);
  36 + }
  37 + else
  38 + dane = 0;
  39 + };
  40 + mystring& operator=(const mystring& src)
  41 + {
  42 + if (this != &src) {
  43 + delete[] dane;
  44 + if (src.dane) {
  45 + dane = new char[strlen(src.dane) + 1];
  46 + strcpy(dane, src.dane);
  47 + }
  48 + else
  49 + dane = 0;
  50 + };
  51 + return *this;
  52 + }
91 53
92 - friend ostream & operator<< (ostream & o, const mystring & s)  
93 - {  
94 - if (s.dane)  
95 - o << s.dane;  
96 - return o;  
97 - } 54 + mystring& operator+=(const mystring& src)
  55 + {
  56 + char* newstr = new char[length() + src.length() + 1];
  57 + if (length())
  58 + strcpy(newstr, dane);
  59 + else
  60 + newstr[0] = '\0';
  61 + if (src.length())
  62 + strcat(newstr, src.dane);
  63 + delete[] dane;
  64 + dane = newstr;
  65 + return *this;
  66 + }
98 67
  68 + char operator[](unsigned int i) const
  69 + {
  70 + if (!dane)
  71 + abort();
  72 + if (i >= strlen(dane))
  73 + abort();
  74 + return dane[i];
  75 + }
  76 + char& operator[](unsigned int i)
  77 + {
  78 + if (!dane)
  79 + abort();
  80 + if (i >= strlen(dane))
  81 + abort();
  82 + return dane[i];
  83 + }
99 84
  85 + friend ostream& operator<<(ostream& o, const mystring& s)
  86 + {
  87 + if (s.dane)
  88 + o << s.dane;
  89 + return o;
  90 + }
100 }; 91 };
101 92
102 - inline mystring operator+ (const mystring & s1, const mystring & s2)  
103 - {  
104 - mystring s (s1); 93 +inline mystring operator+(const mystring& s1, const mystring& s2)
  94 +{
  95 + mystring s(s1);
105 return s += s2; 96 return s += s2;
106 - }  
107 - 97 +}
108 98
109 #endif /* __MYSTRING_H__ */ 99 #endif /* __MYSTRING_H__ */
examples04/00-string/teststring.cpp
1 -#include <string.h>  
2 #include <iostream> 1 #include <iostream>
  2 +#include <string.h>
3 using namespace std; 3 using namespace std;
4 #include "mystring.h" 4 #include "mystring.h"
5 5
6 int main() 6 int main()
7 { 7 {
8 - mystring a("ala");  
9 - mystring b=a;  
10 - b[0]='c';  
11 - cout << b << endl;  
12 - mystring c=a+b;  
13 - c+="ola";  
14 - cout << c+"ula"<<endl; 8 + mystring a("ala");
  9 + mystring b = a;
  10 + b[0] = 'c';
  11 + cout << b << endl;
  12 + mystring c = a + b;
  13 + c += "ola";
  14 + cout << c + "ula" << endl;
15 } 15 }
examples04/01-stringopchar/mystring.h
1 #ifndef __MYSTRING_H__ 1 #ifndef __MYSTRING_H__
2 #define __MYSTRING_H__ 2 #define __MYSTRING_H__
3 3
4 -class mystring  
5 -{  
6 - char *dane;  
7 -public:  
8 - mystring ():dane (0)  
9 - {  
10 - };  
11 - ~mystring ()  
12 - {  
13 - delete[]dane;  
14 - };  
15 - mystring (const char *s)  
16 - {  
17 - if (s)  
18 - {  
19 - dane = new char[strlen (s) + 1];  
20 - strcpy (dane, s);  
21 - }  
22 - else  
23 - dane = 0;  
24 - }; 4 +class mystring {
  5 + char* dane;
25 6
26 - unsigned int length () const  
27 - {  
28 - if (!dane)  
29 - return 0;  
30 - else  
31 - return strlen (dane);  
32 - } 7 + public:
  8 + mystring() : dane(0){};
  9 + ~mystring()
  10 + {
  11 + delete[] dane;
  12 + };
  13 + mystring(const char* s)
  14 + {
  15 + if (s) {
  16 + dane = new char[strlen(s) + 1];
  17 + strcpy(dane, s);
  18 + }
  19 + else
  20 + dane = 0;
  21 + };
33 22
34 - mystring (const mystring & src)  
35 - {  
36 - if (src.dane)  
37 - {  
38 - dane = new char[strlen (src.dane) + 1];  
39 - strcpy (dane, src.dane);  
40 - }  
41 - else  
42 - dane = 0;  
43 - };  
44 - mystring & operator= (const mystring & src)  
45 - {  
46 - if (this != &src)  
47 - {  
48 - delete[]dane;  
49 - if (src.dane)  
50 - {  
51 - dane = new char[strlen (src.dane) + 1];  
52 - strcpy (dane, src.dane);  
53 - }  
54 - else  
55 - dane = 0;  
56 - };  
57 - return *this;  
58 - } 23 + unsigned int length() const
  24 + {
  25 + if (!dane)
  26 + return 0;
  27 + else
  28 + return strlen(dane);
  29 + }
59 30
60 - mystring & operator+= (const mystring & src)  
61 - {  
62 - char *newstr = new char[length () + src.length () + 1];  
63 - if (length ())  
64 - strcpy (newstr, dane);  
65 - else  
66 - newstr[0] = '\0';  
67 - if (src.length ())  
68 - strcat (newstr, src.dane);  
69 - delete[]dane;  
70 - dane = newstr;  
71 - return *this;  
72 - } 31 + mystring(const mystring& src)
  32 + {
  33 + if (src.dane) {
  34 + dane = new char[strlen(src.dane) + 1];
  35 + strcpy(dane, src.dane);
  36 + }
  37 + else
  38 + dane = 0;
  39 + };
  40 + mystring& operator=(const mystring& src)
  41 + {
  42 + if (this != &src) {
  43 + delete[] dane;
  44 + if (src.dane) {
  45 + dane = new char[strlen(src.dane) + 1];
  46 + strcpy(dane, src.dane);
  47 + }
  48 + else
  49 + dane = 0;
  50 + };
  51 + return *this;
  52 + }
73 53
74 - operator char*()  
75 - {  
76 - if(!dane) 54 + mystring& operator+=(const mystring& src)
77 { 55 {
78 - dane=new char[1];  
79 - dane[0]='\0'; 56 + char* newstr = new char[length() + src.length() + 1];
  57 + if (length())
  58 + strcpy(newstr, dane);
  59 + else
  60 + newstr[0] = '\0';
  61 + if (src.length())
  62 + strcat(newstr, src.dane);
  63 + delete[] dane;
  64 + dane = newstr;
  65 + return *this;
80 } 66 }
81 - return dane;  
82 - };  
83 67
84 - friend ostream & operator<< (ostream & o, const mystring & s)  
85 - {  
86 - if (s.dane)  
87 - o << s.dane;  
88 - return o;  
89 - } 68 + operator char*()
  69 + {
  70 + if (!dane) {
  71 + dane = new char[1];
  72 + dane[0] = '\0';
  73 + }
  74 + return dane;
  75 + };
  76 +
  77 + friend ostream& operator<<(ostream& o, const mystring& s)
  78 + {
  79 + if (s.dane)
  80 + o << s.dane;
  81 + return o;
  82 + }
90 }; 83 };
91 84
92 - inline mystring operator+ (const mystring & s1, const mystring & s2)  
93 - {  
94 - mystring s (s1); 85 +inline mystring operator+(const mystring& s1, const mystring& s2)
  86 +{
  87 + mystring s(s1);
95 return s += s2; 88 return s += s2;
96 - }  
97 - 89 +}
98 90
99 #endif /* __MYSTRING_H__ */ 91 #endif /* __MYSTRING_H__ */
examples04/01-stringopchar/teststring.cpp
1 -#include <string.h>  
2 #include <iostream> 1 #include <iostream>
  2 +#include <string.h>
3 using namespace std; 3 using namespace std;
4 #include "mystring.h" 4 #include "mystring.h"
5 5
6 int main() 6 int main()
7 { 7 {
8 - mystring a("ala");  
9 - mystring b=a;  
10 - b[0]='c';  
11 - cout << b << endl;  
12 - mystring c=a+b;  
13 - c+="ola";  
14 - cout << c+"ula"<<endl;  
15 - mystring d;  
16 - cout << strlen(d)<<endl; 8 + mystring a("ala");
  9 + mystring b = a;
  10 + b[0] = 'c';
  11 + cout << b << endl;
  12 + mystring c = a + b;
  13 + c += "ola";
  14 + cout << c + "ula" << endl;
  15 + mystring d;
  16 + cout << strlen(d) << endl;
17 } 17 }
examples04/02-exception/exception1.cpp
1 #include <iostream> 1 #include <iostream>
2 using namespace std; 2 using namespace std;
3 3
4 -class myexception{}; 4 +class myexception {};
5 5
6 -class tester  
7 -{  
8 - string name;  
9 -public:  
10 - tester(const string& n): name(n)  
11 - {  
12 - cout<<name<<"()"<<endl;  
13 - };  
14 - ~tester()  
15 - {  
16 - cout<<"~"<<name<<"()"<<endl;  
17 - }; 6 +class tester {
  7 + string name;
  8 +
  9 + public:
  10 + tester(const string& n) : name(n)
  11 + {
  12 + cout << name << "()" << endl;
  13 + };
  14 + ~tester()
  15 + {
  16 + cout << "~" << name << "()" << endl;
  17 + };
18 }; 18 };
19 19
20 void f1() 20 void f1()
21 { 21 {
22 - tester f1("f1");  
23 - throw myexception();  
24 - cout << "Exiting f1"<<endl; 22 + tester f1("f1");
  23 + throw myexception();
  24 + cout << "Exiting f1" << endl;
25 } 25 }
26 26
27 void f2() 27 void f2()
28 { 28 {
29 - tester f2("f2");  
30 - f1();  
31 - cout << "Exiting f2"<<endl; 29 + tester f2("f2");
  30 + f1();
  31 + cout << "Exiting f2" << endl;
32 } 32 }
33 - 33 +
34 int main() 34 int main()
35 { 35 {
36 - tester main("main");  
37 - try {  
38 - f2();  
39 - } catch(myexception&)  
40 - {  
41 - cout << "myxception caught"<<endl;  
42 - }  
43 - return 0; 36 + tester main("main");
  37 + try {
  38 + f2();
  39 + }
  40 + catch (myexception&) {
  41 + cout << "myxception caught" << endl;
  42 + }
  43 + return 0;
44 } 44 }
examples04/02-exception/exception2.cpp
1 #include <iostream> 1 #include <iostream>
2 using namespace std; 2 using namespace std;
3 3
4 -class myexception{}; 4 +class myexception {};
5 5
6 -class tester  
7 -{  
8 - string name;  
9 -public:  
10 - tester(const string& n): name(n)  
11 - {  
12 - cout<<name<<"()"<<endl;  
13 - };  
14 - ~tester()  
15 - {  
16 - cout<<"~"<<name<<"()"<<endl;  
17 - }; 6 +class tester {
  7 + string name;
  8 +
  9 + public:
  10 + tester(const string& n) : name(n)
  11 + {
  12 + cout << name << "()" << endl;
  13 + };
  14 + ~tester()
  15 + {
  16 + cout << "~" << name << "()" << endl;
  17 + };
18 }; 18 };
19 19
20 void f1() 20 void f1()
21 { 21 {
22 - tester f1("f1");  
23 - throw myexception();  
24 - cout << "Exiting f1"<<endl; 22 + tester f1("f1");
  23 + throw myexception();
  24 + cout << "Exiting f1" << endl;
25 } 25 }
26 26
27 void f2() 27 void f2()
28 { 28 {
29 - tester f2("f2");  
30 - f1();  
31 - cout << "Exiting f2"<<endl; 29 + tester f2("f2");
  30 + f1();
  31 + cout << "Exiting f2" << endl;
32 } 32 }
33 - 33 +
34 int main() 34 int main()
35 { 35 {
36 - tester main("main");  
37 - f2();  
38 - return 0; 36 + tester main("main");
  37 + f2();
  38 + return 0;
39 } 39 }
examples04/02-exception/exception3.cpp
@@ -2,25 +2,24 @@ @@ -2,25 +2,24 @@
2 #include <memory> 2 #include <memory>
3 using namespace std; 3 using namespace std;
4 4
5 -  
6 void f1() 5 void f1()
7 { 6 {
8 - int *a = new int[500000000];  
9 - delete [] a; 7 + int* a = new int[500000000];
  8 + delete[] a;
10 } 9 }
11 10
12 void f2() 11 void f2()
13 { 12 {
14 - f1(); 13 + f1();
15 } 14 }
16 - 15 +
17 int main() 16 int main()
18 { 17 {
19 -try {  
20 - f2();  
21 -}  
22 -catch(bad_alloc&) {  
23 - cout << "bad_alloc"<<endl;  
24 -}  
25 - return 0; 18 + try {
  19 + f2();
  20 + }
  21 + catch (bad_alloc&) {
  22 + cout << "bad_alloc" << endl;
  23 + }
  24 + return 0;
26 } 25 }
examples04/03-excstring/mystring.h
1 #ifndef __MYSTRING_H__ 1 #ifndef __MYSTRING_H__
2 #define __MYSTRING_H__ 2 #define __MYSTRING_H__
3 3
4 -class mystring  
5 -{  
6 - char *dane;  
7 -public:  
8 - class index_out_of_range{};  
9 - mystring ():dane (0)  
10 - {  
11 - };  
12 - ~mystring ()  
13 - {  
14 - delete[]dane;  
15 - };  
16 - mystring (const char *s)  
17 - {  
18 - if (s)  
19 - {  
20 - dane = new char[strlen (s) + 1];  
21 - strcpy (dane, s);  
22 - }  
23 - else  
24 - dane = 0;  
25 - };  
26 -  
27 - unsigned int length () const  
28 - {  
29 - if (!dane)  
30 - return 0;  
31 - else  
32 - return strlen (dane);  
33 - }  
34 -  
35 - mystring (const mystring & src)  
36 - {  
37 - if (src.dane)  
38 - {  
39 - dane = new char[strlen (src.dane) + 1];  
40 - strcpy (dane, src.dane);  
41 - }  
42 - else  
43 - dane = 0;  
44 - };  
45 - mystring & operator= (const mystring & src)  
46 - {  
47 - if (this != &src)  
48 - {  
49 - char* nowedane;  
50 - if (src.dane)  
51 - {  
52 - nowedane = new char[strlen (src.dane) + 1];  
53 - strcpy (nowedane, src.dane);  
54 - }  
55 - else  
56 - nowedane = 0;  
57 - delete [] dane;  
58 - dane=nowedane;  
59 - };  
60 - return *this;  
61 - } 4 +class mystring {
  5 + char* dane;
62 6
63 - mystring & operator+= (const mystring & src)  
64 - {  
65 - char *newstr = new char[length () + src.length () + 1];  
66 - if (length ())  
67 - strcpy (newstr, dane);  
68 - else  
69 - newstr[0] = '\0';  
70 - if (src.length ())  
71 - strcat (newstr, src.dane);  
72 - delete[]dane;  
73 - dane = newstr;  
74 - return *this;  
75 - } 7 + public:
  8 + class index_out_of_range {};
  9 + mystring() : dane(0){};
  10 + ~mystring()
  11 + {
  12 + delete[] dane;
  13 + };
  14 + mystring(const char* s)
  15 + {
  16 + if (s) {
  17 + dane = new char[strlen(s) + 1];
  18 + strcpy(dane, s);
  19 + }
  20 + else
  21 + dane = 0;
  22 + };
76 23
  24 + unsigned int length() const
  25 + {
  26 + if (!dane)
  27 + return 0;
  28 + else
  29 + return strlen(dane);
  30 + }
77 31
78 - char operator[] (unsigned int i) const  
79 - {  
80 - if (!dane)  
81 - throw index_out_of_range();  
82 - if (i >= strlen (dane))  
83 - throw index_out_of_range();  
84 - return dane[i];  
85 - }  
86 - char &operator[] (unsigned int i)  
87 - {  
88 - if (!dane)  
89 - throw index_out_of_range();  
90 - if (i >= strlen (dane))  
91 - throw index_out_of_range();  
92 - return dane[i];  
93 - } 32 + mystring(const mystring& src)
  33 + {
  34 + if (src.dane) {
  35 + dane = new char[strlen(src.dane) + 1];
  36 + strcpy(dane, src.dane);
  37 + }
  38 + else
  39 + dane = 0;
  40 + };
  41 + mystring& operator=(const mystring& src)
  42 + {
  43 + if (this != &src) {
  44 + char* nowedane;
  45 + if (src.dane) {
  46 + nowedane = new char[strlen(src.dane) + 1];
  47 + strcpy(nowedane, src.dane);
  48 + }
  49 + else
  50 + nowedane = 0;
  51 + delete[] dane;
  52 + dane = nowedane;
  53 + };
  54 + return *this;
  55 + }
94 56
95 - friend ostream & operator<< (ostream & o, const mystring & s)  
96 - {  
97 - if (s.dane)  
98 - o << s.dane;  
99 - return o;  
100 - } 57 + mystring& operator+=(const mystring& src)
  58 + {
  59 + char* newstr = new char[length() + src.length() + 1];
  60 + if (length())
  61 + strcpy(newstr, dane);
  62 + else
  63 + newstr[0] = '\0';
  64 + if (src.length())
  65 + strcat(newstr, src.dane);
  66 + delete[] dane;
  67 + dane = newstr;
  68 + return *this;
  69 + }
101 70
  71 + char operator[](unsigned int i) const
  72 + {
  73 + if (!dane)
  74 + throw index_out_of_range();
  75 + if (i >= strlen(dane))
  76 + throw index_out_of_range();
  77 + return dane[i];
  78 + }
  79 + char& operator[](unsigned int i)
  80 + {
  81 + if (!dane)
  82 + throw index_out_of_range();
  83 + if (i >= strlen(dane))
  84 + throw index_out_of_range();
  85 + return dane[i];
  86 + }
102 87
  88 + friend ostream& operator<<(ostream& o, const mystring& s)
  89 + {
  90 + if (s.dane)
  91 + o << s.dane;
  92 + return o;
  93 + }
103 }; 94 };
104 95
105 - inline mystring operator+ (const mystring & s1, const mystring & s2)  
106 - {  
107 - mystring s (s1); 96 +inline mystring operator+(const mystring& s1, const mystring& s2)
  97 +{
  98 + mystring s(s1);
108 return s += s2; 99 return s += s2;
109 - }  
110 - 100 +}
111 101
112 #endif /* __MYSTRING_H__ */ 102 #endif /* __MYSTRING_H__ */
examples04/03-excstring/teststring.cpp
1 -#include <string.h>  
2 #include <iostream> 1 #include <iostream>
  2 +#include <string.h>
3 using namespace std; 3 using namespace std;
4 #include "mystring.h" 4 #include "mystring.h"
5 5
6 int main() 6 int main()
7 { 7 {
8 - mystring a("ala");  
9 - mystring b=a;  
10 - b[0]='c';  
11 - cout << b << endl;  
12 - mystring c=a+b;  
13 - c+="ola";  
14 - cout << c+"ula"<<endl;  
15 - try {  
16 - c[40]='a';  
17 - } catch (mystring::index_out_of_range&)  
18 - {  
19 - cout << "Index out of range" << endl;  
20 - }; 8 + mystring a("ala");
  9 + mystring b = a;
  10 + b[0] = 'c';
  11 + cout << b << endl;
  12 + mystring c = a + b;
  13 + c += "ola";
  14 + cout << c + "ula" << endl;
  15 + try {
  16 + c[40] = 'a';
  17 + }
  18 + catch (mystring::index_out_of_range&) {
  19 + cout << "Index out of range" << endl;
  20 + };
21 } 21 }
examples04/04-rcstring/rcstring.cpp
@@ -2,24 +2,24 @@ @@ -2,24 +2,24 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 -rcstring a,b,c;  
6 -a="10";  
7 -b="ala ma kota";  
8 -cout << a << " " << b << endl; // 10 ala ma ma kota  
9 -c=a+b;  
10 -cout << c<<endl; // 10ala ma kota  
11 -c=a+rcstring(" ")+b;  
12 -cout << c << endl; //10 ala ma kota  
13 -rcstring d("a");  
14 -cout << d <<endl; //a  
15 -d+="ula";  
16 -cout << d << endl; //aula  
17 -d+="15";  
18 -cout << d << endl; //aula15  
19 -cout << d[3]<<endl; //a  
20 -d[3]='b';  
21 -cout << d << endl; //aulb15  
22 -d[2]=d[1]=d[0];  
23 -cout << d << endl; //aaab15  
24 -return 0; 5 + rcstring a, b, c;
  6 + a = "10";
  7 + b = "ala ma kota";
  8 + cout << a << " " << b << endl; // 10 ala ma ma kota
  9 + c = a + b;
  10 + cout << c << endl; // 10ala ma kota
  11 + c = a + rcstring(" ") + b;
  12 + cout << c << endl; // 10 ala ma kota
  13 + rcstring d("a");
  14 + cout << d << endl; // a
  15 + d += "ula";
  16 + cout << d << endl; // aula
  17 + d += "15";
  18 + cout << d << endl; // aula15
  19 + cout << d[3] << endl; // a
  20 + d[3] = 'b';
  21 + cout << d << endl; // aulb15
  22 + d[2] = d[1] = d[0];
  23 + cout << d << endl; // aaab15
  24 + return 0;
25 } 25 }
examples04/04-rcstring/rcstring.h
1 #ifndef __RCSTRING_H__ 1 #ifndef __RCSTRING_H__
2 #define __RCSTRING_H__ 2 #define __RCSTRING_H__
3 -#include <string.h>  
4 -#include <stdio.h>  
5 -#include <malloc.h>  
6 #include <iostream> 3 #include <iostream>
  4 +#include <malloc.h>
  5 +#include <stdio.h>
  6 +#include <string.h>
7 using namespace std; 7 using namespace std;
8 8
9 -class rcstring{  
10 - struct rctext;  
11 - rctext* data;  
12 -public:  
13 -class Range{};  
14 -rcstring();  
15 -rcstring(const char*);  
16 -rcstring(const rcstring&);  
17 -~rcstring();  
18 -rcstring& operator=(const rcstring&);  
19 -rcstring& operator+=(const rcstring &);  
20 -rcstring operator+(const rcstring &) const;  
21 -friend ostream& operator<<(ostream&, const rcstring&);  
22 -void check (unsigned int i) const;  
23 -char read(unsigned int i) const;  
24 -void write(unsigned int i, char c);  
25 -  
26 -char operator[](unsigned int i) const;  
27 -char& operator[](unsigned int i); 9 +class rcstring {
  10 + struct rctext;
  11 + rctext* data;
  12 +
  13 + public:
  14 + class Range {};
  15 + rcstring();
  16 + rcstring(const char*);
  17 + rcstring(const rcstring&);
  18 + ~rcstring();
  19 + rcstring& operator=(const rcstring&);
  20 + rcstring& operator+=(const rcstring&);
  21 + rcstring operator+(const rcstring&) const;
  22 + friend ostream& operator<<(ostream&, const rcstring&);
  23 + void check(unsigned int i) const;
  24 + char read(unsigned int i) const;
  25 + void write(unsigned int i, char c);
  26 +
  27 + char operator[](unsigned int i) const;
  28 + char& operator[](unsigned int i);
28 }; 29 };
29 30
30 -struct rcstring::rctext  
31 -{  
32 - char* s;  
33 - unsigned int size;  
34 - unsigned int n;  
35 -  
36 - rctext(unsigned int nsize, const char* p)  
37 - {  
38 - n=1;  
39 - size=nsize;  
40 - s=new char[size+1];  
41 - strncpy(s,p,size);  
42 - s[size]='\0';  
43 - };  
44 - ~rctext()  
45 - {  
46 - delete [] s;  
47 - };  
48 - rctext* detach()  
49 - {  
50 - if(n==1)  
51 - return this;  
52 - rctext* t=new rctext(size, s);  
53 - n--;  
54 - return t;  
55 - };  
56 -private:  
57 - rctext(const rctext&);  
58 - rctext& operator=(const rctext&); 31 +struct rcstring::rctext {
  32 + char* s;
  33 + unsigned int size;
  34 + unsigned int n;
  35 +
  36 + rctext(unsigned int nsize, const char* p)
  37 + {
  38 + n = 1;
  39 + size = nsize;
  40 + s = new char[size + 1];
  41 + strncpy(s, p, size);
  42 + s[size] = '\0';
  43 + };
  44 + ~rctext()
  45 + {
  46 + delete[] s;
  47 + };
  48 + rctext* detach()
  49 + {
  50 + if (n == 1)
  51 + return this;
  52 + rctext* t = new rctext(size, s);
  53 + n--;
  54 + return t;
  55 + };
  56 +
  57 + private:
  58 + rctext(const rctext&);
  59 + rctext& operator=(const rctext&);
59 }; 60 };
60 61
61 inline rcstring::rcstring() 62 inline rcstring::rcstring()
62 - {  
63 - data = new rctext(0,"");  
64 - } 63 +{
  64 + data = new rctext(0, "");
  65 +}
65 66
66 inline rcstring::rcstring(const rcstring& x) 67 inline rcstring::rcstring(const rcstring& x)
67 - { 68 +{
68 x.data->n++; 69 x.data->n++;
69 - data=x.data;  
70 - } 70 + data = x.data;
  71 +}
71 inline rcstring::~rcstring() 72 inline rcstring::~rcstring()
72 { 73 {
73 - if(--data->n==0)  
74 - delete data; 74 + if (--data->n == 0)
  75 + delete data;
75 } 76 }
76 77
77 -rcstring& rcstring::operator=(const rcstring & x) 78 +rcstring& rcstring::operator=(const rcstring& x)
78 { 79 {
79 - x.data->n++;  
80 - if(--data->n == 0)  
81 - delete data;  
82 - data=x.data;  
83 - return *this; 80 + x.data->n++;
  81 + if (--data->n == 0)
  82 + delete data;
  83 + data = x.data;
  84 + return *this;
84 } 85 }
85 86
86 rcstring::rcstring(const char* s) 87 rcstring::rcstring(const char* s)
87 { 88 {
88 - data=new rctext(strlen(s),s); 89 + data = new rctext(strlen(s), s);
89 } 90 }
90 91
91 -ostream& operator << (ostream& o, const rcstring& s) 92 +ostream& operator<<(ostream& o, const rcstring& s)
92 { 93 {
93 - return o<<s.data->s; 94 + return o << s.data->s;
94 } 95 }
95 96
96 -rcstring& rcstring::operator+=(const rcstring & s) 97 +rcstring& rcstring::operator+=(const rcstring& s)
97 { 98 {
98 -unsigned int newsize=data->size+s.data->size;  
99 -rctext *newdata=new rctext(newsize,data->s);  
100 -strcat(newdata->s,s.data->s);  
101 -if(--data->n==0)  
102 - delete data;  
103 -data = newdata;  
104 -return *this; 99 + unsigned int newsize = data->size + s.data->size;
  100 + rctext* newdata = new rctext(newsize, data->s);
  101 + strcat(newdata->s, s.data->s);
  102 + if (--data->n == 0)
  103 + delete data;
  104 + data = newdata;
  105 + return *this;
105 } 106 }
106 107
107 -rcstring rcstring::operator+(const rcstring & s) const 108 +rcstring rcstring::operator+(const rcstring& s) const
108 { 109 {
109 - return rcstring(*this)+=s; 110 + return rcstring(*this) += s;
110 } 111 }
111 112
112 -inline void rcstring::check (unsigned int i) const 113 +inline void rcstring::check(unsigned int i) const
113 { 114 {
114 -if(data->size<=i)  
115 - throw Range(); 115 + if (data->size <= i)
  116 + throw Range();
116 } 117 }
117 118
118 inline char rcstring::read(unsigned int i) const 119 inline char rcstring::read(unsigned int i) const
119 { 120 {
120 - return data->s[i]; 121 + return data->s[i];
121 } 122 }
122 123
123 inline void rcstring::write(unsigned int i, char c) 124 inline void rcstring::write(unsigned int i, char c)
124 { 125 {
125 - data = data->detach();  
126 - data->s[i] = c; 126 + data = data->detach();
  127 + data->s[i] = c;
127 } 128 }
128 129
129 char rcstring::operator[](unsigned int i) const 130 char rcstring::operator[](unsigned int i) const
130 { 131 {
131 - cout << "char rcstring::operator[](unsigned int i) const"<<endl;  
132 - check(i);  
133 - return data->s[i]; 132 + cout << "char rcstring::operator[](unsigned int i) const" << endl;
  133 + check(i);
  134 + return data->s[i];
134 } 135 }
135 136
136 char& rcstring::operator[](unsigned int i) 137 char& rcstring::operator[](unsigned int i)
137 { 138 {
138 - cout << "char& rcstring::operator[](unsigned int i)"<<endl;  
139 - check(i);  
140 - data = data->detach();  
141 - return data->s[i]; 139 + cout << "char& rcstring::operator[](unsigned int i)" << endl;
  140 + check(i);
  141 + data = data->detach();
  142 + return data->s[i];
142 } 143 }
143 144
144 #endif /* __RCSTRING_H__ */ 145 #endif /* __RCSTRING_H__ */
examples04/05-rcstring/rcstring.cpp
@@ -2,24 +2,24 @@ @@ -2,24 +2,24 @@
2 2
3 int main() 3 int main()
4 { 4 {
5 -rcstring a,b,c;  
6 -a="10";  
7 -b="ala ma kota";  
8 -cout << a << " " << b << endl; // 10 ala ma ma kota  
9 -c=a+b;  
10 -cout << c<<endl; // 10ala ma kota  
11 -c=a+rcstring(" ")+b;  
12 -cout << c << endl; //10 ala ma kota  
13 -rcstring d("a");  
14 -cout << d <<endl; //a  
15 -d+="ula";  
16 -cout << d << endl; //aula  
17 -d+="15";  
18 -cout << d << endl; //aula15  
19 -cout << d[3]<<endl; //a  
20 -d[3]='b';  
21 -cout << d << endl; //aulb15  
22 -d[2]=d[1]=d[0];  
23 -cout << d << endl; //aaab15  
24 -return 0; 5 + rcstring a, b, c;
  6 + a = "10";
  7 + b = "ala ma kota";
  8 + cout << a << " " << b << endl; // 10 ala ma ma kota
  9 + c = a + b;
  10 + cout << c << endl; // 10ala ma kota
  11 + c = a + rcstring(" ") + b;
  12 + cout << c << endl; // 10 ala ma kota
  13 + rcstring d("a");
  14 + cout << d << endl; // a
  15 + d += "ula";
  16 + cout << d << endl; // aula
  17 + d += "15";
  18 + cout << d << endl; // aula15
  19 + cout << d[3] << endl; // a
  20 + d[3] = 'b';
  21 + cout << d << endl; // aulb15
  22 + d[2] = d[1] = d[0];
  23 + cout << d << endl; // aaab15
  24 + return 0;
25 } 25 }
examples04/05-rcstring/rcstring.h
1 #ifndef __RCSTRING_H__ 1 #ifndef __RCSTRING_H__
2 #define __RCSTRING_H__ 2 #define __RCSTRING_H__
3 -#include <string.h>  
4 -#include <stdio.h>  
5 -#include <malloc.h>  
6 #include <iostream> 3 #include <iostream>
  4 +#include <malloc.h>
  5 +#include <stdio.h>
  6 +#include <string.h>
7 using namespace std; 7 using namespace std;
8 8
9 -class rcstring{  
10 - struct rctext;  
11 - rctext* data;  
12 -public:  
13 -class Range{};  
14 -class Cref;  
15 -rcstring();  
16 -rcstring(const char*);  
17 -rcstring(const rcstring&);  
18 -~rcstring();  
19 -rcstring& operator=(const rcstring&);  
20 -rcstring& operator+=(const rcstring &);  
21 -rcstring operator+(const rcstring &) const;  
22 -friend ostream& operator<<(ostream&, const rcstring&);  
23 -void check (unsigned int i) const;  
24 -char read(unsigned int i) const;  
25 -void write(unsigned int i, char c);  
26 -char operator[](unsigned int i) const;  
27 -Cref operator[](unsigned int i); 9 +class rcstring {
  10 + struct rctext;
  11 + rctext* data;
  12 +
  13 + public:
  14 + class Range {};
  15 + class Cref;
  16 + rcstring();
  17 + rcstring(const char*);
  18 + rcstring(const rcstring&);
  19 + ~rcstring();
  20 + rcstring& operator=(const rcstring&);
  21 + rcstring& operator+=(const rcstring&);
  22 + rcstring operator+(const rcstring&) const;
  23 + friend ostream& operator<<(ostream&, const rcstring&);
  24 + void check(unsigned int i) const;
  25 + char read(unsigned int i) const;
  26 + void write(unsigned int i, char c);
  27 + char operator[](unsigned int i) const;
  28 + Cref operator[](unsigned int i);
28 }; 29 };
29 30
30 -struct rcstring::rctext  
31 -{  
32 - char* s;  
33 - unsigned int size;  
34 - unsigned int n;  
35 -  
36 - rctext(unsigned int nsize, const char* p)  
37 - {  
38 - n=1;  
39 - size=nsize;  
40 - s=new char[size+1];  
41 - strncpy(s,p,size);  
42 - s[size]='\0';  
43 - };  
44 - ~rctext()  
45 - {  
46 - delete [] s;  
47 - };  
48 - rctext* detach()  
49 - {  
50 - if(n==1)  
51 - return this;  
52 - rctext* t=new rctext(size, s);  
53 - n--;  
54 - return t;  
55 - };  
56 -private:  
57 - rctext(const rctext&);  
58 - rctext& operator=(const rctext&); 31 +struct rcstring::rctext {
  32 + char* s;
  33 + unsigned int size;
  34 + unsigned int n;
  35 +
  36 + rctext(unsigned int nsize, const char* p)
  37 + {
  38 + n = 1;
  39 + size = nsize;
  40 + s = new char[size + 1];
  41 + strncpy(s, p, size);
  42 + s[size] = '\0';
  43 + };
  44 + ~rctext()
  45 + {
  46 + delete[] s;
  47 + };
  48 + rctext* detach()
  49 + {
  50 + if (n == 1)
  51 + return this;
  52 + rctext* t = new rctext(size, s);
  53 + n--;
  54 + return t;
  55 + };
  56 +
  57 + private:
  58 + rctext(const rctext&);
  59 + rctext& operator=(const rctext&);
59 }; 60 };
60 61
61 -class rcstring::Cref  
62 -{  
63 - friend class rcstring;  
64 - rcstring& s;  
65 - int i;  
66 - Cref (rcstring& ss, unsigned int ii): s(ss), i(ii) {};  
67 -public:  
68 - operator char() const  
69 - {  
70 - cout << "operator char() const"<<endl;  
71 - return s.read(i);  
72 - }  
73 - rcstring::Cref& operator = (char c)  
74 - {  
75 - cout << "void operator = (char c)"<<endl;  
76 - s.write(i,c);  
77 - return *this;  
78 - }  
79 - rcstring::Cref& operator = (const Cref& ref)  
80 - {  
81 - return operator= ((char)ref);  
82 - } 62 +class rcstring::Cref {
  63 + friend class rcstring;
  64 + rcstring& s;
  65 + int i;
  66 + Cref(rcstring& ss, unsigned int ii) : s(ss), i(ii){};
  67 +
  68 + public:
  69 + operator char() const
  70 + {
  71 + cout << "operator char() const" << endl;
  72 + return s.read(i);
  73 + }
  74 + rcstring::Cref& operator=(char c)
  75 + {
  76 + cout << "void operator = (char c)" << endl;
  77 + s.write(i, c);
  78 + return *this;
  79 + }
  80 + rcstring::Cref& operator=(const Cref& ref)
  81 + {
  82 + return operator=((char)ref);
  83 + }
83 }; 84 };
84 inline rcstring::rcstring() 85 inline rcstring::rcstring()
85 - {  
86 - data = new rctext(0,"");  
87 - } 86 +{
  87 + data = new rctext(0, "");
  88 +}
88 89
89 inline rcstring::rcstring(const rcstring& x) 90 inline rcstring::rcstring(const rcstring& x)
90 - { 91 +{
91 x.data->n++; 92 x.data->n++;
92 - data=x.data;  
93 - } 93 + data = x.data;
  94 +}
94 inline rcstring::~rcstring() 95 inline rcstring::~rcstring()
95 { 96 {
96 - if(--data->n==0)  
97 - delete data; 97 + if (--data->n == 0)
  98 + delete data;
98 } 99 }
99 100
100 -rcstring& rcstring::operator=(const rcstring & x) 101 +rcstring& rcstring::operator=(const rcstring& x)
101 { 102 {
102 - x.data->n++;  
103 - if(--data->n == 0)  
104 - delete data;  
105 - data=x.data;  
106 - return *this; 103 + x.data->n++;
  104 + if (--data->n == 0)
  105 + delete data;
  106 + data = x.data;
  107 + return *this;
107 } 108 }
108 109
109 rcstring::rcstring(const char* s) 110 rcstring::rcstring(const char* s)
110 { 111 {
111 - data=new rctext(strlen(s),s); 112 + data = new rctext(strlen(s), s);
112 } 113 }
113 114
114 -ostream& operator << (ostream& o, const rcstring& s) 115 +ostream& operator<<(ostream& o, const rcstring& s)
115 { 116 {
116 - return o<<s.data->s; 117 + return o << s.data->s;
117 } 118 }
118 119
119 -rcstring& rcstring::operator+=(const rcstring & s) 120 +rcstring& rcstring::operator+=(const rcstring& s)
120 { 121 {
121 -unsigned int newsize=data->size+s.data->size;  
122 -rctext *newdata=new rctext(newsize,data->s);  
123 -strcat(newdata->s,s.data->s);  
124 -if(--data->n==0)  
125 - delete data;  
126 -data = newdata;  
127 -return *this; 122 + unsigned int newsize = data->size + s.data->size;
  123 + rctext* newdata = new rctext(newsize, data->s);
  124 + strcat(newdata->s, s.data->s);
  125 + if (--data->n == 0)
  126 + delete data;
  127 + data = newdata;
  128 + return *this;
128 } 129 }
129 130
130 -rcstring rcstring::operator+(const rcstring & s) const 131 +rcstring rcstring::operator+(const rcstring& s) const
131 { 132 {
132 - return rcstring(*this)+=s; 133 + return rcstring(*this) += s;
133 } 134 }
134 135
135 -inline void rcstring::check (unsigned int i) const 136 +inline void rcstring::check(unsigned int i) const
136 { 137 {
137 -if(data->size<=i)  
138 - throw Range(); 138 + if (data->size <= i)
  139 + throw Range();
139 } 140 }
140 inline char rcstring::read(unsigned int i) const 141 inline char rcstring::read(unsigned int i) const
141 { 142 {
142 - return data->s[i]; 143 + return data->s[i];
143 } 144 }
144 inline void rcstring::write(unsigned int i, char c) 145 inline void rcstring::write(unsigned int i, char c)
145 { 146 {
146 - data = data->detach();  
147 - data->s[i] = c; 147 + data = data->detach();
  148 + data->s[i] = c;
148 } 149 }
149 150
150 char rcstring::operator[](unsigned int i) const 151 char rcstring::operator[](unsigned int i) const
151 { 152 {
152 - cout << "char rcstring::operator[](unsigned int i) const"<<endl;  
153 - check(i);  
154 - return data->s[i]; 153 + cout << "char rcstring::operator[](unsigned int i) const" << endl;
  154 + check(i);
  155 + return data->s[i];
155 } 156 }
156 157
157 rcstring::Cref rcstring::operator[](unsigned int i) 158 rcstring::Cref rcstring::operator[](unsigned int i)
158 { 159 {
159 - cout << "Cref rcstring::operator[](unsigned int i)"<<endl;  
160 - check(i);  
161 - return Cref(*this,i); 160 + cout << "Cref rcstring::operator[](unsigned int i)" << endl;
  161 + check(i);
  162 + return Cref(*this, i);
162 } 163 }
163 164
164 -  
165 #endif /* __RCSTRING_H__ */ 165 #endif /* __RCSTRING_H__ */
examples04/06-threads/rcstring.h
1 #ifndef __RCSTRING_H__ 1 #ifndef __RCSTRING_H__
2 #define __RCSTRING_H__ 2 #define __RCSTRING_H__
3 -#include <string.h>  
4 -#include <stdio.h>  
5 -#include <malloc.h>  
6 #include <iostream> 3 #include <iostream>
  4 +#include <malloc.h>
  5 +#include <stdio.h>
  6 +#include <string.h>
7 using namespace std; 7 using namespace std;
8 8
9 -class rcstring{  
10 - struct rctext;  
11 - rctext* data;  
12 -public:  
13 -class Range{};  
14 -class Cref;  
15 -rcstring();  
16 -rcstring(const char*);  
17 -rcstring(const rcstring&);  
18 -~rcstring();  
19 -rcstring& operator=(const rcstring&);  
20 -rcstring& operator+=(const rcstring &);  
21 -rcstring operator+(const rcstring &) const;  
22 -friend ostream& operator<<(ostream&, const rcstring&);  
23 -void check (unsigned int i) const;  
24 -char read(unsigned int i) const;  
25 -void write(unsigned int i, char c);  
26 -char operator[](unsigned int i) const;  
27 -Cref operator[](unsigned int i);  
28 -unsigned int getRefCount(); 9 +class rcstring {
  10 + struct rctext;
  11 + rctext* data;
  12 +
  13 + public:
  14 + class Range {};
  15 + class Cref;
  16 + rcstring();
  17 + rcstring(const char*);
  18 + rcstring(const rcstring&);
  19 + ~rcstring();
  20 + rcstring& operator=(const rcstring&);
  21 + rcstring& operator+=(const rcstring&);
  22 + rcstring operator+(const rcstring&) const;
  23 + friend ostream& operator<<(ostream&, const rcstring&);
  24 + void check(unsigned int i) const;
  25 + char read(unsigned int i) const;
  26 + void write(unsigned int i, char c);
  27 + char operator[](unsigned int i) const;
  28 + Cref operator[](unsigned int i);
  29 + unsigned int getRefCount();
29 }; 30 };
30 31
31 -struct rcstring::rctext  
32 -{  
33 - char* s;  
34 - unsigned int size;  
35 - unsigned int n;  
36 -  
37 - rctext(unsigned int nsize, const char* p)  
38 - {  
39 - n=1;  
40 - size=nsize;  
41 - s=new char[size+1];  
42 - strncpy(s,p,size);  
43 - s[size]='\0';  
44 - };  
45 - ~rctext()  
46 - {  
47 - delete [] s;  
48 - };  
49 - rctext* detach()  
50 - {  
51 - if(n==1)  
52 - return this;  
53 - rctext* t=new rctext(size, s);  
54 - n--;  
55 - return t;  
56 - };  
57 -private:  
58 - rctext(const rctext&);  
59 - rctext& operator=(const rctext&); 32 +struct rcstring::rctext {
  33 + char* s;
  34 + unsigned int size;
  35 + unsigned int n;
  36 +
  37 + rctext(unsigned int nsize, const char* p)
  38 + {
  39 + n = 1;
  40 + size = nsize;
  41 + s = new char[size + 1];
  42 + strncpy(s, p, size);
  43 + s[size] = '\0';
  44 + };
  45 + ~rctext()
  46 + {
  47 + delete[] s;
  48 + };
  49 + rctext* detach()
  50 + {
  51 + if (n == 1)
  52 + return this;
  53 + rctext* t = new rctext(size, s);
  54 + n--;
  55 + return t;
  56 + };
  57 +
  58 + private:
  59 + rctext(const rctext&);
  60 + rctext& operator=(const rctext&);
60 }; 61 };
61 62
62 -class rcstring::Cref  
63 -{  
64 - friend class rcstring;  
65 - rcstring& s;  
66 - int i;  
67 - Cref (rcstring& ss, unsigned int ii): s(ss), i(ii) {};  
68 -public:  
69 - operator char() const  
70 - {  
71 - cout << "operator char() const"<<endl;  
72 - return s.read(i);  
73 - }  
74 - rcstring::Cref& operator = (char c)  
75 - {  
76 - cout << "void operator = (char c)"<<endl;  
77 - s.write(i,c);  
78 - return *this;  
79 - };  
80 - rcstring::Cref& operator = (const Cref& ref)  
81 - {  
82 - return operator= ((char)ref);  
83 - }; 63 +class rcstring::Cref {
  64 + friend class rcstring;
  65 + rcstring& s;
  66 + int i;
  67 + Cref(rcstring& ss, unsigned int ii) : s(ss), i(ii){};
  68 +
  69 + public:
  70 + operator char() const
  71 + {
  72 + cout << "operator char() const" << endl;
  73 + return s.read(i);
  74 + }
  75 + rcstring::Cref& operator=(char c)
  76 + {
  77 + cout << "void operator = (char c)" << endl;
  78 + s.write(i, c);
  79 + return *this;
  80 + };
  81 + rcstring::Cref& operator=(const Cref& ref)
  82 + {
  83 + return operator=((char)ref);
  84 + };
84 }; 85 };
85 86
86 inline rcstring::rcstring() 87 inline rcstring::rcstring()
87 - {  
88 - data = new rctext(0,"");  
89 - } 88 +{
  89 + data = new rctext(0, "");
  90 +}
90 91
91 inline rcstring::rcstring(const rcstring& x) 92 inline rcstring::rcstring(const rcstring& x)
92 - { 93 +{
93 x.data->n++; 94 x.data->n++;
94 - data=x.data;  
95 - } 95 + data = x.data;
  96 +}
96 inline rcstring::~rcstring() 97 inline rcstring::~rcstring()
97 { 98 {
98 - if(--data->n==0)  
99 - delete data; 99 + if (--data->n == 0)
  100 + delete data;
100 } 101 }
101 102
102 -rcstring& rcstring::operator=(const rcstring & x) 103 +rcstring& rcstring::operator=(const rcstring& x)
103 { 104 {
104 - x.data->n++;  
105 - if(--data->n == 0)  
106 - delete data;  
107 - data=x.data;  
108 - return *this; 105 + x.data->n++;
  106 + if (--data->n == 0)
  107 + delete data;
  108 + data = x.data;
  109 + return *this;
109 } 110 }
110 111
111 rcstring::rcstring(const char* s) 112 rcstring::rcstring(const char* s)
112 { 113 {
113 - data=new rctext(strlen(s),s); 114 + data = new rctext(strlen(s), s);
114 } 115 }
115 116
116 -ostream& operator << (ostream& o, const rcstring& s) 117 +ostream& operator<<(ostream& o, const rcstring& s)
117 { 118 {
118 - return o<<s.data->s; 119 + return o << s.data->s;
119 } 120 }
120 121
121 -rcstring& rcstring::operator+=(const rcstring & s) 122 +rcstring& rcstring::operator+=(const rcstring& s)
122 { 123 {
123 -int newsize=data->size+s.data->size;  
124 -rctext *newdata=new rctext(newsize,data->s);  
125 -strcat(newdata->s,s.data->s);  
126 -if(--data->n==0)  
127 - delete data;  
128 -data = newdata;  
129 -return *this; 124 + int newsize = data->size + s.data->size;
  125 + rctext* newdata = new rctext(newsize, data->s);
  126 + strcat(newdata->s, s.data->s);
  127 + if (--data->n == 0)
  128 + delete data;
  129 + data = newdata;
  130 + return *this;
130 } 131 }
131 132
132 -rcstring rcstring::operator+(const rcstring & s) const 133 +rcstring rcstring::operator+(const rcstring& s) const
133 { 134 {
134 - return rcstring(*this)+=s; 135 + return rcstring(*this) += s;
135 } 136 }
136 137
137 -inline void rcstring::check (unsigned int i) const 138 +inline void rcstring::check(unsigned int i) const
138 { 139 {
139 -if(data->size<=i)  
140 - throw Range(); 140 + if (data->size <= i)
  141 + throw Range();
141 } 142 }
142 inline char rcstring::read(unsigned int i) const 143 inline char rcstring::read(unsigned int i) const
143 { 144 {
144 - return data->s[i]; 145 + return data->s[i];
145 } 146 }
146 inline void rcstring::write(unsigned int i, char c) 147 inline void rcstring::write(unsigned int i, char c)
147 { 148 {
148 - data = data->detach();  
149 - data->s[i] = c; 149 + data = data->detach();
  150 + data->s[i] = c;
150 } 151 }
151 152
152 char rcstring::operator[](unsigned int i) const 153 char rcstring::operator[](unsigned int i) const
153 { 154 {
154 - cout << "char rcstring::operator[](unsigned int i) const"<<endl;  
155 - check(i);  
156 - return data->s[i]; 155 + cout << "char rcstring::operator[](unsigned int i) const" << endl;
  156 + check(i);
  157 + return data->s[i];
157 } 158 }
158 159
159 rcstring::Cref rcstring::operator[](unsigned int i) 160 rcstring::Cref rcstring::operator[](unsigned int i)
160 { 161 {
161 - cout << "Cref rcstring::operator[](unsigned int i)"<<endl;  
162 - check(i);  
163 - return Cref(*this,i); 162 + cout << "Cref rcstring::operator[](unsigned int i)" << endl;
  163 + check(i);
  164 + return Cref(*this, i);
164 } 165 }
165 166
166 -  
167 unsigned int rcstring::getRefCount() 167 unsigned int rcstring::getRefCount()
168 { 168 {
169 - return data->n; 169 + return data->n;
170 } 170 }
171 171
172 -  
173 #endif /* __RCSTRING_H__ */ 172 #endif /* __RCSTRING_H__ */
examples04/06-threads/threads1.cpp
1 -#include <stdio.h>  
2 #include <pthread.h> 1 #include <pthread.h>
  2 +#include <stdio.h>
3 3
4 -  
5 -void *  
6 -thread1 (void *p) 4 +void* thread1(void* p)
7 { 5 {
8 - int *pp = (int *) p;  
9 - printf ("Parameter of thread1: %d\n", *pp);  
10 - *pp = 1;  
11 - for (unsigned int i = 0; i < 100000; i++)  
12 - printf ("Hello from thread 1\n");  
13 - pthread_exit (p); 6 + int* pp = (int*)p;
  7 + printf("Parameter of thread1: %d\n", *pp);
  8 + *pp = 1;
  9 + for (unsigned int i = 0; i < 100000; i++)
  10 + printf("Hello from thread 1\n");
  11 + pthread_exit(p);
14 } 12 }
15 13
16 -void *  
17 -thread2 (void *p) 14 +void* thread2(void* p)
18 { 15 {
19 - int *pp = (int *) p;  
20 - printf ("Parameter of thread2: %d\n", *pp);  
21 - *pp = 2;  
22 - for (unsigned int i = 0; i < 100000; i++)  
23 - printf ("Hello from thread 2\n");  
24 - pthread_exit (p); 16 + int* pp = (int*)p;
  17 + printf("Parameter of thread2: %d\n", *pp);
  18 + *pp = 2;
  19 + for (unsigned int i = 0; i < 100000; i++)
  20 + printf("Hello from thread 2\n");
  21 + pthread_exit(p);
25 } 22 }
26 23
27 -  
28 -int  
29 -main () 24 +int main()
30 { 25 {
31 - pthread_t t1;  
32 - pthread_t t2;  
33 - int *retval1;  
34 - int *retval2;  
35 - int r1 = 10, r2 = 11;  
36 -  
37 -  
38 - pthread_create (&t1, NULL, thread1, &r1);  
39 - pthread_create (&t2, NULL, thread2, &r2); 26 + pthread_t t1;
  27 + pthread_t t2;
  28 + int* retval1;
  29 + int* retval2;
  30 + int r1 = 10, r2 = 11;
40 31
41 - pthread_join (t1, (void **) &retval1);  
42 - pthread_join (t2, (void **) &retval2); 32 + pthread_create(&t1, NULL, thread1, &r1);
  33 + pthread_create(&t2, NULL, thread2, &r2);
43 34
44 - printf ("%d\n", *retval1);  
45 - printf ("%d\n", *retval2); 35 + pthread_join(t1, (void**)&retval1);
  36 + pthread_join(t2, (void**)&retval2);
46 37
  38 + printf("%d\n", *retval1);
  39 + printf("%d\n", *retval2);
47 } 40 }