Next Generation Emulation banner

Post the code written by you thread. :D

58465 Views 405 Replies 36 Participants Last post by  @ruantec
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");
}
See less See more
1 - 20 of 406 Posts
Have you learned about loops yet? A loop would be much better suited to that code than using 'goto'. Not a bad first try, though.
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.
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;
                }
            }
        }
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;
        }
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.");
                }
            }
        }
But i not learnt loop yet

Dax but teacher told me the use of switch in console is more elegant than else if
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.
See less See more
Thank u techno man :D how many languge do u know
Thank u techno man :D how many languge do u know
Atm C/C++, C#, VB/VB.Net, ASP.Net, some Javascript, a small knowledge in Java, PHP, Delphi, Objective-C and some rusty ASM(refreshing it atm) why???
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
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...
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?
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
I am done major in economic management now i am coputer science major in programming in year 2 now
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:
See less See more
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
See less See more
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.
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...
See less See more
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();
}
See less See more
1 - 20 of 406 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top