Quantcast

Maximum PC

It is currently Thu Aug 21, 2014 12:25 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 46 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: Wed Jul 30, 2008 5:02 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
hbgrg wrote:
Here it is:

Code:
void hexadecimal(int number)
{
   stack hex;
   int remainder;
   while (number > 0)
   {
      remainder = number % 16;
      number = number/16;
      if (remainder > 9)
         hex.push('A'  + remainder - 10);
      else
         hex.push(remainder);
   }
      hex.pop();
};


Ah, DOH! Why did I miss this one earlier?

casting an int to a char starts it at ASCII \0, or NULL. Adding '0' to the else remainder works
Code:
else {
    hex.push('0' + remainder);
}


BTW, isEmpty is broke with an assignment in the if statement (single equals should be double)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 5:08 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
hahah YAYYY i actually fixed everything (including the isEmpty) before I saw this post :D:D:D:D I'm finally doneee


Thanks EVERYONE who helped!!


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 6:01 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Great work! :)

Now, if you really want to learn .. step back and tell us what you'd do differently if you had to start the project again from scratch.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 9:28 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
ummm I'd prolly connect all three functions in a way because they are very similar (use modulus and divide operators)... And I'd use the stack class for octal too... :) that sound good?


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 6:50 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Yep. :)

In each of the conversion functions, you did the same thing. You just did it with different bases. So, instead of having binary(int), hex(int) and octal(int), you could have a function called convert that takes a starting base, an end base and a number and converts accordingly.

Code:
int convert(int baseStart, int baseEnd, int number)


Of course, if you want to make this possible with return base greater than 10, you would need a return type that includes characters. A character string or an array, for instance. ;)


Also, you could definitely use the stack for each of the functions in your original code or in the 'new' code.

This makes your code reusable because it can convert from any base to any other. It also makes it easier to read, easier to maintain and it looks nicer. It is not necessarily more efficient, though. I won't get into efficiency here, but I will point out that efficiency has nothing to do with the number of lines of code you have in your source .. a common misconception among programmers until they get bitchslapped by the higher maths and learn algorithmic proofs of efficiency and correctness. That is for another day.

Damn it, I should be working here. ;) Hope we've helped. Feel free to post your next assignment so we can help you think it through. I think we did a good job of leading you without handing you the answers or losing you along the way. Yes?


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 9:10 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Yes thank you all!!! I learned a lot from you guys...

(Not so) sadly, my class is over so I dont think I'll be coming back with anymore assignments but I think I'm going to take flash sometime so I might comeback with questions about that :D


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 46 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group