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

            DraculaW

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              19 隨筆 :: 0 文章 :: 7 評論 :: 0 Trackbacks

            Calculating "The Sum of Its Parts"


            Background

            "The whole is greater than the sum of its parts" is a central theme of Gestalt psychology, and for humans it may be a true observation. Computers are much more literal-minded.

            Description

            In this assessment, you will construct a representation of a complex structure, a hospital that is composed of many parts. There are too many parts for a human to keep track of, which is why we want the computer to do it for us. The description of the hospital is contained in the file definitions.txt.


            picture of a top-level view of a hospital floor
            Figure 1 Layout of a floor in the hospital

            The building has ten floors. Each floor has four wings emanating from a central core.

            Each wing contains two long corridors joined at the end by a short connecting corridor.

            Each long corridor contains twenty-one patient rooms. Each connecting corridor contains five supply rooms.

            The hospital is described by a labeled tree, whose nodes are of type Part. A node contains children nodes corresponding to its subparts, as shown in the figure below. Each edge is labeled by the number of subparts the node contains. For example, the label 10 on the edge from hospital to floor indicates that the hospital has ten floors. You can assume there are no duplicate edges, that is, there is at most one edge between any two nodes.


            tree representation of the parts in a hospital
            Figure 2 Tree representation of the hospital

            The driver program main.cpp first loads the file definitions.txt, which contains the subpart relationships that define the hospital. It then processes queries from the file queries.txt, which contains two kinds of queries about the hospital. The whatis query requests the description of a Part. The howmany query is the heart of the exercise. It asks how many instances of a Part are contained in another Part.

            You are provided with main.cpp and a skeleton version of parts.h as a starting point. Your job is to complete the parts.h file (and write parts.cpp, if you deem necessary). The steps below point the way toward a solution, but they do not cover every detail, so if you find you need to create additional functions or member items, feel free to do so.





            #ifndef _PARTS_H_
            #define _PARTS_H_

            #include <vector>
            #include <map>
            #include <string>

            using namespace std;

            //**************** Part ****************
            class Part {
               
            public:
                string name;

                map<Part*, int> subparts;
               
                Part(string const &n) : name(n) {};
                void describe(void);
                int count_howmany(Part const *p);
            };


            //**************** NameContainer ****************
            class NameContainer {

            private:
                map<string,Part*> name_map;

            public:

                NameContainer(void) {};

                Part* lookup(string const &name) {

                    for( map<string, Part*>::iterator it = name_map.begin();
                         it != name_map.end(); it++ )
                    {
                        if( it->first == name )
                            return it->second;
                    }
                    Part *new_part = new Part( name );
                    name_map[name] = new_part;
                    return name_map[name];
                }
            };

            NameContainer partContainer;

            //**************** Part Member Functions ****************
            void Part::describe(void) {
               
                cout << "Part " << name << "subparts are" << endl;

                if( subparts.empty())
                {
                    cout << " There no subparts" << endl;
                    return;
                }
                   

                for( map<Part*, int>::iterator it = subparts.begin();
                     it != subparts.end(); it++ )
                {
                    cout << " " << it->second << " " << it->first->name << endl;
                }

                cout << endl;
            }

            int Part::count_howmany(Part const *p) {
                   
                if( this->name == p->name ) return 1;

                if( this->subparts.empty() )return 0;

                int sum = 0;

                for( map<Part*, int>::iterator it = subparts.begin();
                     it != subparts.end(); it++ )
                {
                    sum += ( ( it->second ) * ( it->first->count_howmany( p )) );
                }

                return sum;
            }

            //**************** Miscellaneous Functions ****************
            void add_part(string const &x, int q, string const &y) {

                // TODO: Finish implementation
                Part *parent = partContainer.lookup(x);
                Part *child = partContainer.lookup(y);

                parent->subparts.insert( make_pair( child, q ) );
            }

            #endif

            posted on 2007-11-15 20:39 DraculaW 閱讀(448) 評論(0)  編輯 收藏 引用
            久久一区二区免费播放| 久久久精品波多野结衣| 久久亚洲AV成人出白浆无码国产| 国产亚洲精品久久久久秋霞| 91精品国产乱码久久久久久 | 麻豆精品久久久久久久99蜜桃| 亚洲av日韩精品久久久久久a | 久久久久亚洲精品天堂| 久久99久久无码毛片一区二区| 无码人妻精品一区二区三区久久久 | 97久久综合精品久久久综合| 一级做a爰片久久毛片毛片| 99久久精品这里只有精品| 久久人人爽人人爽人人片AV高清| 国产精品VIDEOSSEX久久发布 | 国产精品久久久香蕉| 国产精品久久久久久久午夜片| 无码日韩人妻精品久久蜜桃| 婷婷久久精品国产| 久久久青草青青国产亚洲免观| 免费观看久久精彩视频| 久久精品无码午夜福利理论片| 久久久久久久波多野结衣高潮| 国产亚州精品女人久久久久久| 久久久青草青青亚洲国产免观| 国产精品久久久久久久久鸭| 色综合久久久久无码专区 | 亚洲AV无一区二区三区久久| 久久精品日日躁夜夜躁欧美| 伊人久久大香线蕉综合热线| 久久99精品久久久久久噜噜| 久久精品亚洲欧美日韩久久| 久久久久国产视频电影| 色播久久人人爽人人爽人人片aV| 欧洲性大片xxxxx久久久| 久久国产V一级毛多内射| 国产精品亚洲综合专区片高清久久久| 2020最新久久久视精品爱| 国产亚洲色婷婷久久99精品91| 久久久久久毛片免费看| 久久亚洲国产精品成人AV秋霞|