Programmer test replenishment notes - Six Day

zhaozj2021-02-16  47

Today's entire course has only such questions, but there are many things that have been learned. Here are this topic:

Character numbers are converted to integer values ​​(characters can be arbitrary: such as "342a") encountered other no numbers to take the front number.

The procedures I have written are as follows, I think it is good:

#define n 10;

INT CATIO (const char * str) / * const is constant, so the address here will not return to the inactive parameters * /

{

Int num [n];

INT i = 0; j = 1, n = 0;

For (; * str ; i )

{

IF (* STR <48 || * STR> 57)

Break; / * Judging whether the numeric value * /

NUM [I] = * STR-48;

}

For (i- = 1; i> = 0; i -) {n = NUM ​​[i] * j; j * = 10;} return n;} Are you said that it is more simple? Now I can't see another program of reading the following, I will conclude. as follows:

Long catio (char C []); {INT N, D; Char * q, * p; long E = 1, s = 0; for (q = p = c, n = 0; * p! = '/ 0 '&& * p> =' 0 '&& * p <=' 9 '; p , n , e * = 10); while (n> 0) {d = * q ; switch (d) {case 48: d = 0; Break; / * too long, slight * /:: case 57: d = 9; Break;} S = D * (E / = 10); n -;} return (s);}

Now let's take a look, but although this procedure is complicated than me, there is also his ideas and ideas. Like that for loop, a command has been very convenient. In fact, we can continue to transform this procedure, we follow the teacher's ideas to evolve it step by step, now look at the following: long catio (char c []); {INT N, D; char * q, * p; long e = 1, S = 0; for (q = p = c, n = 0; * p && * p> = '0' && * p <= '9'; p , n , e * = 10); while (n > 0) {d = * q - '0'; s = D * (E / = 10); n--;} return (s);}

转载请注明原文地址:https://www.9cbs.com/read-27220.html

New Post(0)