Self-writing atoi----strings into shaping

Source: Internet
Author: User

Strings are converted into various cases of shaping:

       char *str1 = "12345";      //normal
< /span>char *str2 = "+12345";    //positive
char *STR3 = " -12345";      //negative
char *STR4 = "  12345";    //preceded by a number of spaces
char *STR5 = "1 2 3 4 5"; There are spaces between  //numbers
char *STR6 = "0x12345";    //hex
char *str7 = "012345";    //Eight binary
char *str8 = "abc123";    //with other characters

Char *str2 = "+12345"; Positive
Char *STR3 = "12345"; Negative
Char *STR4 = "12345"; There are a number of spaces in front
Char *STR5 = "1 2 3 4 5"; There are spaces between numbers
Char *STR6 = "0x12345"; Hexadecimal
Char *STR7 = "012345"; Octal

Char *str8 = "abc123"; with other characters

The code is as follows:

#include <stdio.h>int my_atoi (const char *str) {int result = 0;        Store the converted value int sign = 1;     Sign bit int weight = 10;       Weights (decimal, octal, hex) while (*str! = ') ') {if (*str = = ' + ')//indicates positive number {sign = 1;str++;       Will ' + ' skim}if (*str = = '-')//indicate negative number {sign = -1;str++;  Will '-' skim}/*if (*str = = ' 0 ' && * (++str)! = ' x ') {weight = 8; Attention!!!!! : Cannot add str++ here, otherwise you will miss a character//str++; because after executing * (++STR)! = ' x ', STR has pointed to an incompletely determined character (although not equal to X)}if (*str = = ' 0 ' && * (++str) = = ' X '       )//Indicates is 16 binary {weight = 16;str++;  Will ' X ' skim}*/if (*str = = ' 0 ') {if (* (str + 1) = = ' x ') {weight = 16;str + 2;  Move ' 0 ' and ' x ' over}else{weight = 8;str + + 1; Will ' 0 ' skim}}if (*str >= ' 0 ' && *str <= ' 9 ') {result = result * weight + (*str-' 0 '); str++;} else{str++;//non-valid characters all flitted (+-0x x exp)}}return result * sign;     int main () {char *str1 = "12345";    Ordinary char *str2 = "+12345";    Positive char *STR3 = "12345";  Negative char *STR4 = "12345";   There are several spaces in front of char *STR5 = "1 2 3 4 5";//There is a space between the numbers char *STR6 = "0x12345"; Hex char *STR7 = "012345 "; octal char *str8 = "abc123";p rintf ("%6d\n", My_atoi (str1));p rintf ("%6d\n", My_atoi (str2));p rintf ("%6d\n", My_atoi ( STR3));p rintf ("%6d\n", My_atoi (STR4));p rintf ("%6d\n", My_atoi (STR5));p rintf ("%6d\n", My_atoi (STR6));p rintf ("%6d\n ", My_atoi (STR7));p rintf ("%6d\n ", My_atoi (STR8)); return 0;}


Self-writing atoi----strings into shaping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.