|
Operating system: Win98 Programming Tools: TC 2.0 Problem: Handle each of the 4-digit numbers, each digit plus 7, then divide by 10 and take the remainder. My idea is to use this four-digit ABCD. Divided by 1000, get a A, then the remainder except 100 get B, the remainder except 10, get C, ....... But I don't know how to do the rounding. Can you help me, please? Level: Just getting Started (cflanker) |
|
|
|
The C language has the following several rounding methods: 1, the direct assignment to the integer variable. Such as: int i = 2.5; or i = (int) 2.5; This method uses a fractional part that can be used for your problem. 2. The integer division Operator "/" in C + + has its own rounding function (Int/int), and the return value of the rounding function described below is double. Integer division to the positive number of the rounding off the decimal part, can be used for your problem. But integer division is concerned with the rounding result of negative numbers and the C compiler used. 3, use the floor function. Floor (x) returns the largest integer less than or equal to X. Such as: Floor (2.5) = 2 Floor (-2.5) =-3 4, use the Ceil function. Ceil (x) returns the smallest integer greater than X. Such as: Ceil (2.5) = 3 Ceil (-2.5) =-2 Floor () is rounded to negative infinity, floor ( -2.5) = -3;ceil () is rounded to positive infinity, ceil (-2.5) =-2. The floor function can be used for your problem.
Hyh's Opinion: int x,a,b,c,d; a=x/1000; b=x%1000/100; C=X%100/10; d=x%10;
Guo Li: Several experts explain the floor and ceil two functions can only be applied in TC 2.0, I tried in VC + +, do not recognize this function, is not to include a special header file AH. Please advise the experts, I am a novice. Thank you, sir. Answer: These two functions can also be used in VC + +, but you need to include MATH.H files when you use them. You can add the following code at the beginning of your program: #include <math.h>
|
|