字符转成数字

来源:互联网 发布:中国经济数据折线图 编辑:程序博客网 时间:2024/06/05 23:04

#include "Stdio.h"
#include "Conio.h"
int strint(const char *str);
main()
{
    int n;
    char p[]="-123";
    n=strint(p);
    printf("%d",n);

    getch();
}
int strint(const char *str)
{
    int temp=0;
    const char *ptr=str;
    if (*str=='-'||*str=='+')
        str++;
    while(*str!=0)
    {
        if((*str<'0')||(*str>'9'))
        break;
        temp=temp*10+(*str-'0');
        str++;
    }
    if(*ptr=='-')
    temp=-temp;
    return temp;
}

原创粉丝点击