Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59820
  • 博文数量: 22
  • 博客积分: 780
  • 博客等级: 军士长
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-08 17:48
文章分类

全部博文(22)

文章存档

2010年(8)

2009年(14)

我的朋友
最近访客

分类: C/C++

2010-01-10 18:02:29

(1) safe copying overlapping strings
char *sstrcpy(char *to, char *from)
{
    memmove(to, from, 1+strlen(from));
    return to;
}


char *sstrcat(char *to, char *from)
{
    sstrcpy(to + strlen(to), from);
    return to;
}

(2)determine the size of a file.

long flength(char *fname)
{
      FILE *fptr;
      long length = 0L;


      fptr = fopen(fname, "rb");
      if(fptr != NULL)
      {
            fseek(fptr, 0L, SEEK_END);
            length = ftell(fptr);
            fclose(fptr);
      }


      return length;
}

(3)

/* round to integer */
#define iround(x) floor((x)+0.5)
/* round number n to d decimal points */
#define fround(n,d)  (floor((n)/pow(.1,(d))+.5)*pow(.1,(d)))

(4)shell sort an array of strings

void strsort (char **array, size_t array_size)
{
      size_t gap, i, j;
      char **a, **b, *tmp;


      for (gap = 0; ++gap < array_size; )
            gap *= 2;
      while (gap /= 2)
      {
            for (i = gap; i < array_size; i++)
            {
                  for (j = i - gap; ;j -= gap)
                  {
                        a = array + j;
                        b = a + gap;
                        if (strcmp(*a, *b) <= 0)
                              break;
                        tmp = *a;
                        *a = *b;
                        *b = tmp;
                        if (j < gap)
                              break;
                  }
            }
      }
}

阅读(268) | 评论(0) | 转发(0) |
0

上一篇:Mistakes of C

下一篇:Python的os.popen无法执行

给主人留下些什么吧!~~