The implementation converts a string into a corresponding shaping number

Source: Internet
Author: User

Enter a string such as "+1234", convert it to the corresponding number 1234 output, if "1234", then output-1234:

#include  <stdio.h>enum charec{true,false};int check = false;int my_atoi ( CONST&NBSP;CHAR&NBSP;*STR) { int num = 0; if (str != null) && (*str  !=  ' + ')  {  int minus = 0;  if (*str ==  ' + ')    str++;  else if (*str ==  '-')   {   str++;    minus = 1;  }  while (*str !=  ')   {    if ((*str >=  ' 0 ') && (*str <=  ' 9 '))    {     int flag = minus?-1:1;    num =  (num*10) +flag* ( *str-' 0 ');     if ((!minus&& (num>0x7fffffff)) | | (minus&& ((signed int) num<0x80000000)))     {     num = 0;     break;     }    str++;   }   else   {     num = 0;    break;   }  }   if (*str ==  ')   {   check = true;  } }  return num;} Int main () { char str[50]; int ret = 0; scanf ("%s", str); ret  = my_atoi (str);  printf ("%d\n", ret);  return 0;}

For the input string "0" output result 0 and the input empty string output is also the same as 0 of the two cases, deliberately set a global enumeration constant, so that you can check the value of the enumeration constant to differentiate, but also to consider the input string converted to the corresponding number outside the range of int.

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1707218

The implementation converts a string into a corresponding shaping number

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.