• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜


            第一個程序:
                  演示數(shù)組函數(shù)是如何運作的。
            #include "stdafx.h"
            #include 
            <iostream>
            using namespace std;
            const int ArSize=8;

            /* arr實際上并不是數(shù)組,而是一個指針。在編寫函數(shù)的
             * 其余部分時,可以將arr看作是數(shù)組。C++將數(shù)組名解釋
             * 為其第一個元素的地址。
             * int sum_arr(int * arr,int n) 這個函數(shù)頭也是正確的。
             * int * arr和int arr[]含義是相同,都意味著arr是一個int指針
             * 但不能在函數(shù)體中使用int tip[]來聲明指針
             
            */

            int sum_arr(int arr[],int n);    //prototype
            int main(int argc, char* argv[])
            {
                
            int cookies[ArSize]={1,2,4,8,16,32,64,128};

                
            int sum=sum_arr(cookies,ArSize);
                cout
            <<"Total cookies eaten: "<<sum<<"\n";
                
            return 0;
            }


            int sum_arr(int arr[], int n)
            {
                
            int total=0;
                
            for(int i=0;i<n;i++)
                    total
            =total+arr[i];
                
            return total;
            }

            第二個程序:
                  cookies和arr的值相同。并且還演示了指針概念如何使sum_arr函數(shù)比以前更通用。該程序是用限定符std::而不是編譯指令using來提供對cout和endl的訪問權(quán)。
            #include "stdafx.h"
            #include 
            <iostream>
            const int ArSize=8;
            int sum_arr(int arr[],int n);
            //use std::instead of using directive
            int main(int argc, char* argv[])
            {
                
            int cookies[ArSize]={1,2,4,8,16,32,64,128};

                std::cout
            <<cookies<<" = array address, ";
             
            // some system require a type cast: unsigned (cookies)
                
                std::cout
            <<sizeof cookies<<" = sizeof cookies\n";
                
            int sum=sum_arr(cookies,ArSize);
                std::cout
            <<"Total cookies eaten: "<<sum<<std::endl;
                sum
            =sum_arr(cookies,3);    //a lie
                std::cout<<"First three eaters ate "<<sum<<" cookies.\n";
                sum
            =sum_arr(cookies+4,4);//another lie
                std::cout<<"Last four eaters ate "<<sum<<" cookies.\n";
                
            return 0;
            }


            int sum_arr(int arr[], int n)
            {
                
            int total=0;
                std::cout
            <<arr<<" = arr, ";
             
            // some systems require a type cast: unsigned (arr)

                std::cout
            <<sizeof arr<<" = sizeof arr\n";
                
            for(int i=0;i<n;i++)
                    total
            =total+arr[i];
                
            return total;
            }

            // 注意,地址值和數(shù)組的長度將隨系統(tǒng)而異,另外,有些C++實現(xiàn)將以十進制而不是十六進制格式顯示地址。
            /* 為將數(shù)組類型和元素數(shù)量告訴數(shù)組處理函數(shù),請通過兩個不同的參數(shù)來傳遞它們:
             * void fillArray(int arr[],int size);//prototype
             * 而不要試圖使用方括號表示法來傳遞數(shù)組長度
             * void fillArray(int arr[size]);//No--bad prototype
             
            */

            第三個程序:
            #include "stdafx.h"
            #include 
            <iostream>
            using namespace std;
            const int Max=5;

            // function prototypes
            int fill_array(double ar[], int limit);
            void show_array(const double ar[], int n);// don't change data
            void revalue(double r, double ar[], int n);
            int main(int argc, char* argv[])
            {
                
            double properties[Max];

                
            int size=fill_array(properties,Max);
                show_array(properties,size);
                cout
            <<"Enter revaluation factor: ";
                
            double factor;
                cin
            >>factor;
                revalue(factor,properties,size);
                show_array(properties,size);
                cout
            <<"Done.\n";
                
            return 0;
            }


            int fill_array(double ar[],int limit)
            {
                
            double temp;
                
            int i;
                
            /* 可以使用循環(huán)連續(xù)地將值讀入到數(shù)組中,但如何提早結(jié)束循環(huán)呢?一種方法使,使用一個特殊值來指出輸入結(jié)束。
                 * 由于所有的屬性都不為負,因此可以使用復(fù)述來指出輸入結(jié)束。另外,該函數(shù)應(yīng)對錯誤輸入做出反應(yīng),如停止輸入等。
                 
            */

                
            for(i=0;i<limit;i++)
                
            {
                    cout
            <<"Enter value #"<<(i+1)<<":";
                    cin
            >>temp;
                    
            if(!cin) //bad input
                    {
                        cin.clear();
                        
            while(cin.get()!='\n')
                            
            continue;
                        cout
            <<"Bad input: input process terminated.\n";
                        
            break;
                    }

                    
            else if(temp<0)        //signale to terminate
                        break;
                    ar[i] 
            =temp;
                }

                
            return i;
            }


            // the following function can use, but not alter,
            // the array whose address is ar
            /* 要確保顯示函數(shù)不修改原始數(shù)組。除非函數(shù)的目的就是修改傳遞給它的數(shù)據(jù),
             * 否則應(yīng)避免發(fā)生這種情況。使用普通參數(shù)時,這種保護將自動實現(xiàn),這是由
             * 于C++按值傳遞給它的書籍,而且函數(shù)使用數(shù)據(jù)的拷貝。不過,接受數(shù)組名的
             * 函數(shù)將使用原始數(shù)據(jù),這正是fill_array()函數(shù)能夠完成其工作的原因。
             
            */

            void show_array(const double ar[], int n)
            {
                
            /* 注意,const并不是意味著原始數(shù)組必須是常量,而只是意味著不能在
                 * show_array()函數(shù)中使用ar來修改這些數(shù)據(jù)。因此,show_array()將數(shù)組
                 * 視為只讀數(shù)據(jù)。C++將聲明const double ar []解釋為const double *ar,
                 * 實際上是說,ar指向的是一個常量值。
                 
            */

                
            for(int i=0;i<n;i++)
                
            {
                    cout
            <<"Property #"<<(i+1)<<": $";
                    cout
            <<ar[i]<<endl;
                }

            }


            // multiplies each element of ar[] by r
            void revalue(double r,double ar[], int n)
            {
                
            for(int i=0;i<n;i++)
                    ar[i]
            *=r;
            }

            第四個程序:
            #include "stdafx.h"
            #include 
            <iostream>
            using namespace std;
            const int ArSize=8;
            int sum_arr(const int *begin,const int *end);
            int main(int argc, char* argv[])
            {
                
            int cookies[ArSize]={1,2,4,8,16,32,64,128};
                
            int sum=sum_arr(cookies,cookies+ArSize);

                cout
            <<"Total cookies eaten: "<<sum<<std::endl;
                sum
            =sum_arr(cookies,cookies+3);    //first 3 elements
                cout<<"First three eaters ate "<<sum<<" cookies.\n";
                sum
            =sum_arr(cookies+4,cookies+8);//last 4 elements
                cout<<"Last four eaters ate "<<sum<<" cookies.\n";
                
            return 0;
            }


            int sum_arr(const int *begin,const int * end)
            {
                
            //注意,根據(jù)指針減法規(guī)則,表達式end-begin是一個整數(shù)值,等于數(shù)組的元素數(shù)目。
                const int * pt;
                
            int total=0;
                
            for(pt=begin;pt!=end;pt++)
                    total
            =total+*pt;
                
            return total;
            }
            posted on 2010-02-12 14:59 煙皚 閱讀(457) 評論(0)  編輯 收藏 引用 所屬分類: C++ primer plus學習筆記
            精品久久久久一区二区三区 | 久久久久久无码国产精品中文字幕 | 超级97碰碰碰碰久久久久最新| 亚洲国产天堂久久综合| 精品久久久久久无码不卡| 亚洲色欲久久久综合网 | 欧美久久一区二区三区| 精品国产日韩久久亚洲| 久久不见久久见免费视频7| 久久黄视频| 蜜臀久久99精品久久久久久小说| 久久综合丁香激情久久| 久久乐国产综合亚洲精品| 久久777国产线看观看精品| 亚洲国产视频久久| 一本伊大人香蕉久久网手机| 欧美伊人久久大香线蕉综合| 亚洲国产天堂久久综合网站| 亚洲精品无码久久久久去q| 爱做久久久久久| a高清免费毛片久久| 久久只有这精品99| 久久综合给合综合久久| 日本久久久精品中文字幕| 日产精品久久久一区二区| 久久精品国产男包| 欧美激情精品久久久久久久九九九| 国产精品99久久精品| 欧美午夜精品久久久久免费视| 亚洲人成无码网站久久99热国产| 精品无码久久久久久久久久| 亚洲国产精久久久久久久| 久久ZYZ资源站无码中文动漫| 久久婷婷五月综合国产尤物app| 久久综合九色综合久99| 久久久网中文字幕| 亚洲日本va午夜中文字幕久久| 久久精品无码专区免费| 色播久久人人爽人人爽人人片aV| 久久亚洲中文字幕精品一区| 日本久久中文字幕|