• <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>

            ▲1、C語言標(biāo)準(zhǔn)庫函數(shù)atoi()等。

            函數(shù)名: atoi
            功 能: 把字符串轉(zhuǎn)換成整型數(shù)
            用 法: int atoi(const char *nptr);
            程序例:
            #include <stdlib.h>
            int main(void)
            {
            int n;
            char *str = "435";
            n = atoi(str);
            printf("string = %s integer = %d\n", str, n);
            return 0;
            }

            其他相關(guān)函數(shù)——

            函數(shù)名: atof
            功 能: 把字符串轉(zhuǎn)換成浮點數(shù)
            用 法: double atof(const char *nptr);
            程序例:
            #include <stdlib.h>
            int main(void)
            {
            float f;
            char *str = "12345.67";
            f = atof(str);
            printf("string = %s float = %f\n", str, f);
            return 0;
            }

            函數(shù)名: atol
            功 能: 把字符串轉(zhuǎn)換成長整型數(shù)
            用 法: long atol(const char *nptr);
            程序例:
            #include <stdlib.h>
            int main(void)
            {
            long l;
            char *str = "98765432";
            l = atol(lstr);
            printf("string = %s integer = %ld\n", str, l);
            return(0);
            }

            ▲2、sprintf與Format構(gòu)造字符串——

                sprintf和printf都是C的產(chǎn)物,用法幾乎一樣,只是前者打印到字符串,后者直接在命令行上輸出。
                int sprintf( char *buffer, const char *format [, argument] … );
                除了前兩個參數(shù)類型固定外,后面可以接任意多個參數(shù)。而它的精華,顯然就在第二個參數(shù):格式化字符串(想想printf吧,一樣的)。例:
            #include <iostream>
            int main()
            {
                double a(3112);
                char s1[10],s2[10];
                sprintf(s1,"%d\n",(int)a);
                sprintf(s2,"$%.2lf",a);
                std::cout<<s1<<s2<<std::endl;
            }

                在C++里,也可用Format(CString) :
            /*VS2005中,項目/屬性/配置屬性里字符集設(shè)置為未配置*/
            #include <iostream>
            #define _AFXDLL
            #include <afx.h>
            int main()
            {
                double a(32);
                CString s;
                s.Format("$%.2lf",a);
                std::cout<<s<<std::endl;
            }

            ▲3、字符串流stringstream提供的轉(zhuǎn)換和/或格式化。

            #include <iostream>
            #include <vector>
            #include <string>
            #include <sstream>
            using namespace std;
            int main()
            {
                int num(435);
                string s;
                ostringstream mystream;
                mystream<<num<<"\n";
                /*創(chuàng)建一個名為mystream的ostringstream類型空對象,
            并將指定的內(nèi)容插入該對象。此時mystream的內(nèi)容是以下字符:
            435\n*/
                s=mystream.str();
                cout<<s;
            }

            #include <iostream>
            #include <vector>
            #include <string>
            #include <sstream>
            using namespace std;
            int main()
            {
                string s("435");
                int num;
                istringstream mystream(s);
                mystream>>num;/*num=435*/
                cout<<num<<endl;
            }

            ▲4、自己寫函數(shù)。

            /*串到數(shù),實參如("435",&number)*/
            void getnumber_from_string(char nums[],int *p)
            {
                int i,k=strlen(nums);
                for(i=0,(*p)=0;i<k;++i)
                (*p)+=pow_10(k-i-1)*(*(nums+i)-48);
            }
            int pow_10(int k) /*10的k次方*/
            {
                return (k==0?1:10*pow_10(k-1));
            }

            ▲5、想沒到/~!親想到?jīng)]?望補(bǔ)充>>>>

            Posted on 2007-07-23 22:50 小王子 閱讀(4841) 評論(5)  編輯 收藏 引用 所屬分類: 分類1:C++文章

            Feedback

            # re: ★(原創(chuàng))C/C++字符串和數(shù)字互換方案收集★  回復(fù)  更多評論   

            2007-07-24 00:34 by 隨風(fēng)而過

            # re: ★(原創(chuàng))C/C++字符串和數(shù)字互換方案收集★[未登錄]  回復(fù)  更多評論   

            2007-07-24 08:39 by 漂舟
            樓主收集的方法還真不少,
            偶補(bǔ)充一個,strtol, strtoul, strtod, 這一系列標(biāo)準(zhǔn)庫函數(shù)

            # re: ★(原創(chuàng))C/C++字符串和數(shù)字互換方案收集★  回復(fù)  更多評論   

            2007-07-24 09:40 by nickey
            boost::lexical_cast

            # re: ★(原創(chuàng))C/C++字符串和數(shù)字互換方案收集★  回復(fù)  更多評論   

            2007-07-24 09:54 by pass86
            強(qiáng)烈推薦boost::lexical_cast
            SO EASY.

            # re: ★(原創(chuàng))C/C++字符串和數(shù)字互換方案收集★[未登錄]  回復(fù)  更多評論   

            2008-09-06 03:25 by Robert
            Could you write down:

            char* itoa(int n, char* buf)
            国产亚洲欧美精品久久久| 日本强好片久久久久久AAA| 久久国产热这里只有精品| 久久久久国产成人精品亚洲午夜| 久久婷婷人人澡人人| 精品综合久久久久久97| 精品精品国产自在久久高清| 久久久精品日本一区二区三区 | 人人妻久久人人澡人人爽人人精品 | 久久综合精品国产一区二区三区| 一本色道久久88精品综合| 久久www免费人成精品香蕉| 伊人久久大香线焦AV综合影院| 日本道色综合久久影院| 中文字幕热久久久久久久| 久久乐国产精品亚洲综合| 国产精品久久久福利| 色综合久久夜色精品国产| 亚洲欧美日韩久久精品| aaa级精品久久久国产片| 亚洲国产欧洲综合997久久| 久久99精品久久久久久9蜜桃| 国内精品久久久久影院优| 久久久久亚洲av综合波多野结衣| 99久久婷婷国产综合精品草原 | 91精品国产91热久久久久福利| 久久人人爽人人爽人人片av麻烦| 99久久国产综合精品网成人影院| 99久久精品午夜一区二区 | 亚洲精品午夜国产va久久| 久久久WWW成人免费毛片| 99久久国产综合精品五月天喷水| 久久精品国产亚洲AV无码麻豆| 亚洲伊人久久精品影院| 99久久精品免费看国产一区二区三区| 久久人人爽人人爽人人片AV麻豆| 四虎国产永久免费久久| 成人亚洲欧美久久久久| 久久久久亚洲AV综合波多野结衣| 99久久成人18免费网站| 久久www免费人成精品香蕉|