Programmer account notes (6)

xiaoxiao2021-03-06  82

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;

}

Do you say 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, slightly * /

:

:

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-124134.html

New Post(0)