本来再想,写这样的初级代码发还是不发,后来还是帖了出来。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static void genIntArray(int *arr, int nlen)
{
srand((unsigned)time(NULL));
while(nlen--) *arr++ = rand() % 300 + 5;
}
static void insertion_sort(int *arr, int nlen, int (*sort_func)(int, int))
{
int i, j, tmp;
for( i = 1; i < nlen; i++) {
j = i;
while( j > 0 && sort_func(arr[j], arr[j - 1])) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
--j;
}
}
}
static int lower_sort(int num1, int num2)
{
return num1 > num2;
}
static int upper_sort(int num1, int num2)
{
return num1 < num2;
}
static void printArray(int *arr, int nlen)
{
printf("[");
while(--nlen) printf("%d, ", *arr++);
printf("%d]\n", *arr);
}
int main(void)
{
int len = 10;
int arr[len];
genIntArray(arr, len);
printf("before sort:");
printArray(arr, len);
insertion_sort(arr, len, lower_sort);
printf("after lower sort:");
printArray(arr, len);
insertion_sort(arr, len, upper_sort);
printf("after upper sort:");
printArray(arr, len);
return 0;
}
后记:在编程中,时常把一个函数当做参数传来传去,这也是编程里面比较有意思的事情。
#include <stdlib.h>
struct List {
int index;
struct List *next;
};
void create_list(struct List **list, int len)
{
*list = (struct List *) malloc(sizeof(struct List));
(*list)->next = NULL;
struct List* p;
while(len)
{
p = (struct List*) malloc(sizeof(struct List));
p->index = len--;
p->next = (*list)->next;
(*list)->next = p;
}
}
void reverse_list(struct List **list)
{
struct List *next, *prev, *node;
prev = NULL;
node = *list;
while(node) {
next = node->next;
if(next == NULL) *list = node;
node->next = prev;
prev = node;
node = next;
}
}
void destory_list(struct List *list)
{
if(list->next) destory_list(list->next);
free(list);
list->index = 0;
list->next = NULL;
}
int main(void)
{
struct List *data;
create_list(&data, 20);
reverse_list(&data);
destory_list(data);
return 0;
}
东抄抄, 西抄抄, 写了个大概, 玩着玩着都会感觉混淆了.
先前看了SDL的SDL_Event, 不大明白其中的用法,后来翻了翻书中关于union的用法,再则看到了云风的这篇文章C 语言中统一的函数指针, 就写了一个不成文的例子, 把union作为函数的参数传递, 可以将两个毫不相干的不同类型的结构数据粘合在一起, 的确很有意思.
#include <stdio.h>
#include <stdlib.h>
typedef enum bool {false, true} bool;
typedef struct Number {
unsigned char type;
unsigned int num;
bool sign;
} Number;
typedef struct OP {
unsigned char type;
char ch;
} OP;
typedef union Button {
unsigned char type;
Number num;
OP op;
} Button;
void printNum(int num)
{
printf("Print Number:%d\n", num);
}
void printOP(char op)
{
printf("Print Op:%c\n", op);
}
void cal(Button p)
{
switch(p.type)
{
case 1:
printNum(p.num.num * ((p.num.sign) ? 1 : -1));
break;
case 2:
printOP(p.op.ch);
break;
}
}
int main()
{
Button p1;
p1.num = (Number) {1, 9, false};
cal(p1);
p1.op = (OP) {2, '='};
cal(p1);
exit(0);
}
这里要注意结构体的第一个字段一定要与union的第一个字符对应起来, 至于为什么, 去翻看union用法吧...
int
utf8_char_size(const unsigned char c)
{
if(c < 0x80) return 1;
if((c & 0xc0) == 0x80) return 0;
int mask = 0x80;
int num = 0;
while(mask & c)
{
++num;
mask >>= 1;
}
return num;
}
int
utf8_len(const char *utf8)
{
const unsigned char *input = (const unsigned char *)utf8;
int count = 0;
while(*input) {
while((*input & 0xc0) == 0x80)
++input;
count++;
input++;
};
return count;
}
int
utf8_char_at(const char *utf8, int index)
{
const unsigned char *input = (const unsigned char *)utf8;
int count = 0;
while(*input) {
if(count == index) return input - (const unsigned char *)utf8;
input += utf8_char_size(*input);
count++;
};
return -1;
}
int utf8_char_size(const unsigned char c) //取得utf-8字符的长度.
例子: char *str="中文";utf8_char_size(*str)=3;utf8_char_size(*++str)=0
int utf8_len(const char *utf8) //取得字符窜的长度,类似wcslen(wchar_t)
例子: utf8_len("hello你好,world") = 17
int utf8_char_at(const char *utf8, int index) // 取得utf-8字符窜真实地址偏移值
例子:utf8_char_at("hello你好,world", 7) = 11
先前在百度空间里帖了一个函数,很乱也蛮烂的
http://hi.baidu.com/pallove/blog/item/ea3c0b469c4bf0076a63e53e.html
早上使用strstr函数再写了一个,一下感觉清爽多了。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char* replace(const char *src, const char *from, const char *to, char *dest)
{
int idx = 0;
char *find;
while(1) {
if(find = strstr(src, from)) {
idx = find - src;
strncat(dest, src, idx);
strncat(dest, to, strlen(to));
src += idx + strlen(from);
}
else {
strncat(dest, src, strlen(src));
break;
}
}
return dest;
}
int main()
{
char *dest = malloc(1024);
strcpy(dest, "result:");
printf("%s\n", replace("hello, abcabctestasdlfklasdjabc ldsafabc", "abc", "123", dest));
exit(0);
}
指向整形数组的指针