国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法

2020-01-26 13:58:16
字體:
供稿:網(wǎng)友

C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法

字符去重函數(shù)

功能:去重字符串相鄰重復(fù)的字符,不相鄰的不用去重

參數(shù):

arg1 -- 輸入字符串
arg2 -- 字符串開始位置
arg3 -- 字符串結(jié)束位置

要求:

輸入?yún)?shù)為arg1時, 對這個字符串去重
輸入?yún)?shù)為arg1,arg2時, 從arg2位置到字符串結(jié)束,去重
輸入?yún)?shù)為arg1,arg2,arg3時,從arg2到arg3位置,去重

src/include/catalog/pg_proc.h

DATA(insert OID = 6669 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg1 _null_ _null_ _null_ ));DESCR("Remove duplicate characters.");DATA(insert OID = 6670 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg2 _null_ _null_ _null_ ));DESCR("Remove duplicate characters.");DATA(insert OID = 6671 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 23 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg3 _null_ _null_ _null_ ));DESCR("Remove duplicate characters.");

 src/backend/utils/adt/myfuncs.c

/*  * Remove duplicate characters  * author:young */Datum remove_dup_char_arg1 (PG_FUNCTION_ARGS){ int n = 0; text *arg0 = PG_GETARG_TEXT_P(0); char *str = text_to_cstring(arg0); n = strlen(str); remove_dup(str, 0, n); PG_RETURN_TEXT_P(cstring_to_text(str));}Datum remove_dup_char_arg2 (PG_FUNCTION_ARGS){ int n = 0; text *arg0 = PG_GETARG_TEXT_P(0); int32 arg1 = PG_GETARG_INT32(1); char *str = text_to_cstring(arg0); n = strlen(str); if (!(1 <= arg1 && arg1 <= n)) { ereport(ERROR,  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),  errmsg("out of range"))); } remove_dup(str, arg1 - 1, n); PG_RETURN_TEXT_P(cstring_to_text(str));}Datum remove_dup_char_arg3 (PG_FUNCTION_ARGS){ int n = 0; text *arg0 = PG_GETARG_TEXT_P(0); int32 arg1 = PG_GETARG_INT32(1); int32 arg2 = PG_GETARG_INT32(2); char *str = text_to_cstring(arg0); n = strlen(str); if (!(1 <= arg1 && arg1 <= arg2 && arg2 <= n)) { ereport(ERROR,  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),  errmsg("out of range"))); } remove_dup(str, arg1 - 1, arg2 - 1); PG_RETURN_TEXT_P(cstring_to_text(str));}void remove_dup(char *str, int start, int end){ int i = start, k = start; for (i = start; i <= end; i++)  { if (str[i + 1] && str[i + 1] == str[i] && i + 1 <= end) {  k++; }  else  {  str[i-k] = str[i]; }    } str[i-k] = '/0';}

比較繁瑣,再做一下修改,三個函數(shù)放到一個中

src/include/catalog/pg_proc.h

DATA(insert OID = 6669 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ _null_ remove_dup_char _null_ _null_ _null_ ));DESCR("Remove duplicate characters.");DATA(insert OID = 6670 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char _null_ _null_ _null_ ));DESCR("Remove duplicate characters.");DATA(insert OID = 6671 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 23 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char _null_ _null_ _null_ ));DESCR("Remove duplicate characters.");

src/backend/utils/adt/myfuncs.c

添加定義:

#define PG_GETARG_IF_EXISTS(n, type, defval) / ((PG_NARGS() > (n) && !PG_ARGISNULL(n)) ? PG_GETARG_##type(n) : (defval)) 

 修改方法:

/*  * Remove duplicate characters  * author:yangjie */Datum remove_dup_char (PG_FUNCTION_ARGS){ text *arg0 = PG_GETARG_IF_EXISTS(0, TEXT_P, NULL); int32 arg1 = PG_GETARG_IF_EXISTS(1, INT32, 0); int32 arg2 = PG_GETARG_IF_EXISTS(2, INT32, 0); int n = 0; char *str = text_to_cstring(arg0); n = strlen(str); if(PG_NARGS() == 1) { remove_dup(str, 0, n); } if(PG_NARGS() == 2) { if (!(1 <= arg1 && arg1 <= n)) {  ereport(ERROR,  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),  errmsg("out of range"))); } remove_dup(str, arg1 - 1, n); } if(PG_NARGS() == 3) { if (!(1 <= arg1 && arg1 <= arg2 && arg2 <= n)) {  ereport(ERROR,  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),  errmsg("out of range"))); } remove_dup(str, arg1 - 1, arg2 - 1); } PG_RETURN_TEXT_P(cstring_to_text(str));}

 再修改一下,如果有輸入?yún)?shù)就用 沒有就用默認(rèn)值  最后再去重處理減少代碼重用

/*  * Remove duplicate characters  * author:yangjie */Datum remove_dup_char (PG_FUNCTION_ARGS){ text *arg0 = PG_GETARG_IF_EXISTS(0, TEXT_P, NULL); int n = 0; char *str = text_to_cstring(arg0); n = strlen(str); int32 arg1 = PG_GETARG_IF_EXISTS(1, INT32, 0); int32 arg2 = PG_GETARG_IF_EXISTS(2, INT32, n);  if (!(1 <= arg1 && arg1 <= n)) { ereport(ERROR,  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),  errmsg("1 <= arg1 && arg1 <= n"))); } if (!(1 <= arg1 && arg1 <= arg2 && arg2 <= n)) { ereport(ERROR,  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),  errmsg("1 <= arg1 && arg1 <= arg2 && arg2 <= n"))); } remove_dup(str, arg1, arg2);  PG_RETURN_TEXT_P(cstring_to_text(str));}void remove_dup(char *str, int start, int end){ int i = start -1, k = start - 1; for (i = start - 1; i <= end - 1; i++)  { if (str[i + 1] && str[i + 1] == str[i] && i + 1 <= end - 1) {  k++; }  else  {  str[i-k] = str[i]; }    } str[i-k] = '/0';} 

以上就是C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 安义县| 峨眉山市| 安多县| 桦川县| 文安县| 夏邑县| 嘉峪关市| 闽清县| 滦南县| 武宁县| 玉林市| 共和县| 张家港市| 临夏县| 乌拉特中旗| 天峨县| 共和县| 福州市| 色达县| 屏山县| 塔河县| 凤城市| 克什克腾旗| 江孜县| 商河县| 灵石县| 班玛县| 金塔县| 花莲县| 织金县| 邻水| 邯郸市| 连南| 彩票| 额济纳旗| 原平市| 夏邑县| 遵义县| 澄江县| 白玉县| 阿巴嘎旗|