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 @@
/*

View file

@ -0,0 +1 @@
a

View file

@ -0,0 +1,3 @@
int f(int x, int x) {
return x;
}

View file

@ -0,0 +1,4 @@
int main)( {
return 0;
return 1;
}

View file

@ -0,0 +1 @@
foo() {}

View file

@ -0,0 +1,4 @@
int main() {
x = 14;
return 0 ;
}

View file

@ -0,0 +1,5 @@
int main() {
int x;
int x;
return 0 ;
}

View file

@ -0,0 +1,4 @@
int main() {
if (false)
return 0;
}

View file

@ -0,0 +1,5 @@
int main() {
int x;
x = true;
return 1;
}

View file

@ -0,0 +1,6 @@
int main() {
if (true)
return;
;
return 1;
}

View file

@ -0,0 +1,3 @@
int main() {
return true;
}

View file

@ -0,0 +1,6 @@
int main() {
int i = foo(true);
return 0 ;
}
int foo(boolean b) { b = true; }

View file

@ -0,0 +1,5 @@
int main() {
double x ;
x = 2 * 3.14 ;
return 0 ;
}

View file

@ -0,0 +1,6 @@
// passing doubles to printInt().
int main() {
printInt(1.0);
return 0 ;
}

View file

@ -0,0 +1,6 @@
// passing integers to printDouble().
int main() {
printDouble(1);
return 0 ;
}

View file

@ -0,0 +1,11 @@
// 0 instead of 1 argument
int main() {
int x = foo();
return 0 ;
}
int foo(int y) {
return y;
}

View file

@ -0,0 +1,11 @@
// 1 instead of 2 arguments
int main() {
int x = foo(1);
return 0 ;
}
int foo(int y,int z) {
return y;
}

View file

@ -0,0 +1,11 @@
// 2 instead of 1 arguments
int main() {
int x = foo(1,2);
return 0 ;
}
int foo(int y) {
return y;
}

View file

@ -0,0 +1,8 @@
// Compare double with boolean.
int main() {
if (2.0 == true) {
printString("foo");
}
return 0 ;
}

View file

@ -0,0 +1,6 @@
/* Testing that main must return. */
/* All functions should return a value of their value type. This is not a valid Javalette program: */
int main() {
}

View file

@ -0,0 +1,6 @@
// Assigning double to int variable.
int main () {
int x = 1.0;
return 0 ;
}

View file

@ -0,0 +1,6 @@
// Assigning int to double variable.
int main () {
double x = 1;
return 0 ;
}

View file

@ -0,0 +1,4 @@
int main() {
if (false)
return 0;
}

View file

@ -0,0 +1,8 @@
int main() {
return f(3);
}
int f(int x) {
if (x<0)
return x;
}

View file

@ -0,0 +1,7 @@
// Assigning double to int variable.
int main () {
int x;
x = 1.0;
return 0 ;
}

View file

@ -0,0 +1,7 @@
// Assigning int to double variable.
int main () {
double x;
x = 1;
return 0 ;
}

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!

View file

@ -0,0 +1,73 @@
int main() {
printInt(fac(10));
printInt(rfac(10));
printInt(mfac(10));
printInt(ifac(10));
double r ; // just to test blocks
{
int n = 10;
int r = 1;
while (n>0) {
r = r * n;
n--;
}
printInt(r);
}
printDouble(dfac(10.0));
printString ("hello */");
printString ("/* world") ;
return 0 ;
}
int fac(int a) {
int r;
int n;
r = 1;
n = a;
while (n > 0) {
r = r * n;
n = n - 1;
}
return r;
}
int rfac(int n) {
if (n == 0)
return 1;
else
return n * rfac(n-1);
}
int mfac(int n) {
if (n == 0)
return 1;
else
return n * nfac(n-1);
}
int nfac(int n) {
if (n != 0)
return mfac(n-1) * n;
else
return 1;
}
double dfac(double n) {
if (n == 0.0)
return 1.0;
else
return n * dfac(n-1.0);
}
int ifac(int n) { return ifac2f(1,n); }
int ifac2f(int l, int h) {
if (l == h)
return l;
if (l > h)
return 1;
int m;
m = (l + h) / 2;
return ifac2f(l,m) * ifac2f(m+1,h);
}

View file

@ -0,0 +1,8 @@
3628800
3628800
3628800
3628800
3628800
3628800.0
hello */
/* world

View file

@ -0,0 +1,12 @@
/* void expression as statement */
int main() {
foo();
return 0 ;
}
void foo() {
printString("foo");
return;
}

View file

@ -0,0 +1 @@
foo

View file

@ -0,0 +1,23 @@
// Testing the return checker
int f () {
if (true)
return 0;
else
{}
}
int g () {
if (false)
{}
else
return 0;
}
void p () {}
int main() {
p();
return 0;
}

View file

View file

@ -0,0 +1,7 @@
/* allow comparing booleans. */
int main() {
if (true == true) { printInt(42); }
return 0 ;
}

View file

@ -0,0 +1 @@
42

View file

@ -0,0 +1,14 @@
/* usage of variable initialized in both branches. */
int main () {
int x;
int y = 56;
if (y + 45 <= 2) {
x = 1;
} else {
x = 2;
}
printInt(x);
return 0 ;
}

View file

@ -0,0 +1 @@
2

View file

@ -0,0 +1,11 @@
// Declaration of multiple variables of the same type in one statement:
int main () {
int x, y;
x = 45;
y = -36;
printInt(x);
printInt(y);
return 0 ;
}

View file

@ -0,0 +1,2 @@
45
-36

View file

@ -0,0 +1,8 @@
// declaration and initialization in same statement
int main() {
int x = 7;
printInt(x);
return 0 ;
}

View file

@ -0,0 +1 @@
7

View file

@ -0,0 +1,11 @@
// multiple variables of the same type declared
// and possibly initialized in the same statement
int main() {
int x, y = 7;
x = -1234234;
printInt(x);
printInt(y);
return 0 ;
}

View file

@ -0,0 +1,2 @@
-1234234
7

View file

@ -0,0 +1,13 @@
// Calling functions which take zero parameters
int main() {
int x = foo();
printInt(x);
return 0 ;
}
int foo() {
return 10;
}

View file

@ -0,0 +1 @@
10

View file

@ -0,0 +1,19 @@
// count function parameters as initialized
int main() {
printInt(fac(5));
return 0 ;
}
int fac (int a) {
int r;
int n;
r = 1;
n = a;
while (n > 0)
{
r = r * n;
n = n - 1;
}
return r;
}

View file

@ -0,0 +1 @@
120

View file

@ -0,0 +1,6 @@
/* Test pushing -1. */
int main() {
printInt(-1);
return 0 ;
}

View file

@ -0,0 +1 @@
-1

View file

@ -0,0 +1,26 @@
/* Test arithmetic and comparisons. */
int main() {
int x = 56;
int y = -23;
printInt(x+y);
printInt(x-y);
printInt(x*y);
printInt(45/2);
printInt(78%3);
double z = -9.3;
double w = 5.1;
printBool(z+w > z-w);
printBool(z/w <= z*w);
return 0 ;
}
void printBool(boolean b) {
if (b) {
printString("true");
return;
} else {
printString("false");
return;
}
}

View file

@ -0,0 +1,7 @@
33
79
-1288
22
0
true
false

View file

@ -0,0 +1,33 @@
/* Test boolean operators. */
int main() {
printString("&&");
printBool(test(-1) && test(0));
printBool(test(-2) && test(1));
printBool(test(3) && test(-5));
printBool(test(234234) && test(21321));
printString("||");
printBool(test(-1) || test(0));
printBool(test(-2) || test(1));
printBool(test(3) || test(-5));
printBool(test(234234) || test(21321));
printString("!");
printBool(true);
printBool(false);
return 0 ;
}
void printBool(boolean b) {
if (!b) {
printString("false");
} else {
printString("true");
}
return;
}
boolean test(int i) {
printInt(i);
return i > 0;
}

View file

@ -0,0 +1,25 @@
&&
-1
false
-2
false
3
-5
false
234234
21321
true
||
-1
0
false
-2
1
true
3
true
234234
true
!
true
false

View file

@ -0,0 +1,17 @@
/* Fibonacci. */
int main () {
int lo,hi,mx ;
lo = 1 ;
hi = lo ;
mx = 5000000 ;
printInt(lo) ;
while (hi < mx) {
printInt(hi) ;
hi = lo + hi ;
lo = hi - lo ;
}
return 0 ;
}

View file

@ -0,0 +1,33 @@
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578

View file

@ -0,0 +1,16 @@
/* parity of positive integers by recursion */
int main () {
printInt(ev(17)) ;
return 0 ;
}
int ev (int y) {
if (y > 0)
return ev (y-2) ;
else
if (y < 0)
return 0 ;
else
return 1 ;
}

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,15 @@
/* parity of positive integers by loop */
int main () {
int y = 17;
while (y > 0)
y = y - 2;
if (y < 0) {
printInt(0);
return 0 ;
}
else {
printInt(1);
return 0 ;
}
}

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,40 @@
/* Test boolean operators */
int main () {
int x = 4;
if (3 <= x && 4 != 2 && true) {
printBool(true);
} else {
printString("apa");
}
printBool(true == true || dontCallMe(1));
printBool(4.0 < -50.0 && dontCallMe(2));
printBool(4 == x && true == !false && true);
printBool(implies(false,false));
printBool(implies(false,true));
printBool(implies(true,false));
printBool(implies(true,true));
return 0 ;
}
boolean dontCallMe(int x) {
printInt(x);
return true;
}
void printBool(boolean b) {
if (b) {
printString("true");
} else {
printString("false");
}
return;
}
boolean implies(boolean x, boolean y) {
return !x || x == y;
}

View file

@ -0,0 +1,8 @@
true
true
false
true
true
true
false
true

View file

@ -0,0 +1,2 @@
-5673
42.5

View file

@ -0,0 +1,15 @@
/* test input */
int main() {
int x = readInt();
double y = readDouble();
printInt(x-5);
if (y > 42.0 || y < 43.0)
printString("yay!");
else
printString("nay!");
return 0 ;
}

View file

@ -0,0 +1,2 @@
-5678
yay!

View file

@ -0,0 +1,26 @@
int main() {
int i = 78;
{
int i = 1;
printInt(i);
}
printInt(i);
while (i > 76) {
i--;
printInt(i);
// this is a little tricky
// on the right hand side, i refers to the outer i
int i = i + 7;
printInt(i);
}
printInt(i);
if (i > 4) {
int i = 4;
printInt(i);
} else {
printString("foo");
}
printInt(i);
return 0 ;
}

View file

@ -0,0 +1,9 @@
1
78
77
84
76
83
76
4
76

View file

@ -0,0 +1,7 @@
int main() {
p();
printInt(1);
return 0;
}
void p() {}

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,6 @@
int main() {
if (true) {
printInt(1);
return 0;
}
}

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,7 @@
int main() {
int x;
double y;
printInt(x);
printDouble(y);
return 0;
}

View file

@ -0,0 +1,2 @@
0
0.0