Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Write a function with the same function as atoi in C++ to realize the conversion from characters to plastics.
Write a function with the same function as atoi in C++ to realize the conversion from characters to plastics.
The following is the code to realize this function. The name of this function is myAtoi.

int myAtoi (const char *value) {

Register int ret, I, signFlag

ret = I = 0;

sign flag = 1;

while ((*(value + i))! = 0x00) {

if ((*(value + i)) == 0x2d) {

sign flag =- 1;

}

else if((*(value+I))& gt; = 0x30 & amp& amp(*(value+I))& lt; = 0x39) {

ret * = 10;

ret+=((int)(*(value+I))-48);

}

else if(

(ret! = 0)& amp; & amp

((*(value+I))& lt; 0x 30 | |(*(value+I))& gt; 0x39)

) {

ret(ret * sign flag);

}

I+= 1;

}

ret(ret * sign flag);

}

If you want to test, use the following code.

# include & ltstdio.h & gt

int myAtoi (const char *value) {

Register int ret, I, signFlag

ret = I = 0;

sign flag = 1;

while ((*(value + i))! = 0x00) {

if ((*(value + i)) == 0x2d) {

sign flag =- 1;

}

else if((*(value+I))& gt; = 0x30 & amp& amp(*(value+I))& lt; = 0x39) {

ret * = 10;

ret+=((int)(*(value+I))-48);

}

else if(

(ret! = 0)& amp; & amp

((*(value+I))& lt; 0x 30 | |(*(value+I))& gt; 0x39)

) {

ret(ret * sign flag);

}

I+= 1;

}

ret(ret * sign flag);

}

int main () {

char ch 1[]= "- 100 ";

char CH2[]= "-034g 1234 ";

char CH3[]= " ";

printf ("%d\n%d\n%d\n ",myAtoi (ch 1),myAtoi (ch2),myAtoi(CH3));

Returns 0;

}