#include <stdio.h>
用递归的方式进行数组的打印
void printArray(int *begin, int *end)
{
if(begin > end)
{
return ;
}
else
{
printf("%d\n", *begin);
printArray(begin + 1, end);
}
/* while(begin <= end)
{
printf("%d\n", *begin);
++begin;
}
*/
}
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
用迭代器的方法进行函数的逆序
void reverse(int *begin, int *end)
{
if(begin >= end)
{
return ;
}
else
{
swap(begin, end);
reverse(begin + 1, end - 1);
}
// while(begin < end)
// {
// swap(begin++, end--);
// }
}
选择排序void choiceSort(int *begin, int *end)
{
int *p;
for(;begin < end;++begin)
{
for(p = begin + 1;p <= end;++p)
{
if(*begin > *p)
{
swap(begin, p);
}
}
}
}
迭代器查找的实现
int *binaryFind(int *begin, int *end, int n)
{
}
void qSort(int *begin, int *end)
{
if(begin > end)
{
return ;
}
int t = *begin;
int *p = begin;
int *q = end;
while(p < q)
{
while(p < q && *q >= t)
{
--q;
}
while(p < q && *p <= t)
{
++p;
}
swap(p, q);
}
swap(begin, p);
qSort(begin, p - 1);
qSort(p + 1, end);
}
int main(void)
{
int a[] = {1,-2,3,-4,5,-6,7,-8,9,0};
int len = sizeof(a) / sizeof(*a);
qSort(a, a + len - 1);
printArray(a, a + len - 1);
return 0;
}
#endif
void Puts(const char *s)
{
while(*s)
{
putchar(*s++);
}
putchar('\n');
}
对于const修饰指针,不可以改变指针的所指的值,但可以改变指针的指向
int Strlen(const char *s)
{
if(*s == '\0')
{
return 0;
}
else
{
return Strlen(s + 1) + 1;
}
/* int counter = 0;
while(*s)
{
++s;
++counter;
}
return counter;
*/
}
char型数组拷贝函数
char *Strcpy(char *dest,const char *src)
{
char *ret = dest;
while(*src)
{
*dest++ = *src++;
}
*dest = 0;
return ret;
}
void类型修饰的可以应用所有类型的变量,但是不能用*p的形式进行赋值,要强转,不推荐这种方法
void *Memcpy(void *dest, const void *src, unsigned long n)
{
char *p = (char *)dest;
char *q = (char *)src;
while(n)
{
*p++ = *q++;
--n;
}
return dest;
}
char *Strcat(char *dest, const char *src)
{
char *ret = dest;
while(*dest)
{
++dest;
}
while(*src)
{
*dest++ = *src++;
}
*dest = 0;
return ret;
}
实现数组的相互连接
char *Strncat(char *dest, const char *src, unsigned long n)
{
char *ret = dest;
while(*dest++)
{
}
while(*src && n)
{
*dest++ = *src++;
--n;
}
*dest = 0;
return ret;
}
int Strcmp(const char *s1, const char *s2)
{
while(*s1 == *s2 && *s1 && *s2)
{
++s1;
++s2;
}
return *s1 - *s2;
}
int Strncmp(const char *s1, const char *s2, unsigned long n)
{
while(--n && *s1 == *s2 && *s1 && *s2)
{
++s1;
++s2;
}
return *s1 - *s2;
}
int main(void)
{
printf("%d\n", Strncmp(" Hello" + 1, "Herld", 2));
/* char s[100] = "Hello";
Strncat(s, "World!" + 1, 4);
puts(s);
void *p;
int i;
double d = 100.23;
p = &d;
double *q;
q = (double *)p;
printf("%f\n", *q);
int a[10] = {1,2,3,4,5,6,7,8,9,0};
int b[10];
Memcpy(a + 3, a, sizeof(a));
int i;
for(i = 0;i < sizeof(a) / sizeof(*a);++i)
{
printf("%d\n", b[i]);
}
int i = 10;
const int *p;
p = &i;
*p = 100;
*/
// printf("%d\n", sizeof(char *));
// putchar(*p);
/* char s1[100] = "Hello";
char s2[100];
Strncpy(s2, "Hello World!" + 3, 5);
puts(s2);
*/
return 0;
}