"C Language" write function implement library function atoi, convert string into shaping (consider number too large)

Source: Internet
Author: User

Write function implement library function Atoi, convert string to shaping # include <stdio.h> #include <string.h> #include <assert.h> #include < Ctype.h>long long Calculate (const char *SRC, int flag) {long Long num = 0;while (*SRC) {if (*src >= ' 0 ') && (*src <= ' 9 ')) Determine if the input is a non-array character {num = num * + (*SRC-' 0 '); if (flag = = 1 && num>0x7fffffff) | | (Flag = =-1 && num < (signed int) 0x80000000)) Consider the number too big {num = 0;break;} src++;} Else{num = 0;break;}} return flag * NUM;} Long Long str_to_int (const char *str) {long Long num = 0;if (str = NULL && *str! = ') ') {while (Isspace (*STR)) {str ++;} int flag = 1;if (*str = = ' + ') str++;else if (*str = = '-') {str++;flag =-1;} if (*str! = ') "{num = calculate (str, flag);}} return num;} int main () {printf ("%lld\n", Str_to_int ("+2345"));p rintf ("%lld\n", Str_to_int (" -2345"));p rintf ("%lld\n", Str_to_int ("+2345")); printf ("%lld\n", Str_to_int (" -2345"));p rintf ("%lld\n", Str_to_int ("2345"));p rintf ("%lld\n", Str_to_int ("- 111111111 "));p rintf ("%lld\n ", str_to_int (""));p rintf ("%lld\n", Str_to_int ("123ab")); return 0;} 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C Language" write function implement library function atoi, convert string into shaping (consider number too large)

Related Article

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.