冒泡排序
#include
#include
#include
static void bubble_sort(int array[],int n)
{
int i,j,tmp;
for(j=0;jarray[i+1])
{
tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
}
}
}
}
int main (int argc, char **argv)
{
int a[6] = {5,2,6,4,1,3};
bubble_sort(a,6);
int i = 0;
for(i;i<6;i++)
printf("%d\n",a[i]);
return 0;
}
strstr (判断子串)
#include
#include
#include
#include
const char *my_strstr(const char *str,const char *sub_str)
{
int i;
for(i=0;str[i]!='\0';i++)
{
int tmp = i;
int j=0;
while(str[i++]==sub_str[j++])
{
if(sub_str[j]=='\0')
return &str[tmp];
}
i = tmp;
}
return NULL;
}
int main(void)
{
char *s1="12345";
char *s2="34";
printf("%s\n",my_strstr(s1,s2));
}
strcpy(字符串复制)
#include
#include
#include
#include
char *my_strcpy(char *dest, const char *src)
{
assert(dest);
assert(src);
char *tmp = dest;
while((*dest++=*src++)!='\0')
;
return tmp;
}
int main(int argc, char ** argv)
{
char a[10];
char *b = "hello";
printf("%s\n",my_strcpy(a,b));
return 0;
}
strncpy(复制子串前n个字节)
#include
#include
#include
#include
char *my_strncpy(char *dst, const char *src,size_t count)
{
assert(dst&&src);
char *tmp = dst;
while(count)
{
if((*dst=*src)!=0)
{
dst++;
src++;
}
count--;
}
*dst = '\0';
return tmp;
}
int main()
{
char a[5] = "hello";
char *b = "12345";
printf("%s\n",my_strncpy(a,b,6));
}
strcat(字符串连接)
#include
#include
#include
#include
char *my_strcat(char *dest, const char *src)
{
assert(dest);
assert(src);
char *tmp = dest;
while(*dest)
dest++;
while((*dest++=*src++)!='\0')
;
return tmp;
}
int main(int argc, char ** argv)
{
char a[10] = "world";
char *b = "hello";
printf("%s\n",my_strcat(a,b));
return 0;
}
strncat
#include
#include
#include
#include
char *my_strncat(char *dest, const char *src, size_t count)
{
assert(dest);
assert(src);
char *tmp = dest;
if(count)
{
while(*dest)
dest++;
while((*dest++=*src++)!='\0')
{
if(--count==0)
{
*dest = '\0';
break;
}
}
}
return tmp;
}
int main(int argc, char ** argv)
{
char a[10] = "world";
char *b = "hello";
printf("%s\n",my_strncat(a,b,3));
return 0;
}
strcmp(字符串比较)
#include
#include
#include
#include
int my_strcmp(const char *cs, const char *ct)
{
assert(cs);
assert(ct);
unsigned char c1,c2;
while(1)
{
c1 = *cs++;
c2 = *ct++;
if(c1!=c2)
return c1 < c2 ? -1 : 1;
if(!c1)
break;
}
return 0;
}
int main(int argc, char ** argv)
{
char a[10] = "gello";
char *b = "hello";
printf("%d\n",my_strcmp(a,b));
return 0;
}
memmove:
#include
#include
void my_memmove(void *p1,void const *p2,size_t count)
{
assert(p1);
assert(p2);
char *dest = (char *)p1;
char *src = (char *)p2;
if((dest > src) && (dest < src +count)) //判断内存重叠时的情况
{
while(count--)
{
*(dest + count) = *(src + count);
}
}
else //判断不重叠是的情况
{
while(count--)
{
*dest++ = *src++;
}
return p1;
}
}
int main()
{
int arr1[10]={1,2,3,4,5,6,7,8,9,10};
int i = 0;
my_memmove(arr1+4,arr1+2,16);
for(i=0;i<10;i++)
{
printf("%d ",arr1[i]);
}
printf("\n");
return 0;
}
memcpy
void *memcpy(void *dest, const void *src, size_t count)
{
char *tmp = dest;
const char *s = src;
while (count--)
*tmp++ = *s++;
return dest;
}
字符串逆序
#include
#include
#include
#include
char *reverse(char *dest, const char *src)
{
assert(dest);
assert(src);
char *tmp = dest;
int len,i;
len = strlen(src);
dest[len] = '\0';
for(i=0;i
二分法/折半法
#include
//递归算法
int recurbinary(int *a, int key, int low, int high)
{
int mid;
if(low > high)
return -1;
mid = low + (high-low)/2;
if(a[mid] == key) return mid;
else if(a[mid] > key)
return recurbinary(a,key,low,mid -1);
else
return recurbinary(a,key,mid + 1,high);
}
//非递归算法
int binary( int *a, int key, int n )
{
int left = 0, right = n - 1, mid = 0;
mid = left + (right - left) / 2;
while( left < right && a[mid] != key )
{
if( a[mid] < key ) {
left = mid + 1;
} else if( a[mid] > key ) {
right = mid;
}
mid = ( left + right ) / 2;
}
if( a[mid] == key )
return mid;
return -1;
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,12,13,45,67,89,99,101,111,123,134,565,677};
int b[] = {677,1,7,11,67};
int i;
for( i=0; i
volatile关键字
定义为volatile的变量表示该变量可能被意想不到的改变,使用该变量时,必须每次重新从内存中读取数据,而不是使用保存在寄存器中的备份。
eg:
(1)并行设备的硬件寄存器;如状态寄存器
(2)一个中断服务子程序中会访问的非自动变量;
(3)多线程应用中被几个任务共享的变量;
static关键字
(1)函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值;
(2)在模块内的static全局变量可以被模块内所用函数访问,但不能被模块外其它函数访问;
(3)在模块内的static函数只可被这一模块内的其它函数调用,这个函数的使用范围被限制在声明它的模块内;
const关键字
(1)欲阻止一个变量被改变,可以使用const关键字。在定义该const变量时,通常需要对它进行初始化,因为以后就没有机会再去改变它了;
(2)对指针来说,可以指定指针本身为const,也可以指定指针所指的数据为const,或二者同时指定为const;
(3)在一个函数声明中,const可以修饰形参,表明它是一个输入参数,在函数内部不能改变其值;
(4)对于类的成员函数,若指定其为const类型,则表明其是一个常函数,不能修改类的成员变量;