Next Generation Emulation banner

Post the code written by you thread. :D

65K views 405 replies 36 participants last post by  @ruantec  
#1 ·
After i learn C# for two weeks here is my first try.:lol:
What it do is: it ask 1+2=?
if you answer 3 it gives "Congrategulation you are right"
if not it ask you to continue:Y or N
if press Y it return to 1+2=? again
if you press other later than Y and N it return to Y or N.
:)


Code:
char b;
            int a;
            begin:
            Console.WriteLine("1+2=?");
            a=Int32.Parse(Console.ReadLine());
            switch (a)
            {case 3:
                    Console.WriteLine("Congrategulation you are right");break;
                default:
                    Console.WriteLine("wrong");break;}
       
            Console.WriteLine("Y or N");

            b=char.Parse(Console.ReadLine());
  continue1:           
            switch(b)
            {case 'Y':
            goto begin;
            
                case 'N':
            Console.WriteLine("bye"); break;

               
                
                default:

goto continue1;
                   
                    break;
                Console.WriteLine("Only Y and N is inputed");
}
 
#3 · (Edited)
nice you´re learning a programming language raksmey1309!

about your code... just as Dax mentioned the use of a loop will fit better in this case.. i noticed you´re doing your best to accomplish the job however its probably not the right way to do it.. what´s the right way??? there are many :p i would suggest you to use a bool variable that by default is "False" this variable is used in a "While" loop for example and its used to loop through the code until its true.

now your job is to write the necessary stuff in your code to check whatever the result is correct or not and if is correct then just set the bool variable to True and exit the loop... however you should use loops wisely as they could lead you to many problems/performance problems in many areas depending on the way you use them.
 
#4 · (Edited)
Here's how I would do it(Spoiler tags, because if this is a homework assignment, you shouldn't be using it! :p):

No error checking, btw. I wanted to keep it simple.

Code:
        static void Main(string[] args)
        {
            bool done = false;

            while (!done)
            {
                Console.WriteLine("1 + 2 = ?");

                if (int.Parse(Console.ReadLine()) != 3)
                    Console.WriteLine("Incorrect. Try again! :)");
                else
                {
                    Console.WriteLine("Correct! Good job. Quit? Y/N");
                    if (Console.ReadLine() == "Y".ToLower())
                        done = true;
                }
            }
        }
 
#5 · (Edited)
Heyyyyyyyyyyy!!! that´s exactly what i explained on my post :p

since this is a thread to post code written by us i think i should post one of the codes i wrote recently here... i can do it better but the deadline its just too short to mess around with that :lol: :lol:
Code:
public static List<Werkstoffpruefung> getTeilPruefungen(Teil teil,bool add_werkstoffe)
        {
            List<Werkstoffpruefung> wpruefungen = new List<Werkstoffpruefung>();

            if (teil != null)
            {
                /*get data from our 'Teil'*/
                PruefungsPaket paket = DBService.getPruefungsPaket(teil);
                if (paket != null)
                    teil.PruefungsPaket = paket;
                else
                    teil.PruefungsPaket = null;

                if (teil.PruefungsPaket != null && teil.PruefungsPaket.WerkstoffPruefungen != null)
                {
                    foreach (KeyValuePair<int, Werkstoffpruefung> werkprue in teil.PruefungsPaket.WerkstoffPruefungen)
                    {
                        Werkstoffpruefung pruefung = teil.PruefungsPaket.WerkstoffPruefungen[werkprue.Key];
                        if (!pruefung.Messergebnis.IsNull || !pruefung.MessergebnisChar.IsNull)
                            pruefung.ContainsData = true;
                        //pruefung.ContainsData = DBService.checkPruefungsErgebnis(pruefung.WerkstoffPruefungsID);
                        pruefung.Pruefungsergebnisse = DBService.getPruefungsergebnisse(ObjectType.WerkstoffPruefungen, pruefung.PruefungsID);

                        wpruefungen.Add(pruefung);
                    }
                }
            }

            /*get data from 'Werkstoffe'*/
            if (teil.Werkstoffe != null && add_werkstoffe == true)
            {
                foreach (Werkstoff werkstoff in teil.Werkstoffe)
                {
                    TeilWerkstoffe teilw = DBService.getTeilwerkstoffe(teil.TeileID, werkstoff.WerkstoffID);
                    PruefungsPaket paket = DBService.getPruefungsPaket(ObjectType.Werkstoffe, teilw.TeilewerkstoffID);

                    if(paket != null && paket.PruefungsRelevant == 1 && paket.WerkstoffPruefungen != null)
                    {
                        foreach (KeyValuePair<int, Werkstoffpruefung> werkprue in paket.WerkstoffPruefungen)
                        {
                            Werkstoffpruefung pruefung = paket.WerkstoffPruefungen[werkprue.Key];
                            if (!pruefung.Messergebnis.IsNull || !pruefung.MessergebnisChar.IsNull)
                                pruefung.ContainsData = true;
                            //pruefung.ContainsData = DBService.checkPruefungsErgebnis(pruefung.WerkstoffPruefungsID);
                            pruefung.Pruefungsergebnisse = DBService.getPruefungsergebnisse(ObjectType.WerkstoffPruefungen, pruefung.PruefungsID);

                            wpruefungen.Add(pruefung);
                        }
                    }
                }
            }

            return wpruefungen;
        }
 
#6 ·
Here's a "version 2.0" of the previous code(I added basic error checking and removed magic numbers):

Code:
        static void Main(string[] args)
        {
            bool done = false;
            string input = "";
            int number = 0, answer = 3;

            while (!done)
            {
                Console.WriteLine("1 + 2 = ?");

                input = Console.ReadLine();

                if (int.TryParse(input, out number))
                {
                    if (number != answer)
                    {
                        Console.WriteLine("Incorrect. Try again! :)");
                    }
                    else
                    {
                        Console.WriteLine("Correct! Good job.");

                        while (input != "Y".ToLower() && input != "N".ToLower())
                        {
                            Console.WriteLine("Quit? Y/N");

                            input = Console.ReadLine();
                        }
                        if (input == "Y".ToLower())
                        {
                            done = true;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Please enter a number, not letters, or a mix.");
                }
            }
        }
 
#7 · (Edited)
But i not learnt loop yet

Dax but teacher told me the use of switch in console is more elegant than else if
 
#8 · (Edited)
But i not learnt loop yet

Dax but teacher told me the use of switch in console is more elegant than else if
switch is more elegant when you´re checking multiple cases of the same type like numbers or whatever.

for example:
Code:
switch(number)
{
	case 1:
	/*do something*/ 
	break;
	
	case 2:
	/*do something*/ 
	break;

	case Default:
	/*do something*/
	break;
}
the "if" equivalent would be:
Code:
if(number == 1)
	/*do something*/
else if(number == 2)
	/*do something*/
else
	/*do something*/
if you´re checking just 1 i would use "if" but that´s my opinion. anyways in case you want to know more about loops here is my old C# course which is going to be re-vived and re-worked soon.
 
#9 ·
Thank u techno man :D how many languge do u know
 
#11 ·
My teacher told me c# is the best so i skip c++ in year 1 last year i am yet to learn java in year 3 and vb in year 4 all 4 years no asp and other like yours am i going the right direction
 
#12 ·
My teacher told me c# is the best so i skip c++ in year 1 last year i am yet to learn java in year 3 and vb in year 4 all 4 years no asp and other like yours am i going the right direction
mmm C# is one of the best but i doubt its the best(at least for me). it actually pwns other languages in some areas but still need some more additions/changes.. anyways i wouldn´t skip C++ at all.. i recomend you to learn some C/C++ still at a later time if you wish. lots of code and stuff nowdays are done using pure C or just C++ and they may help you too. ASP.Net is nice too atm i master that language and it really allows you to do awesome stuff and by using AJAX and some Javascript you can do a lot of fancy stuff aswell... but as any other languages it has downsides too.

eitherway C# is a great language and quite usefull nowdays but it still need some changes and thanks God the team behind is working on it day by day so a lot of things may change in the future i guess...
 
#13 ·
Yeah, because people just love to read random code written by other people for who knows what.

Corollary:
I think if you have a particular question about some feature you should create a properly tagged thead.

ASP.Net is nice too atm i master that language and it really allows you to do awesome stuff and by using AJAX and some Javascript you can do a lot of fancy stuff aswell... but as any other languages it has downsides too.
Well yeah, and Prolog is totally awesome, and Cobol is the r0x0rz... sad point is that they are application specific language (dynamic web pages/artificial intelligence/business servers) that people won't use unless they want to do something related to that application, and that however awesome we think they are people won't be interested into hearing our praises unless they are interested into the application in question.

My teacher told me c# is the best so i skip c++ in year 1 last year i am yet to learn java in year 3 and vb in year 4 all 4 years no asp and other like yours am i going the right direction
Well, as aruantek mentioned (what? wow! me agreeing with aruantek) this is not necessarely true. First of all, what is your major/what are you interested into?
 
#14 · (Edited)
btw my nick is @ruantec proto :p lots of people writes my nick wrong lol... i´ve seen several variations such as "ruantic", "@rauntec", "@runtec" and now yours "aruantek" :p nice!!!! i have to admit i like your more as it sounds somewhat German hehehehehe
 
#15 ·
I am done major in economic management now i am coputer science major in programming in year 2 now
 
#16 · (Edited)
I am done major in economic management now i am coputer science major in programming in year 2 now
Nice!

in case it helps........
in my opinion Every language is "The Best" at something... your task is to choose the language you like and the right one for the right task. that´s why i suggest to try different ones and not skip C/C++ at all as you can compare them and choose the one that fits your needs. teachers always recommend what they like or preffer but it doesn´t need to be the right choice for you as there point of view is mostly focused on what they teach.

now you probably may ask why do @ruantec choose C# now and not C/C++?
the reason i choose C# atm is because is a more balanced language which is something i really missed in C/C++... yes i have power but power alone is not everything.. in fact the fastest GT car can´t garantee you to win a rally race against a mitsubishi evo.. C# in other hands even having slightly less power in some areas it still offers a confortable to use syntax and also productivity is quite awesome and that is what i like most... however that doesn´t mean C# is the right language for you. C/C++ are great languages and quite powerfull and were the language of choice for me in the past.. there main problem actually is productivity and some other stuff not to mention i just got tired of the overrated syntax... still that doesn´t make C/C++ bad and its for sure worth the time you spend learning those.

in any case i will recommend not to skip C/C++ by any means... but again the choice is yours :thumb:
 
#17 ·
my almost finished homework

written with some somewhat sloppy java, still cleaning it up.

It's done except I need to rewrite to support O(n) set operations instead of O(n log(n))

Code:
package redblacksets;

import java.util.Collection;
import java.util.Iterator;

public class RedBlackSet<E extends Comparable<E>>
{
	private RedBlackNode<E> root;
	private boolean needsHeightAdjust;
	
	public RedBlackSet()
	{
		root = null;
	}
	
	public RedBlackSet(E value)
	{
		root = null;
		insert(value);
	}
	
	public RedBlackSet(Collection<E> c)                 
	{
		Iterator<E> i = c.iterator();
		root = null;
		while(i.hasNext())
		{
			insert(i.next());
		}
	}
	
	public void insert(Collection<E> c)
	{
		Iterator<E> i = c.iterator();
		while(i.hasNext())
		{
			insert(i.next());
		}
		if(root.colour == Colour.RED)
			root.colour = Colour.BLACK;
	}
	
	public void insert(E value)
	{
		root = recInsert(value, root, null);
		if(root.colour == Colour.RED)
			root.colour = Colour.BLACK;
	}
	
	public String toString()
	{
		if(root == null)
			return "null";
		else return root.toString();
	}
	
	public RedBlackSet<E> union(RedBlackSet<E> set)
	{
		RedBlackSet<E> ret = new RedBlackSet<E>();
		RedBlackIterator<E> i1 = getIterator();
		RedBlackIterator<E> i2 = set.getIterator();
		if(this.root == null)
			return set.clone();
		else if(set.root == null)
			return this.clone();
		E current1 = i1.next(), current2 = i2.next();
		while(i1.hasNext() && i2.hasNext())
		{
			if(current1.equals(current2))
			{
				ret.insert(current1);
				current1 = i1.next();
				current2 = i2.next();
			}
			else if(current1.compareTo(current2) < 0)
			{
				ret.insert(current1);
				current1 = i1.next();
			}
			else
			{
				ret.insert(current2);
				current2 = i2.next();
			}
		}
		if(!ret.member(current1))
			ret.insert(current1);
		if(!ret.member(current2))
			ret.insert(current2);
		while(i1.hasNext())
		{
			ret.insert(i1.next());
		}
		while(i2.hasNext())
		{
			ret.insert(i2.next());
		}
		return ret;
	}
	
	public RedBlackSet<E> difference(RedBlackSet<E> set)
	{
		RedBlackSet<E> ret = new RedBlackSet<E>();
		RedBlackIterator<E> i1 = getIterator();
		E current = i1.next();
		while(i1.hasNext())
		{
			if(!set.member(current))
				ret.insert(current);
			current = i1.next();
		}
		return ret;
	}
	
	public RedBlackSet<E> symmetricDifference(RedBlackSet<E> set)
	{
		RedBlackSet<E> ret = new RedBlackSet<E>();
		RedBlackIterator<E> i1 = getIterator();
		RedBlackIterator<E> i2 = set.getIterator();
		if(this.root == null)
			return set.clone();
		else if(set.root == null)
			return this.clone();
		E current1 = i1.next(), current2 = i2.next();
		while(i1.hasNext() && i2.hasNext())
		{
			if(current1.equals(current2))
			{
				current1 = i1.next();
				current2 = i2.next();
			}
			else if(current1.compareTo(current2) < 0)
			{
				ret.insert(current1);
				current1 = i1.next();
			}
			else
			{
				ret.insert(current2);
				current2 = i2.next();
			}
		}
		if(!ret.member(current1))
			ret.insert(current1);
		if(!ret.member(current2))
			ret.insert(current2);
		while(i1.hasNext())
		{
			ret.insert(i1.next());
		}
		while(i2.hasNext())
		{
			ret.insert(i2.next());
		}
		return ret;
	}
	
	public RedBlackSet<E> intersection(RedBlackSet<E> set)
	{
		RedBlackSet<E> ret = new RedBlackSet<E>();
		RedBlackIterator<E> i1 = getIterator();
		RedBlackIterator<E> i2 = set.getIterator();
		if(this.root == null || set.root == null)
			return ret;
		E current1 = i1.next(), current2 = i2.next();
		while(i1.hasNext() && i2.hasNext())
		{
			if(current1.equals(current2))
			{
				ret.insert(current1);
				current1 = i1.next();
				current2 = i2.next();
			}
			else if(current1.compareTo(current2) < 0)
			{
				current1 = i1.next();
			}
			else
			{
				current2 = i2.next();
			}
		}
		return ret;
	}
	
	public RedBlackSet<E> clone()
	{
		RedBlackSet<E> temp = new RedBlackSet<E>();
		if(root == null)
			return temp;
		RedBlackIterator<E> i = getIterator();
		while(i.hasNext())
			temp.insert(i.next());
		return temp;
	}
	/**
	 * Recursively inserts into a Red Black Tree
	 * @param value the value to be inserted
	 * @param node the node to insert into
	 * @return the new tree
	 */
	public RedBlackNode<E> recInsert(E value, RedBlackNode<E> node, RedBlackNode<E> parent)
	{
		if(node == null)
		{
			node = new RedBlackNode<E>(value, Colour.RED, parent);
		}
		else
		{
			if(node.equals(value))
				return node;
			else if(value.compareTo(node.value) < 0)	//value less than node, insert into left subtree
			{
				if(node.left == null || node.left.colour == Colour.BLACK)
					node.left = recInsert(value, node.left, node);
				else
				{
					if(node.left.equals(value))
						return node;
					else if(value.compareTo(node.left.value) < 0)	//insert into left grandchild
					{
						node.left.left = recInsert(value, node.left.left, node.left);
						if(checkRotate(Rotate.LL, node.left))
						{
							rotate(Rotate.LL, node);
						}
					}
					else //insert into right grandchild
					{
						node.left.right = recInsert(value, node.left.right, node.left);
						if(checkRotate(Rotate.LR, node.left))
						{
							rotate(Rotate.LR, node);
						}
					}
				}
			}
			else if(value.compareTo(node.value) > 0)	//value greater than node, insert into right subtree
			{
				if(node.right == null || node.right.colour == Colour.BLACK)
					node.right = recInsert(value, node.right, node);
				else
				{
					if(node.right.equals(value))
						return node;
					else if(value.compareTo(node.right.value) < 0)	//insert into left grandchild
					{
						node.right.left = recInsert(value, node.right.left, node.right);
						if(checkRotate(Rotate.RL, node.right))
						{
							rotate(Rotate.RL, node);
						}
					}
					else //insert into right grandchild
					{
						node.right.right = recInsert(value, node.right.right, node.right);
						if(checkRotate(Rotate.RR, node.right))
						{
							rotate(Rotate.RR, node);
						}
					}
				}
			}
		}
		return node;
	}
	
	/**
	 * Tests if a rotate of a certain type is necessary
	 * @param type Rotate type
	 * @param start check for a rotate at this node
	 * @return true if rotate is needed, otherwise false
	 */
	private boolean checkRotate(Rotate type, RedBlackNode<E> start)
	{
		switch(type)
		{
		case LL:
			{
				if(start.colour == Colour.RED && start.left.colour == Colour.RED)
					return true;
				return false;
			}
		case LR:
		{
			if(start.colour == Colour.RED && start.right.colour == Colour.RED)
				return true;
			return false;
		}
		case RL:
		{
			if(start.colour == Colour.RED && start.left.colour == Colour.RED)
				return true;
			return false;
		}
		case RR:
		{
			if(start.colour == Colour.RED && start.right.colour == Colour.RED)
				return true;
			return false;
		}
		default: return false;
		}
	}
	
	/**
	 * performs a rotate after insertion
	 * @param type type of rotate to perform
	 * @param start starting point
	 */
	private void rotate(Rotate type, RedBlackNode<E> start)
	{
		switch(type)
		{
		case LL:
			{
				RedBlackNode<E> temp = start.clone();
				temp.parent = start;
				if(temp.right != null)
					temp.right.parent = temp;
				temp.left = start.left.right;
				if(temp.left != null)
					temp.left.parent = temp;
				start.value = start.left.value;
				start.right = temp;
				start.left = start.left.left;
				start.left.parent = start;
			}break;
		case LR:
			{
				RedBlackNode<E> temp = start.clone();
				RedBlackNode<E> temp2 = start.left.clone();
				temp.parent = start;
				temp2.parent = start;
				if(temp.right != null)
					temp.right.parent = temp;
				temp.left = start.left.right.right;
				if(temp.left != null)
					temp.left.parent = temp;
				if(temp2.left != null)
					temp2.left.parent = temp2;
				temp2.right = start.left.right.left;
				if(temp2.right != null)
					temp2.right.parent = temp2;
				start.value = start.left.right.value;
				start.left = temp2;
			} break;
		case RR:
		{
			RedBlackNode<E> temp = start.clone();
			temp.parent = start;
			if(temp.left != null)
				temp.left.parent = temp;
			temp.right = start.right.left;
			if(temp.right != null)
				temp.right.parent = temp;
			start.value = start.right.value;
			start.left = temp;
			start.right = start.right.right;
			start.right.parent = start;
		}break;
		case RL:
		{
			RedBlackNode<E> temp = start.clone();
			RedBlackNode<E> temp2 = start.left.clone();
			temp.parent = start;
			temp2.parent = start;
			if(temp.left != null)
				temp.left.parent = temp;
			temp.right = start.right.left.left;
			if(temp.right != null)	
				temp.right.parent = temp;
			if(temp2.right != null)
				temp2.right.parent = temp2;
			temp2.left = start.right.left.right;
			if(temp2.left != null)
				temp2.left.parent = temp2;
			start.value = start.right.left.value;
			start.right = temp2;
		} break;
		}
		start.left.colour = Colour.BLACK;
		start.colour = Colour.RED;
		start.right.colour = Colour.BLACK;
	}
	
	public boolean member(E value)
	{
		RedBlackNode<E> node = root;
		while(node != null)
		{
			if(node.value.equals(value))
				return true;
			else if(value.compareTo(node.value) < 0)
				node = node.left;
			else node = node.right;
		}
		return false;
	}
	
	public void delete(E value)
	{
		root = delete(value, root);
		assert root.colour == Colour.BLACK;
		assert checkColour(root);
		assert checkBlackHeight(root);
	}
	
	private boolean checkBlackHeight(RedBlackNode<E> node)
	{
		if(node == null) return true;
		int left = 0;
		RedBlackNode<E> temp = node.clone();
		while(temp.left != null)
		{
			temp = temp.left;
			if(temp.colour == Colour.BLACK)
				left++;
		}
		temp = node.clone();
		int right = 0;
		while(temp.right != null)
		{
			temp = temp.right;
			if(temp.colour == Colour.BLACK)
				right++;
		}
		return (right==left)&&checkBlackHeight(node.left)&&checkBlackHeight(node.right);
	}
	
	private RedBlackNode<E> delete(E value, RedBlackNode<E> node)
	{
		if(node == null)
		{
			needsHeightAdjust = false;
			return node;
		}
		if(node.value.equals(value))
		{
			boolean left = false;
			if(node != root) left = (node.parent.value.compareTo(node.value) > 0);
			node = deleteThisNode(node);
			node = rebalance(node,left);
		}
		else if(value.compareTo(node.value) < 0)
		{
			node.left = delete(value, node.left);
			rebalance(node, true);
		}
		else
		{
			node.right = delete(value, node.right);
			rebalance(node, false);
		}
		
		return node;
	}
	
	private RedBlackNode<E> deleteThisNode(RedBlackNode<E> node)
	{
		if(node.left == null && node.right == null)	//adjustment required if node is black
		{
			needsHeightAdjust = (node.colour == Colour.BLACK);
			node = null;
		}
		else if(node.left != null && node.right == null)	//no adjustment required
		{
			node.value = node.left.value;
			node.left = node.left.left; 
			node.right = node.left.right;
			if(node.left != null) node.left.parent = node;
			if(node.right != null) node.right.parent = node;
			needsHeightAdjust = false;
		}
		else if(node.left == null && node.right != null)	//no adjustment required
		{
			node.value = node.right.value;
			node.left = node.right.left; 
			node.right = node.right.right;
			if(node.left != null) node.left.parent = node;
			if(node.right != null) node.right.parent = node;
			needsHeightAdjust = false;
		}
		else 
		{	
			node.value = retLeftMost(node.right);
			node.right = deleteLeftMost(node.right);
			rebalance(node, false);
		}
		return node;
	}
	
	private E retLeftMost(RedBlackNode<E> node)
	{
		if(node.left == null)
		{
			return node.value;
		}
		else return retLeftMost(node.left);
	}
	private RedBlackNode<E> deleteLeftMost(RedBlackNode<E> node)
	{
		if(node.left == null)
		{
			node = deleteThisNode(node);
			rebalance(node, true);
			return node;
		}
		else
		{	
			node.left = deleteLeftMost(node.left);
			rebalance(node, true);
			return node;
		}
	}
	private RedBlackNode<E> rebalance(RedBlackNode<E> node, boolean isleft)
	{
		if(!needsHeightAdjust)
			return node;
		else if(node == null)
			return node;
		if(isleft)
		{
			if(node.right != null && node.right.colour == Colour.RED)
			{
				RedBlackNode<E> temp = node.clone();
				if(temp.left != null)
				{
					temp.left.parent = temp;
				}
				temp.right = node.right.left;
				if(temp.right != null)
					temp.right.parent = temp;
				node.value = node.right.value;
				node.colour = Colour.BLACK;
				temp.colour = Colour.RED;
				node.left = temp;
				temp.parent = node;
				node.right = node.right.right;
				if(node.right != null)
					node.right.parent = node;
				rebalance(temp, true);
				rebalance(node, true);
			}
			else if(node.right != null && node.right.colour == Colour.BLACK && 
					(node.right.left == null || node.right.left.colour == Colour.BLACK) && 
					(node.right.right == null || node.right.right.colour == Colour.BLACK))
			{
				node.right.colour = Colour.RED;
				if(node.colour == Colour.RED)
				{
					node.colour = Colour.BLACK;
					needsHeightAdjust = false;
				}
			}
			else if(node.right != null && node.right.colour == Colour.BLACK &&
					(node.right.left == null || node.right.left.colour == Colour.BLACK))
			{
				RedBlackNode<E> temp = node.clone();
				temp.right = node.right.left;
				if(temp.right != null)
					temp.right.parent = temp;
				temp.parent = node;
				temp.colour = Colour.BLACK;
				node.value = node.right.value;
				node.left = temp;
				node.right = node.right.right;
				node.right.parent = node;
				node.right.colour = Colour.BLACK;
				needsHeightAdjust = false;
			}
			else if(node.right != null && node.right.colour == Colour.BLACK &&
					node.right.left.colour == Colour.RED)
			{
				RedBlackNode<E> temp = node.clone();
				temp.colour = Colour.BLACK;
				temp.right = node.right.left.left;
				if(temp.right != null)
					temp.right.parent = temp;
				node.value = node.right.left.value;
				temp.parent = node;
				node.left = temp;
				node.right.left = node.right.left.right;
				if(node.right.left != null)
					node.right.left.parent = node.right;
				needsHeightAdjust = false;
			}
		}
		else
		{
			if(node.left != null && node.left.colour == Colour.RED)
			{
				RedBlackNode<E> temp = node.clone();
				if(temp.right != null)
				{
					temp.right.parent = temp;
				}
				temp.left = node.left.right;
				if(temp.left != null)
					temp.left.parent = temp;
				node.value = node.left.value;
				node.colour = Colour.BLACK;
				temp.colour = Colour.RED;
				node.right = temp;
				temp.parent = node;
				node.left = node.left.left;
				if(node.left != null)
					node.left.parent = node;
				rebalance(temp, false);
				rebalance(node, false);
			}
			else if(node.left != null && node.left.colour == Colour.BLACK && 
					(node.left.left == null || node.left.left.colour == Colour.BLACK) && 
					(node.left.right == null || node.left.right.colour == Colour.BLACK))
			{
				node.left.colour = Colour.RED;
				if(node.colour == Colour.RED)
				{
					node.colour = Colour.BLACK;
					needsHeightAdjust = false;
				}
			}
			else if(node.left != null && node.left.colour == Colour.BLACK &&
					(node.left.left == null || node.left.left.colour == Colour.BLACK))
			{
				RedBlackNode<E> temp = node.clone();
				temp.left = node.left.right;
				temp.colour = Colour.BLACK;
				if(temp.left != null)
					temp.left.parent = temp;
				temp.parent = node;
				node.value = node.left.value;
				node.right = temp;
				node.left = node.left.left;
				node.left.parent = node;
				node.left.colour = Colour.BLACK;
				needsHeightAdjust = false;
			}
			else if(node.left != null && node.left.colour == Colour.BLACK &&
					node.left.left.colour == Colour.RED)
			{
				RedBlackNode<E> temp = node.clone();
				temp.left = node.left.right.right;
				temp.colour = Colour.BLACK;
				if(temp.left != null)
					temp.left.parent = temp;
				node.value = node.left.right.value;
				temp.parent = node;
				node.right = temp;
				node.left.right = node.left.right.left;
				if(node.left.right != null)
					node.left.right.parent = node.left;
				needsHeightAdjust = false;
				
			}
		}
		return node;
	}
	
	private boolean checkColour(RedBlackNode<E> node)
	{
		if(node == null) return true;
		if(node.left == null && node.right == null)
		{
			return true;
		}
		else if(node.colour == Colour.RED && 
				((node.left != null && node.left.colour == Colour.RED) || 
						(node.right != null && node.right.colour == Colour.RED)))			
		{
			return false;
		}
		
		return checkColour(node.left)&&checkColour(node.right);
	}
	
	/**
	 * Returns an iterator for the set.
	 * @return an iterator for the current tree or null if the tree is null
	 */
	public RedBlackIterator<E> getIterator()
	{
		if(root == null)
			return null;
		return new RedBlackIterator<E>(this);
	}
	/**
	 * 
	 * @author Anthony McCann 230077439
	 *
	 * @param <T> The type of object beign stored in the node, must be Comparable
	 * 
	 * Red Black Node, the nodes store the values and from the tree
	 */
	private static class RedBlackNode<T extends Comparable<T>>
	{
		private RedBlackNode<T> left;
		private RedBlackNode<T> right;
		private RedBlackNode<T> parent;
		private Colour colour;
		private T value;
		
		public RedBlackNode(T value, Colour colour, RedBlackNode<T> parent)
		{
			this.value = value;
			this.colour = colour;
			this.parent = parent;
			left = null;
			right = null;
		}
		public RedBlackNode<T> clone()
		{
			RedBlackNode<T> temp = new RedBlackNode<T>(value, colour, parent);
			temp.left = left;
			temp.right = right;
			return temp;
		}
		
		public String toString()
		{
			String ret = "(";
			ret += "(" + value.toString() + ", " + colour.toString() + ") ";
			if(left != null)
				ret += " " + left.toString();
			else ret += " null";
			if(right != null)
				ret += " " + right.toString();
			else ret += " null";
			return ret += ")";
		}
	}
	
	public static class RedBlackIterator<T extends Comparable<T>>
	{
		private java.util.Stack<RedBlackNode<T>> stack;
		public RedBlackIterator(RedBlackSet<T> set)
		{
			stack = new java.util.Stack<RedBlackNode<T>>();
			pushLeft(set.root);
		}
		private void pushLeft(RedBlackNode<T> node)
		{
			stack.push(node);
			if(node.left != null)
				pushLeft(node.left);
		}
		
		public T next()
		{
			RedBlackNode<T> temp = stack.pop();
			if(temp.right != null)
				pushLeft(temp.right);
			return temp.value;
		}
		
		public boolean hasNext()
		{
			return !stack.isEmpty();
		}
	}
	private static enum Colour
	{
		RED, BLACK;
	}
	private static enum Rotate
	{
		LL,LR,RR,RL;
	}
}
oh and obviously going to be commenting a lot more
 
#18 ·
Here's how I would do it(Spoiler tags, because if this is a homework assignment, you shouldn't be using it! :p):

No error checking, btw. I wanted to keep it simple.

Code:
        static void Main(string[] args)
        {
            bool done = false;

            while (!done)
            {
                Console.WriteLine("1 + 2 = ?");

                if (int.Parse(Console.ReadLine()) != 3)
                    Console.WriteLine("Incorrect. Try again! :)");
                else
                {
                    Console.WriteLine("Correct! Good job. Quit? Y/N");
                    if (Console.ReadLine() == "Y".ToLower())
                        done = true;
                }
            }
        }
static void Main(string[] args) {
run();
}

static void run() {
Console.WriteLine("1 + 2 = ?");
if (int.Parse(Console.ReadLine()) != 3) Console.WriteLine("Incorrect. Try again! :)");
else Console.WriteLine("Correct! Good job. Quit? Y/N");
if (Console.ReadLine() == "Y".ToLower()) run();
}

I don't know if that's legal or not since I don't even know C# to begin with... but... I'd try it that way. :p Helps you learn about functions, too, and... it eliminates the loop and the loop check.
 
#19 ·
Well, in that case here is a CUDA function i just finished i few hours ago

Code:
__global__ static void combination(int* molecularPopulation,cType* reactionConstants,const int reduceMP,cType* result){
	extern __shared__ int2 combi[];
	const int block = (blockIdx.x);
	const int  reaction = block * blockDim.x + threadIdx.x;
	if(reaction >= constrc)
		return;
	int factor1 = floor(reaction*0.25);
	int factor2 = (reaction%4);
	int maxFactor = blockDim.x *0.25;
	cType loc = 1;
//	int2* inputRow2 = (int2*)((char*)dependencyMatrix + dependencyPitch* factor1);
	combi[factor1*4+factor2] = (factor2 < reduceMP) ? tex2D(dependencyMatrix2Texture,factor2,factor1) : make_int2(-1,0);
	
	factor1 += maxFactor;
	combi[factor1*4+factor2] = (factor2 < reduceMP) ? tex2D(dependencyMatrix2Texture,factor2,factor1) : make_int2(-1,0);
	
	factor1 += maxFactor;
	combi[factor1*4+factor2] = (factor2 < reduceMP) ? tex2D(dependencyMatrix2Texture,factor2,factor1) : make_int2(-1,0);
	
	factor1 += maxFactor;
	combi[factor1*4+factor2] = (factor2 < reduceMP) ? tex2D(dependencyMatrix2Texture,factor2,factor1) : make_int2(-1,0);
	__syncthreads();

	const cType molpol = ((combi[threadIdx.x*4]).y<0 && (combi[threadIdx.x*4]).x >= 0) ? (cType)molecularPopulation[(combi[threadIdx.x*4]).x] : 1.0f;
	loc *= molpol;
	if((combi[threadIdx.x*4]).y <= -2)
		loc *= (molpol-1)/0.5;
	
	const cType molpol2 = ((combi[threadIdx.x*4 + 1]).y<0 && (combi[threadIdx.x*4 + 1]).x >= 0) ? (cType)molecularPopulation[combi[threadIdx.x*4 + 1].x] : 1.0f;
	loc *= molpol2;
	loc *= reactionConstants[reaction];		
	result[reaction] = loc;
}
A little piece of code to calculate the propensities from a series of reaction channels for a computational biology project we are working on right now...
 
#20 · (Edited)
static void Main(string[] args) {
run();
}

static void run() {
Console.WriteLine("1 + 2 = ?");
if (int.Parse(Console.ReadLine()) != 3) Console.WriteLine("Incorrect. Try again! :)");
else Console.WriteLine("Correct! Good job. Quit? Y/N");
if (Console.ReadLine() == "Y".ToLower()) run();
}

I don't know if that's legal or not since I don't even know C# to begin with... but... I'd try it that way. :p Helps you learn about functions, too, and... it eliminates the loop and the loop check.
the main problem with the code you provided there is that its not going to loop and that´s ok but...."Houston we have problem" :p the reason for that is because you will get the "Incorrect. Try again!" message but after that the code is going to end and nothing will happen after that. actually it should show the message but it should go back again and expect an input ;)

last but not least there´s no error handling and if you type "Sdad3" and hit enter you will get a big fat exception :p

this is probably what you wanted to do(be aware i used editor so it may have some typos :p):
Code:
static void Main(string[] args) {
    run();
}

static void run()
{
    Console.WriteLine("1 + 2 = ?");
    if (int.Parse(Console.ReadLine()) != 3)
    {
	Console.WriteLine("Incorrect. Try again! :)");
	run();
    }
    else 
             Console.WriteLine("Correct! Good job. Quit? Y/N");
    
    if (Console.ReadLine() != "Y".ToLower())
             run();
}
 
#21 ·
hey, huffman encoding assignment. yay.

Code:
#include <iostream>
#include <fstream>
#include <queue>
#include <string>
#include <ostream>
#include <sstream>
#define ENCODE 1
#define DECODE 0
#define QUIT 2
using namespace std;

class Encoder
{
public:
	//----------methods----------------
	//constructor for the Encoder class
	Encoder(){}
	//DESTRUCTOR
	~Encoder(){}
	//the method used to encode a file into huffman's code
	void encode(const char* inputName,const char* outputName)
	{
		FREQUENCY_NODE* huffmanTree;
		priority_queue<FREQUENCY_NODE*,vector<FREQUENCY_NODE*>,node_compare > freqQueue;
		int* frequencies;
		//read the input file and store the frequencies of each char
		frequencies = frequencyCounter(inputName);
		//convert the array which is stores the frequencies to a min priority queue
		freqQueue=convertArrayToQueue(frequencies);
		//convert the min priority queue into a huffman tree
		huffmanTree = huffman(freqQueue);
		//write the characters of the input file into the output file after encoding them terminated by a line feed
		printEncoding(huffmanTree,inputName,outputName);
		//write the tree into an output file
		ofstream output;
		output.open(outputName,ios::app);
		output<< '\n' + appendFreqs(frequencies);
		output.flush();
		output.close();
		//clean up the allocated memory
		cleanTree(huffmanTree);
		delete frequencies;
	}//encode
	//a method to decode a huffman encoded file and then return it to an original encoding
	void decode(const char* inputName,const char* outputName)
	{	
		//create a tree from the input file
		FREQUENCY_NODE* huffmanTree;
		huffmanTree = huffmanFromFile(inputName);
		//write the output file
		writeDecoded(inputName,outputName,huffmanTree);
		//clean up the memory
		cleanTree(huffmanTree);
	}
private:


	//-----------struct def------------------
	// a struct to represent the nodes of the B-Tree created by the Huffman algorithm
	struct FREQUENCY_NODE{
		int frequency;
		unsigned char charValue ;
		FREQUENCY_NODE* leftChild;
		FREQUENCY_NODE* rightChild;
		string toString()
		{

			string returnString(1,charValue);
			returnString+=",";
			stringstream s;
			s << frequency;
			returnString+=s.str();
			returnString+=",";
			return returnString;
		}

	};
	//this is needed to prevent the compare of two nodes from comparing addresses instead of key values
	struct node_compare : binary_function<FREQUENCY_NODE*,FREQUENCY_NODE*, bool>
   {
    bool operator()(const FREQUENCY_NODE* v1, const FREQUENCY_NODE* v2) const
    {
        return (v1->frequency > v2->frequency);
    }
   }; 
	//-----------methods---------------------
	//a function that populates an array from 0-'127' where the index is the int value of the corresponding character. 
	int* frequencyCounter(const char* inputName)
	{
		unsigned char currentChar=0;
		int* frequencies = new int[256];
		for(int i=0;i<256;i++)
		{
			frequencies[i]=0;
		}
		ifstream inputFile;
		inputFile.open(inputName, ios::in);
		//fill the array of the frequencies using the char's ascii values as the index of the array
		while(inputFile.good())
		{
				currentChar=inputFile.get();
				if(inputFile.good())
				{
					frequencies[currentChar]++;	
				}
	
		}
		inputFile.close();
		return frequencies;
	}//frequencyCounter()

	//a function for creating a priority queue from the frequencies read from the input file
	priority_queue<FREQUENCY_NODE*,vector<FREQUENCY_NODE*>,node_compare > convertArrayToQueue(int* input)
	{
		// create min priority queue
		priority_queue<FREQUENCY_NODE*,vector<FREQUENCY_NODE*>,node_compare > frequency_queue;
		// for all of the elements in frequencies[], if the value of the frequency is not equal to zero, 
		// create a frequency node and insert it into the queue keyed by frequency. 
		for(int i =0; i < 256;i++)
		{
			if(input[i]!=0)
			{
				FREQUENCY_NODE* newNode = new FREQUENCY_NODE;
				newNode->charValue=i;
				newNode->frequency=input[i];
				newNode->leftChild=NULL;
				newNode->rightChild=NULL;
				frequency_queue.push(newNode);
			}
		}
		return frequency_queue;
	}//convertArrayToQueue()
	//a function for creating a huffman's tree from a min priority queue
	FREQUENCY_NODE* huffman(priority_queue<FREQUENCY_NODE*,vector<FREQUENCY_NODE*>,node_compare > inputQueue)
	{

		//while the queue has more than one element
		while(inputQueue.size()>1)
		{		
			//	pop off 2 elements
			//	create a new node with those two elements as its children and with the sum of their frequencies as its own frequency

			FREQUENCY_NODE* compositeNode = new FREQUENCY_NODE;
			compositeNode->charValue='`';
			compositeNode->leftChild=(inputQueue.top());
			inputQueue.pop();
			compositeNode->rightChild=(inputQueue.top());
			inputQueue.pop();
			(compositeNode->frequency)=((compositeNode->rightChild)->frequency+(compositeNode->leftChild)->frequency);
			//	push the new node back into the queue
			inputQueue.push(compositeNode);
		}


		//return the final remaining node
		return (inputQueue.top());
	}//huffman()


	//free all the memory in the huffman tree
	void cleanTree(FREQUENCY_NODE* tree)
	{
		if(tree->leftChild == NULL && tree->rightChild == NULL)
		{
			delete tree;
		}
		else
		{		
			cleanTree(tree->leftChild);
			cleanTree(tree->rightChild);
			delete tree;
		}


	}
	//this method will print out the encoding of the input 
	void printEncoding(FREQUENCY_NODE* tree,const char* inputFile,const char* outputFile)
	{
		unsigned char currentChar;
		string encoding = "";
		ifstream input;
		input.open(inputFile, ios::in);
		ofstream output;
		output.open(outputFile);
		while(input.good())
		{
			//while the input still has more characters, use the recursive helper function to generate
			//the encoding of the character.
			currentChar=input.get();
			if(input.good())
			{
				output<<generateEncoding(tree,currentChar,encoding);	
			}

		}
		input.close();
		output.flush();
		output.close();

	}//printencoding()
	// a recursive method to generate the encoding of a character
	string generateEncoding(FREQUENCY_NODE* _tree, char currentChar, string encodingString)
	{
		if(_tree->leftChild==NULL && _tree->rightChild==NULL)
		{
			//if we are in a leaf node check for the character match
			if(_tree->charValue==currentChar)
			{
				//if this isnt true, the character is not in this node, so we reset the string
				return encodingString;
			}

			return "";
		}
		string leftValue;
		string rightValue;
		leftValue=generateEncoding(_tree->leftChild, currentChar,encodingString+'0');
		rightValue=generateEncoding(_tree->rightChild, currentChar,encodingString+'1');
		//check for which branch the character is in
		if(!leftValue.empty())return leftValue;
		if(!rightValue.empty())return rightValue;
		return "";
	}//generateEncoding()

	//a function for traversing the tree and appending the characters and frequencies
	string appendFreqs(int* freqs)
	{
		string returnString;
		for(int i=0;i<256;i++)
		{
			if(freqs[i]!=0)
			{
				unsigned char currentChar = i;
				returnString.push_back(i);
				returnString.push_back(',');
				stringstream s;
				s<<freqs[i];
				returnString+=s.str();
				returnString.push_back(',');
			}
		}
		return returnString;

	}//appendFreqs()
	//used to create a huffman tree from an input file
	FREQUENCY_NODE* huffmanFromFile(const char* inputFile)
	{
		priority_queue<FREQUENCY_NODE*,vector<FREQUENCY_NODE*>,node_compare > freqQueue;
		ifstream input(inputFile,ios::in);
		unsigned char currentChar=0;
		//scan to the beginning of the frequencies 
		input.ignore(100000,'\n');
		//begin to generate nodes from the frequencies 
		while(input.good())
		{
			currentChar=input.get();
			if(input.good())
			{
				string frequencyValue ="";
				FREQUENCY_NODE* newNode = new FREQUENCY_NODE;
				newNode->charValue=currentChar;
				newNode->leftChild=NULL;
				newNode->rightChild=NULL;
				//skip the comma
				input.ignore(1);
				//read until the next comma
				do
				{
					currentChar=input.get();
					frequencyValue.push_back(currentChar);			
				}while(currentChar!=','&&input.good());
				
				newNode->frequency=atoi(frequencyValue.c_str());
				freqQueue.push(newNode);
			}	
		}
		input.close();
		//return a huffman tree made from the queue
		return huffman(freqQueue);
	}//huffmanFromFile()

	//this function will write to a file the result of the decoding
	void writeDecoded(const char* inputFile,const char* outputFile, FREQUENCY_NODE* tree)
	{
		FREQUENCY_NODE* currentNode=tree;
		ifstream input;
		input.open(inputFile);
		ofstream output;
		output.open(outputFile);
		char currentChar;
		while(input.good())
		{
			currentChar=input.get();
			if(input.good())
			{
				//read in 1's and 0's and follow the path until at a leaf node
				if(currentChar=='0')
				{
					currentNode=(currentNode->leftChild);
				}
				else if(currentChar=='1')
				{
					currentNode=(currentNode->rightChild);
				}
				//if at a terminal node, spit out the char
				if(currentNode->rightChild==NULL)
				{
					output.put((currentNode->charValue));
					currentNode=tree;
				}
			}

		}
		input.close();
		output.flush();
		output.close();
	}
};




int main()
{
	

	Encoder encoder = Encoder();
	int goodInput=0;
	int encodeOrDecode=3;//random number here for initialization purposes
	while(encodeOrDecode!=QUIT)
	{
		Encoder encoder = Encoder();
		string inputFile="";
		string outputFile="";
		cout<<"Enter the name of the file you want to input.\n";
		getline(cin,inputFile);
		cout<<"Now enter the name of the file you wish to output to.\n";
		getline(cin,outputFile);
		while(goodInput==0)
		{
			string input="";
			cout<<"Type a 0 if you want to decode a file and a 1 if you want to encode a file\n";
			getline(cin,input);
			encodeOrDecode = atoi(input.c_str());
			if(encodeOrDecode==ENCODE||encodeOrDecode==DECODE || encodeOrDecode==QUIT)
			{
				goodInput=1;
			}

		}
		if(encodeOrDecode==DECODE)
		{
			encoder.decode(inputFile.c_str(),outputFile.c_str());
		}
		else if(encodeOrDecode=ENCODE)
		{
			encoder.encode(inputFile.c_str(),outputFile.c_str());
		}
		goodInput=0;
	}


}
 
#22 ·
the main problem with the code you provided there is that its not going to loop and that´s ok but...."Houston we have problem" :p the reason for that is because you will get the "Incorrect. Try again!" message but after that the code is going to end and nothing will happen after that. actually it should show the message but it should go back again and expect an input ;)

last but not least there´s no error handling and if you type "Sdad3" and hit enter you will get a big fat exception :p

this is probably what you wanted to do(be aware i used editor so it may have some typos :p):
Code:
static void Main(string[] args) {
    Console.WriteLine("1 + 2 = ?");
    check((Console.ReadLine()).ToLower());
}

static void check(string a)
{
    if (a != "y")
        if (a == "3") Console.WriteLine("Correct, good job!");
        else if (a == "n") Console.WriteLine("1 + 2 = ?");
        else Console.WriteLine("Incorrect, try again!");
        Console.WriteLine(n0 + " + " + n1 + " = ? (Type in '0' to quit)");
        check((Console.ReadLine()).ToLower());
    }
}
My bad. I just got back from work and I was worn out to death... :p Fixed and... (hopefully) improved.

Just for kicks...

Code:
static void Main(string[] args) {
    int n0 = 1;
    int n1 = 2;
    Console.WriteLine(n0 + " + " + n1 + " = ? (Type in '0' to quit)");
    check(n0,n1,int.Parse(Console.ReadLine()));
}

static void check(int n0, int n1, int a)
{
    if (a != 0)
        if (a != (n0 + n1)) Console.WriteLine("Incorrect, try again!");
        else Console.WriteLine("Correct, good job!");
        n0++;
        n1++;
        Console.WriteLine(n0 + " + " + n1 + " = ? (Type in '0' to quit)");
        check(n0,n1,int.Parse(Console.ReadLine()));
    }
}
And here's one with the least number of lines I can possibly cough up... I used to do a lot of code-compacting in a certain AS forums... Makes it all the more interesting since you learn more techniques that may prove helpful. :)

Code:
static void Main(string[] args) {
    string n = "";
    while(n != "y") {
        Console.WriteLine("1 + 2 = ?");
        n = (Console.ReadLine()).ToLower();
        if (n == "3") Console.WriteLine("Correct, good job!");
        else if (n != "n") Console.WriteLine("Incorrect, try again!");
    }
}
Maybe I'll try poking C# someday... It looks like it won't be too complicated to learn...
 
#23 ·
Maybe I'll try poking C# someday... It looks like it won't be too complicated to learn...
Nice.. in case you´re interested am planning to re-open my C# course this weekend and also re-work them. this time i´ll try to provide more classes such as C/C++, ASM, VB.Net, WPF, Silverlight, ASP.Net, AJAX, Javascript and many more in fact all the languages i know lol. i hope it could be of help for others ;)
 
#24 · (Edited)
Here's my OGL fragment program based N64 color combiner "emulator" written in VB.NET. Just a small but very important portion of my N64 graphics work. :p

Code:
#Region "Color Combiner"
    Private Sub SETCOMBINE(ByVal w0 As UInteger, ByVal w1 As UInteger)
        If GLExtensions.GLFragProg Then
            EnableCombiner = True

            Dim ShaderCachePos As Integer = -1

            For i As Integer = 0 To FragShaderCache.Length - 1
                If (w0 = FragShaderCache(i).MUXS0) And (w1 = FragShaderCache(i).MUXS1) Then
                    ShaderCachePos = i
                    Exit For
                End If
            Next

            If ShaderCachePos > -1 Then
                Gl.glBindProgramARB(Gl.GL_FRAGMENT_PROGRAM_ARB, FragShaderCache(ShaderCachePos).FragShader)
            Else
                DecodeMUX(w0, w1, FragShaderCache, FragShaderCache.Length)
            End If
        Else
            EnableCombiner = False
        End If
    End Sub

    Private Sub SETFOGCOLOR(ByVal CMDDword() As Byte)
        FogColor(0) = CMDDword(4) / 255
        FogColor(1) = CMDDword(5) / 255
        FogColor(2) = CMDDword(6) / 255
        FogColor(3) = CMDDword(7) / 255
    End Sub

    Private Sub SETBLENDCOLOR(ByVal CMDDword() As Byte)
        BlendColor(0) = CMDDword(4) / 255
        BlendColor(1) = CMDDword(5) / 255
        BlendColor(2) = CMDDword(6) / 255
        BlendColor(3) = CMDDword(7) / 255
    End Sub

    Private Sub SETENVCOLOR(ByVal CMDDword() As Byte)
        EnvironmentColor(0) = CMDDword(4) / 255
        EnvironmentColor(1) = CMDDword(5) / 255
        EnvironmentColor(2) = CMDDword(6) / 255
        EnvironmentColor(3) = CMDDword(7) / 255
    End Sub

    Private Sub SETPRIMCOLOR(ByVal CMDDword() As Byte)
        PrimColor(0) = CMDDword(4) / 255
        PrimColor(1) = CMDDword(5) / 255
        PrimColor(2) = CMDDword(6) / 255
        PrimColor(3) = CMDDword(7) / 255
    End Sub

    Public Sub PrecompileMUXS(ByVal MUXLIST1() As UInteger, ByVal MUXLIST2() As UInteger)
        If MUXLIST1.Length = MUXLIST2.Length Then
            For i As Integer = 0 To MUXLIST1.Length - 1
                DecodeMUX(MUXLIST1(i), MUXLIST2(i), FragShaderCache, i)
            Next
        End If
        PrecompiledCombiner = True
    End Sub

    Public Sub DecodeMUX(ByVal MUXS0 As UInteger, ByVal MUXS1 As UInteger, ByRef Cache() As ShaderCache, ByVal CacheEntry As Integer)
        ColorA(0) = (MUXS0 >> 20) And &HF
        ColorB(0) = (MUXS1 >> 28) And &HF
        ColorC(0) = (MUXS0 >> 15) And &H1F
        ColorD(0) = (MUXS1 >> 15) And &H7

        AlphaA(0) = (MUXS0 >> 12) And &H7
        AlphaB(0) = (MUXS1 >> 12) And &H7
        AlphaC(0) = (MUXS0 >> 9) And &H7
        AlphaD(0) = (MUXS1 >> 9) And &H7

        ColorA(1) = (MUXS0 >> 5) And &HF
        ColorB(1) = (MUXS1 >> 24) And &HF
        ColorC(1) = (MUXS0 >> 0) And &H1F
        ColorD(1) = (MUXS1 >> 6) And &H7

        AlphaA(1) = (MUXS1 >> 21) And &H7
        AlphaB(1) = (MUXS1 >> 3) And &H7
        AlphaC(1) = (MUXS1 >> 18) And &H7
        AlphaD(1) = (MUXS1 >> 0) And &H7

        If GLExtensions.GLFragProg Then
            ReDim Preserve Cache(CacheEntry)
            Cache(CacheEntry).MUXS0 = MUXS0
            Cache(CacheEntry).MUXS1 = MUXS1
            CreateShader(2, Cache(CacheEntry).FragShader)
        Else
            'TODO - At least limited color combiner emulation without using pixel shaders *shrug*
        End If
    End Sub

    Private Sub CreateShader(ByVal Cycles As Integer, ByRef prog As Int32)
        Dim ShaderLines As String = "!!ARBfp1.0" & Environment.NewLine & Environment.NewLine & _
         "TEMP CCReg;" & Environment.NewLine & _
         "TEMP ACReg;" & Environment.NewLine & Environment.NewLine & _
         "TEMP CCRegister_0;" & Environment.NewLine & _
         "TEMP CCRegister_1;" & Environment.NewLine & _
         "TEMP ACRegister_0;" & Environment.NewLine & _
         "TEMP ACRegister_1;" & Environment.NewLine & _
         "TEMP Texel0;" & Environment.NewLine & _
         "TEMP Texel1;" & Environment.NewLine & _
         "PARAM EnvColor = program.env[0];" & Environment.NewLine & _
         "PARAM PrimColor = program.env[1];" & Environment.NewLine & _
         "ATTRIB Shade = fragment.color.primary;" & Environment.NewLine & Environment.NewLine & _
         "OUTPUT FinalColor = result.color;" & Environment.NewLine & Environment.NewLine & _
         "TEX Texel0, fragment.texcoord[0], texture[0], 2D;" & Environment.NewLine & _
         "TEX Texel1, fragment.texcoord[1], texture[1], 2D;" & Environment.NewLine & Environment.NewLine
        For i As Integer = 0 To Cycles - 1
            Select Case ColorA(i)
                Case RDP.G_CCMUX_COMBINED
                    ShaderLines += "MOV CCRegister_0.rgb, CCReg;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0
                    ShaderLines += "MOV CCRegister_0.rgb, Texel0;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1
                    ShaderLines += "MOV CCRegister_0.rgb, Texel1;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE
                    ShaderLines += "MOV CCRegister_0.rgb, PrimColor;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE
                    ShaderLines += "MOV CCRegister_0.rgb, Shade;" & Environment.NewLine
                Case RDP.G_CCMUX_ENVIRONMENT
                    ShaderLines += "MOV CCRegister_0.rgb, EnvColor;" & Environment.NewLine
                Case RDP.G_CCMUX_1
                    ShaderLines += "MOV CCRegister_0.rgb, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_CCMUX_COMBINED_ALPHA
                    ShaderLines += "MOV CCRegister_0.rgb, CCReg.a;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0_ALPHA
                    ShaderLines += "MOV CCRegister_0.rgb, Texel0.a;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1_ALPHA
                    ShaderLines += "MOV CCRegister_0.rgb, Texel1.a;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE_ALPHA
                    ShaderLines += "MOV CCRegister_0.rgb, PrimColor.a;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE_ALPHA
                    ShaderLines += "MOV CCRegister_0.rgb, Shade.a;" & Environment.NewLine
                Case RDP.G_CCMUX_ENV_ALPHA
                    ShaderLines += "MOV CCRegister_0.rgb, EnvColor.a;" & Environment.NewLine
                Case RDP.G_CCMUX_LOD_FRACTION
                    ShaderLines += "MOV CCRegister_0.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case RDP.G_CCMUX_PRIM_LOD_FRAC
                    ShaderLines += "MOV CCRegister_0.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case 15
                    ShaderLines += "MOV CCRegister_0.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV CCRegister_0.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            Select Case ColorB(i)
                Case RDP.G_CCMUX_COMBINED
                    ShaderLines += "MOV CCRegister_1.rgb, CCReg;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0
                    ShaderLines += "MOV CCRegister_1.rgb, Texel0;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1
                    ShaderLines += "MOV CCRegister_1.rgb, Texel1;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE
                    ShaderLines += "MOV CCRegister_1.rgb, PrimColor;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE
                    ShaderLines += "MOV CCRegister_1.rgb, Shade;" & Environment.NewLine
                Case RDP.G_CCMUX_ENVIRONMENT
                    ShaderLines += "MOV CCRegister_1.rgb, EnvColor;" & Environment.NewLine
                Case RDP.G_CCMUX_1
                    ShaderLines += "MOV CCRegister_1.rgb, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_CCMUX_COMBINED_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, CCReg.a;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, Texel0.a;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, Texel1.a;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, PrimColor.a;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, Shade.a;" & Environment.NewLine
                Case RDP.G_CCMUX_ENV_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, EnvColor.a;" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV CCRegister_1.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            ShaderLines += "SUB CCRegister_0, CCRegister_0, CCRegister_1;" & Environment.NewLine & Environment.NewLine
            Select Case ColorC(i)
                Case RDP.G_CCMUX_COMBINED
                    ShaderLines += "MOV CCRegister_1.rgb, CCReg;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0
                    ShaderLines += "MOV CCRegister_1.rgb, Texel0;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1
                    ShaderLines += "MOV CCRegister_1.rgb, Texel1;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE
                    ShaderLines += "MOV CCRegister_1.rgb, PrimColor;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE
                    ShaderLines += "MOV CCRegister_1.rgb, Shade;" & Environment.NewLine
                Case RDP.G_CCMUX_ENVIRONMENT
                    ShaderLines += "MOV CCRegister_1.rgb, EnvColor;" & Environment.NewLine
                Case RDP.G_CCMUX_1
                    ShaderLines += "MOV CCRegister_1.rgb, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_CCMUX_COMBINED_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, CCReg.a;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, Texel0.a;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, Texel1.a;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, PrimColor.a;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, Shade.a;" & Environment.NewLine
                Case RDP.G_CCMUX_ENV_ALPHA
                    ShaderLines += "MOV CCRegister_1.rgb, EnvColor.a;" & Environment.NewLine
                Case RDP.G_CCMUX_K5
                    ShaderLines += "MOV CCRegister_1.rgb, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_CCMUX_0
                    ShaderLines += "MOV CCRegister_1.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV CCRegister_1.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            ShaderLines += "MUL CCRegister_0, CCRegister_0, CCRegister_1;" & Environment.NewLine & Environment.NewLine
            Select Case ColorD(i)
                Case RDP.G_CCMUX_COMBINED
                    ShaderLines += "MOV CCRegister_1.rgb, CCReg;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL0
                    ShaderLines += "MOV CCRegister_1.rgb, Texel0;" & Environment.NewLine
                Case RDP.G_CCMUX_TEXEL1
                    ShaderLines += "MOV CCRegister_1.rgb, Texel1;" & Environment.NewLine
                Case RDP.G_CCMUX_PRIMITIVE
                    ShaderLines += "MOV CCRegister_1.rgb, PrimColor;" & Environment.NewLine
                Case RDP.G_CCMUX_SHADE
                    ShaderLines += "MOV CCRegister_1.rgb, Shade;" & Environment.NewLine
                Case RDP.G_CCMUX_ENVIRONMENT
                    ShaderLines += "MOV CCRegister_1.rgb, EnvColor;" & Environment.NewLine
                Case RDP.G_CCMUX_1
                    ShaderLines += "MOV CCRegister_1.rgb, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV CCRegister_1.rgb, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            ShaderLines += "ADD CCRegister_0, CCRegister_0, CCRegister_1;" & Environment.NewLine & Environment.NewLine
            Select Case AlphaA(i)
                Case RDP.G_ACMUX_COMBINED
                    ShaderLines += "MOV ACRegister_0.a, ACReg;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL0
                    ShaderLines += "MOV ACRegister_0.a, Texel0;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL1
                    ShaderLines += "MOV ACRegister_0.a, Texel1;" & Environment.NewLine
                Case RDP.G_ACMUX_PRIMITIVE
                    ShaderLines += "MOV ACRegister_0.a, PrimColor;" & Environment.NewLine
                Case RDP.G_ACMUX_SHADE
                    ShaderLines += "MOV ACRegister_0.a, Shade;" & Environment.NewLine
                Case RDP.G_ACMUX_ENVIRONMENT
                    ShaderLines += "MOV ACRegister_0.a, EnvColor;" & Environment.NewLine
                Case RDP.G_ACMUX_1
                    ShaderLines += "MOV ACRegister_0.a, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_ACMUX_0
                    ShaderLines += "MOV ACRegister_0.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV ACRegister_0.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            Select Case AlphaB(i)
                Case RDP.G_ACMUX_COMBINED
                    ShaderLines += "MOV ACRegister_1.a, ACReg.a;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL0
                    ShaderLines += "MOV ACRegister_1.a, Texel0.a;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL1
                    ShaderLines += "MOV ACRegister_1.a, Texel1.a;" & Environment.NewLine
                Case RDP.G_ACMUX_PRIMITIVE
                    ShaderLines += "MOV ACRegister_1.a, PrimColor.a;" & Environment.NewLine
                Case RDP.G_ACMUX_SHADE
                    ShaderLines += "MOV ACRegister_1.a, Shade.a;" & Environment.NewLine
                Case RDP.G_ACMUX_ENVIRONMENT
                    ShaderLines += "MOV ACRegister_1.a, EnvColor.a;" & Environment.NewLine
                Case RDP.G_ACMUX_1
                    ShaderLines += "MOV ACRegister_1.a, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_ACMUX_0
                    ShaderLines += "MOV ACRegister_1.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV ACRegister_1.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            ShaderLines += "SUB ACRegister_0.a, ACRegister_0.a, ACRegister_1.a;" & Environment.NewLine
            Select Case AlphaC(i)
                Case RDP.G_ACMUX_COMBINED
                    ShaderLines += "MOV ACRegister_1.a, ACReg.a;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL0
                    ShaderLines += "MOV ACRegister_1.a, Texel0.a;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL1
                    ShaderLines += "MOV ACRegister_1.a, Texel1.a;" & Environment.NewLine
                Case RDP.G_ACMUX_PRIMITIVE
                    ShaderLines += "MOV ACRegister_1.a, PrimColor.a;" & Environment.NewLine
                Case RDP.G_ACMUX_SHADE
                    ShaderLines += "MOV ACRegister_1.a, Shade.a;" & Environment.NewLine
                Case RDP.G_ACMUX_ENVIRONMENT
                    ShaderLines += "MOV ACRegister_1.a, EnvColor.a;" & Environment.NewLine
                Case RDP.G_ACMUX_1
                    ShaderLines += "MOV ACRegister_1.a, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_ACMUX_0
                    ShaderLines += "MOV ACRegister_1.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV ACRegister_1.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            ShaderLines += "MUL ACRegister_0.a, ACRegister_0.a, ACRegister_1.a;" & Environment.NewLine
            Select Case AlphaD(i)
                Case RDP.G_ACMUX_COMBINED
                    ShaderLines += "MOV ACRegister_1.a, ACReg.a;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL0
                    ShaderLines += "MOV ACRegister_1.a, Texel0.a;" & Environment.NewLine
                Case RDP.G_ACMUX_TEXEL1
                    ShaderLines += "MOV ACRegister_1.a, Texel1.a;" & Environment.NewLine
                Case RDP.G_ACMUX_PRIMITIVE
                    ShaderLines += "MOV ACRegister_1.a, PrimColor.a;" & Environment.NewLine
                Case RDP.G_ACMUX_SHADE
                    ShaderLines += "MOV ACRegister_1.a, Shade.a;" & Environment.NewLine
                Case RDP.G_ACMUX_ENVIRONMENT
                    ShaderLines += "MOV ACRegister_1.a, EnvColor.a;" & Environment.NewLine
                Case RDP.G_ACMUX_1
                    ShaderLines += "MOV ACRegister_1.a, {1.0,1.0,1.0,1.0};" & Environment.NewLine
                Case RDP.G_ACMUX_0
                    ShaderLines += "MOV ACRegister_1.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
                Case Else
                    ShaderLines += "MOV ACRegister_1.a, {0.0,0.0,0.0,0.0};" & Environment.NewLine
            End Select
            ShaderLines += "ADD ACRegister_0.a, ACRegister_0.a, ACRegister_1.a;" & Environment.NewLine & Environment.NewLine
            ShaderLines += "MOV CCReg.rgb, CCRegister_0;" & Environment.NewLine
            ShaderLines += "MOV ACReg.a, ACRegister_0.a;" & Environment.NewLine
        Next
        ShaderLines += "MOV CCReg.a, ACReg.a;" & Environment.NewLine
        ShaderLines += "MOV FinalColor, CCReg;" & Environment.NewLine
        ShaderLines += "END" & Environment.NewLine

        Dim program() As Byte = System.Text.Encoding.ASCII.GetBytes(ShaderLines)

        Gl.glGenProgramsARB(1, prog)
        Gl.glBindProgramARB(Gl.GL_FRAGMENT_PROGRAM_ARB, prog)
        Gl.glProgramStringARB(Gl.GL_FRAGMENT_PROGRAM_ARB, Gl.GL_PROGRAM_FORMAT_ASCII_ARB, program.Length, program)

    End Sub
#End Region
 
#26 · (Edited)
Nice code cooliscool! makes me remember my VB6 days hehehehehe.
Hehe. While I know C# and C++, to me VB.NET's clear and concise syntax makes for much more understandable/maintainable code. Its power (along with C#.NET, thanks to .NET's intermediate language) is vastly underrated, too. I've had no language limitations in any app I've written, including my relatively complex Zelda 64 editor. It's pretty well optimized (but there's room for more as it's a huge project), and models load in milliseconds and achieve ~400 to ~2000 FPS during runtime. Taking into account that it's an editor and keeps track of thousands and thousands of things constantly (including parsing each N64 display list per each frame - I can't take advantage of OGL display list objects because, as an editor, graphical properties such as vertices can be changed), that's not too bad.

Too many people, but not all, have an elitist point of view when it comes to language choice, which I've found to be completely unfounded in most cases. The only problem I have is the obvious platform restrictions, but it's not much of a loss when most computer users either run Windows alone or multiboot with their OS of choice. Also can't forget how incredibly well designed Microsoft's IDEs and debuggers are. Nothing like them. :D