亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年08月11日 10:11:53   投稿:lqh  
這篇文章主要介紹了C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法的相關(guān)資料,實(shí)現(xiàn)去重字符串相鄰重復(fù)的字符,不相鄰的不用去重的功能,需要的朋友可以參考下

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

字符去重函數(shù)

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

參數(shù):

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

要求:

輸入?yún)?shù)為arg1時(shí), 對(duì)這個(gè)字符串去重
輸入?yún)?shù)為arg1,arg2時(shí), 從arg2位置到字符串結(jié)束,去重
輸入?yún)?shù)為arg1,arg2,arg3時(shí),從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';
}

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

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ǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • C++中static和const的作用和用法

    C++中static和const的作用和用法

    在C++中,"static"和"const"都是用于修飾變量的關(guān)鍵字,它們具有不同的作用,本文通過代碼示例給大家詳細(xì)的介紹static和const的作用和用法,,需要的朋友可以參考下
    2023-06-06
  • C語言實(shí)現(xiàn)動(dòng)態(tài)版通訊錄的代碼分享

    C語言實(shí)現(xiàn)動(dòng)態(tài)版通訊錄的代碼分享

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)一個(gè)簡單的動(dòng)態(tài)版通訊錄,主要運(yùn)用了結(jié)構(gòu)體,一維數(shù)組,函數(shù),分支與循環(huán)語句等等知識(shí),需要的可以參考一下
    2023-01-01
  • C++如何切割String對(duì)象的方法

    C++如何切割String對(duì)象的方法

    C++相較于Java,Python 并沒有提供的字符串分割的函數(shù)split,因此需要自己進(jìn)行編寫,本文主要介紹了C++如何切割String對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C語言實(shí)現(xiàn)對(duì)文件進(jìn)行操作的示例詳解

    C語言實(shí)現(xiàn)對(duì)文件進(jìn)行操作的示例詳解

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)對(duì)文件進(jìn)行操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定的幫助,需要的可以參考一下
    2023-04-04
  • 用C語言簡單實(shí)現(xiàn)掃雷小游戲

    用C語言簡單實(shí)現(xiàn)掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了用C語言簡單實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C++ STL中的容器適配器實(shí)現(xiàn)

    C++ STL中的容器適配器實(shí)現(xiàn)

    這篇文章主要介紹了C++ STL中的容器適配器實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 使用C語言生成圖片的base64編碼的代碼實(shí)現(xiàn)

    使用C語言生成圖片的base64編碼的代碼實(shí)現(xiàn)

    Base64編碼是一種廣泛使用的編碼方案,將任意二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為可打印的ASCII字符字符串,在實(shí)際應(yīng)用中,Base64編碼常見于電子郵件附件、數(shù)據(jù)庫中存儲(chǔ)非文本數(shù)據(jù)等多種場景,本文將給大家介紹使用C語言生成圖片的base64編碼的代碼實(shí)現(xiàn),需要的朋友可以參考下
    2024-08-08
  • C++中const關(guān)鍵字的用法圖文詳解

    C++中const關(guān)鍵字的用法圖文詳解

    在C++中const是一個(gè)關(guān)鍵字,用于聲明常量,它可以用于多種情況,包括聲明常量變量、常量指針、以及成員函數(shù)中的常量性,這篇文章主要給大家介紹了關(guān)于C++中const關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • C++菱形繼承和虛繼承的實(shí)現(xiàn)

    C++菱形繼承和虛繼承的實(shí)現(xiàn)

    本文主要介紹了C++菱形繼承和虛繼承的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • C語言中g(shù)etchar函數(shù)詳解看這一篇就夠了(函數(shù)功能、使用、返回值)

    C語言中g(shù)etchar函數(shù)詳解看這一篇就夠了(函數(shù)功能、使用、返回值)

    getchar讀取字符的函數(shù),今天通過本文給大家介紹C語言中g(shù)etchar函數(shù)簡介用法示例詳解,感興趣的朋友跟隨小編一起看看吧
    2023-02-02

最新評(píng)論