1、abs
功能:求绝对值
2、atof
功 能: 把字符串转换成浮点数
例子:
#include <stdlib.h>
#include <stdio.h>
int main()
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
3、atoi
例子:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
执行结果:
string = 12345.67 integer = 12345
4、atol
功能:把字符串转换成长整型数
功能:二分法搜索
参数:第一个:要查找的关键字。第二个:要查找的数组。第三个:指定数组中元素的数目。第四个:每个元素的长度(以字符为单位)。第五个:指向比较函数的指针。
例子:
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
int compareints (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int values[] = { 50, 20, 60, 40, 10, 30 };
int main ()
{
int * pItem;
int key = 40;
qsort (values, 6, sizeof (int), compareints); //必须有序才可以进行二分查找
pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem!=NULL)
printf ("%d is in the array.\n",*pItem);
else
printf ("%d is not in the array.\n",key);
return 0;
}
例2:
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
int compareints (const void * a, const void * b)
{
return ( *(char*)a - *(char*)b );
}
char test[] = "chenchong";
int main ()
{
char* pItem;
char key = 'e';
qsort (test, 6, sizeof (char), compareints);
printf("%s\n",test);
pItem = (char*) bsearch (&key, test, 6, sizeof (char), compareints);
printf("%s\n",test);
if (pItem!=NULL)
printf ("%d is in the array.\n",*pItem);
else
printf ("%d is not in the array.\n",key);
return 0;
}