• 笔试面试记录-字符串转换成整型数等(aatoi,itoa)


      C语言中经常用到字符串与数字之间的相互转换,常见的此类库函数有atof(字符串转换成浮点数)、atoi(字符串转换成整型数)、atol(字符串转换成长整形)、itoa(整型数转换成字符串)、ltoa(长整形转换为字符串)等。

      以下为自定义Myatoi()函数的实现以及测试代码。

     1 #include <stdio.h>
     2 int Myatoi(char*str){
     3     if (str == NULL){
     4         printf("Invalid Input");
     5         return -1;
     6     }
     7     while (*str == ' '){
     8         str++;
     9     }
    10     while ((*str == (char)0xA1) && (*(str + 1) == (char)0xA1)){
    11         str += 2;
    12     }
    13     int nSign = (*str == '-') ? -1 : 1;//确定符号位
    14     if (*str == '+' || *str == '-'){
    15         str++;
    16     }
    17     int nResult = 0;
    18     while (*str >= '0'&& *str <= '9'){
    19         nResult = nResult * 10 + (*str - '0');
    20         str++;
    21     }
    22     return nResult *nSign;
    23 }
    24 
    25 int main(){
    26     printf("%d
    ", Myatoi("12345"));
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    (Eclipse) Selection does not contain main type
    Spring Web Flow 学习笔记(2)-流程的组件
    Spring Web Flow 学习笔记(1)
    快速排序java实现
    二路归并排序
    剪切板
    Java中空白final
    对象引用和对象
    Java复用类
    Experimenrs on Foursquare
  • 原文地址:https://www.cnblogs.com/haoyul/p/7623011.html
Copyright © 2020-2023  润新知