Friday, March 30, 2007

C#: using the prev LinkList

LinkList<Dog> list = new LinkList<Dog>();
list.addLast(new Dog("Rocky", 16));
list.addLast(new Dog("Waggie", 6));
list.remove(0);
list.get(0).bark();

C#: reading text files

using System;
using System.IO;

namespace FileIOText
{
class ReadTextFile
{
public ReadTextFile()
{
try
{
//-- "using" also closes the file
using (StreamReader sr = new StreamReader("data.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch(Exception e){
Console.WriteLine("File could not be read");
Console.WriteLine(e.Message);
}
}
}
}

C#: reading binary file

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace FileIOText
{
class ReadBinaryFile
{
const String filename = "binfile.dat";
public ReadBinaryFile()
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
/*
//create reader
BinaryReader br = new BinaryReader(fs);

Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadDouble());
Console.WriteLine(br.ReadBoolean());
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadChar());

br.Close();
*/

BinaryFormatter formatter = new BinaryFormatter();
Dog d = (Dog)formatter.Deserialize(fs);
Console.WriteLine(d.Name);
Console.WriteLine(d.Age);
fs.Close();
}
}
}

C#: writing to text file

using System;
using System.IO;

namespace FileIOText
{
class WriteTextFile
{
public WriteTextFile()
{
try
{
//-- "using" also closes the file
using (StreamWriter sw = new StreamWriter("data.txt"))
{
sw.Write("Hello...");
sw.WriteLine("This is the start of the writing...");
sw.WriteLine("------------------");
//-- write arbitrary objects
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
catch (Exception e)
{
Console.WriteLine("File could not be written");
Console.WriteLine(e.Message);
}
}
}
}

C#: Writing binary file

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace FileIOText
{
class WriteBinaryFile
{
const String filename = "binfile.dat";
public WriteBinaryFile()
{
if(File.Exists(filename)){
Console.WriteLine("{0} already exists!", filename);
return;
}
FileStream fs = new FileStream(filename, FileMode.CreateNew);
/*
//create writer
BinaryWriter bw = new BinaryWriter(fs);

//create test data to write to file
int i = 299;
double d = 2.5999;
bool b = false;
String s = "Haha";
char c = 'A';

bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
bw.Write(c);
*/

Dog d = new Dog("Rocky", 16);
Dog e = new Dog("Waggie", 10);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, d);
formatter.Serialize(fs, e);
//bw.Close();
fs.Close();
}
}
}

C#: LinkList

using System;
using System.Collections.Generic;
using System.Text;

namespace List
{
[Serializable]
class LinkList<T>
{
int numOfElements = 0;
ListNode<T> headNode = null;
public LinkList() { }
public LinkList(T data) {
headNode = new ListNode<T>(data);
}
public bool isEmpty()
{
return (numOfElements == 0);
}
public int getNumOfElements()
{
return numOfElements;
}
public ListNode<T> find(int index)
{
ListNode<T> node = headNode;
if(index < 0 || index >= numOfElements){
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
for (int i = 0; i < index; i++ )
{
node = node.Next;
}
return node;
}
public T get(int index)
{
/*
if (index >= 0 && index < numOfElements)
{
return find(index).Data;
}*/
return find(index).Data;
}
public void add(int index, T data)
{
if (index == 0)
{
ListNode<T> newNode = new ListNode<T>(data, headNode);
headNode = newNode;
}
else if (index > 0 && index <= numOfElements)
{
ListNode<T> prevNode = find(index - 1);
ListNode<T> newNode = new ListNode<T>(data, prevNode.Next);
prevNode.Next = newNode;
}else{
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
numOfElements++;
}
public void addLast(T data)
{
add(getNumOfElements(), data);
}
public void remove(int index)
{
if (index == 0)
{
headNode = headNode.Next;
}
else if (index > 0 && index < numOfElements)
{
ListNode<T> prev = find(index - 1);
ListNode<T> current = prev.Next;
prev.Next = current.Next;
}
else
{
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
numOfElements--;
}
public void removeLast()
{
remove(numOfElements - 1);
}
}
}

C#: ListNode

using System;
using System.Collections.Generic;
using System.Text;

namespace List
{
[Serializable]
class ListNode<T>
{
ListNode<T> next = null; //-- self referencing node
T data; //-- data segment
public ListNode(T t)
{
data = t;
}
public ListNode(T t, ListNode<T> n):this(t) //-- call the prev constructor 1st
{
//-- then proceed to following line
next = n;
}
public T Data{
get { return data; }
set { data = value; }
}
public ListNode<T> Next
{
get { return next; }
set { next = value; }
}
}
}

Thursday, March 29, 2007

NTU Assignment Demos

Inverse Kinematics using Flash
Fluid Simulation using Flash




http://psalmhundred.net/NTU/

My favourite: the fluid simulation one. : )