#include <QCoreApplication>
int str2int(char *pStr, int* i){
if(pStr == nullptr) return 0;
bool flag = false;
char* pTem = pStr;
int temp = 0;
if(*pStr == '+' || *pStr == '-'){
pTem++;
}
//遍历字符串
while(pTem != '\0'){
if(*pTem < '0' || *pTem > '9'){ //字符串中有非数字的字符
break;
}
//从pTemp的第一位字符开始按十进制的方式累加
//每增加一位字符,前一次的数字乘以10累加
temp = temp * 10 + *pTem - '0';
pTem++;
}
//如果开头是负数,temp取相反数
if(*pStr == '-'){
temp = -temp;
}
*i = temp;
return temp;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char* p = "-1234";
int i = 0;
int ret = str2int(p, &i);
// printf("i = %d\n",i);
printf("int ret = %d\n", ret);
return a.exec();
}
str2int
最新推荐文章于 2023-12-28 14:03:24 发布