first commit

This commit is contained in:
Jeena Paradies 2011-04-19 11:37:05 +02:00
commit 063194f8be
349 changed files with 36508 additions and 0 deletions

View file

@ -0,0 +1,16 @@
int main() {
int[] a = new int[10];
int j=0;
while (j<a.length) {
a[j] = j;
j++;
}
for (int x : a)
printInt(x);
int x = 45;
printInt(x);
return 0;
}

View file

@ -0,0 +1,11 @@
0
1
2
3
4
5
6
7
8
9
45

View file

@ -0,0 +1,46 @@
int [] doubleArray (int [] a){
int [] res = new int [a . length];
int i = 0 ;
for (int n : a){
res [i] = 2 * n ;
i ++ ;
}
return res ;
}
void shiftLeft (int [] a){
int x = a [0];
int i = 0 ;
while (i < a.length - 1){
a [i] = a [i + 1];
i ++ ;
}
a[a.length - 1]= x ;
return;
}
int scalProd(int[] a, int[] b) {
int res = 0;
int i = 0;
while (i < a.length) {
res = res + a[i] * b[i];
i++;
}
return res;
}
int main () {
int [] a = new int [5];
int i = 0 ;
while (i < a.length){
a [i]= i ;
i ++ ;
}
shiftLeft (a);
int [] b = doubleArray (a);
for (int x : a)printInt (x);
for (int x : b)printInt (x);
printInt(scalProd(a,b));
return 0 ;
}

View file

@ -0,0 +1,11 @@
1
2
3
4
0
2
4
6
8
0
60

View file

@ -0,0 +1,35 @@
int main() {
double[] vector = new double[4];
double[][] matrix = new double[3][4];
int i=0;
while (i<matrix.length) {
int j=0;
while (j<matrix[0].length) {
matrix[i][j] = 5.0;
j++;
}
i++;
}
i=0;
while (i<vector.length-1) {
vector[i] = 3.0;
i++;
}
matrix[0] = vector;
int j = 0;
while (j<vector.length) {
matrix[1][j] = vector[j]+1.0;
j++;
}
for (double[] x : matrix)
for (double y : x)
printDouble(y);
return 0 ;
}

View file

@ -0,0 +1,12 @@
3.0
3.0
3.0
0.0
4.0
4.0
4.0
1.0
5.0
5.0
5.0
5.0

View file

@ -0,0 +1,19 @@
int main () {
Counter c;
c = new Counter;
c.incr();
c.incr();
c.incr();
int x = c.value();
printInt(x);
return 0;
}
class Counter {
int val;
void incr () {val++; return;}
int value () {return val;}
}

View file

@ -0,0 +1 @@
3

View file

@ -0,0 +1,50 @@
class Node {
int elem;
Node next;
void setElem(int c) { elem = c; }
void setNext(Node n) { next = n; }
int getElem() { return elem; }
Node getNext() { return next; }
}
class Stack {
Node head;
void push(int c) {
Node newHead = new Node;
newHead.setElem(c);
newHead.setNext(head);
head = newHead;
}
boolean isEmpty() {
return head==(Node)null;
}
int top() {
return head.getElem();
}
void pop() {
head = head.getNext();
}
}
int main() {
Stack s = new Stack;
int i= 0;
while (i<10) {
s.push(i);
i++;
}
while (!s.isEmpty()) {
printInt(s.top());
s.pop();
}
return 0;
}

View file

@ -0,0 +1,10 @@
9
8
7
6
5
4
3
2
1
0

View file

@ -0,0 +1,62 @@
class Point2 {
int x;
int y;
void move (int dx, int dy) {
x = x + dx;
y = y + dy;
}
int getX () { return x; }
int getY () { return y; }
}
class Point3 extends Point2 {
int z;
void moveZ (int dz) {
z = z + dz;
}
int getZ () { return z; }
}
class Point4 extends Point3 {
int w;
void moveW (int dw) {
w = w + dw;
}
int getW () { return w; }
}
int main () {
Point2 p = new Point3;
Point3 q = new Point3;
Point4 r = new Point4;
q.move(2,4);
q.moveZ(7);
p = q;
p.move(3,5);
r.move(1,3);
r.moveZ(6);
r.moveW(2);
printInt(p.getX());
printInt(p.getY());
printInt(q.getZ());
printInt(r.getW());
return 0;
}

View file

@ -0,0 +1,4 @@
5
9
7
2

View file

@ -0,0 +1,61 @@
class Node {
int elem;
Node next;
void setElem (int e) { elem = e; }
void setNext (Node n) { next = n; }
int getElem () { return elem; }
Node getNext () { return next; }
}
class IntQueue {
Node front;
Node rear;
boolean isEmpty () { return front == (Node)null; }
void insert (int x) {
Node last = new Node;
last.setElem(x);
if (self.isEmpty())
front = last;
else
rear.setNext(last);
rear = last;
}
int first () { return front.getElem(); }
void rmFirst () {
front = front.getNext();
}
int size () {
Node n = front;
int res = 0;
while (n != (Node)null) {
n = n.getNext();
res++;
}
return res;
}
}
int f (int x) {
return x*x + 3;
}
int main () {
IntQueue q = new IntQueue;
q.insert(f(3));
q.insert(5);
q.insert(7);
printInt(q.first());
q.rmFirst();
printInt(q.size());
return 0;
}

View file

@ -0,0 +1,2 @@
12
2

View file

@ -0,0 +1,82 @@
class Node {
Shape elem;
Node next;
void setElem(Shape c) { elem = c; }
void setNext(Node n) { next = n; }
Shape getElem() { return elem; }
Node getNext() { return next; }
}
class Stack {
Node head;
void push(Shape c) {
Node newHead = new Node;
newHead.setElem(c);
newHead.setNext(head);
head = newHead;
}
boolean isEmpty() {
return head==(Node)null;
}
Shape top() {
return head.getElem();
}
void pop() {
head = head.getNext();
}
}
class Shape {
void tell () {
printString("I'm a shape");
}
void tellAgain() {
printString("I'm just a shape");
}
}
class Rectangle extends Shape {
void tellAgain() {
printString("I'm really a rectangle");
}
}
class Circle extends Shape {
void tellAgain() {
printString("I'm really a circle");
}
}
class Square extends Rectangle {
void tellAgain() {
printString("I'm really a square");
}
}
int main() {
Stack stk = new Stack;
Shape s = new Shape;
stk.push(s);
s = new Rectangle;
stk.push(s);
s = new Square;
stk.push(s);
s = new Circle;
stk.push(s);
while (!stk.isEmpty()) {
s = stk.top();
s.tell();
s.tellAgain();
stk.pop();
}
return 0;
}

View file

@ -0,0 +1,8 @@
I'm a shape
I'm really a circle
I'm a shape
I'm really a square
I'm a shape
I'm really a rectangle
I'm a shape
I'm just a shape

View file

@ -0,0 +1,48 @@
typedef struct Node *list;
struct Node {
int elem;
list next;
};
int main() {
printInt(length(fromTo(1,50)));
printInt(length2(fromTo(1,100)));
return 0;
}
int head (list xs) {
return xs -> elem;
}
list cons (int x, list xs) {
list n;
n = new Node;
n->elem = x;
n->next = xs;
return n;
}
int length (list xs) {
if (xs==(list)null)
return 0;
else
return 1 + length (xs->next);
}
list fromTo (int m, int n) {
if (m>n)
return (list)null;
else
return cons (m,fromTo (m+1,n));
}
int length2 (list xs) {
int res = 0;
while (xs != (list)null) {
res++;
xs = xs->next;
}
return res;
}

View file

@ -0,0 +1,2 @@
50
100

View file

@ -0,0 +1,78 @@
struct Node {
tree left;
int val;
tree right;
};
typedef struct Node *tree;
struct Header {
tree elems;
};
typedef struct Header *intset;
void insert (int x, intset s) {
s->elems = insertTree(x,s->elems);
return;
}
boolean isElem(int x, intset s) {
return isElemTree (x,s->elems);
}
tree insertTree(int x,tree t) {
if (t==(tree)null) {
tree n1;
n1 = new Node;
n1->left = (tree)null;
n1->val = x;
n1->right = (tree)null;
return n1;
}
else if (x < t->val)
t->left = insertTree (x,t->left);
else if (x > t->val)
t->right = insertTree (x,t->right);
return t;
}
boolean isElemTree(int x,tree t) {
if (t==(tree)null)
return false;
else if (x==t->val)
return true;
else if (x < t->val)
return isElemTree (x, t->left);
else
return isElemTree(x, t->right);
}
void printElem(int n, intset s) {
if (isElem(n,s))
printString("Elem!");
else
printString("Not elem!");
return;
}
int main () {
intset s;
s = new Header;
s->elems = (tree)null;
int x = 3;
int i = 0;
while (i<100) {
x = (x * 37) % 100;
i++;
insert(x, s);
}
printElem(23,s);
printElem(24,s);
printElem(25,s);
return 0;
}

View file

@ -0,0 +1,3 @@
Elem!
Not elem!
Not elem!