把字符串转换为整数
cpp
int main()
{
char pn[21];
cout << "请输入一个由数字组成的字符串: ";
cin >> pn;
int last = 0;
int res[10];
int j = strlen(pn);
int idx = 2;
cout << "请选择(2-二进制,10-十进制): ";
cin >> idx;
for (int i = 0,len = strlen(pn);i < len; i++)
{
if (pn[i] < '0' || pn[i] > '9') { cout << "非法字符!!!"; break; }
last += (pn[i] - '0') * pow(idx,j-1);
j = j - 1 ;
}
cout << last;
}
重写strcat和strncat
cpp
char* mystrcat(char* dest, const char* src)
{
memcpy(dest + strlen(dest), src, strlen(src) + 1);
return dest;
}
char* mystrncat(char* dest, const char* src, const size_t n)
{
size_t len = strlen(dest);
memcpy(dest + len, src, n);
*(dest + len + n) = 0;
return dest;
}
重写strchr和strrchr
注意⚠️:这里不能从后往前找,因为想从后往前找就只能使用strlen,而strlen本质就是遍历一遍字符串,这样就更慢了。
cpp
const char* mystrchr(const char* s, int c)
{
char* p = (char*)s;
while (*p)
{
if (*p == c) return p;
p++;
}
return 0;
}
const char* mystrrchr(const char* s, int c) //查找字符
{
char* p = (char*)s;
char* p1 = 0;
while (*p)
{
if (*p == c)
{
p1 = p;
}
p++;
}
return p1;
}
重写strcmp和strncmp
cpp
int mystrcmp(const char* str1, const char* str2)
{
char* p1 = (char*)str1;
char* p2 = (char*)str2;
while (*p1 && *p2)
{
if (*p1 != *p2)
{
if (*(p1) > *(p2)) return -1;
else return 1;
}
p1++;
p2++;
}
return 0;
}
int mystrncmp(const char* str1, const char* str2, size_t n)
{
char* p1 = (char*)str1;
char* p2 = (char*)str2;
for (int i = 0; i < n; i++)
{
if (*p1 != *p2)
{
if (*(p1) > *(p2)) return -1;
else return 1;
}
p1++;
p2++;
}
return 0;
}
重写strstr
cpp
char* caijimystrstr(const char* haystack, const char* needle)
{
char* p1 = (char*)haystack;
char* p2 = (char*)needle;
char* tmp1;
char* tmp2 = p2;
while (*p1)
{
while (*p1 == *p2)
{
tmp1 = p1;
for (int i = 0; i < strlen(needle); i++)
{
if (*p1 != *p2)
{
continue;
}
p1++;
p2++;
if (*p2 == 0)
{
return p1-strlen(needle);
}
}
continue;
}
p2 = tmp2;
p1++;
}
return nullptr;
}
const char* dalaomystrstr(const char* haystack, const char* needle)
{
size_t ii = 0, jj = 0;
size_t len = strlen(haystack), slen = strlen(needle);
while ((ii < len) && (jj < slen))
{
if (haystack[ii] == needle[jj])
{
ii++, jj++;
}
else
{
ii = ii - jj;
jj = 0;
ii++;
}
}
if (jj == slen) return (haystack+(ii-jj));
return 0;
}
解析XML
cpp
bool xmlbuffer(const char* xmlbuf, const char* fieldname, char* value = 0, const size_t ilen = 0)
{
if (xmlbuf == 0 || fieldname == 0) return false;
//创建字段
int lenfieldname = strlen(fieldname);
//开辟空间
char* sfieldname = new char[lenfieldname + 3];
char* efieldname = new char[lenfieldname + 4];
//初始化
memset(sfieldname, 0, lenfieldname +3); // 错误代码 memset(sfieldname, 0, sfieldname); 原因:new出来的空间不能用memset
memset(efieldname, 0, lenfieldname +4);
//创建字段
strcpy(sfieldname, "<"); strcat(sfieldname, fieldname); strcat(sfieldname,">");
strcpy(efieldname, "</"); strcat(efieldname, fieldname); strcat(efieldname, ">");
//找字段;
char* start;
char* end;
start = (char*)strstr(xmlbuf, sfieldname);
end = (char*)strstr(xmlbuf, efieldname);
//没找到
if (start == 0 || end == 0)
{
delete[] sfieldname;
delete[] efieldname;
return 0;
}
size_t vallen = (end - start - lenfieldname - 2); //指针减去指针等于数值,表示的是从 start 到 end 的距离减去了 lenfieldname(<fieldname> 的长度)和 2(< 和 > 的长度)。
if (ilen == 0)
{
strncpy(value, start + lenfieldname + 2, vallen);
value[vallen] = 0;
}
else if (vallen <= ilen)
{
strncpy(value, start + lenfieldname + 2, vallen);
value[vallen] = 0;
}
else
{
strncpy(value, start + lenfieldname + 2, ilen);
value[ilen] = 0;
}
delete[] sfieldname;
delete[] efieldname;
return 1;
}
不重复的随机数
cpp
void rrand(int arr[], const size_t len, const int minvalue = 0)
{
srand(time(0));
for (int i = 0; i < len; i++)
{
while (1)
{
int tmp = rand() % len + minvalue;
int j = 0;
for (j = 0; j < i; j++)
{
if (tmp == arr[j]) break;
}
if (j == i)
{
arr[i] = tmp;
break;
}
}
}
}
重写strcpy和strncpy
cpp
char* my2strcpy(char* dest, const char* src)
{
int i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = 0;
return dest;
}
char* my2strncpy(char* dest, const char* src , const size_t n )
{
if (n < 0) return 0;
size_t lens = strlen(src);
if (n > lens)
{
my2strcpy(dest, src);
}
int i = 0;
for (i = 0; i < n; i++)
{
dest[i] = src[i];
}
dest[i++] = 0;
return dest;
}