程式設計常用子函式

2021-07-11 01:25:22 字數 3688 閱讀 2632

1,有序陣列中二分查詢(相等時最左或最右)

int binarysearchvalue(int a, int n, int value, bool is_left)

} return result;

}

2,字串的動態增加並排序

思想:1,擴容:利用動態陣列思想,當字串陣列array_str元素滿了時,通過分配拷貝釋放辦法擴容

2,排序:開闢乙個字串陣列的下標的排序陣列array_pos,利用二分查詢,找到新元素下標該插入位置,然後將新元素下標插入到下標陣列。

#include #include #include typedef char bool;

typedef struct buffer buffer;

typedef struct array array;

typedef struct num_array num_array;

#define picece_size 64

buffer *buffer_init(void)

return b;

}void buffer_struct_free(buffer *b);

void buffer_exit(buffer *b)

return ;

}buffer *buffer_copy_string(buffer *b, char *str)

if (!(b->str = calloc(b->size, sizeof(char))))

} memcpy(b->str, str, strlen(str));

b->str[strlen(str)] = '\0';

b->used = strlen(str) + 1;

}int buffer_reset(buffer *b)

void buffer_context_free(buffer *b)

void buffer_struct_free(buffer *b)

void buffer_free(buffer *b)

int buffer_copy(buffer *dst, const buffer *src)

} memcpy(dst->str, src->str, src->used);

dst->used = src->used;

}int buffer_compare(buffer *b1, buffer *b2)

return b1->used - b2->used;

}array *array_init(void)

bool array_is_full(array *a)

void array_contxt_free(array *a)

void array_struct_free(array *a)

void array_free(array *a)

void array_exit(array *a)

free(a);

a = null;

}#else

array_struct_free(a);

#endif

return ;

}num_array *num_array_init(void)

void num_array_free(num_array *n)

void num_array_exit(num_array *n)

return ;

}#if 0

void realloc_array(array *a)

int i;

for (i = 0; i < used; i++)

array_free(a);

a->bptr = b;

a->used = used;

a->size = size;

return ;

}#else

void enlarge_array(array *a)

#endif

int get_insert_pos(array *a, num_array *n, int left, int right, buffer *target)

return left;

}void enlarge_num_array(num_array *n);

void soted_array_dupinclude_insert(array *a, num_array *n, int index)

if (n->used + 1 > n->size)

int i;

for (i = 0; i < used; i++)

tmp[i] = n->nptr[i];

free(n->nptr);

n->nptr = tmp;

n->used = used;

n->size = size;

#else

enlarge_num_array(n);

#endif

} int pos = get_insert_pos(a, n, 0, n->used - 1, a->bptr[index]);

if (pos >= 0) else if (pos == -1)

n->used++;

return ;

}void enlarge_num_array(num_array *n)

void array_insert(array *a, num_array *n, char *str)

if (!(a->bptr[a->used] = buffer_init()))

exit(1);

buffer_copy_string(a->bptr[a->used], str);

a->used++;

soted_array_dupinclude_insert(a, n, a->used - 1);

return ;

}int rand_str_generate(char *str, int max_len)

str[count - 1] = '\0';

return 0;

}void print_sorted_str(array *a, num_array *n)

}int main(void)

print_sorted_str(a, n);

array_free(a);

num_array_free(n);

return 0;

}

3,命令列引數解析函式getopt()

每呼叫一次getopt()函式,返回乙個選項,如果該選項有引數,則optarg指向該引數

#include #include #include int main(int argc, char **argv)

4,字串hash函式

unsigned int dek_hash(char *str)

return hash;

}

5,隨機字串生成函式

int rand_str_generate(char *str, int max_len)

str[count - 1] = '\0';

return 0;

}

常用子函式

一.回文數的判斷函式 1.整數型別 bool hws int n if sum cun return true else return false 利用反轉的數字與之前的數比較,若相同,則它是回文數。return sum 2.字串型別 bool hws string a if i len retur...

python函式式程式設計例子 函式式程式設計

函式是python內建支援的一種封裝,我們通過把大段 拆成函式,通過一層一層的函式呼叫,就可以把複雜任務分解成簡單的任務,這種分解可以稱之為面向過程的程式設計。函式就是面向過程的程式設計的基本單元。而函式式程式設計 請注意多了乙個 式 字 functional programming,雖然也可以歸結...

vue常用的鉤子函式

beforecreate 這個時候,this變數還不能使用,在data下的資料,和methods下的方法,watcher中的事件都不能獲得到 beforecreate data methods created 這個時候可以操作vue例項中的資料和各種方法,但是還不能對 dom 節點進行操作 crea...