zoukankan      html  css  js  c++  java
  • 网易面试题:写一段程序,实现atoi(const char* s)方法

    描述:

      写一段程序,实现atoi(const char* s)方法。

    示例代码:

     1 #include <iostream>
     2 using namespace std;
     3 // 写一段程序,实现atoi(const char* s)方法。
     4 // atoi用于将字符串转换成为整数。
     5 // 比如 “123” => 123, “-246” => -246。
     6 
     7 int Aatoi(const char *s)
     8 {    
     9     // 主要的问题是负号的处理,不考虑其他字符的处理
    10     int len = strlen(s);
    11     if(0 == len) return -1;
    12     bool is_plus = true;
    13     if(s[0] != '-')
    14     {
    15         is_plus = true;
    16     }
    17     else
    18     {
    19         is_plus = false;
    20         s++;
    21     }
    22     int result = 0;
    23     while(*s!='\0')
    24     {
    25         result = result*10 + *s - '0';
    26         s++;
    27     }
    28     if(is_plus == false)
    29         return -result;
    30     else
    31         return result;
    32 }
    33 
    34 void main()
    35 {
    36     char s[] = "123";
    37     cout << Aatoi(s) << endl;
    38 } 
  • 相关阅读:
    JVM字节码(七)
    JVM字节码(六)
    JVM字节码(五)
    JVM字节码(四)
    JVM字节码(三)
    JVM字节码(二)
    JVM字节码(一)
    JVM类加载器(五)
    JVM类加载器(四)
    php之 人员的权限管理
  • 原文地址:https://www.cnblogs.com/xuxu8511/p/2442833.html
Copyright © 2011-2022 走看看