//函数名:myatof //功能:把字符串转化成double浮点型 //名字来源:my array to floating point numbers //函数说明:接收一个字符串判断第一个字符的符号,没有符号默认为正值,然后对剩余字符串进行转换,//遇到\0结束,最后返回一个double double myatof(const char* sptr) { double temp=10; bool ispnum=true; double ans=0; if(*sptr=='-')//判断是否是负数 { ispnum=false; sptr++ ; } else if(*sptr==' +')//判断是否为正数 { sptr++ ; } while(*sptr!='\0')//寻找小数点之前的数 { if(*sptr=='.'){ sptr++ ;break;} ans=ans*10 +(*sptr-'0'); sptr ++; } while(*sptr!='\0')//寻找小数点之后的数 { ans=ans+ (*sptr-'0')/temp; temp*=10; sptr++ ; } if(ispnum) return ans; else return ans*(-1); } //函数名:myatoi //功能:把字符串转化成int整型 //名字来源:my array to integer //函数说明:接收一个字符串判断第一个字符的符号,没有符号默认为正值,然后对剩余字符串进行转换,//遇到\0结束,最后返回一个int int myatoi(const char* sptr) { bool ispnum=true; int ans=0; if(*sptr=='-')//判断是否是负数 { ispnum=false; sptr++ ; } else if(*sptr=='+ ')//判断是否为正数 { sptr++ ; } while(*sptr!='\0')//类型转化 { ans=ans*10+ (*sptr-'0'); sptr++ ; } if(ispnum) return ans; else return ans*(-1); } |
|
来自: piaoyedu > 《C/C 面试笔试》