1.
#include
#include
int cmp( const void *a , const void *b )
{
return *(char *)a - *(char *)b;
}
int main(){
char num[][11] = {"hello","world","we","dgut","university","abc","china","Shandon","Changqing","Jinan","computer"};
qsort(num,10,sizeof(num[0]),cmp);
for (int i = 0; i < 11; i++)
{
printf("%s、",num[i]);
}
}
2.
#include
#include
#include
#include
void root = NULL;
void *xmalloc(unsigned n)
{
void *p;
p = malloc(n);
if(p) return p;
fprintf(stderr,"insufficient memory\n");
exit(EXIT_FAILURE);
}
int compare(const void *pa,const void *pb)
{
if ((int *)pa < *(int *)pb)
return -1;
if (*(int *)pa > *(int *)pb)
return 1;
return 0;
}
void action(const void *nodep,const VISIT which,const int depth)
{
int *datap;
switch (which)
{
case preorder:
break;
case postorder:
datap = *(int **)nodep;
printf("%6d\n",*datap);
break;
case endorder:
break;
case leaf:
datap = *(int **)nodep;
printf("%6d\n",*datap);
break;
}
}
int main(void)
{
int i,*ptr;void val;
stand(time(NULL));
for ( i = 0; i < 12; i++)
{
ptr = xmalloc(sizeof(int));
*ptr = rand()&0xff;
val = tsearch((void *)ptr,&root,compare);
if (val == NULL)
{
exit(EXIT_FAILURE);
}else if (((int **)val) != ptr)
{
free(ptr);
}
}
twalk(root,action);
tdestory(root,free);
exit(EXIT_SUCCESS)
}
思考与练习题3.1:
思考与练习题3.6
SYNOPSIS
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
DESCRIPTION
The functions gettimeofday() and settimeofday() can get and set the
time as well as a timezone. The tv argument is a struct timeval (as
specified in ):
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
and gives the number of seconds and microseconds since the Epoch (see
time(2)). The tz argument is a struct timezone:
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
include
include
include
int main()
{
struct timeval start,end;
gettimeofday(&start,NULL);
sleep(1);
gettimeofday(&end,NULL);
suseconds_t msec = end.tv_usec-start.tv_usec;
time_t sec = end.tv_sec-start.tv_sec;
printf("time used:%u.%us\n",sec,msec);
return 0;
}
3.7:
#include
#include
int cmp ( const void *a , const void *b )
{
return *(float *)a > *(float *)b ? 1 : -1;;
}
int main(){
float num[10] = {90.9,51.8,32.7,83.6,94.5,45.4,36.3,47.2,28.1,19.0};
qsort(num,10,sizeof(num[0]),cmp);
for (int i = 0; i < 10; i++)
{
printf("%0.1f、",num[i]);
}
3.17
argc=4
argv[3]=complex param
argv[2]=praram2
argv[1]=param1
PHONE =10086