summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-15 03:50:12 +0200
committerPatrick Simianer <p@simianer.de>2014-06-15 03:50:12 +0200
commit258e1b92ebbfdebefabc120969ab87c3d8b75c3d (patch)
treeef4ab11fe0bf9d720cea23b35711358a8465feeb
parentcf3a29feb5887344b6633ead1b4b6d5657a15a4b (diff)
old c,cc examples
-rw-r--r--c,cc/Makefile17
-rw-r--r--c,cc/animal.cc37
-rw-r--r--c,cc/bitwise.c51
-rw-r--r--c,cc/cast.cc27
-rw-r--r--c,cc/cat_char.cc16
-rw-r--r--c,cc/class.cc31
-rw-r--r--c,cc/class_member.cc33
-rw-r--r--c,cc/compression_test.cc45
-rw-r--r--c,cc/cond.h9
-rw-r--r--c,cc/ctype.c43
-rw-r--r--c,cc/double_pointer.c35
-rw-r--r--c,cc/filelib.c87
-rw-r--r--c,cc/filelib.h25
-rw-r--r--c,cc/float_int.cc26
-rw-r--r--c,cc/gz.c16
-rw-r--r--c,cc/in_out.c14
-rw-r--r--c,cc/matrix.cc210
-rw-r--r--c,cc/ooc.c78
-rw-r--r--c,cc/ooc.h6
-rw-r--r--c,cc/pointers.c46
-rw-r--r--c,cc/precedence.cc16
-rw-r--r--c,cc/scanf.c17
-rw-r--r--c,cc/snprintf.c14
-rw-r--r--c,cc/sort.cc47
-rw-r--r--c,cc/split.cc55
-rw-r--r--c,cc/sscanf.c17
-rw-r--r--c,cc/stringstream.cc26
-rw-r--r--c,cc/stringutil.c22
-rw-r--r--c,cc/stringutil.h13
-rw-r--r--c,cc/stringutil_test.c53
-rw-r--r--c,cc/struct.c20
-rw-r--r--c,cc/substr.cc23
-rw-r--r--c,cc/thread_test.c43
-rw-r--r--c,cc/to_hex.cc14
-rw-r--r--c,cc/type_size.c12
-rw-r--r--c,cc/vector_addition.c29
-rw-r--r--c,cc/vector_addition.cu61
-rw-r--r--c,cc/vector_addition1.cu48
38 files changed, 1382 insertions, 0 deletions
diff --git a/c,cc/Makefile b/c,cc/Makefile
new file mode 100644
index 0000000..64008d1
--- /dev/null
+++ b/c,cc/Makefile
@@ -0,0 +1,17 @@
+CFLAGS += -pedantic -Wall -std=c99
+
+compression_test: compression_test.cc
+ g++ compression_test.cc -lboost_filesystem -lboost_iostreams -o compression_test
+
+vector_addition: vector_addition.c
+ gcc -Os vector_addition.c -o vector_addition
+
+vector_addition_cuda: vector_addition.cu
+ nvcc vector_addition.cu -o vector_addition_cuda
+
+vector_addition_cuda1: vector_addition1.cu
+ nvcc vector_addition1.cu -o vector_addition_cuda1
+
+thread_test: thread_test.c
+ gcc thread_test.c -lpthread -o thread_test
+
diff --git a/c,cc/animal.cc b/c,cc/animal.cc
new file mode 100644
index 0000000..3bad1b1
--- /dev/null
+++ b/c,cc/animal.cc
@@ -0,0 +1,37 @@
+#include <iostream>
+
+using namespace std;
+
+
+class Animal
+{
+ public:
+ Animal() { // Konstruktor
+ cout << "Der Konstruktor wird aufgerufen." << endl;
+ m_alive = true;
+ }
+
+ ~Animal() {cout << "Der Destruktor wird aufgerufen." << endl;}
+
+ bool isAlive() { // Methode
+ return m_alive;
+ }
+
+ void kill() { // Methode
+ m_alive=false;
+ }
+
+ private:
+ bool m_alive; // Member-Variable
+};
+
+
+int main(void)
+{
+ int i;
+ bool alive;
+
+ Animal a; // Instanz von Animal
+ Animal b; // noch eine ^
+}
+
diff --git a/c,cc/bitwise.c b/c,cc/bitwise.c
new file mode 100644
index 0000000..c90bfbd
--- /dev/null
+++ b/c,cc/bitwise.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+
+
+void
+test1()
+{
+ int foo[4] = {147, 142, 207, 19};
+ unsigned long result = 0;
+
+ for( int i = 0; i < 4; i += 1 )
+ {
+ result = result<<8; // 8 bit left
+ result = foo[i]|result;
+ }
+
+ printf("%lx\n", result);
+}
+
+void
+test2(void)
+{
+ int foo[4];
+ unsigned long bar = 0x938ecf13;
+
+ for( int i = 0; i < 4; i += 1 )
+ {
+ foo[i] = (bar>>i*8)&255;
+
+ printf("%d\n", foo[i]);
+ }
+}
+
+void
+test3(void)
+{
+ int s = 12;
+ unsigned long foo = 0xffffffff;
+
+ foo = foo<<s;
+ printf("%lx\n", foo);
+}
+
+int main(void)
+{
+ test1();
+ test2();
+ test3();
+
+ return 0;
+}
+
diff --git a/c,cc/cast.cc b/c,cc/cast.cc
new file mode 100644
index 0000000..2e84a64
--- /dev/null
+++ b/c,cc/cast.cc
@@ -0,0 +1,27 @@
+#include <iostream>
+
+using namespace std;
+
+
+struct A {
+ virtual void p() { cout << "A" << endl; }
+ void q() { cout << "q" << endl; }
+};
+
+struct Ba : public A {
+ void p() { cout << "Ba" << endl; }
+};
+
+struct Bb : public A {
+ void p() { cout << "Bb" << endl; }
+};
+
+int main(void)
+{
+ A* x = new A;
+ x = dynamic_cast<Ba*>(new Ba);
+ cout << "-" << endl;
+ x->q();
+ x->p();
+}
+
diff --git a/c,cc/cat_char.cc b/c,cc/cat_char.cc
new file mode 100644
index 0000000..3daf564
--- /dev/null
+++ b/c,cc/cat_char.cc
@@ -0,0 +1,16 @@
+#include <iostream>
+#include <string.h>
+
+using namespace std;
+
+
+int main(void)
+{
+ char c[] = "c";
+ char x[1024];
+ string s("s");
+ strcpy(x, s.c_str());
+ strcat(x, c);
+ cout << x << endl;
+}
+
diff --git a/c,cc/class.cc b/c,cc/class.cc
new file mode 100644
index 0000000..b493b51
--- /dev/null
+++ b/c,cc/class.cc
@@ -0,0 +1,31 @@
+#include <iostream>
+
+using namespace std;
+
+
+class A {
+ public:
+ A(int a):_a(a) {}
+ int geta() {
+ return _a;
+ }
+ static int _b;
+ private:
+ int _a;
+};
+
+int A::_b = 0;
+
+int main(void){
+ A a(2);
+ A *b = new A(2);
+ cout << b->geta() << endl;
+ cout << b->_b << endl;
+ cout << a._b << endl;
+ A::_b = 42;
+ cout << b->geta() << endl;
+ cout << b->_b << endl;
+ cout << a._b << endl;
+ return 0;
+}
+
diff --git a/c,cc/class_member.cc b/c,cc/class_member.cc
new file mode 100644
index 0000000..f78ca64
--- /dev/null
+++ b/c,cc/class_member.cc
@@ -0,0 +1,33 @@
+#include <iostream>
+
+using namespace std;
+
+
+struct A
+{
+};
+
+struct B : public A
+{
+
+};
+
+struct C : public A
+{
+
+};
+
+struct D
+{
+ A* a;
+};
+
+int main(void)
+{
+ B b;
+ D d;
+ A a;
+
+ d.a = &b;
+}
+
diff --git a/c,cc/compression_test.cc b/c,cc/compression_test.cc
new file mode 100644
index 0000000..21bfafc
--- /dev/null
+++ b/c,cc/compression_test.cc
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <fstream>
+#include <boost/iostreams/device/file.hpp>
+#include <boost/iostreams/filter/zlib.hpp>
+#include <boost/iostreams/filter/bzip2.hpp>
+#include <boost/iostreams/filter/gzip.hpp>
+#include <boost/iostreams/filtering_stream.hpp>
+
+using namespace boost::iostreams;
+using namespace std;
+
+
+int main(void)
+{
+ //ofstream raw("out-raw");
+ filtering_ostream out_z;
+ out_z.push(gzip_compressor());
+ //out_gz.push(gzip_compressor());
+ out_z.push(file_sink("out-z", std::ios::binary));
+ //out_gz.push(file_sink("out-gz", std::ios::binary));
+ for (size_t i = 0; i < 10; i++) {
+ out_z << "line #" << i << endl;
+ //out_gz << "line #" << i << endl;
+ //out_bz << "line #" << i << endl;
+ //raw << "line #" << i << endl;
+ }
+ // flush(out);
+ close(out_z);
+ //close(out_gz);
+ //close(out_bz);
+ //raw.close();
+
+ for (size_t i = 0; i < 5; i++) {
+ ifstream file("out-z", ios_base::in | ios_base::binary);
+ filtering_istream in;
+ in.push(gzip_decompressor());
+ in.push(file);
+ string s;
+ while (getline(in, s)) {
+ cout << s << endl;
+ }
+ file.close();
+ }
+}
+
diff --git a/c,cc/cond.h b/c,cc/cond.h
new file mode 100644
index 0000000..0d9e572
--- /dev/null
+++ b/c,cc/cond.h
@@ -0,0 +1,9 @@
+#ifndef _COND_H_
+#define _COND_H_
+
+
+#define cond(expr, a, b) (expr) ? (a) : (b)
+
+
+#endif /* _COND_H_ */
+
diff --git a/c,cc/ctype.c b/c,cc/ctype.c
new file mode 100644
index 0000000..0331566
--- /dev/null
+++ b/c,cc/ctype.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+
+
+int main(void)
+{
+ char *a="abc";
+ char *x="xyz";
+ char *b="abc";
+ char *y="Xyz";
+
+ int r;
+
+ if(isupper(y[0]))
+ {
+ printf("%c\n",y[0]);
+ }
+
+ if(r=(!strcmp(a,b)))
+ {
+ printf("%d\n",r);
+ }
+
+ printf("%c\n",*x);
+
+ if(r=(!strcmp(a,x)))
+ {
+ printf("%d\n",r);
+ }
+
+ char bla[4]="aAc";
+
+ for( unsigned int i = 0; i < 4; i += 1 )
+ {
+ if(isupper(bla[i])) {
+ printf("%c\n",bla[i]);
+ }
+ }
+
+ return 0;
+}
+
diff --git a/c,cc/double_pointer.c b/c,cc/double_pointer.c
new file mode 100644
index 0000000..1c66b9a
--- /dev/null
+++ b/c,cc/double_pointer.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+
+
+int main(void)
+{
+ char word[] = "abcdef";
+
+ char *wp = word;
+
+ printf("%s\n", wp);
+
+ wp++;
+
+ printf("%s\n",wp);
+
+ wp++;
+
+ printf("%s\n",wp);
+
+ wp++;
+
+ printf("%s\n",wp);
+
+ wp++;
+
+ printf("%s\n",wp);
+
+ wp++;
+
+ printf("%s\n",wp);
+
+
+ return 0;
+}
+
diff --git a/c,cc/filelib.c b/c,cc/filelib.c
new file mode 100644
index 0000000..57a27c3
--- /dev/null
+++ b/c,cc/filelib.c
@@ -0,0 +1,87 @@
+#include "filelib.h"
+
+
+typedef struct
+{
+ hFile b_;
+ FILE* fp;
+} hFile_;
+
+typedef struct
+{
+ hFile b_;
+ gzFile* fp;
+} hgzFile;
+
+gzmode(const char* mode)
+{
+ // TODO
+ return true;
+}
+
+hgzFile*
+gzmakef(char* fname, const char* mode)
+{
+ hgzFile* hgz = malloc(sizeof(hgzFile));
+ hgz->b_.name = fname;
+ hgz->b_.gz = true;
+ gzFile* fp = malloc(sizeof(gzFile));
+ *fp = gzopen(fname, mode);
+ hgz->fp = fp;
+ return hgz;
+}
+
+hFile*
+makef(char* fname, const char* mode)
+{
+ if (endswith(fname, GZIP_EXT))
+ return gzmakef(fname, mode);
+ hFile_* f = malloc(sizeof(hFile_));
+ if (!f) return NULL;
+ f->b_.name = fname;
+ f->b_.gz = false;
+ if (!strcmp(fname, "-")) {
+ if (!strcmp(mode, "r") && !strcmp(mode, "r+")) f->fp = stdin;
+ else f->fp = stdout;
+ } else {
+ f->fp = fopen(fname, mode);
+ }
+ return f;
+}
+
+bool
+gzwriteln(const char* s, hFile* f)
+{
+ unsigned len = strlen(s);
+ gzwrite(*((hgzFile*)f)->fp, s, len);
+ gzwrite(*((hgzFile*)f)->fp, "\n", 1);
+ return gzflush(*((hgzFile*)f)->fp, Z_FINISH) == Z_OK;
+}
+
+bool
+writeln(const char* s, hFile* f)
+{
+ if (f->gz)
+ return gzwriteln(s, f);
+ return fputs(s, ((hFile_*)f)->fp)&&fputs("\n", ((hFile_*)f)->fp);
+}
+
+bool
+gzclosef(hFile* f)
+{
+ bool ret = gzclose(*((hgzFile*)f)->fp);
+ free(((hgzFile*)f)->fp);
+ free(f);
+ return ret;
+}
+
+bool
+closef(hFile* f)
+{
+ if (f->gz)
+ return gzclosef(f);
+ bool ret = fclose(((hFile_*)f)->fp);
+ free(f);
+ return ret;
+}
+
diff --git a/c,cc/filelib.h b/c,cc/filelib.h
new file mode 100644
index 0000000..587cddf
--- /dev/null
+++ b/c,cc/filelib.h
@@ -0,0 +1,25 @@
+#ifndef FILELIB_H
+#define FILELIB_H
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <zlib.h>
+#include <assert.h>
+#include "util.h"
+
+#define GZIP_EXT ".gz"
+
+
+typedef struct
+{
+ char* name; // relative
+ bool gz;
+} hFile;
+
+hFile* makef(char* fname, const char* mode);
+bool writeln(const char* s, hFile* f);
+
+
+#endif
+
diff --git a/c,cc/float_int.cc b/c,cc/float_int.cc
new file mode 100644
index 0000000..9d0ece4
--- /dev/null
+++ b/c,cc/float_int.cc
@@ -0,0 +1,26 @@
+#include <iostream>
+
+using namespace std;
+
+
+int main(void)
+{
+ int a = 1;
+ int b = 2;
+ double q = (double)a/b;
+ cout << q << endl;
+
+ cout << 1 - (float)a/b << endl;
+
+ float x = 0.1;
+ unsigned y = 105;
+ unsigned z = x*y;
+ cout << z << endl;
+
+ float u = -1;
+ if (u) cout << "XXXXXX" << endl;
+
+ float j = 0.0;
+ if (j) cout << "j" << endl;
+}
+
diff --git a/c,cc/gz.c b/c,cc/gz.c
new file mode 100644
index 0000000..a0dcd90
--- /dev/null
+++ b/c,cc/gz.c
@@ -0,0 +1,16 @@
+#include <zlib.h>
+#include <string.h>
+
+int
+main(void)
+{
+ gzFile gz = gzopen("asdf.gz", "wb");
+ int len = 0;
+ gzwrite(gz, "asdf\n", len);
+ char *s = "asdf\n";
+ gzwrite(gz, "asdf\n", strlen(s));
+ gzclose(gz);
+
+ return 0;
+}
+
diff --git a/c,cc/in_out.c b/c,cc/in_out.c
new file mode 100644
index 0000000..448eaa5
--- /dev/null
+++ b/c,cc/in_out.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+
+int main(void)
+{
+ int ch;
+
+ while ((ch=fgetc(stdin))!=EOF) {
+ fputc(ch, stdout);
+ }
+ printf("\n");
+ return 0;
+}
+
diff --git a/c,cc/matrix.cc b/c,cc/matrix.cc
new file mode 100644
index 0000000..9d1a1c0
--- /dev/null
+++ b/c,cc/matrix.cc
@@ -0,0 +1,210 @@
+#include <iostream>
+#include <stdlib.h>
+
+using namespace std;
+
+
+class MyMatrix
+{
+ public:
+ MyMatrix(int dimension);
+ MyMatrix(int rows, int columns);
+ ~MyMatrix();
+
+ void print();
+ int get_rows();
+ int get_columns();
+
+ double get_element(int row, int column);
+ void set_element(int row, int column, double value);
+
+ MyMatrix operator + (MyMatrix A);
+ MyMatrix operator * (MyMatrix A);
+ void operator = (MyMatrix A);
+
+
+ private:
+ double* m;
+ int row_d;
+ int col_d;
+ int d;
+};
+
+MyMatrix::MyMatrix(int dimension)
+{
+ row_d = dimension;
+ col_d = dimension;
+ d = dimension*dimension;
+ m = new double[d];
+
+ unsigned int k=0;
+ for(unsigned int i=0; i<row_d; i++) {
+ for(unsigned int j=0; j<col_d; j++) {
+ if(i==j) {
+ m[k] = 1;
+ } else {
+ m[k] = 0;
+ }
+ k++;
+ }
+ }
+}
+
+MyMatrix::MyMatrix(int rows, int columns)
+{
+ row_d = rows;
+ col_d = columns;
+ d = rows*columns;
+ m = new double[d];
+
+
+ srand(time(NULL));
+ for(unsigned int i=0; i<d; i++) {
+ m[i] = 1+(rand()%(100-1+1));
+ }
+}
+
+MyMatrix::~MyMatrix()
+{
+ delete[] m;
+}
+
+void MyMatrix::print()
+{
+ unsigned int k=0;
+ for(unsigned int i=0; i<row_d; i++) {
+ for(unsigned int j=0; j<col_d; j++) {
+ cout << m[k] << " ";
+ k++;
+ }
+ cout << endl;
+ }
+}
+
+int MyMatrix::get_rows()
+{
+ return row_d;
+}
+
+int MyMatrix::get_columns()
+{
+ return col_d;
+}
+
+
+double MyMatrix::get_element(int row, int column)
+{
+ // first element is 1,1!
+ if((row*column)<=0) {
+ return -1;
+ }
+ return m[((row-1)*col_d)+(column-1)];
+}
+
+void
+MyMatrix::set_element(int row, int column, double value)
+{
+ if((row*column)>0) {
+ m[((row-1)*col_d)+(column-1)] = value;
+ }
+}
+
+
+MyMatrix
+MyMatrix::operator + (MyMatrix A)
+{
+ MyMatrix* res = new MyMatrix(row_d, col_d);
+
+ unsigned int k=0;
+ for(unsigned int i=0; i<row_d; i++) {
+ for(unsigned int j=0; j<col_d; j++) {
+ res->m[k] = m[k]+A.get_element(i+1, j+1);
+ k++;
+ }
+ }
+
+ return *res;
+}
+
+MyMatrix MyMatrix::operator * (MyMatrix A)
+{
+ MyMatrix* res = new MyMatrix(row_d, col_d);
+
+ unsigned int k=0;
+ for(unsigned int i=0; i<row_d; i++) {
+ for(unsigned int j=0; j<col_d; j++) {
+ res->m[k] = m[k]*A.get_element(i+1, j+1);
+ k++;
+ }
+ }
+
+ return *res;
+}
+
+void MyMatrix::operator = (MyMatrix A)
+{
+ unsigned int k=0;
+ for(unsigned int i=0; i<A.get_rows(); i++) {
+ for(unsigned int j=0; j<A.get_columns(); j++) {
+ m[k] = A.get_element(i+1, j+1);
+ k++;
+ }
+ }
+}
+
+
+int main (int argc, char* const argv[]) {
+ cout << "9x9 identity matrix:" << endl;
+ MyMatrix* m = new MyMatrix(9);
+ m->print();
+
+ cout << " rows: " << m->get_rows() << ", ";
+ cout << "columns: " << m->get_columns() << endl;
+
+ delete m;
+
+
+ cout << endl << "2x5 matrix:" << endl;
+ MyMatrix n(2,5);
+ n.print();
+
+ cout << " rows: " << n.get_rows() << ", ";
+ cout << "columns: " << n.get_columns() << endl;
+
+ cout << "(1,3) = " << n.get_element(1,3) << endl;
+
+ cout << "last: (2,5) = " << n.get_element(2,5) << endl;
+
+ cout << "second to last element = 100: " << endl;
+ n.set_element(2, 4, 100);
+ n.print();
+
+ cout << endl << "addition:" << endl;
+ cout << "matrix 1:" << endl;
+ MyMatrix* a1 = new MyMatrix(3,3);
+ a1->print();
+ cout << "matrix 2:" << endl;
+ MyMatrix* a2 = new MyMatrix(3,3);
+ a2->print();
+ cout << "result:" << endl;
+ MyMatrix* e1 = new MyMatrix(3,3);
+ *e1 = *a1+*a2;
+ e1->print();
+ delete e1;
+
+ cout << endl << "multiplication" << endl;
+ cout << "matrix 1:" << endl;
+ MyMatrix* m1 = new MyMatrix(3,3);
+ a1->print();
+ cout << "matrix 2:" << endl;
+ MyMatrix* m2 = new MyMatrix(3,3);
+ m2->print();
+ cout << "result:" << endl;
+ MyMatrix* e2 = new MyMatrix(3,3);
+ *e2 = *m1 * *m2;
+ e2->print();
+ delete e2;
+
+ return 0;
+}
+
diff --git a/c,cc/ooc.c b/c,cc/ooc.c
new file mode 100644
index 0000000..aa20488
--- /dev/null
+++ b/c,cc/ooc.c
@@ -0,0 +1,78 @@
+#include "ooc.h"
+#include "stdio.h"
+#include "string.h"
+
+
+struct van {
+ struct vehicle base;
+ int cubic_size;
+};
+
+struct bus {
+ struct vehicle base;
+ int seats;
+};
+
+struct van*
+make_van()
+{
+ struct van* v = malloc(sizeof(struct van));
+ v->base.type = "van";
+ v->cubic_size = 12;
+ return v;
+}
+
+struct bus*
+make_bus()
+{
+ struct bus* v = malloc(sizeof(struct bus));
+ v->base.type = "bus";
+ v->seats=112;
+ return v;
+}
+
+struct vehicle*
+make_vehicle(const char* type)
+{
+ if(!strcmp(type, "van")) return make_van();
+ if(!strcmp(type, "bus")) return make_bus();
+ return NULL;
+}
+
+void
+do_something_with_a_bus(struct vehicle* v)
+{
+ ((struct bus*)v)->seats = 13;
+}
+
+void
+do_something_with_a_van(struct vehicle* v)
+{
+ ((struct van*)v)->cubic_size = 11;
+}
+
+void
+do_something(struct vehicle* v)
+{
+ if(!strcmp(v->type, "van")) return do_something_with_a_van(v);
+ if(!strcmp(v->type, "bus")) return do_something_with_a_bus(v);
+}
+
+int
+main(void) {
+ struct van my_van;
+ struct vehicle *something = &my_van;
+ my_van.cubic_size = 100;
+ my_van.base.power = 99;
+ printf("%d\n", something->power);
+ printf("%d\n", my_van.base.power);
+
+ struct bus* bus = make_vehicle("bus");
+ printf("%s\n", bus->base.type);
+ printf("%d\n", bus->seats);
+ do_something(bus);
+ printf("%d\n", bus->seats);
+
+ return 0;
+}
+
diff --git a/c,cc/ooc.h b/c,cc/ooc.h
new file mode 100644
index 0000000..0585d95
--- /dev/null
+++ b/c,cc/ooc.h
@@ -0,0 +1,6 @@
+struct vehicle {
+ int power;
+ int weight;
+ char* type;
+};
+
diff --git a/c,cc/pointers.c b/c,cc/pointers.c
new file mode 100644
index 0000000..1efc25e
--- /dev/null
+++ b/c,cc/pointers.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+
+void
+test1(void)
+{
+ char *sp;
+ char s[] = "str";
+ sp = s;
+ printf("sp: %s\n", sp);
+ printf("s: %s\n", s);
+
+ int a = 513;
+ int *ap = &a;
+ int **app = &ap;
+ printf("a: %d\n", a);
+ printf("ap: %d\n", *ap);
+ printf("app: %d\n", **app);
+}
+
+void
+test2()
+{
+ char *str = "abc";
+
+ printf("for:\n");
+ for (unsigned int i = 0; i < 3; i += 1)
+ {
+ printf("%c\n", str[i]);
+ }
+
+ printf("---\nwhile:\n");
+ while(*str) {
+ printf("%c\n", *str++);
+ }
+
+}
+
+int main(void)
+{
+ test1();
+ test2();
+
+ return 0;
+}
+
diff --git a/c,cc/precedence.cc b/c,cc/precedence.cc
new file mode 100644
index 0000000..08e29b6
--- /dev/null
+++ b/c,cc/precedence.cc
@@ -0,0 +1,16 @@
+#include <iostream>
+
+using namespace std;
+
+
+int main(void)
+{
+ bool a = false;
+ bool b = true;
+
+ cout << (!a && b) << endl;
+
+ double c = 0;
+ if (c) cout << "xx" << endl;
+}
+
diff --git a/c,cc/scanf.c b/c,cc/scanf.c
new file mode 100644
index 0000000..21dda28
--- /dev/null
+++ b/c,cc/scanf.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+
+int main (void)
+{
+ char str[80];
+ int res;
+
+ res = scanf(" %s", str);
+ printf("%d %s\n", res , str);
+ res = scanf("%s ", str);
+ printf("%d %s", res, str);
+ res = scanf(" %s", str);
+ printf("%d %s\n", res, str);
+ return 0;
+}
+
diff --git a/c,cc/snprintf.c b/c,cc/snprintf.c
new file mode 100644
index 0000000..a2cf1c2
--- /dev/null
+++ b/c,cc/snprintf.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+
+int main(void)
+{
+ int foo[4] = {192, 168, 1, 1};
+ char bla[20];
+
+ snprintf(bla, 19, "%d.%d.%d.%d", foo[0], foo[1], foo[2], foo[3]);
+
+ printf("%s\n", bla);
+
+ return 0;
+}
diff --git a/c,cc/sort.cc b/c,cc/sort.cc
new file mode 100644
index 0000000..165b086
--- /dev/null
+++ b/c,cc/sort.cc
@@ -0,0 +1,47 @@
+#include <iostream>
+#include <algorithm>
+#include <utility>
+#include <vector>
+
+using namespace std;
+
+
+struct X
+{
+ pair<string,int> p;
+};
+
+bool myf(X a, X b)
+{
+ return a.p.second > b.p.second;
+}
+
+int main(void)
+{
+ X a;
+ a.p.first = "a";
+ a.p.second = 1;
+
+ X b;
+ b.p.first = "b";
+ b.p.second = 2;
+
+ X c;
+ c.p.first = "c";
+ c.p.second = 3;
+
+ vector<X> v;
+ v.push_back(a);
+ v.push_back(b);
+ v.push_back(c);
+
+ for (unsigned i = 0; i < v.size(); i++) {
+ cout << v[i].p.first << endl;
+ }
+ sort(v.begin(), v.end(), myf);
+ cout << endl;
+ for (unsigned i = 0; i < v.size(); i++) {
+ cout << v[i].p.first << endl;
+ }
+}
+
diff --git a/c,cc/split.cc b/c,cc/split.cc
new file mode 100644
index 0000000..e01ad64
--- /dev/null
+++ b/c,cc/split.cc
@@ -0,0 +1,55 @@
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+
+using namespace std;
+
+
+int main(void)
+{
+ string s("a\tb\tc\td");
+ string::iterator it = s.begin();
+ char d = '\t';
+ string tmp;
+ size_t parts = 4;
+ size_t c = 0;
+ while(true) {
+ if (parts > 0 && c == parts-1) {
+ while(it != s.end()) {
+ tmp += *it;
+ it++;
+ }
+ cout << tmp << endl;
+ break;
+ }
+ if (it == s.end()) { cout << tmp << endl; break; }
+ if (*it != d) tmp += *it;
+ else {
+ cout << tmp << endl;
+ tmp.clear();
+ c++;
+ }
+ it++;
+ }
+
+ cout << "---" << endl;
+
+ stringstream ss(s);
+ string si;
+ parts = 0;
+ c = 0;
+
+ while(true)
+ {
+ if (parts > 0 && c == parts-1) {
+ getline(ss, si);
+ cout << si << endl;
+ break;
+ }
+ if(!getline(ss, si, '\t')) break;
+ cout << si << endl;
+ c++;
+ }
+}
+
diff --git a/c,cc/sscanf.c b/c,cc/sscanf.c
new file mode 100644
index 0000000..221852e
--- /dev/null
+++ b/c,cc/sscanf.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+
+int main(void)
+{
+ char ip[15] = {"192.168.100.100"};
+ int retval, val0, val1, val2, val3;
+
+ if(4!=(retval=sscanf(ip, "%d.%d.%d.%d", &val0, &val1, &val2, &val3))) {
+ printf("Error: sscanf failed, retval was: %d\n", retval);
+ }
+ else {
+ printf("%d\n%d\n%d\n%d\n", val0, val1, val2, val3);
+ }
+
+ return 0;
+}
diff --git a/c,cc/stringstream.cc b/c,cc/stringstream.cc
new file mode 100644
index 0000000..3d68db3
--- /dev/null
+++ b/c,cc/stringstream.cc
@@ -0,0 +1,26 @@
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+#include <stdio.h>
+#include <iomanip>
+
+using namespace std;
+
+
+int main(void) {
+ string s = "1 2 3 4";
+ int i;
+ vector<int> j;
+ stringstream ss(s);
+ while (ss >> i) {
+ j.push_back(i);
+ cout << i << endl;
+ }
+
+ printf("%4d %4d %4d %4d\n", j[0], j[1], j[2], j[3]);
+
+ cout.width(100);
+ cout << setw(100) << j.size() << endl;
+}
+
diff --git a/c,cc/stringutil.c b/c,cc/stringutil.c
new file mode 100644
index 0000000..aab4f03
--- /dev/null
+++ b/c,cc/stringutil.c
@@ -0,0 +1,22 @@
+#include "stringutil.h"
+
+
+char*
+strend(char* s)
+{
+ if (strlen(s) == 0) return '\0';
+ while(*s != '\0')
+ ++s;
+ return --s;
+}
+
+bool
+endswith(char* s, char* suff)
+{
+ if (strlen(s) < strlen(suff)) return false;
+ char* a = strend(s)-(strlen(suff)-1);
+ if (!a) return false;
+ if (!strcmp(a, suff)) return true;
+ return false;
+}
+
diff --git a/c,cc/stringutil.h b/c,cc/stringutil.h
new file mode 100644
index 0000000..153068a
--- /dev/null
+++ b/c,cc/stringutil.h
@@ -0,0 +1,13 @@
+#ifndef STRINGUTIL_H
+#define STRINGUTIL_H
+
+#include <string.h>
+#include <stdbool.h>
+
+
+char* strend(char* s);
+bool endswith(char* s, char* suff);
+
+
+#endif
+
diff --git a/c,cc/stringutil_test.c b/c,cc/stringutil_test.c
new file mode 100644
index 0000000..798bccd
--- /dev/null
+++ b/c,cc/stringutil_test.c
@@ -0,0 +1,53 @@
+#include "filelib.h"
+#include "stringutil.h"
+
+
+int
+main(void)
+{
+ /*
+ * strend
+ *
+ */
+ char* s= "";
+ char* end = strend(s);
+ if (end)
+ printf("should not output: %s\n", end);
+
+ s = "asdf.gz";
+ end = strend(s);
+ if (end)
+ printf("expect 'z': %s\n", end);
+
+ /*
+ * endswith
+ *
+ */
+ if (endswith(s, ".gz"))
+ printf("%s ends with %s\n", s, ".gz");
+
+ if (endswith(".gz", ".gz"))
+ printf(".gz ends with .gz\n");
+
+ if(!endswith("gz", ".gz"))
+ printf("gz does not end with .gz\n");
+
+ /*
+ * filelib
+ *
+ */
+ hFile* f = makef("-", "a");
+ if (!f)
+ printf("Error creating file '%s'\n", "-");
+ writeln("this goes to stdout", f);
+ closef(f);
+
+ hFile* g = makef("gzfile.gz", "a");
+ if (!g)
+ printf("Error creating file '%s'\n", "-");
+ writeln("this should be compressed", g);
+ closef(g);
+
+ return 0;
+}
+
diff --git a/c,cc/struct.c b/c,cc/struct.c
new file mode 100644
index 0000000..4111526
--- /dev/null
+++ b/c,cc/struct.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+
+struct bla {
+ int a, b;
+};
+
+int main(void)
+{
+ struct bla test, *ptest;
+ ptest = &test;
+
+ ptest->a = 3;
+ ptest->b = 5;
+
+ printf("%d-%d\n", ptest->a, ptest->b);
+
+ return 0;
+}
+
diff --git a/c,cc/substr.cc b/c,cc/substr.cc
new file mode 100644
index 0000000..9a91adc
--- /dev/null
+++ b/c,cc/substr.cc
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+
+int main(void)
+{
+ // 0 12 34 56
+ string s("aaax\tbx\tcxasd\tdx");
+ unsigned f = 0;
+ for(unsigned i = 0; i < 3; i++) {
+ unsigned e = f;
+ f = s.find("\t", f+1);
+ cout << "e:" << e << " f:" << f << endl;
+ if (e !=0) cout << "'" << s.substr(e+1, f-e-1) << "'" << endl;
+ else cout << "'" << s.substr(0, f) << "'" << endl;
+ }
+ cout << "---" << endl;
+ s.erase(0, f+1);
+ cout << "'" << s << "'" <<endl;
+}
+
diff --git a/c,cc/thread_test.c b/c,cc/thread_test.c
new file mode 100644
index 0000000..e1ba9f3
--- /dev/null
+++ b/c,cc/thread_test.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <pthread.h>
+#include <stdlib.h>
+
+
+void saying_pthread_name(void* ptr);
+int value=1;
+
+int
+main(void)
+{
+ pthread_t wthread, bthread, rthread;
+ char* wname = "white";
+ char* bname = "black";
+ char* rname = "red";
+ pthread_attr_t* pthread_attr_default = NULL;
+
+ pthread_create(&wthread, pthread_attr_default,
+ (void*)&saying_pthread_name, (void*) wname);
+
+ pthread_create(&bthread, pthread_attr_default,
+ (void*)&saying_pthread_name, (void*) bname);
+
+ pthread_create(&rthread, pthread_attr_default,
+ (void*)&saying_pthread_name, (void*) rname);
+
+ pthread_join(wthread,NULL);
+ //printf("%d\n", value);
+ pthread_join(rthread,NULL);
+ pthread_join(bthread,NULL);
+
+ exit(0);
+}
+
+void
+saying_pthread_name(void* ptr)
+{
+ char* message;
+ message = (char*)ptr;
+ printf("I am a %s pthread and the value is %d \n", message, value);
+ value = value+1;
+}
+
diff --git a/c,cc/to_hex.cc b/c,cc/to_hex.cc
new file mode 100644
index 0000000..6e577dc
--- /dev/null
+++ b/c,cc/to_hex.cc
@@ -0,0 +1,14 @@
+#include <iostream>
+
+using namespace std;
+
+
+int main(void)
+{
+ int r = 116;
+ int g = 10;
+ int b = 10;
+
+ cout << hex << r << " " << g << " " << b << endl;
+}
+
diff --git a/c,cc/type_size.c b/c,cc/type_size.c
new file mode 100644
index 0000000..18edb1f
--- /dev/null
+++ b/c,cc/type_size.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+
+int main(void)
+{
+ printf("char: %d\n", sizeof(char));
+ printf("int: %d\n", sizeof(int));
+ printf("float: %d\n", sizeof(float));
+ printf("double: %d\n", sizeof(double));
+ return 0;
+}
+
diff --git a/c,cc/vector_addition.c b/c,cc/vector_addition.c
new file mode 100644
index 0000000..decb3ff
--- /dev/null
+++ b/c,cc/vector_addition.c
@@ -0,0 +1,29 @@
+#include "stdio.h"
+#include "stdlib.h"
+
+
+int main(void) {
+ int N = 10000, M=100000;
+ float **x = malloc(N*sizeof(float*));
+ float *y = malloc(M*sizeof(float*));
+ int i,j;
+ for (i = 0; i < N; i++) {
+ x[i] = malloc(M*sizeof(float));
+ for (j = 0; j < M; j++) {
+ x[i][j] = (float) j;
+ }
+ }
+
+ for (i = 0; i < N ; i++) {
+ for (j = 0; j < M; j++) {
+ y[i] += x[i][j];
+ }
+ }
+
+ printf("%f\n", y[100]);
+
+ free(x); free(y);
+
+ return 0;
+}
+
diff --git a/c,cc/vector_addition.cu b/c,cc/vector_addition.cu
new file mode 100644
index 0000000..4f16bc3
--- /dev/null
+++ b/c,cc/vector_addition.cu
@@ -0,0 +1,61 @@
+#include "stdio.h"
+
+
+__global__ void add_arrays_gpu( float *in1, float *in2, float *out, int Ntot)
+{
+ int idx=blockIdx.x*blockDim.x+threadIdx.x;
+ if (idx < Ntot)
+ out[idx]=in1[idx]+in2[idx];
+}
+
+
+int main(void)
+{
+ /* pointers to host memory */
+ float *a, *b, *c;
+ /* pointers to device memory */
+ float *a_d, *b_d, *c_d;
+ int N=100000000;
+ int i;
+
+ /* Allocate arrays a, b and c on host*/
+ a = (float*) malloc(N*sizeof(float));
+ b = (float*) malloc(N*sizeof(float));
+ c = (float*) malloc(N*sizeof(float));
+
+ /* Allocate arrays a_d, b_d and c_d on device*/
+ cudaMalloc ((void **) &a_d, sizeof(float)*N);
+ cudaMalloc ((void **) &b_d, sizeof(float)*N);
+ cudaMalloc ((void **) &c_d, sizeof(float)*N);
+
+ /* Initialize arrays a and b */
+ for (i=0; i<N; i++) {
+ a[i]= (float) i;
+ b[i]=(float) -i;
+ }
+
+
+ /* Copy data from host memory to device memory */
+ cudaMemcpy(a_d, a, sizeof(float)*N, cudaMemcpyHostToDevice);
+ cudaMemcpy(b_d, b, sizeof(float)*N, cudaMemcpyHostToDevice);
+
+ /* Compute the execution configuration */
+ int block_size=256;
+ dim3 dimBlock(block_size);
+ dim3 dimGrid ( (N/dimBlock.x) + (!(N%dimBlock.x)?0:1) );
+
+ /* Add arrays a and b, store result in c */
+ add_arrays_gpu<<<dimGrid,dimBlock>>>(a_d, b_d, c_d, N);
+
+ /* Copy data from deveice memory to host memory */
+ //cudaMemcpy(c, c_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
+
+ /* Print c */
+/*for(i=0; i<N; i++)
+ printf(" c[%d]=%f\n",i,c[i]);*/
+
+ /* Free the memory */
+ free(a); free(b); free(c);
+ cudaFree(a_d); cudaFree(b_d);cudaFree(c_d);
+}
+
diff --git a/c,cc/vector_addition1.cu b/c,cc/vector_addition1.cu
new file mode 100644
index 0000000..9490ff3
--- /dev/null
+++ b/c,cc/vector_addition1.cu
@@ -0,0 +1,48 @@
+#include "stdio.h"
+
+
+__global__ void add_arrays_gpu( float *in1, float *in2, float *out, int Ntot)
+{
+ int idx=blockIdx.x*blockDim.x+threadIdx.x;
+ if (idx < Ntot)
+ out[idx]=in1[idx]+in2[idx];
+}
+
+
+int main(void)
+{
+ int N=10000, M=100000;
+ float **x = (float**) malloc(N*sizeof(float));
+ float **x_d = (float**) malloc(N*sizeof(float));
+ int i,j;
+
+ for (i = 0; i < N; i++) {
+ x[i] = (float*) malloc(M*sizeof(float));
+ cudaMalloc ((void **) &x_d[i], sizeof(float)*M);
+ for (j = 0; j < M; j++) {
+ x[i][j] = (float) j;
+ }
+ cudaMemcpy(x_d[i], x[i], sizeof(float)*M, cudaMemcpyHostToDevice);
+ }
+
+
+ /* Compute the execution configuration */
+// int block_size=256;
+ // dim3 dimBlock(block_size);
+ //dim3 dimGrid ( (N/dimBlock.x) + (!(N%dimBlock.x)?0:1) );
+
+ /** Add arrays a and b, store result in c */
+ // add_arrays_gpu<<<dimGrid,dimBlock>>>(a_d, b_d, c_d, N);
+
+ /* Copy data from deveice memory to host memory */
+ //cudaMemcpy(c, c_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
+
+ /* Print c */
+/*for(i=0; i<N; i++)
+ printf(" c[%d]=%f\n",i,c[i]);*/
+
+ /* Free the memory */
+// free(a); free(b); free(c);
+// cudaFree(a_d); cudaFree(b_d);cudaFree(c_d);
+}
+