|
|
|||||||
| Home | Register | Downloads | FAQ | Members List | Calendar | Arcade | Mark Forums Read |
» Less advertising throughout
» Post and participate in discussions
» Network with other forum members
» Free private messaging
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
បងស្រលាញ់អូន!
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2005
Location: ព្រះរាជាណាចក្រកម្ពុជា
Posts: 4,117
|
Post the code written by you thread. :D
After i learn C# for two weeks here is my first try. ![]() 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");
}
|
|
|
|
| Advertisement | [Remove Advertisement] | ||
|
|
|
#2 |
|
ライチュウ|タオ
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2006
Location: USA
Posts: 3,956
|
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.
__________________
|
|
|
|
|
|
#3 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
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 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.
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 1st, 2009 at 10:47.. |
|
|
|
|
|
#4 |
|
ライチュウ|タオ
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2006
Location: USA
Posts: 3,956
|
Here's how I would do it(Spoiler tags, because if this is a homework assignment, you shouldn't be using it! ):No error checking, btw. I wanted to keep it simple. Spoiler:
__________________
Last edited by Dax; December 1st, 2009 at 11:01.. |
|
|
|
|
|
#5 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
Heyyyyyyyyyyy!!! that´s exactly what i explained on my post ![]() 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 Spoiler:
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 1st, 2009 at 12:59.. |
|
|
|
|
|
#6 |
|
ライチュウ|タオ
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2006
Location: USA
Posts: 3,956
|
Here's a "version 2.0" of the previous code(I added basic error checking and removed magic numbers): Spoiler:
__________________
|
|
|
|
|
|
#7 |
|
បងស្រលាញ់អូន!
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2005
Location: ព្រះរាជាណាចក្រកម្ពុជា
Posts: 4,117
|
But i not learnt loop yet Dax but teacher told me the use of switch in console is more elegant than else if Last edited by raksmey1309; December 1st, 2009 at 13:54.. Reason: Automerged Doublepost |
|
|
|
|
|
#8 | |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
Quote:
for example: Code:
switch(number)
{
case 1:
/*do something*/
break;
case 2:
/*do something*/
break;
case Default:
/*do something*/
break;
}
Code:
if(number == 1) /*do something*/ else if(number == 2) /*do something*/ else /*do something*/
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 1st, 2009 at 14:10.. |
|
|
|
|
|
|
#9 |
|
បងស្រលាញ់អូន!
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2005
Location: ព្រះរាជាណាចក្រកម្ពុជា
Posts: 4,117
|
Thank u techno man how many languge do u know
|
|
|
|
|
|
#10 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
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???
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 |
|
|
|
|
|
#11 |
|
បងស្រលាញ់អូន!
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2005
Location: ព្រះរាជាណាចក្រកម្ពុជា
Posts: 4,117
|
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 | |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
Quote:
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...
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 |
|
|
|
|
|
|
#13 | ||
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
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. Quote:
Quote:
__________________
|
||
|
|
|
|
|
#14 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
btw my nick is @ruantec proto lots of people writes my nick wrong lol... i´ve seen several variations such as "ruantic", "@rauntec", "@runtec" and now yours "aruantek" nice!!!! i have to admit i like your more as it sounds somewhat German hehehehehe
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 1st, 2009 at 15:16.. |
|
|
|
|
|
#15 |
|
បងស្រលាញ់អូន!
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2005
Location: ព្រះរាជាណាចក្រកម្ពុជា
Posts: 4,117
|
I am done major in economic management now i am coputer science major in programming in year 2 now
|
|
|
|
|
|
#16 | |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
Quote:
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
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 1st, 2009 at 16:03.. |
|
|
|
|
|
|
#17 |
|
Registered User
![]() ![]() Join Date: Sep 2006
Location: surrey
Posts: 111
|
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;
}
}
|
|
|
|
|
|
#18 | |
|
Level 9998
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2006
Location: Java
Posts: 9,377
|
Quote:
Spoiler:
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. Helps you learn about functions, too, and... it eliminates the loop and the loop check.
|
|
|
|
|
|
|
#19 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
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;
}
__________________
|
|
|
|
|
|
#20 | |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,106
|
Quote:
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 ![]() this is probably what you wanted to do(be aware i used editor so it may have some typos ):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();
}
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 1st, 2009 at 23:23.. |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|