整型转换为字符串
void itoa(int a, char *str)
{
char numStr[] = {'0','1','2','3','4','5','6','7','8','9'};
char tempC;
int temp = a;
int i = 0;
int begin = 0;
int end = 0;
if (a < 0)//a是负数要做特殊处理
{
temp = -a;
str[i++] = '-';
begin = 1;
}
do
{
str[i++] = numStr[temp%10];
temp = temp / 10;
}while (temp);//得到了对应的字符串数组了,但是元素是原本的倒序排放的,还需倒序回来
str[i] = '\0';
end = i - 1;//最后一个元素在数组中的索引
while(begin < end)//前后交换数组的元素,把数组的元素倒序放回来
{
tempC = str[end];
str[end] = str[begin];
str[begin] = tempC;
end--;
begin++;
}
}
输出int类型在计算机中存储的二进制
//整型在计算机中存储的二进制转成字符串并存储在对应的数组中
void Int2Str(int num, char *a)
{
unsigned int temp = 1;
unsigned int len = (sizeof(int) * 8);//int类型的字节数
char *pa = a;
union
{
int num;
unsigned int unum;
}un;
un.num = num;//使用联合体的方式吧有符号数使用无符号数的方式读取出来
//应为有符号数做右移操作会补位1与无符号的补位0不一样
temp = temp << (len - 1);//1000
for (int i = 0; i < len; i++)
{
//与1000 做与操作就会取出 数的第一个比特位
*pa++ = ((un.unum & temp) >> (len - 1)) + 48;
un.unum = un.unum << 1;//取了第一个比特位后数调整,左移,为取下一个数的第一位做准备
}
*pa = '\0';
return;
}
输出浮点数在计算机中存储的二进制
void PrintFlo2Bin(float a)
{
typedef union
{
float f;//float是4个字节的共32位
unsigned int i;//unsigned int也是4个字节的共32位
}f2i;
f2i fi;
fi.f = a;
cout<<"float:"<<a<<endl;
unsigned int b = fi.i;
cout<<"unsigned float:"<<b<<endl;
int c[32];
int j = 31;
for (int i = 0; i < 32 && j >= 0; i++)//float是4个字节的共32位
{
c[j--] = b & 1;
b = b >> 1;
}
j = 0;
while (j < 32)
{
cout <<c[j++]<<" ";
}
cout <<endl;
}
在字符串中找字符串
//字符串中找字符串,找到返回对应的地址,否则返回NULL
const char *StrFinStr(const char *destStr, const char *sourStr)
{
const char *tempDestStr = destStr;
const char *tempSourStr = sourStr;
if (destStr == NULL || sourStr == NULL)
{
return NULL;
}
while (*destStr != '\0')//遍历目的的字符串
{
tempDestStr = destStr;
tempSourStr = sourStr;
do
{
if (*tempSourStr == '\0')//找到了,即子串以及到了结束符的位置
{
return destStr;
}
}while (*tempDestStr++ == *tempSourStr++);//连续相等
destStr++;
}
return NULL;
}
比较字符串
int MyStrCmp(const char *destStr, const char *sourStr)
{
int ret = 0;
while (!(ret = (*(unsigned char *)destStr - *(unsigned char *)sourStr)) && (*sourStr))//只要不相等或者sourStr遍历完了都跳出循环了
{
destStr++;
sourStr++;
}
if (ret < 0)
{
return -1;
}
else if(ret > 0)
{
return 1;
}
else
{
return 0;
}
}
字符串拷贝
//字符串拷贝前提条件:sour指向的字符串必须以\0结束
char *strcpy(char *dest, const char *sour)
{
if (dest == NULL || sour == NULL) return dest;
char *d = dest;
while(*d++ = *sour++); /*这里有三个知识点 :
1、会把源字符的'\0' 一起拷贝
2、相当于(*d++ = *sour) != 0
3、*d++ 与 *(d++)是一样的
*/
return dest;
}
内存拷贝函数
//内存拷贝,前提条件是copyLen不能超过dest的实际内存长度
void *memcpy(void *dest, const void *sour, int copyLen)
{
if (dest == NULL || sour == NULL) return dest;
char *d = (char *)dest;
const char *s = (char *)sour;
while(copyLen > 0)
{
*d++ = *s++;
--copyLen;
}
return dest;
}
找出两个字符串中的最长公共子串
方法1://列如shortStr:adc longStr:efgdc 即 依次搜索字符adc ad dc a d c 是否在longStr中存在
//找出两个字符串中的最长公共子串
char *ComStr(char *str1, char *str2)
{
char *shortStr = str1;
char *longStr = str2;
char *subStr = NULL;
unsigned int shortLen = strlen(str1);
if (str1 == NULL || str2 == NULL)
{
return NULL;
}
//先判断哪个是最短的字符串以及其长度
if (strlen(str1) > strlen(str2))
{
shortStr = str2;
longStr = str1;
shortLen = strlen(str2);
}
//分配存储最长公共子串的长度
subStr = (char *)malloc(shortLen + 1);
//最长公共子串由最大的个数的子串开始遍历 :
//列如shortStr:adc longStr:efgdc 即 依次搜索字符adc ad dc a d c 是否在longStr中存在
//缺陷如果有多个同长的公共子串那么只会返回第一个找到的
for (int i = shortLen; i > 0; i--)
{
for (int j = 0; j <= shortLen - i; j++)
{
MyMemCpy(subStr, &shortStr[j], i);
subStr[i] = '\0';
if (StrFinStr(longStr, subStr) != NULL)
{
return subStr;
}
}
}
return NULL;
}
方法2:
char *getLCS(char *str1, char *str2)
{
char *shortStr = NULL;//Str1和Str2中最短的字符串
char *longStr = NULL;
unsigned int shortLen;//最短的字符串的长度
unsigned int longLen;
char *subStr = NULL;//返回去存储最长LCS的 内存的地址指针
int **array = NULL;//二维数组
int maxLCSLen = -1;//最长公共子序列的长度
unsigned int LCSIndexInShortStr = 0;//最长公共子序列在最短字符串中的开始索引
if (str1 == NULL || str2 == NULL)
{
return NULL;
}
if(strlen(str1) > strlen(str2))
{
shortStr = str2;
longStr = str1;
shortLen = strlen(str2);
longLen = strlen(str1);
}
else
{
shortStr = str1;
longStr = str2;
shortLen = strlen(str1);
longLen = strlen(str2);
}
subStr = (char *)malloc(shortLen + 1);//字符串中有‘\0’内存大小等于字符串长度加1
array = (int **)malloc(shortLen * sizeof(int *));//申请二维数组内存空间
for (int i = 0; i < shortLen; i++)
{
array[i] = (int *)malloc(longLen * sizeof(int));
}
for (int i = 0; i < shortLen; i++)
{
for (int j = 0; j < longLen; j++)
{
if (*(shortStr + i) == *(longStr + j))
{
if (i == 0 || j == 0)
{
array[i][j] = 1;
}
else
{
array[i][j] = array[i-1][j-1] + 1;
}
}
else
{
array[i][j] = 0;
}
if (maxLCSLen < array[i][j])
{
maxLCSLen = array[i][j];//记录最大的LCS的长度
LCSIndexInShortStr = i - maxLCSLen + 1;//最长公共子序列在最短字符串中的开始索引
}
}
}
MyMemCpy(subStr, &shortStr[LCSIndexInShortStr], maxLCSLen);
subStr[maxLCSLen] = '\0';
return subStr;
}
获取整型对应的16进制的字符串
char *get16String(int a)
{
char *buffer = (char *) malloc(11);//申请11个字节的内存空间int类型32bit = 4 * 8 另外多久三个字节存'0' 'x' '\0'
char *temp;
buffer[0] = '0';
buffer[1] = 'x';
buffer[10] = '\0';
temp = &buffer[2];
for (int i = 0; i < 8; i++)
{
temp[i] = (char)(a<<4 * i >>28);//从头到尾4个bit的取出来,注意哦有符号数的右移操作左边空位是补1的,
temp[i] = temp[i] >= 0 ? temp[i] : temp[i] + 16;//例如char类型的10010000 右移4位后变为 11111001 了所以得加上16:10000消去前面补上的1
temp[i] = (temp[i] < 10) ? temp[i] + 48 : temp[i] + 55;//'0'=48, 'A'=65
}
return buffer;
}
char字符读取的练习
//char *a = "1234|5678|我是来讲|||||撒旦法asdf|asfdf|123545";
//读出上述的字符,以|分隔
void print(char *a)
{
char *begin = a;
char *end = a;
int count = 0;
int i = 0;
char word[100];
while (*begin)
{
count = 0;
while(*end != '|')
{
if (*end == '\0') break;
end++;
count++;
}
if (count)
{
memcpy(word, begin, count);
word[count] = '\0';
cout<<"File"<<i++<<":"<<word<<endl;
}
if (*end)
{
end++;
begin = end;
}
else
{
break;
}
}
}
//字符串转成二进制
void C2Bin(const char *a)
{
int array[8];
char cmp = 0x80;
int j = 7;
for (int i = 0; i < 8; i++)
{
//(((*a) << (7 - i)) & (0x80));//注意必须加个char类型的转换
array[j--] = ((char)(((*a) << (7 - i)) & (0x80)) == (cmp) ? 1:0);
}
for (int i = 0; i < 8; i++)
{
cout<<array[i];
}
cout<<" ";
}
//字符类型转换为16进制
void C2Hex(const char *a)
{
char temp;
cout<<"0x";
for (int i = 0; i < 2; i++)
{
temp = *a<<i*4;
temp = temp>>4;
//上面不能写为temp = *a << i * 4 >> 4,,对于i = 1的情况有问题,,好像编译器会优化掉。
temp = temp >= 0 ? temp:(temp + 16);
temp = temp < 10 ? (temp + 48): (temp + 55);
cout<<temp ;
}
cout<<",";
}