程序設(shè)計(jì)競(jìng)賽――C++常用庫(kù)函數(shù)_第1頁(yè)
程序設(shè)計(jì)競(jìng)賽――C++常用庫(kù)函數(shù)_第2頁(yè)
程序設(shè)計(jì)競(jìng)賽――C++常用庫(kù)函數(shù)_第3頁(yè)
程序設(shè)計(jì)競(jìng)賽――C++常用庫(kù)函數(shù)_第4頁(yè)
程序設(shè)計(jì)競(jìng)賽――C++常用庫(kù)函數(shù)_第5頁(yè)
已閱讀5頁(yè),還剩30頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、轉(zhuǎn)自:/question/.html首先就是memcpy表頭文件: #include 定義函數(shù): void *memcpy(void *dest, const void *src, size_t n)函數(shù)說(shuō)明: memcpy()用來(lái)拷貝src所指的內(nèi)存內(nèi)容前n個(gè)字節(jié)到dest所指的內(nèi)存地址上。與strcpy()不同的是,memcpy()會(huì)完整的復(fù)制n個(gè)字節(jié),不會(huì)因?yàn)橛龅阶址Y(jié)束0而結(jié)束返回值: 返回指向dest的指針附加說(shuō)明: 指針src和dest所指的內(nèi)存區(qū)域不可重疊例如:你需要復(fù)制串str=“wangyucao1989”中的“yucao”,那么

2、可以這么寫(xiě):memcpy(newstr,str+4,5);除了memcpy之外,string還提供了strncpy函數(shù):函數(shù)名稱(chēng): strncpy函數(shù)原型: char *strncpy(char *dest, const char *src,int count)函數(shù)功能: 將字符串src中的count個(gè)字符拷貝到字符串dest中去函數(shù)返回: 指向dest的指針參數(shù)說(shuō)明: dest-目的字符串,src-源字符串,count-拷貝的字符個(gè)數(shù)所屬文檔: 還是上面的例子,這個(gè)程序可以這樣寫(xiě):#include#includeint main() char str = wangyucao1989; cha

3、r newstr6; /memcpy(newstr,str+4,5); strncpy(newstr,str+4,5); newstr5 = 0; printf(%sn,newstr); return 0;=位運(yùn)算:運(yùn)算方法有六種:& 與運(yùn)算 | 或運(yùn)算 異或運(yùn)算 非運(yùn)算(求補(bǔ)) 右移運(yùn)算 10110) | x 1在最后加一個(gè)0 | (-) | x ) | x ) | x | 1把最后一位變成0 | (-) | x | 1-1最后一位取反 | (-) | x 1把右數(shù)第k位變成1 | (-,k=3) | x | (1 ,k=3) | x & (1 ,k=3) | x (1 101) | x &

4、 7取末k位 | (-1101,k=5) | x & (1 1,k=4) | x (k-1) & 1把末k位變成1 | (-,k=4) | x | (1 ,k=4) | x (1 ) | x & (x+1)把右起第一個(gè)0變成1 | (-) | x | (x+1)把右邊連續(xù)的0變成1 | (-) | x | (x-1)取右邊連續(xù)的1 | (-1111) | (x (x+1) 1去掉右起第一個(gè)1的左邊 | (-1000) | x & (x (x-1)判斷奇數(shù) (x&1)=1判斷偶數(shù) (x&1)=0 取右邊第一個(gè)1所在位置 x&-x=類(lèi)型轉(zhuǎn)換:函數(shù)名: abs 功 能: 求整數(shù)的絕對(duì)值用 法: in

5、t abs(int i);程序例:#include #include int main(void) int number = -1234; printf(number: %d absolute value: %dn, number, abs(number); return 0;函數(shù)名: atof功 能: 把字符串轉(zhuǎn)換成浮點(diǎn)數(shù)用 法: double atof(const char *nptr);程序例:#include #include int main(void) float f; char *str = 12345.67; f = atof(str); printf(string = %s

6、float = %fn, str, f); return 0;函數(shù)名: atoi功 能: 把字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)用 法: int atoi(const char *nptr);程序例:#include #include int main(void) int n; char *str = 12345.67; n = atoi(str); printf(string = %s integer = %dn, str, n); return 0;函數(shù)名: atol功 能: 把字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)用 法: long atol(const char *nptr);程序例:#include #include

7、 int main(void) long l; char *str = ; l = atol(lstr); printf(string = %s integer = %ldn, str, l); return(0);=其他函數(shù):函數(shù)名: bsearch功 能: 二分法搜索用 法: void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *);程序例:#include #include #define NELEMS(arr) (size

8、of(arr) / sizeof(arr0)int numarray = 123, 145, 512, 627, 800, 933;int numeric (const int *p1, const int *p2) return(*p1 - *p2);int lookup(int key) int *itemptr; /* The cast of (int(*)(const void *,const void*) is needed to avoid a type mismatch error at compile time */ itemptr = bsearch (&key, numar

9、ray, NELEMS(numarray), sizeof(int), (int(*)(const void *,const void *)numeric); return (itemptr != NULL);int main(void) if (lookup(512) printf(512 is in the table.n); else printf(512 isnt in the table.n); return 0;函數(shù)名: fabs功 能: 返回浮點(diǎn)數(shù)的絕對(duì)值用 法: double fabs(double x);程序例:#include #include int main(void)

10、 float number = -1234.0; printf(number: %f absolute value: %fn, number, fabs(number); return 0;函數(shù)名: fcvt功 能: 把一個(gè)浮點(diǎn)數(shù)轉(zhuǎn)換為字符串用 法: char *fcvt(double value, int ndigit, int *decpt, int *sign);程序例:#include #include #include int main(void) char *string; double value; int dec, sign; int ndig = 10; clrscr();

11、value = 9.876; string = ecvt(value, ndig, &dec, &sign); printf(string = %s dec = %d sign = %dn, string, dec, sign); value = -123.45; ndig= 15; string = ecvt(value,ndig,&dec,&sign); printf(string = %s dec = %d sign = %dn, string, dec, sign); value = 0.6789e5; /* scientific notation */ ndig = 5; strin

12、g = ecvt(value,ndig,&dec,&sign); printf(string = %s dec = %d sign = %dn, string, dec, sign); return 0;函數(shù)名: gcvt功 能: 把浮點(diǎn)數(shù)轉(zhuǎn)換成字符串用 法: char *gcvt(double value, int ndigit, char *buf);程序例:#include #include int main(void) char str25; double num; int sig = 5; /* significant digits */ /* a regular number */

13、 num = 9.876; gcvt(num, sig, str); printf(string = %sn, str); /* a negative number */ num = -123.4567; gcvt(num, sig, str); printf(string = %sn, str); /* scientific notation */ num = 0.678e5; gcvt(num, sig, str); printf(string = %sn, str); return(0);函數(shù)名: itoa功 能: 把一整數(shù)轉(zhuǎn)換為字符串用 法: char *itoa(int value,

14、 char *string, int radix);程序例:#include #include int main(void) int number = 12345; char string25; itoa(number, string, 10); printf(integer = %d string = %sn, number, string); return 0;函數(shù)名: labs功 能: 取長(zhǎng)整型絕對(duì)值用 法: long labs(long n);程序例:#include #include int main(void) long result; long x = -L; result= l

15、abs(x); printf(number: %ld abs value: %ldn, x, result); return 0;函數(shù)名: memcpy功 能: 從源source中拷貝n個(gè)字節(jié)到目標(biāo)destin中用 法: void *memcpy(void *destin, void *source, unsigned n);程序例:#include #include int main(void) char src = *; char dest = abcdefghijlkmnopqrstuvwxyz; char *ptr; printf(destination before memcpy:

16、%sn, dest); ptr = memcpy(dest, src, strlen(src); if (ptr) printf(destination after memcpy: %sn, dest); else printf(memcpy failedn); return 0;函數(shù)名: memset功 能: 設(shè)置s中的所有字節(jié)為ch, s數(shù)組的大小由n給定用 法: void *memset(void *s, char ch, unsigned n);程序例:#include #include #include int main(void) char buffer = Hello world

17、n; printf(Buffer before memset: %sn, buffer); memset(buffer, *, strlen(buffer) - 1); printf(Buffer after memset: %sn, buffer); return 0;函數(shù)名: pow 功 能: 指數(shù)函數(shù)(x的y次方)用 法: double pow(double x, double y);程序例:#include #include int main(void) double x = 2.0, y = 3.0; printf(%lf raised to %lf is %lfn, x, y, p

18、ow(x, y); return 0;函數(shù)名: qsort功 能: 使用快速排序例程進(jìn)行排序用 法: void qsort(void *base, int nelem, int width, int (*fcmp)();程序例:#include #include #include int sort_function( const void *a, const void *b);char list54 = cat, car, cab, cap, can ;int main(void) int x; qsort(void *)list, 5, sizeof(list0), sort_functio

19、n); for (x = 0; x 5; x+) printf(%sn, listx); return 0;int sort_function( const void *a, const void *b) return( strcmp(a,b) );函數(shù)名: sqrt功 能: 計(jì)算平方根用 法: double sqrt(double x);程序例:#include #include int main(void) double x = 4.0, result; result = sqrt(x); printf(The square root of %lf is %lfn, x, result);

20、 return 0;=字符串函數(shù):函數(shù)名: sscanf功 能: 執(zhí)行從字符串中的格式化輸入用 法: int sscanf(char *string, char *format,argument,.);程序例:#include #include int main(void) char label20; char name20; int entries = 0; int loop, age; double salary; struct Entry_struct char name20; int age; float salary; entry20;/* Input a label as a st

21、ring of characters restricting to 20 characters */ printf(nnPlease enter a label for the chart: ); scanf(%20s, label); fflush(stdin); /* flush the input stream in case of bad input */* Input number of entries as an integer */ printf(How many entries will there be? (less than 20) ); scanf(%d, &entrie

22、s); fflush(stdin); /* flush the input stream in case of bad input */* input a name restricting input to only letters upper or lower case */ for (loop=0;loopentries;+loop) printf(Entry %dn, loop); printf( Name : ); scanf(%A-Za-z, ); fflush(stdin); /* flush the input stream in case of ba

23、d input */* input an age as an integer */ printf( Age : ); scanf(%d, &entryloop.age); fflush(stdin); /* flush the input stream in case of bad input */* input a salary as a float */ printf( Salary : ); scanf(%f, &entryloop.salary); fflush(stdin); /* flush the input stream in case of bad input */ /* I

24、nput a name, age and salary as a string, integer, and double */ printf(nPlease enter your name, age and salaryn); scanf(%20s %d %lf, name, &age, &salary);/* Print out the data that was input */ printf(nnTable %sn,label); printf(Compiled by %s age %d $%15.2lfn, name, age, salary); printf(-n); for (lo

25、op=0;loopentries;+loop) printf(%4d | %-20s | %5d | %15.2lfn, loop + 1, , entryloop.age, entryloop.salary); printf(-n); return 0;函數(shù)名: stpcpy功 能: 拷貝一個(gè)字符串到另一個(gè)用 法: char *stpcpy(char *destin, char *source);程序例:#include #include int main(void) char string10; char *str1 = abcdefghi; stpcpy(st

26、ring, str1); printf(%sn, string); return 0;函數(shù)名: strcat功 能: 字符串拼接函數(shù)用 法: char *strcat(char *destin, char *source);程序例:#include #include int main(void) char destination25; char *blank = , *c = C+, *Borland = Borland; strcpy(destination, Borland); strcat(destination, blank); strcat(destination, c); prin

27、tf(%sn, destination); return 0;函數(shù)名: strchr功 能: 在一個(gè)串中查找給定字符的第一個(gè)匹配之處用 法: char *strchr(char *str, char c);程序例:#include #include int main(void) char string15; char *ptr, c = r; strcpy(string, This is a string); ptr = strchr(string, c); if (ptr) printf(The character %c is at position: %dn, c, ptr-string)

28、; else printf(The character was not foundn); return 0;函數(shù)名: strcmp功 能: 串比較用 法: int strcmp(char *str1, char *str2);程序例:#include #include int main(void) char *buf1 = aaa, *buf2 = bbb, *buf3 = ccc; int ptr; ptr = strcmp(buf2, buf1); if (ptr 0) printf(buffer 2 is greater than buffer 1n); else printf(buff

29、er 2 is less than buffer 1n); ptr = strcmp(buf2, buf3); if (ptr 0) printf(buffer 2 is greater than buffer 3n); else printf(buffer 2 is less than buffer 3n); return 0;函數(shù)名: strcpy功 能: 串拷貝用 法: char *strcpy(char *str1, char *str2);程序例:#include #include int main(void) char string10; char *str1 = abcdefgh

30、i; strcpy(string, str1); printf(%sn, string); return 0;函數(shù)名: strrev功 能: 串倒轉(zhuǎn)用 法: char *strrev(char *str);程序例:#include #include int main(void) char *forward = string; printf(Before strrev(): %sn, forward); strrev(forward); printf(After strrev(): %sn, forward); return 0;函數(shù)名: strset功 能: 將一個(gè)串中的所有字符都設(shè)為指定字符

31、用 法: char *strset(char *str, char c);程序例:#include #include int main(void) char string10 = ; char symbol = c; printf(Before strset(): %sn, string); strset(string, symbol); printf(After strset(): %sn, string); return 0;函數(shù)名: strstr功 能: 在串中查找指定字符串的第一次出現(xiàn)用 法: char *strstr(char *str1, char *str2);程序例:#incl

32、ude #include int main(void) char *str1 = Borland International, *str2 = nation, *ptr; ptr = strstr(str1, str2); printf(The substring is: %sn, ptr); return 0;函數(shù)名: strtod功 能: 將字符串轉(zhuǎn)換為double型值用 法: double strtod(char *str, char *endptr);程序例:#include #include int main(void) char input80, *endptr; double value; printf(Enter a floating point number:); gets(input); value = strtod(input, &endptr); printf(The string is %s the

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論