C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 19th, 2005, 09:12 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
Evaluating differ. parts of a single #.

Suppose i have an 8 digit code. I want to assign first 'x' digits to represent the first code. The 'y' digits after x i want to represent a different code. And so on.

So for a specified number. I want the first three digits to give value to a variable. The next three to do the same to another variable. And the second last and last to both individually do the same.

For the first three, i would:

divide the number by 100,000. This would yield a three digit code with 5 decimals. Then i would convert the double into integer, and end up with a three digit code (called "number1") which i evaluate via if statements.

For the next three:


Divide the number by 100. This would create a 6 digit number with 2 decimals. I convert into integer, creating a 6 digit integer (this is "number6". Then subtract number6 - 1000*number1. This would produce a the three digits after the first three. So in 12345678, these numbers would be 456.



However, im not sure how to do the second last and last! I was thinking of using remainders, but im unsure of how to do that.


Please, any help or suggestions would be strongly appreciated.

Reply With Quote
  #2  
Old October 20th, 2005, 01:27 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 37 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
I you use integers for the 8 digit code and the divisions, you don't get decimals (you don't get decimals with integers, remember)
try this:
cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     // declarations
  6.     int firstvar, secondvar, thirdvar, fourthvar, initialcode;
  7.  
  8.     // a value for the initial code
  9.     initialcode = 12345678;
  10.  
  11.     // calculate codes
  12.     firstvar = initialcode / 100000;
  13.     secondvar = (initialcode %  100000) / 100;    // use modulo (remainder) to remove the part in front of the code,
  14.     thirdvar = (initialcode % 100) / 10;            // and divide to remove everything behind;
  15.     fourthvar = initialcode % 10;
  16.  
  17.     // output
  18.     printf("Original code:     %8d\n", initialcode);
  19.     printf("First 3 digits:    %8d\n", firstvar);
  20.     printf("Next 3 digits:     %8d\n", secondvar);
  21.     printf("2nd to last digit: %8d\n", thirdvar);
  22.     printf("Last digit:        %8d\n", fourthvar);
  23.  
  24.     // exit
  25.     return 0;
  26. }
Comments on this post
MichaelSoft agrees!
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 275

Reply With Quote
  #3  
Old October 20th, 2005, 01:57 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2Folding Points: 732741 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 37 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Here's a more 'multifunctional' version:
cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int get_digits(int code, int start, int length)
  5. {
  6.     // start is the first digit you want (starting at 0 FROM THE RIGHT!), length is the number of digits you want
  7.     // so get_digits(12345678, 2, 3) will return 456
  8.    
  9.     return ((code / (int) pow(10, start)) % (int) pow(10, length));
  10. }
  11.  
  12. int main()
  13. {
  14.     // declarations
  15.     int firstvar, secondvar, thirdvar, fourthvar, initialcode;
  16.  
  17.     // a value for the initial code
  18.     initialcode = 12345678;
  19.  
  20.     // calculate codes
  21.     firstvar = get_digits(initialcode, 5, 3);
  22.     secondvar = get_digits(initialcode, 2, 3);
  23.     thirdvar = get_digits(initialcode, 1, 1);
  24.     fourthvar = get_digits(initialcode, 0, 1);
  25.  
  26.     // output
  27.     printf("Original code:     %8d\n", initialcode);
  28.     printf("First 3 digits:    %8d\n", firstvar);
  29.     printf("Next 3 digits:     %8d\n", secondvar);
  30.     printf("2nd to last digit: %8d\n", thirdvar);
  31.     printf("Last digit:        %8d\n", fourthvar);
  32.  
  33.     // exit
  34.     return 0;
  35. }

Reply With Quote
  #4  
Old October 20th, 2005, 05:22 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
Fancy code Itsacon ;-)

To Dekoi: using remainders is pretty simple:
123 % 100 = 23
12345 % 100 = 45
12345 % 1000 = 345

And, maybe less obvious:
60 % 25 = 10
110 % 60 = 50

Basically, subtract the number after % as many times as you can. The value left is the remainder. This is always smaller then the number after %.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Evaluating differ. parts of a single #.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway