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

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜


            第一個程序:      
            #include "stdafx.h"
            #include 
            <iostream>
            using namespace std;
            struct travel_time
            {
                
            int hours;
                
            int mines;
            }
            ;
            const int Mines_per_hr = 60;
            travel_time sum(travel_time t1, travel_time t2);
            void show_time(travel_time t);
            int main(int argc, char* argv[])
            {
                travel_time day1 
            = {545}
                travel_time day2 
            = {455};
                travel_time trip 
            = sum (day1, day2);
                cout
            <<"Two-day total: ";
                show_time(trip);
                
                travel_time day3
            ={4,32};
                cout
            <<"Three-day total: ";
                show_time(sum(trip,day3));
                
            return 0;
            }

            travel_time sum(travel_time t1, travel_time t2)
            {
                travel_time total;
                total.mines
            =(t1.mines+t2.mines)%Mines_per_hr;
                total.hours
            =t1.hours+t2.hours+(t1.mines+t2.mines)/Mines_per_hr;
                
            return total;
            }

            void show_time(travel_time t)
            {
                cout
            <<t.hours<<" hours, "<<t.mines<<" minutes\n";
            }

            第二個程序:
                  這個例子將定義兩個結構,用于表示兩種不同的描述位置的方法,然后開發一個函數,將一種格式轉換為另一種格式,并顯示結果。
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <cmath>
            using namespace std;
            //structure declarations
            struct polar
            {
                
            double distance;    //distance from origin
                double angle;        //direction from origin
            }
            ;
            struct rect
            {
                
            double x;        //horizontal distance from origin
                double y;        //vertical distance from origin
            }
            ;

            // prototypes
            polar rect_to_polar(rect xypos);
            void show_polar(polar dapos);
            int main(int argc, char* argv[])
            {
                rect rplace;
                polar pplace;
                cout
            <<"Enter the x and y values: ";
                
            // 將cin>>用作測試條件消除了這種限制,因為它接受任何有效地數字輸入。
                
            // 在需要使用循環來輸入數字時候,別忘了考慮使用這種方式。另外請記住,
                
            // 非數字輸入將設置一個錯誤條件,禁止進一步讀取輸入。如果程序在輸入
                
            // 循環后還需要進行輸入,則必須使用cin.clear()重置輸入,然后可能需
                
            // 要通過讀取不合法的輸入來丟棄它們。
                while(cin>>rplace.x>>rplace.y)    //slick use of cin
                {
                    pplace 
            = rect_to_polar(rplace);
                    show_polar(pplace);
                    cout
            <<"Next two numbers(q to quit): ";
                }

                cout
            <<"Done.\n";
                
            return 0;
            }


            //convert rectangular to polar coordinates
            polar rect_to_polar(rect xypos)
            {
                polar answer;
                answer.distance
            =sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
                answer.angle
            =atan2(xypos.y, xypos.x);
                
            return answer;    
            }

            //show polar coordinates, converting angle to degrees
            void show_polar(polar dapos)
            {
                
            const double Rad_to_deg=57.29577951;
                cout
            <<"distance = "<<dapos.distance;
                cout
            <<", angle = "<<dapos.angle * Rad_to_deg;
                cout
            <<" degrees\n";
            }

            第三個程序
                  傳遞結構的地址。假設要傳遞結構的地址而不是整個結構以節省時間和空間,則需要重新編寫前面的函數,使用指向結構的指針。對于重新編寫show_polar()函數,需要修改3個地方:1、調用函數時,將結構的地址(&pplace)而不是結構本身(pplace)傳遞給它。2、將形參聲明為指向polar的指針,即polar*類型。由于函數不應該修改結構,因此使用const修飾。3、由于形參是指針而不是結構,因此應使用間接成員操作符(->),而不是成員操作符(句點)。
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <cmath>
            using namespace std;
            //structure declarations
            struct polar
            {
                
            double distance;    //distance from origin
                double angle;        //direction from origin
            }
            ;
            struct rect
            {
                
            double x;        //horizontal distance from origin
                double y;        //vertical distance from origin
            }
            ;

            // prototypes
            void rect_to_polar(const rect* pxy, polar* pda);
            void show_polar(const polar *pda);
            int main(int argc, char* argv[])
            {
                rect rplace;
                polar pplace;
                cout
            <<"Enter the x and y values: ";
                
            while(cin>>rplace.x>>rplace.y)    //slick use of cin
                {
                    rect_to_polar(
            &rplace, &pplace);
                    show_polar(
            &pplace);
                    cout
            <<"Next two numbers(q to quit): ";
                }

                cout
            <<"Done.\n";
                
            return 0;
            }

            void rect_to_polar(const rect* pxy, polar* pda)
            {
                pda
            ->distance=sqrt(pxy->* pxy->+ pxy->* pxy->y);
                pda
            ->angle=atan2(pxy->y, pxy->x);
            }

            void show_polar(const polar *pda)
            {
                
            const double Rad_to_deg=57.29577951;
                cout
            <<"distance = "<<pda->distance;
                cout
            <<", angle = "<<pda->angle * Rad_to_deg;
                cout
            <<" degrees\n";
            }
            posted on 2010-02-12 19:02 煙皚 閱讀(353) 評論(0)  編輯 收藏 引用 所屬分類: C++ primer plus學習筆記
            久久精品国产亚洲7777| 99久久国产主播综合精品 | 69久久精品无码一区二区| 色综合久久无码五十路人妻| 精品九九久久国内精品| 久久综合给合综合久久| 99久久99这里只有免费的精品| 成人亚洲欧美久久久久| 2021最新久久久视精品爱| 国产精品久久波多野结衣| 亚洲国产综合久久天堂| 久久综合综合久久97色| 久久精品一区二区三区AV| 亚洲国产成人久久综合一| 久久亚洲AV无码精品色午夜麻豆| 久久天堂电影网| 久久久久亚洲精品无码蜜桃| 亚洲国产成人精品女人久久久 | 中文字幕无码免费久久| 国产精品成人99久久久久| 久久亚洲私人国产精品vA| 青青久久精品国产免费看| 狠狠色丁香久久婷婷综| 亚洲va中文字幕无码久久不卡| 伊人久久国产免费观看视频 | 久久AV无码精品人妻糸列| 久久久久人妻一区精品果冻| 色综合久久天天综合| 久久久久一区二区三区| 久久久久成人精品无码中文字幕 | 欧美久久久久久| 久久久无码精品午夜| 性高湖久久久久久久久AAAAA| 久久精品国产99国产精品| 久久精品国产亚洲AV不卡| 久久国产成人| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 亚洲精品高清一二区久久| 久久天天躁狠狠躁夜夜不卡| 九九久久精品国产| 亚洲精品国产第一综合99久久|