Newer
Older
zweic / tests / 5 / impossible.zwei
@glproj03 glproj03 on 6 Feb 2006 7 KB Added impossible test case
class List {
	Int isEmpty() { return this.isEmpty() }
	Int head() { return this.head() }
	List tail() { return this.tail() }
	List cons(Int x) { return this.cons(x) }
}

class Cons extends List {
	Int head;
	List tail;
	Int isEmpty() { return false }
	Int head() { return this.head }
	List tail() { return this.tail }
	List cons(Int x) { return new Cons(x, this) }
}

class Nil extends List {
	Int isEmpty() { return true }
	List cons(Int x) { return new Cons(x, this) }
}


class String {
	
	// print a string
	Null print(List l) {		
		while(!l.isEmpty()) {
			printChar(l.head());
			l = l.tail();
		}

	}
	
	// print a string with \n
	Null println(List l) {			
		this.print(l);
		printChar(10);
	}
	
	// returns the length of a string
	Int length(List l) {
		Int len = 0;
		while(!l.isEmpty()) {
			len = len+1;
			l = l.tail();
		}
		return len
	}
	
	// reverse a string
	List reverse(List l) {
		List rev = new Nil();
		while(!l.isEmpty()) {
			rev = new Cons(l.head(), rev);
			l = l.tail();
		}
		return rev
	}
	
	// return the substring from start to end (without)
	List substring(List l, Int start, Int end) {
		Int len = end - start;
		List result = new Nil();
			
		while(start > 0) {
			l = l.tail();	
			start = start-1; 
		}				
		while(len > 0) {
			result = new Cons(l.head(), result);
			l = l.tail();
			len = len-1;
		}				
		return this.reverse(result)
	}
	
	// reads a string from the console
	List readString() {
		List res = new Nil();
		Int c = readChar;
		while(c != 10) {
			res = new Cons(c, res);
			c = readChar;		
		}
		return this.reverse(res)
	}
	
	// compare two strings
	Int eq(List l1, List l2) {
		
		Int cont = true;
		Int eq = true;
		while(cont) {
			if(l1.isEmpty() && l2.isEmpty()) {
				cont = false;
			} else {
				if(l1.isEmpty() || l2.isEmpty()) {
					cont = false;
					eq = false;
				} else {
					if(l1.head() != l2.head()) {
						eq = false; 
						cont = false;
					} else {
						l1 = l1.tail();
						l2 = l2.tail();
					};
				};
			};
		}
		return eq					
	}
	
}

class Names extends String {
	
	Null printWelcome() {
		this.println("*************************");
		this.println("*** Play with strings ***");
		this.println("*************************");	
	}
	
	List askName(List ask) {
		this.print(" ");
		this.println(ask);
		this.print(" ->");
		List name = this.readString();
		this.print(" OK, the name is ");
		this.println(name);
		return name
	}
	
	Null main() {
		this.printWelcome();
		List name1 = this.askName("What is your name?");
		List name2 = this.askName("And your brother's?");
		this.print(" The names are: ");
		this.println(if(this.eq(name1, name2)) "equals ;-)" else "different :-0");	
		this.print(" Your name begins with: ");
		this.println(this.substring(name1, 0, 1));
		this.print(" Brother's name reversed: ");
		this.println(this.substring(this.reverse(name2), 1, this.length(this.reverse(name2))));
	}
	
}

class Real {
	Int left;
	Int right;
	
	Null print() {
		printInt(this.left);
		new String().print(".");
		printInt(this.right);
	}
	
	Real normalize() {
		Real r = this;
		Int cont = true;
		while(cont) {
			cont = r.right > 0;
			cont = if(cont) r.right % 10 == 0 else false;
			r = if(cont) new Real(r.left, r.right / 10) else r; 
		}
		return r
	}
	
	Int length() {
		Int l = 1;
		Int div = 10;
		while(this.right / div > 0) {
			l = l+1;
			div = div*10;
		} 
		l = if(this.right % 10 == 0) l-1 else l;
		return l	
	}
	
	Int max(Int i1, Int i2) {
		return if(i1 > i2) i1 else i2
	}
	
	Int min(Int i1, Int i2) {
		return if(i1 < i2) i1 else i2
	}
	
	Real sum(Real that) {
		Real thiz = this;
		Int lenMax = this.max(this.right, that.right);
		Int lenMin = this.min(this.right, that.right);
		Int diff = new Real(0, lenMax).length() - new Real(0, lenMin).length();
		Int factor = 1;
		while(diff > 0) {			
			factor = factor*10;
			diff = diff-1;					
		}
		if(this.right > that.right) {
			that = new Real(that.left, that.right * factor);
		} else {
			thiz = new Real(this.left, this.right * factor);
		};
		
		Int Tleft = thiz.left + that.left;
		Int Tright = thiz.right + that.right;				
		
		Real result = new Real(Tleft, Tright).normalize();
		return result
	}

}

class Reals {

	Real readReal() {
		String s = new String();
		s.println(" Type a real");
		s.print(" ->");
		Int left = readInt;
		s.print(" ->");
		printInt(left);
		s.print(".");
		Int right = readInt;
		Real r = new Real(left, right).normalize();
		s.print(" got ");
		r.print();
		s.println("");
		return r
	}	
	
	Null main() {	
		String s = new String();
		s.println("*************************");
		s.println("**** Play with reals ****");
		s.println("* With some limitations *");
		s.println("*************************");	
		Real r1 = this.readReal();
		Real r2 = this.readReal();
		s.print(" The sum is ");
		r1.sum(r2).print();
		s.println("");
	}

}

class Compop extends String {

	Null test() {
	
		Int i = 0;
		
		this.println(" ------------------------");
		this.println(" -> test == and !=");
		this.println(" ------------------------");
		i = 10;
		this.println(" Print 10");
		this.print(" ");
		while(i == 10) { 
			printInt(i); 
			this.print(" "); 
			i = i+1;
		}
		this.println(""); this.println("");
		
		i = 10;
		this.println(" Print 10..1");
		this.print(" ");
		while(i != 0) { 
			printInt(i); 
			this.print(" "); 
			i = i-1;
		}
		this.println(""); this.println("");
			
		this.println(" ------------------------");
		this.println(" -> test < and >");
		this.println(" ------------------------");
		i = 0;
		this.println(" Print 0..9");
		this.print(" ");
		while(i < 10) { 
			printInt(i); 
			this.print(" "); 
			i = i+1;
		}
		this.println(""); this.println("");
		
		i = 10;
		this.println(" Print 10..1");
		this.print(" ");
		while(i > 0) { 
			printInt(i); 
			this.print(" "); 
			i = i-1;
		}
		this.println(""); this.println("");
		
		this.println(" ------------------------");
		this.println(" -> test <= and =>");
		this.println(" ------------------------");
	  i = 0;
		this.println(" Print 0..10");
		this.print(" ");
		while(i <= 10) { 
			printInt(i); 
			this.print(" "); 
			i = i+1;
		}
		this.println(""); this.println("");
	
		i = 10;
		this.println(" Print 10..0");
		this.print(" ");
		while(i >= 0) { 
			printInt(i); 
			this.print(" "); 
			i = i-1;
		}
		this.println(""); this.println("");
		
		this.println(" ------------------------");
		this.println(" -> test !== and !!=");
		this.println(" ------------------------");
		i = 20;
		this.println(" Print 20..11");
		this.print(" ");
		while(!(i == 10)) { 
			printInt(i); 
			this.print(" "); 
			i = i-1;
		}
		this.println(""); this.println("");
		
		i = 0;
		this.println(" Print 0");
		this.print(" ");
		while(!(i != 0)) { 
			printInt(i); 
			this.print(" "); 
			i = i+1;
		}
		this.println(""); this.println("");
	
		
		this.println(" ------------------------");
		this.println(" -> test !< and !>");
		this.println(" ------------------------");
		i = 20;
		this.println(" Print 20..10");
		this.print(" ");
		while(!(i < 10)) { 
			printInt(i); 
			this.print(" "); 
			i = i-1;
		}
		this.println(""); this.println("");
		
		i = -10;
		this.println(" Print -10..0");
		this.print(" ");
		while(!(i > 0)) { 
			printInt(i); 
			this.print(" "); 
			i = i+1;
		}
		this.println(""); this.println("");
		
		this.println(" ------------------------");
		this.println(" -> test !<= and !>=");
		this.println(" ------------------------");
	  i = 20;
		this.println(" Print 20..11");
		this.print(" ");
		while(!(i <= 10)) { 
			printInt(i); 
			this.print(" "); 
			i = i-1;
		}
		this.println(""); this.println("");
	
		i = -10;
		this.println(" Print -10..-1");
		this.print(" ");
		while(!(i >= 0)) { 
			printInt(i); 
			this.print(" "); 
			i = i+1;
		}
		this.println(""); this.println("");

	}
	
	Null main() {
		this.println("*************************");
		this.println("*** Play with Compop  ***");
		this.println("*************************");	
		this.test();
	}


}

class Test {
	Int a;
}


{
	new Names().main();
	printChar(10);
	new Reals().main();
	printChar(10);
	new Compop().main();
}