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

            天之道

            享受編程的樂趣。
            posts - 118, comments - 7, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            #include<stdio.h>
            #include
            <string.h>
            char* strins(char* str1,char* str2,int pos)
            {
                
            int s_len;
                
            int d_len;
                
            int i,j;
                pos
            --;
                s_len
            =strlen(str1);
                d_len
            =strlen(str2);
                
            for(i=s_len+1;i>=pos;i--/*空出str2的空間*/
                    str1[i
            +d_len]=str1[i];
                
            for(j=pos;str2[j-pos]!='\0';j++/*將字符串str2插入str1中的特定位置*/
                    str1[j]
            =str2[j-pos];

                
            return str1;
            }


            void main()
            {
                
            char string1[200];
                
            char string2[100];
                
            int pos;
                printf(
            "請(qǐng)輸入初始字符串:");
                gets(string1);
                printf(
            "請(qǐng)輸入插入字符串:");
                gets(string2);
                printf(
            "請(qǐng)輸入插入位置:");
                scanf(
            "%d",&pos);
                strins(string1,string2,pos);
                printf(
            "插入后的字符串是%s\n",string1);
            }

            posted @ 2012-02-01 01:13 hoshelly 閱讀(165) | 評(píng)論 (0)編輯 收藏

            在C++中,sizeof運(yùn)算符的作用是返回一個(gè)變量或數(shù)據(jù)類型在內(nèi)存中所占用的字節(jié)數(shù),其語(yǔ)法形式如下:sizeof 變量名; sizeof(變量類型);

            sizeof運(yùn)算符的操作對(duì)象可以是某個(gè)特定的變量,也可以是變量的數(shù)據(jù)類型,例如int、double和float等。當(dāng)對(duì)變量對(duì)象進(jìn)行運(yùn)算時(shí),變量名兩邊的括號(hào)可加可不加,而當(dāng)操作對(duì)象是數(shù)據(jù)類型時(shí),則必須使用括號(hào)把操作對(duì)象括起來。
            #include<iostream>
            #include
            <stddef.h>
            using namespace std;

            int main()
            {
                
            int i;
                
            char c;
                
            float f;
                cout
            <<"\n int"<<sizeof(int)
                    
            <<"\n char"<<sizeof(char)
                    
            <<"\n float"<<sizeof(float)<<endl;//用于數(shù)據(jù)類型
                cout<<"\n int"<<sizeof i
                    
            <<"\n char"<<sizeof c
                    
            <<"\n float"<<sizeof f<<endl;

                
            return 0;
            }

            //sizeof運(yùn)算符的返回類型為size_t類型,size_t定義于頭文件“stddef.h",它是計(jì)算機(jī)特定的無符號(hào)整數(shù)類型,用來表示內(nèi)存中任何對(duì)象的大小。


            posted @ 2011-12-18 12:48 hoshelly 閱讀(232) | 評(píng)論 (0)編輯 收藏

            運(yùn)算符重載函數(shù)一般采用兩種形式,一種是定義為類的成員函數(shù),另一種是定義為類的友元函數(shù)。
            大多數(shù)情況下,使用成員函數(shù)和友元函數(shù)重載運(yùn)算符在功能實(shí)現(xiàn)上是相同的,重載時(shí)如果沒有本質(zhì)的區(qū)別,則應(yīng)該首先考慮使用成員函數(shù)以保證數(shù)據(jù)封裝。然而在某些情況下,如C++不能直接進(jìn)行復(fù)數(shù)加、減、乘、除的四則運(yùn)算,但是使用友元函數(shù)就可以實(shí)現(xiàn)重載這些運(yùn)算符。
            如 定義 
               class complex
            {
                public:
                        complex(){real=imag=0;}
                        complex(double r,double i)
                       {
                         real=r,imag=r;
                        }
               friend complex operator+(const complex &c1,const complex &c2)
              {
                    return complex(c1.real+c2.real,c1.imag+c2.imag);
              }...

            !注意友元運(yùn)算符函數(shù)的參數(shù)類型是引用類型!

            一般而言,以下兩種調(diào)用方法是等價(jià)的:
            aa@ bb //隱式調(diào)用
            operator @ (aa,bb) // 顯式調(diào)用
            @為運(yùn)算符

            在實(shí)際開發(fā)過程中,單目運(yùn)算符建議重載為成員函數(shù),而雙目運(yùn)算符建議重載為友元函數(shù)。通常下雙目運(yùn)算符重載為友元函數(shù)比重載為成員函數(shù)更方便,但是有時(shí)雙目運(yùn)算符必須重載為成員函數(shù),例如賦值運(yùn)算符。

                        

            posted @ 2011-12-18 12:34 hoshelly 閱讀(1293) | 評(píng)論 (0)編輯 收藏

            用友元函數(shù)重載“++”“--”時(shí)需要注意幾點(diǎn):
            1)運(yùn)算符++、--都對(duì)單值操作數(shù)產(chǎn)生影響,因此使用成員運(yùn)算符函數(shù)重載++和--的成員函數(shù)通常返回指針this。
            2)由于友元運(yùn)算符函數(shù)沒有this指針,因此不能引用this指針?biāo)傅膶?duì)象,使用友元函數(shù)重載++、--時(shí),應(yīng)采用引用參數(shù)傳遞數(shù)據(jù)。
            3)采用前綴和后綴方式的函數(shù)內(nèi)部的語(yǔ)句可以相同,也可以不同,這取決于用戶的考慮。
            例子:
            class book
            {
            public:
             book(int i=0,int j=0);
             void print();
             friend book operator++(book &op);
            private:
             int x,y;
            };

            book::book(int i,int j)
            {
             x=i;
             y=j;
            }

            void book::print()
            {
             cout<<" x: "<<x<<", y "<<y<<endl;
            }

            book operator++(book &op)  //此處不能寫成book operator++(book op), 參數(shù)必須是引用傳遞類型,而不是值傳遞。若這樣做,以下主函數(shù)輸出的值是不變的
            {
             ++op.x;
             ++op.y;
             return op;
            }

            void main()
            {
             book ob(20,30);
             ob.print();
             operator++(ob);
             ob.print();
             ++ob;
             ob.print();
            }

             

            posted @ 2011-12-18 12:03 hoshelly 閱讀(367) | 評(píng)論 (0)編輯 收藏

            類的實(shí)現(xiàn):

            class CNumber
            {
            private:
             int n;
            public:
             CNumber(int number)
             {
              n=number;
             }
             ~CNumber(){ }
             int isEven()
             {
              if(n%2==0)
               return 1;
              else
               return 0;
             }
             
             int isOdd()
             {
              if(n%2!=0)
               return 1;
              else
               return 0;
             }
             
             int isPrime()
             {
              int i;
              if(n<1)
               return 0;
              for(i=2;i<=sqrt(n);i++)
               if(n%i==0)
                return 0;
               else
                   return 1;
              return 0;
             }
             bool isAPrime(int n)
             {
              for(int i=2;i<sqrt(n);i++)
               if(n%i==0)
                return false;
               return true;
             }
             
             int isGoldBach()
             {
              int i;
              int halfnum=n/2;
               for(i=2;i<=halfnum;i++)
                      if(isAPrime(i)&&isAPrime(n-i))
                 return 1;
             
              
              return 0;
             }

             void print()
             {
              if(isEven())  cout<<"This number is even."<<endl;
                 if(isOdd())   cout<<"This number is odd."<<endl;
                 if(isPrime())  cout<<"This number is prime."<<endl;
                 if(isGoldBach())  cout<<"This number is goldbach."<<endl;
             }
            };


            主函數(shù):

            void main()
            {
             int num;
             cout<<"Please enter one number:"<<endl;
             cin>>num;
             CNumber numb(num);
              numb.print();
            }



            posted @ 2011-11-26 13:38 hoshelly 閱讀(446) | 評(píng)論 (0)編輯 收藏

            二分法思想:假定f(x)在區(qū)間(x,y)上連續(xù)   
            先找到a、b屬于區(qū)間(x,y),使f(a),f(b)異號(hào),
            說明在區(qū)間(a,b)內(nèi)一定有零點(diǎn),然后求f[(a+b)/2],   
            現(xiàn)在假設(shè)f(a)<0,f(b)>0,a<b    
            ①如果f[(a+b)/2]=0,該點(diǎn)就是零點(diǎn),   如果f[(a+b)/2]<0,則在區(qū)間((a+b)/2,b)內(nèi)有零點(diǎn),(a+b)/2=>a,從①開始繼續(xù)使用   中點(diǎn)函數(shù)值判斷。   如果f[(a+b)/2]>0,則在區(qū)間(a,(a+b)/2)內(nèi)有零點(diǎn),(a+b)/2<=b,從①開始繼續(xù)使用   中點(diǎn)函數(shù)值判斷。   這樣就可以不斷接近零點(diǎn)。   通過每次把f(x)的零點(diǎn)所在小區(qū)間收縮一半的方法,使區(qū)間的兩個(gè)端點(diǎn)逐步迫近函數(shù)的零點(diǎn),以求得零點(diǎn)的近似值,這種方法叫做二分法。

            頭文件定義
            class CEquation
            {
            private:
             double solution;            //方程的近似解
             double a, b;                //近似解的區(qū)間
             double (*p_fx)(double x);   //p_fx是一個(gè)指向函數(shù)的指針,指向方程式求值函數(shù)
             double (*p_solution)(double x, double y); //指向由近似解區(qū)間求近似解的函數(shù)的指針
             double delta;               //求解精度
            public:
             CEquation(double av, double bv, double (*p1)(double), double (*p2)(double,double), double dv);
             double biSection();        //二分法求方程近似解
             void printSolution() const;
            };

            類的實(shí)現(xiàn)及主函數(shù)實(shí)現(xiàn):
            CEquation::CEquation(double a_val, double b_val, double (*p1)(double), double (*p2)(doubledouble), double delta_val)
            {
                a 
            = a_val;
                b 
            = b_val;
                p_fx 
            = p1;
                p_solution 
            = p2;
                solution 
            = p_solution(a, b);
                delta 
            = delta_val;
            }


            double fx(double x)  //方程為: e^x+4^x3-6^x2+3x-2=0
            {
                
            return exp(x)+4.0*x*x*x-6.0*x*x+3.0*x-2.0;
            }


            double middle(double x, double y)  //中值
            {
                
            return 0.5*(x+y);
            }


            double CEquation::biSection()
            {
            double h;

                
            while (fabs(a-b) > delta)
                
            {
                    h 
            = p_solution(a, b);
                    
            if (p_fx(a)*p_fx(h) > 0)
                        a 
            = h;
                    
            else
                        b 
            = h;
                }

                solution 
            = p_solution(a, b);
                
            return solution;
            }


            void CEquation::printSolution() const
            {
                cout 
            << "Solution is: " << solution << endl;
            }


            void main ()
            {
            CEquation a(
            0.01.0, fx, middle, 1e-6);

                a.biSection();
                a.printSolution();
            }




            posted @ 2011-11-25 20:54 hoshelly 閱讀(2167) | 評(píng)論 (0)編輯 收藏

                 摘要:  小累,國(guó)慶假期過去一大半,C++這時(shí)候才把遞歸和函數(shù)這一塊知識(shí)點(diǎn)慢慢地啃完了,結(jié)束之前,今晚自己寫了一個(gè)小程序,實(shí)現(xiàn)四則運(yùn)算,適合小學(xué)生使用。程序說明:1)允許用戶選擇一種類型的算術(shù)問題來學(xué)習(xí),輸入1表示加法,2表示減法,3表示乘法,4表示除法,5表示四種混合運(yùn)算;2)由于程序代碼中反復(fù)無窮遞歸,所以該程序的運(yùn)算會(huì)不斷進(jìn)行下去,退出請(qǐng)自動(dòng)關(guān)閉程序;源代碼如下: Code highl...  閱讀全文

            posted @ 2011-10-05 22:03 hoshelly 閱讀(1301) | 評(píng)論 (1)編輯 收藏



            下午照樣看C++how to program,加油,要到數(shù)組和指針了,接下來就是類的深入剖析了,這幾天在加強(qiáng)火力猛攻這塊地方,哈哈,還是編程菜鳥!不過我自己感覺比大一那時(shí)候真的有點(diǎn)進(jìn)步了,有那種coding 的感覺了,努力學(xué)習(xí)還是有收獲的。閑話休說,把今天下午自己寫的代碼發(fā)上來!
            #include<iostream>
            using namespace std;
            int main()
            {
                
            int row,line;//定義行、列數(shù)的變量
                for(row=1;row<=10;row++)
                {
                    
            for(line=1;line<=row;line++)
                        cout
            <<"*";
                    cout
            <<endl;
                }
                cout
            <<"\n";//第一個(gè)圖形至此打印出來
                for(row=10;row>=1;row--)
                {
                    
            for(line=1;line<=row;line++)
                        cout
            <<"*";
                    cout
            <<endl;
                }
                cout
            <<"\n";//第二個(gè)圖形打印出來
                for(row=10;row>=1;row--)
                {
                    
            for(line=1;line<=row;line++)
                        cout
            <<"*";
                        cout
            <<endl;
                    
            for(line=1;line<=(11-row);line++)
                        cout
            <<" ";
                
                }
                cout
            <<"\n";// 第三個(gè)圖形打印出來
                for(row=1;row<=10;row++)
                {
                    
            for(line=1;line<=10-row;line++)
                        cout
            <<" ";

                    
            for(line=1;line<=row;line++)
                        cout
            <<"*";
                        cout
            <<endl;
                
                } 
            //最后一個(gè)圖形!
                cout<<endl;
                
            return 0;

            }


            程序運(yùn)行結(jié)果





            posted @ 2011-09-25 16:13 hoshelly 閱讀(440) | 評(píng)論 (0)編輯 收藏


            我們知道,std::cout<<endl是使輸入的數(shù)強(qiáng)制輸出,以前我沒發(fā)現(xiàn),今天發(fā)現(xiàn),如果是輸入一行數(shù)的話,使用這個(gè)std::cout<<endl,程序是默認(rèn)每輸出一個(gè)數(shù)就回車的,而不是排成一行!
            請(qǐng)看一下一例:
            該程序要求輸入長(zhǎng)度,然后輸出一個(gè)四條邊都帶相同數(shù)量星號(hào)的矩形。
            #include<iostream>
            using namespace std;
            int main()
            {
                
            int side,rowPosition,size;
                cout
            <<"input the square side: ";//輸入矩形的寬度
                cin>>side;
                size
            =side;//使長(zhǎng)寬的邊所帶星號(hào)數(shù)量相同
                while(side>0)//雙重循環(huán)輸出矩形
                {
                    rowPosition
            =size;
                    
            while(rowPosition>0)
                    
            {
                        
            if(size==side||side==1||rowPosition==1||rowPosition==size)
                            cout
            <<'*'<<;
                        
            else
                            cout
            <<' ';
                        
            --rowPosition;
                    }

                    cout
            <<'\n';//在這里等一行自然輸出后,在利用cout<<‘\n'回車,輸出下一行
                    --side;
                }

                cout
            <<endl;//這里總的強(qiáng)制輸出所有輸入的字符
                  return 0;
                
            }

                    
                    




                

                

            程序運(yùn)行效果如下圖,輸入8;





            如果在程序的每條cout語(yǔ)句中加上<<endl; 那么程序運(yùn)行的效果(圖所限,"end line": inserts a newline into the stream and calls flush.有省略一些)如下:





            后注:剛剛在維基百科里查到std::endl的定義,它說,"end line": inserts a newline into the stream and calls flush. 這就是說endl的功能就是強(qiáng)制輸出和換行,現(xiàn)在懂了,感謝博友的認(rèn)真更正,學(xué)習(xí)了。:)

            posted @ 2011-09-23 05:18 hoshelly 閱讀(452) | 評(píng)論 (2)編輯 收藏


            Tonight I was studying the credit card codes, all of these was copied from my book,"C++ ——how to program".

            This are the program demand:
            1)Account number(an integer);
            2)Balance at the begining of the month;
            3)Total all items charged by this customer this month;
            4)Total of all items applied to this customer's account this month;
            5)Allowed credit limit.


            The program should input each of these facts, calculate the new balance(=begining balance+charges-credits) and determine if the new balance exceeds the customer credit limit.
            For those customers whose credit limit is exceeded, the program should display the customer's account number,credit limit,new balance and the message "Credit limit exceeded".

            #include<iostream>
            #include
            <iomanip>// formatting integers and the precision of floating point values
            using namespace std;

            int main()
            {
                
            int accountNumber;
                
            double balance,charges,credits,limit;

                cout
            <<"Enter account number(-1 to end):"
                    
            <<setiosflags(ios::fixed|ios::showpoint);// to convert data from fixed point number representation to floating point representation
                cin>>accountNumber;
                
            while(accountNumber!=-1)
                
            {
                    cout
            <<"Enter beginning balance:";
                    cin
            >>balance;
                    cout
            <<"Enter total charges: ";
                    cin
            >>charges;
                    cout
            <<"Enter total credits:";
                    cin
            >>credits;
                    cout
            <<"Enter credit limit:";
                    cin
            >>limit;
                    balance
            +=charges-credits;

                    
            if(balance>limit)
                        cout
            <<"Account:  "<<accountNumber
                            
            <<"\nCredit limit: "<<setprecision(2)<<limit //accurate to two decimal point
                            
            <<"\nBalance:    "<<setprecision(2)<<balance
                            
            <<"\nCredit Limit Exceeded.\n";
                    cout
            <<"\nEnter account number(-1 to end): ";
                    cin
            >>accountNumber;
                }


                cout
            <<endl;
                
            return 0;
            }


             

            input -1 and the program will be ended.

            posted @ 2011-09-23 01:13 hoshelly 閱讀(295) | 評(píng)論 (0)編輯 收藏

            僅列出標(biāo)題
            共12頁(yè): First 4 5 6 7 8 9 10 11 12 
            香蕉久久久久久狠狠色| 国产精品成人久久久久三级午夜电影 | 日韩十八禁一区二区久久| 久久精品视屏| 亚洲精品无码久久一线| 久久中文字幕一区二区| 亚洲国产成人精品91久久久 | 欧美午夜精品久久久久免费视| 精品无码久久久久久尤物| 国产精品美女久久久免费| 久久人人爽人人爽人人片AV东京热 | 日本高清无卡码一区二区久久| 久久久免费精品re6| 久久九九久精品国产免费直播| 亚洲av成人无码久久精品| 久久国产V一级毛多内射| 精品无码久久久久国产| 人人妻久久人人澡人人爽人人精品| 久久久国产精品网站| 亚洲中文字幕久久精品无码APP | 9久久9久久精品| 色狠狠久久综合网| 国产精品久久久久乳精品爆| 欧美一区二区三区久久综合| 亚洲国产精品综合久久网络 | 久久精品国产精品亚洲精品 | 国产三级观看久久| 精品久久久噜噜噜久久久| 97精品依人久久久大香线蕉97 | 精品国际久久久久999波多野| 99久久免费国产精品特黄| 午夜福利91久久福利| 综合久久给合久久狠狠狠97色 | 欧美丰满熟妇BBB久久久| 久久精品极品盛宴观看| 久久婷婷人人澡人人| 久久婷婷五月综合成人D啪| 99久久精品免费看国产| 精品久久久久久国产免费了| 久久国产精品国语对白| 伊人色综合久久天天人守人婷|