| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||||
|
|||||
|
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:
__________________
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
![]() |
|
#3
|
|||||
|
|||||
|
Here's a more 'multifunctional' version:
cpp Code:
|
|
#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 %. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Evaluating differ. parts of a single #. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|