• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            #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(
            "請輸入初始字符串:");
                gets(string1);
                printf(
            "請輸入插入字符串:");
                gets(string2);
                printf(
            "請輸入插入位置:");
                scanf(
            "%d",&pos);
                strins(string1,string2,pos);
                printf(
            "插入后的字符串是%s\n",string1);
            }

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

            在C++中,sizeof運算符的作用是返回一個變量或數據類型在內存中所占用的字節數,其語法形式如下:sizeof 變量名; sizeof(變量類型);

            sizeof運算符的操作對象可以是某個特定的變量,也可以是變量的數據類型,例如int、double和float等。當對變量對象進行運算時,變量名兩邊的括號可加可不加,而當操作對象是數據類型時,則必須使用括號把操作對象括起來。
            #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;//用于數據類型
                cout<<"\n int"<<sizeof i
                    
            <<"\n char"<<sizeof c
                    
            <<"\n float"<<sizeof f<<endl;

                
            return 0;
            }

            //sizeof運算符的返回類型為size_t類型,size_t定義于頭文件“stddef.h",它是計算機特定的無符號整數類型,用來表示內存中任何對象的大小。


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

            運算符重載函數一般采用兩種形式,一種是定義為類的成員函數,另一種是定義為類的友元函數。
            大多數情況下,使用成員函數和友元函數重載運算符在功能實現上是相同的,重載時如果沒有本質的區別,則應該首先考慮使用成員函數以保證數據封裝。然而在某些情況下,如C++不能直接進行復數加、減、乘、除的四則運算,但是使用友元函數就可以實現重載這些運算符。
            如 定義 
               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);
              }...

            !注意友元運算符函數的參數類型是引用類型!

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

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

                        

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

            用友元函數重載“++”“--”時需要注意幾點:
            1)運算符++、--都對單值操作數產生影響,因此使用成員運算符函數重載++和--的成員函數通常返回指針this。
            2)由于友元運算符函數沒有this指針,因此不能引用this指針所指的對象,使用友元函數重載++、--時,應采用引用參數傳遞數據。
            3)采用前綴和后綴方式的函數內部的語句可以相同,也可以不同,這取決于用戶的考慮。
            例子:
            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), 參數必須是引用傳遞類型,而不是值傳遞。若這樣做,以下主函數輸出的值是不變的
            {
             ++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 閱讀(365) | 評論 (0)編輯 收藏

            類的實現:

            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;
             }
            };


            主函數:

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



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

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

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

            類的實現及主函數實現:
            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 閱讀(2153) | 評論 (0)編輯 收藏

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

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



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

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

            }


            程序運行結果





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


            我們知道,std::cout<<endl是使輸入的數強制輸出,以前我沒發現,今天發現,如果是輸入一行數的話,使用這個std::cout<<endl,程序是默認每輸出一個數就回車的,而不是排成一行!
            請看一下一例:
            該程序要求輸入長度,然后輸出一個四條邊都帶相同數量星號的矩形。
            #include<iostream>
            using namespace std;
            int main()
            {
                
            int side,rowPosition,size;
                cout
            <<"input the square side: ";//輸入矩形的寬度
                cin>>side;
                size
            =side;//使長寬的邊所帶星號數量相同
                while(side>0)//雙重循環輸出矩形
                {
                    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;//這里總的強制輸出所有輸入的字符
                  return 0;
                
            }

                    
                    




                

                

            程序運行效果如下圖,輸入8;





            如果在程序的每條cout語句中加上<<endl; 那么程序運行的效果(圖所限,"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的功能就是強制輸出和換行,現在懂了,感謝博友的認真更正,學習了。:)

            posted @ 2011-09-23 05:18 hoshelly 閱讀(442) | 評論 (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 閱讀(288) | 評論 (0)編輯 收藏

            僅列出標題
            共12頁: First 4 5 6 7 8 9 10 11 12 
            99久久精品无码一区二区毛片 | 久久久精品免费国产四虎| 偷偷做久久久久网站| 久久精品综合一区二区三区| 99久久国产主播综合精品| 色综合久久中文色婷婷| 亚洲国产精品热久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 99久久精品国产毛片| 久久伊人影视| 国产精品久久久久久久app| 久久久久久久久波多野高潮| 久久中文骚妇内射| 欧美激情精品久久久久| 久久久WWW成人免费精品| 日韩人妻无码一区二区三区久久99| 亚洲欧美一级久久精品| 久久久久亚洲AV成人网人人网站| 亚洲欧美日韩久久精品第一区| 九九精品99久久久香蕉| 国产69精品久久久久99| 麻豆精品久久久久久久99蜜桃| 日日噜噜夜夜狠狠久久丁香五月 | 久久人妻少妇嫩草AV无码专区| 久久亚洲AV成人出白浆无码国产| 91久久精品91久久性色| 久久露脸国产精品| 久久天天躁狠狠躁夜夜躁2O2O| 国产69精品久久久久99| 亚洲国产精品无码久久久不卡| 91精品国产91久久| 久久天天躁狠狠躁夜夜躁2014| 国产精品久久波多野结衣| 理论片午午伦夜理片久久 | 中文字幕久久亚洲一区| jizzjizz国产精品久久| 欧美一区二区久久精品| 国产成人精品久久亚洲高清不卡| 国内精品综合久久久40p| 久久av高潮av无码av喷吹| 久久99国产综合精品|