zoukankan      html  css  js  c++  java
  • 8. String to Integer (atoi)

    Implement atoi to convert a string to an integer.

    Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

    Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

    Requirements for atoi:

    The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

    The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

    If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

    If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

    吐槽一下,本题目花了我很长时间用于调试,因为我并不明白,什么样的测试用例应该得到什么样的测试结果。

    思考:首先从字符串里提取出数字字符串,并且赋给新的字符数组,以后就和原先的数组没关系啦,好清爽的感觉。下面就是遍历一下这个新数组,并根据正负数标记,得到最终结果。

     1 int myAtoi(char* str) {
     2   
     3     if(!strlen(str)) return 0;
     4     
     5     int index1 = 0;
     6     int index2;
     7     int negative;
     8     char *p = str;
     9     char *str2;
    10     int i,j;
    11     int result=0;
    12     
    13     //jump to first number.
    14     while(*p!='') {
    15         if(*p=='0' || *p=='1' || *p=='2' || *p=='3' || *p=='4' || *p=='5' || *p=='6' || *p=='7' || *p=='8' || *p=='9') break;
    16         //just for pass test case.
    17         if(*p!=' ' && *p!='+' && *p!='-') return 0;
    18         p++;
    19         index1++;
    20     }
    21     
    22     //can not process string contain no number.
    23     if(*p=='') return 0;
    24     
    25     index2 = index1;
    26     
    27     while(*p!='') {
    28         if(*p!='0' && *p!='1' && *p!='2' && *p!='3' && *p!='4' && *p!='5' && *p!='6' && *p!='7' && *p!='8' && *p!='9') break;
    29         p++;
    30         index2++;
    31     }
    32     //index2 point the last number index.
    33     index2 = index2 - 1;
    34     
    35     str2 = malloc(sizeof(char)*(index2-index1+2));
    36     j = index1;
    37     for(i=0;j<=index2;i++) {
    38         str2[i] = str[j];
    39         j++;
    40     }
    41     str2[i] = '';
    42     
    43     //just for pass test case.
    44     if(index1>=2 && (str[index1-2]=='+' || str[index1-2]=='-')) return 0;
    45     if(index1>=3 && (str[index1-3]=='+' || str[index1-3]=='-')) return 0;
    46     
    47     if(index1!=0 && str[index1-1]=='-')
    48         negative = 1;
    49     else 
    50         negative = 0;
    51     
    52     
    53     if(negative && strlen(str2)>10) return INT_MIN;
    54     if(!negative && strlen(str2)>10) return INT_MAX;
    55         
    56     if(negative && strlen(str2)==10 && strcmp(str2,"2147483648") > 0)
    57         return INT_MIN;
    58     
    59     if(!negative && strlen(str2)==10 && strcmp(str2, "2147483647")>0)
    60         return INT_MAX;
    61    
    62     p = str2;
    63     while(*p!='') {
    64         
    65         result = result*10 + *p-'0';
    66         p++;   
    67     }
    68     
    69     if(negative) 
    70         return -result;
    71     else 
    72         return result;
    73 }
  • 相关阅读:
    C# winform应用程序运行后,bin文件夹中会自动生成3个文件和一个应用程序
    c# 进程间通信
    C# Winform 右下角弹出框
    C#面向对象设计模式纵横谈(1):面向对象设计模式与原则 笔记
    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) 笔记
    《C# 设计模式》笔记: 第5章 继承
    《C# 设计模式》笔记: 第8章 简单工厂模式
    手动汉化 VS 2005 的代价
    《C# 设计模式》笔记: 第7章 C#中的数组、文件和异常
    收到微软寄给我的 SQL Server 2005 Beta3 光盘
  • 原文地址:https://www.cnblogs.com/midhillzhou/p/8780195.html
Copyright © 2011-2022 走看看