#include <stdlib.h> #include <stdio.h> #include <string.h> typedef enum rtValue{pNULL,txtInvalid,OverFlow,Valid}rtValue; rtValue m_error = Valid; int atoi_fxp(const char* nptr){ int i,len,negtive; long long result; if(nptr == NULL){ m_error = pNULL; return -1; } i = result = 0; negtive = 1; len = strlen(nptr); if(*nptr == '-'){ nptr++; negtive=-1; len--; } while(i < len){ if((nptr[i] < '0')||(nptr[i] > '9')){ m_error = txtInvalid; return -1; } result = result*10 + negtive*(nptr[i] - '0'); if((abs(result) > 0x0FFFFFFF)||(abs(result) & (1 << 31))){ m_error = OverFlow; return -1; } i++; } return result; } int main(int argc,char** argv){ if(argc != 2){ perror("usage wrong\n"); return -1; } printf("the source str is %s\n",argv[1]); printf("the converted data is : %d\n",atoi_fxp(argv[1])); switch(m_error){ case pNULL: printf("the pointer is NULL \n");break; case txtInvalid: printf("the source string is wrong\n");break; case OverFlow: printf("the result is overflow\n");break; default: break; } }
转载于:https://www.cnblogs.com/fxplove/articles/2496434.html