1 atoi()函数
atoi函数是将字符串转换成整数。头文件<stdlib.h>
注意:
1、数字字符前有空格存在则跳过。
2、数字字符前有+、-号作为整数的正负号处理。
3、数字字符前有其他字符则返回0。
atoi()函数的使用:
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include <stdio.h>
int main() {
//atoi 字符串转整形 处理空格、数字、正负开头;其余开头返回0
//int num = atoi("123"); //123
//int num = atoi(" a123"); //0
//int num = atoi(" -123 ");//-123
//int num = atoi(" +123 "); //123
int num = atoi(" 12abc3 ");//12
printf("%d\n", num);
}
2 itoa()函数
itoa()函数是将整数类型转为字符串。头文件<stdlib.h>
itoa()函数有三个参数:
第一个参数是要转换的数字
第二个参数是要写入转换结果的目标字符串
第三个参数是转移数字时所用的基数
itoa()函数的使用:
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include <stdio.h>
int main() {
//itoa 整形转字符串
char buff[128] = { 0 };
//_itoa(100, buff, 10);//100 100以十进制存储在buff中
//_itoa(100, buff, 8);//144 100以八进制存储在buff中
_itoa(100, buff, 16);//64 100以十六进制存储在buff中
printf("%s\n", buff);
}
3 my_atoi()函数的实现
有改进空间!!!
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <cassert>
#include <stdio.h>
#include <stdlib.h>//atoi itoa
#include<string.h>
#include<ctype.h>//isdigit()
//统计连续数字字符个数
int getcount(const char* str) {
int count = 0;
int len = strlen(str);
for (int i = 0; i < len-1; i++) {//不判断如abc123类,核心部分判断
if (str[i] == ' '||str[i]=='-'||str[i]=='+') continue;
if (isdigit(str[i])&&isdigit(str[i+1])) {
count++;
}
else {
break;
}
}
return count+1;
}
int my_atoi(const char* str) {
assert(str != NULL);
while (*str == ' ') { //排除空格
str++;
}
if (*str != '+' && *str != '-' && !isdigit(*str)) { //空格之后为非符号且非数字字符
return 0;
}
int bit = getcount(str); //连续数字字符出现的个数
int result = 0 , flag = 1;
while (*str != '\0') {
if (*str == '-') {
flag = -1;
str++;
if (*str == '-' || *str == '+')
return 0;
}
else if (*str == '+') {
flag = 1;
str++;
if (*str == '-' || *str == '+')
return 0;
}
else {
result += (*str - '0') * (int)pow(10, bit - 1);
str++;
bit--;
}
}
return result*flag;
}
int main() {
//int value = my_atoi(" -12sd3"); -12
//int value = my_atoi(" -123abc");-123
//int value = my_atoi(" +123abc3"); 123
int value = my_atoi(" abc123abc"); //0
printf("%d\n", value);
return 0;
}
4 my_itoa()函数的实现
思想较my_atoi()难。
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <cassert>
#include <stdio.h>
#include <stdlib.h>//atoi itoa
#include<string.h>
char* my_itoa(int value, char* buff, int radix) {
assert(buff != NULL && radix >= 2);
//辗转相除法
//查表法
char alpha[] = "0123456789abcdefghijklmnopqrstuvwxyz";
int flag = 0;
if (value < 0 && radix == 10) {
buff[0] = '-';
value *= -1;
flag = 1;
}
unsigned int val = (unsigned int)value;
int i = flag;
while (val != 0) {
buff[i++] = alpha[val % radix];
val /= radix;
}
buff[i] = '\0';
for (int j = 0; j < (i - 1)/2; j++) {
char temp = buff[j+flag];
buff[j+flag] = buff[i - 1 - j];
buff[i - 1 - j] = temp;
}
return buff;
}
int main() {
char buff[128] = { 0 };
//my_itoa(100, buff, 8);//144
//my_itoa(10, buff, 16);//a
//my_itoa(10, buff, 2);//1010
//my_itoa(-1, buff, 2);//11111111111111111111111111111111
//my_itoa(-1, buff, 16);//ffffffff
//my_itoa(-1, buff, 8);//37777777777 32个1 三位划分 开头补0 011--3 111-7 ……
//my_itoa(-1, buff, 10);//-1
my_itoa(-1234, buff, 10);//-1234
printf("%s\n", buff);
return 0;
}