• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++學(xué)習(xí)過程中的點(diǎn)點(diǎn)滴滴

            C++編程過程中常見問題及錯(cuò)誤

            常用鏈接

            統(tǒng)計(jì)

            聯(lián)想5G免費(fèi)網(wǎng)絡(luò)硬盤

            最新評(píng)論

            C++中字符串的轉(zhuǎn)換

            一、atof(將字符串轉(zhuǎn)換成浮點(diǎn)型數(shù)) 
            相關(guān)函數(shù)  atoi,atol,strtod,strtol,strtoul
             
            表頭文件  #include <stdlib.h>
             
            定義函數(shù)  double atof(const char *nptr);
             
            函數(shù)說明  atof()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時(shí)('')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。參數(shù)nptr字符串可包含正負(fù)號(hào)、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分,如123.456或123e-2。
             
            返回值  返回轉(zhuǎn)換后的浮點(diǎn)型數(shù)。
             
            附加說明  atof()與使用strtod(nptr,(char**)NULL)結(jié)果相同。
             
            范例  /* 將字符串a(chǎn) 與字符串b轉(zhuǎn)換成數(shù)字后相加*/
            #include<stdlib.h>
            main()
            {
            char *a=”-100.23”;
            char *b=”200e-2”;
            float c;
            c=atof(a)+atof(b);
            printf(“c=%.2fn”,c);
            }
             
            執(zhí)行  c=-98.23
            *******************************************************************************
            *******************************************************************************
             二、atoi(將字符串轉(zhuǎn)換成整型數(shù)) 
            相關(guān)函數(shù)  atof,atol,atrtod,strtol,strtoul
             
            表頭文件  #include<stdlib.h>
             
            定義函數(shù)  int atoi(const char *nptr);
             
            函數(shù)說明  atoi()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時(shí)('')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。
             
            返回值  返回轉(zhuǎn)換后的整型數(shù)。
             
            附加說明  atoi()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
             
            范例  /* 將字符串a(chǎn) 與字符串b轉(zhuǎn)換成數(shù)字后相加*/
            #include<stdlib.h>
            mian()
            {
            char a[]=”-100”;
            char b[]=”456”;
            int c;
            c=atoi(a)+atoi(b);
            printf(c=%dn”,c);
            }
             
            執(zhí)行  c=356

            *******************************************************************************
            *******************************************************************************
            三、atol(將字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)) 
            相關(guān)函數(shù)  atof,atoi,strtod,strtol,strtoul
             
            表頭文件  #include<stdlib.h>
             
            定義函數(shù)  long atol(const char *nptr);
             
            函數(shù)說明  atol()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時(shí)('')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。
             
            返回值  返回轉(zhuǎn)換后的長(zhǎng)整型數(shù)。
             
            附加說明  atol()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
             
            范例  /*將字符串a(chǎn)與字符串b轉(zhuǎn)換成數(shù)字后相加*/
            #include<stdlib.h>
            main()
            {
            char a[]=”1000000000”;
            char b[]=” 234567890”;
            long c;
            c=atol(a)+atol(b);
            printf(“c=%dn”,c);
            }
             
            執(zhí)行  c=1234567890

            *******************************************************************************
            *******************************************************************************
            四、gcvt(將浮點(diǎn)型數(shù)轉(zhuǎn)換為字符串,取四舍五入) 
            相關(guān)函數(shù)  ecvt,fcvt,sprintf
             
            表頭文件  #include<stdlib.h>
             
            定義函數(shù)  char *gcvt(double number,size_t ndigits,char *buf);
             
            函數(shù)說明  gcvt()用來將參數(shù)number轉(zhuǎn)換成ASCII碼字符串,參數(shù)ndigits表示顯示的位數(shù)。gcvt()與ecvt()和fcvt()不同的地方在于,gcvt()所轉(zhuǎn)換后的字符串包含小數(shù)點(diǎn)或正負(fù)符號(hào)。若轉(zhuǎn)換成功,轉(zhuǎn)換后的字符串會(huì)放在參數(shù)buf指針?biāo)傅目臻g。
             
            返回值  返回一字符串指針,此地址即為buf指針。
             
            附加說明 
             
            范例  #include<stdlib.h>
            main()
            {
            double a=123.45;
            double b=-1234.56;
            char *ptr;
            int decpt,sign;
            gcvt(a,5,ptr);
            printf(“a value=%sn”,ptr);
            ptr=gcvt(b,6,ptr);
            printf(“b value=%sn”,ptr);
            }
             
            執(zhí)行  a value=123.45
            b value=-1234.56
            *******************************************************************************
            *******************************************************************************
            五、strtod(將字符串轉(zhuǎn)換成浮點(diǎn)數(shù)) 
            相關(guān)函數(shù)  atoi,atol,strtod,strtol,strtoul
             
            表頭文件  #include<stdlib.h>

            定義函數(shù)  double strtod(const char *nptr,char **endptr);
             
            函數(shù)說明  strtod()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,到出現(xiàn)非數(shù)字或字符串結(jié)束時(shí)('')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若endptr不為NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr傳回。參數(shù)nptr字符串可包含正負(fù)號(hào)、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分。如123.456或123e-2。
             
            返回值  返回轉(zhuǎn)換后的浮點(diǎn)型數(shù)。
             
            附加說明  參考atof()。
             
            范例  /*將字符串a(chǎn),b,c 分別采用10,2,16 進(jìn)制轉(zhuǎn)換成數(shù)字*/
            #include<stdlib.h>
            mian()
            {
            char a[]=”1000000000”;
            char b[]=”1000000000”;
            char c[]=”ffff”;
            printf(“a=%dn”,strtod(a,NULL,10));
            printf(“b=%dn”,strtod(b,NULL,2));
            printf(“c=%dn”,strtod(c,NULL,16));
            }
             
            執(zhí)行  a=1000000000
            b=512
            c=65535
            *******************************************************************************
            *******************************************************************************
            六、strtol(將字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)) 
            相關(guān)函數(shù)  atof,atoi,atol,strtod,strtoul
             
            表頭文件  #include<stdlib.h>
             
            定義函數(shù)  long int strtol(const char *nptr,char **endptr,int base);
             
            函數(shù)說明  strtol()會(huì)將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成長(zhǎng)整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方式,如base值為10則采用10進(jìn)制,若base值為16則采用16進(jìn)制等。當(dāng)base值為0時(shí)則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符則會(huì)使用16進(jìn)制做轉(zhuǎn)換。一開始strtol()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時(shí)('')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr返回。
             
            返回值  返回轉(zhuǎn)換后的長(zhǎng)整型數(shù),否則返回ERANGE并將錯(cuò)誤代碼存入errno中。
             
            附加說明  ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
             
            范例  /* 將字符串a(chǎn),b,c 分別采用10,2,16進(jìn)制轉(zhuǎn)換成數(shù)字*/
            #include<stdlib.h>
            main()
            {
            char a[]=”1000000000”;
            char b[]=”1000000000”;
            char c[]=”ffff”;
            printf(“a=%dn”,strtol(a,NULL,10));
            printf(“b=%dn”,strtol(b,NULL,2));
            printf(“c=%dn”,strtol(c,NULL,16));
            }
             
            執(zhí)行  a=1000000000
            b=512
            c=65535
            *******************************************************************************
            *******************************************************************************
            七、strtoul(將字符串轉(zhuǎn)換成無符號(hào)長(zhǎng)整型數(shù)) 
            相關(guān)函數(shù)  atof,atoi,atol,strtod,strtol
             
            表頭文件  #include<stdlib.h>
             
            定義函數(shù)  unsigned long int strtoul(const char *nptr,char **endptr,int base);
             
            函數(shù)說明  strtoul()會(huì)將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成無符號(hào)的長(zhǎng)整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方式,如base值為10則采用10進(jìn)制,若base值為16則采用16進(jìn)制數(shù)等。當(dāng)base值為0時(shí)則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符則會(huì)使用16進(jìn)制做轉(zhuǎn)換。一開始strtoul()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符串,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時(shí)('')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr返回。
             
            返回值  返回轉(zhuǎn)換后的長(zhǎng)整型數(shù),否則返回ERANGE并將錯(cuò)誤代碼存入errno中。
             
            附加說明  ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
             
            范例  參考strtol()
            *******************************************************************************
            *******************************************************************************
            八、toascii(將整型數(shù)轉(zhuǎn)換成合法的ASCII 碼字符) 
            相關(guān)函數(shù)  isascii,toupper,tolower
             
            表頭文件  #include<ctype.h>
             
            定義函數(shù)  int toascii(int c)
             
            函數(shù)說明  toascii()會(huì)將參數(shù)c轉(zhuǎn)換成7位的unsigned char值,第八位則會(huì)被清除,此字符即會(huì)被轉(zhuǎn)成ASCII碼字符。
             
            返回值  將轉(zhuǎn)換成功的ASCII碼字符值返回。
             
            范例  #include<stdlib.h>
            main()
            {
            int a=217;
            char b;
            printf(“before toascii () : a value =%d(%c)n”,a,a);
            b=toascii(a);
            printf(“after toascii() : a value =%d(%c)n”,b,b);
            }
             
            執(zhí)行  before toascii() : a value =217()
            after toascii() : a value =89(Y)
            *******************************************************************************
            *******************************************************************************
            九、tolower(將大寫字母轉(zhuǎn)換成小寫字母) 
            相關(guān)函數(shù)  isalpha,toupper
             
            表頭文件  #include<stdlib.h>
             
            定義函數(shù)  int tolower(int c);
             
            函數(shù)說明  若參數(shù)c為大寫字母則將該對(duì)應(yīng)的小寫字母返回?

            返回值  返回轉(zhuǎn)換后的小寫字母,若不須轉(zhuǎn)換則將參數(shù)c值返回。
             
            范例  /* 將s字符串內(nèi)的大寫字母轉(zhuǎn)換成小寫字母*/
            #include<ctype.h>
            main()
            {
            char s[]=”aBcDeFgH12345;!#$”;
            int i;
            printf(“before tolower() : %sn”,s);
            for(i=0;I<sizeof(s);i++)
            s[i]=tolower(s[i]);
            printf(“after tolower() : %sn”,s);
            }
             
            執(zhí)行  before tolower() : aBcDeFgH12345;!#$
            after tolower() : abcdefgh12345;!#$
            *******************************************************************************
            *******************************************************************************
            十、toupper(將小寫字母轉(zhuǎn)換成大寫字母) 
            相關(guān)函數(shù)  isalpha,tolower
             
            表頭文件  #include<ctype.h>
             
            定義函數(shù)  int toupper(int c);
             
            函數(shù)說明  若參數(shù)c為小寫字母則將該對(duì)映的大寫字母返回。
             
            返回值  返回轉(zhuǎn)換后的大寫字母,若不須轉(zhuǎn)換則將參數(shù)c值返回。
             
            附加說明 
             
            范例  /* 將s字符串內(nèi)的小寫字母轉(zhuǎn)換成大寫字母*/
            #include<ctype.h>
            main()
            {
            char s[]=”aBcDeFgH12345;!#$”;
            int i;
            printf(“before toupper() : %sn”,s);
            for(i=0;I<sizeof(s);i++)
            s[i]=toupper(s[i]);
            printf(“after toupper() : %sn”,s);
            }
             
            執(zhí)行  before toupper() : aBcDeFgH12345;!#$
            after toupper() : ABCDEFGH12345;!#$

            posted on 2009-05-20 21:34 何靖 閱讀(431) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久精品视频免费观看 | 久久久精品人妻一区二区三区蜜桃 | 久久亚洲国产精品一区二区| 久久久91精品国产一区二区三区| 国产 亚洲 欧美 另类 久久| 久久受www免费人成_看片中文| 人妻丰满AV无码久久不卡| 久久午夜电影网| 欧美一区二区久久精品| 久久成人国产精品| 久久久精品国产Sm最大网站| 亚洲色欲久久久综合网| 亚洲国产精品婷婷久久| 久久AV高潮AV无码AV| 国产精品gz久久久| 久久人人爽人人爽人人片AV不| 国内精品久久久久久久久电影网| 精品国产99久久久久久麻豆| 久久精品无码一区二区三区日韩| 久久Av无码精品人妻系列| 九九精品久久久久久噜噜| 亚洲国产成人久久精品动漫| 亚洲欧美伊人久久综合一区二区| 热综合一本伊人久久精品| 久久夜色tv网站| 2021精品国产综合久久| 99精品久久精品一区二区| 性做久久久久久久久久久| 国产日韩久久免费影院| 久久精品一区二区| 久久亚洲国产成人精品性色| 久久人人爽人人爽人人片AV麻烦| 久久亚洲国产精品123区| 久久高潮一级毛片免费| 国产精品永久久久久久久久久| 一本色道久久88加勒比—综合| 久久九九亚洲精品| 国产精品亚洲美女久久久| 久久婷婷五月综合97色直播| 合区精品久久久中文字幕一区| 亚洲精品NV久久久久久久久久|