房天下 > 房天下问答 > 业主生活 > 其他
  • 两种方法实现字符串转换为数字

    函数声明int atoi(char * p);假如p=“5678”;函数的返回值就是5678用两种方法实现函数请高手解答一下

    提问者:mmyy1988

    发布于2011-01-18

共1个回答
  • 梦回 草原 丨Lv 0
    只做了正整数的情况,你看是不你想要的#include <stdio.h>#include <string.h>int String2Int1(char *pHead, char *pEnd){ int retVal=0; while (pEnd>=pHead) { retVal*=10; retVal+=*pHead-'0'; pHead++; } return retVal;}int String2Int2(char *pHead, char *pEnd){ if (pHead==pEnd) { return *pHead - '0'; } else { return 10 * String2Int2(pHead, pEnd-1)+*pEnd-'0'; }}int main(){ char *p="123456"; int r=0,t=0; r=String2Int2(p,p+strlen(p)-1); printf("%d\n",r); t=String2Int2(p,p+strlen(p)-1); printf("%d\n",t); return 0;}
    +1 2011-01-18 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。